From f789e9e94562c146d55188e411e190775faf52cc Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Sun, 9 Mar 2025 21:58:35 +0100 Subject: [PATCH] Fix typos, add .gitignore --- .gitignore | 28 ++++++++++++++++++++++ rebar.config | 8 ++++--- rebar.lock | 12 ++++++++++ src/gm_mining_pool_protocol.app.src | 2 +- src/gmmpp_msgs.erl | 37 +++++++++++++++++++++-------- 5 files changed, 73 insertions(+), 14 deletions(-) create mode 100644 .gitignore create mode 100644 rebar.lock diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a93a71a --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +.rebar3 +.eunit +*.o +*.beam +*.plt +*.swp +*.swo +.erlang.cookie +.directory +ebin +log +erl_crash.dump +.rebar +logs +_build +REVISION +VERSION +*.erl~ +*.aes~ +data/mnesia +_checkouts/ +tmp/ +rebar3.crashdump +eqc-lib +eqc +eunit_report/*.xml +.history +.vscode diff --git a/rebar.config b/rebar.config index 03988b9..6c7f3d0 100644 --- a/rebar.config +++ b/rebar.config @@ -2,12 +2,14 @@ {erl_opts, [debug_info]}. {plugins, [rebar3_hex]}. {deps, [ - {ranch, "2.2.0"}, - {enoise, {git, "https://git.qpq.swiss/QPQ-AG/enoise.git", {ref, "8acbce9"}}} + {gmserialization, {git, "https://git.qpq.swiss/QPQ-AG/gmserialization.git", + {ref, "ac64e01b0f"}}} ]}. {xref_checks, [undefined_function_calls, undefined_functions, locals_not_used, deprecated_function_calls, deprecated_functions]}. -{dialyzer, [{warnings, [unknown]}]}. +{dialyzer, [ {warnings, [unknown]} + , {plt_apps, all_deps} + , {base_plt_apps, [erts, kernel, stdlib]} ]}. diff --git a/rebar.lock b/rebar.lock new file mode 100644 index 0000000..ad3917e --- /dev/null +++ b/rebar.lock @@ -0,0 +1,12 @@ +[{<<"base58">>, + {git,"https://git.qpq.swiss/QPQ-AG/erl-base58.git", + {ref,"e6aa62eeae3d4388311401f06e4b939bf4e94b9c"}}, + 1}, + {<<"enacl">>, + {git,"https://git.qpq.swiss/QPQ-AG/enacl.git", + {ref,"4eb7ec70084ba7c87b1af8797c4c4e90c84f95a2"}}, + 1}, + {<<"gmserialization">>, + {git,"https://git.qpq.swiss/QPQ-AG/gmserialization.git", + {ref,"ac64e01b0f675c1a34c70a827062f381920742db"}}, + 0}]. diff --git a/src/gm_mining_pool_protocol.app.src b/src/gm_mining_pool_protocol.app.src index 80bdfb6..5eb9056 100644 --- a/src/gm_mining_pool_protocol.app.src +++ b/src/gm_mining_pool_protocol.app.src @@ -3,7 +3,7 @@ [{description, "Gajumaru Mining Pool Protocol (server- + client-side)"}, {vsn, "0.1.0"}, {registered, []}, - {application, + {applications, [ kernel , stdlib diff --git a/src/gmmpp_msgs.erl b/src/gmmpp_msgs.erl index 68cc6ce..240cc3a 100644 --- a/src/gmmpp_msgs.erl +++ b/src/gmmpp_msgs.erl @@ -36,7 +36,7 @@ -define(VSN0, <<"0.1">>). -define(VSN, ?VSN0). --define(PROTOCOL_JSON, pool_ws_json). +-define(PROTOCOL_JSON, <<"pool_ws_json">>). -define(PROTOCOL, ?PROTOCOL_JSON). -spec latest_version() -> version(). @@ -54,14 +54,24 @@ versions() -> [?VSN0]. %% List sorted highest priority first protocols(_Vsn) -> [?PROTOCOL]. -validate(#{ connect := #{ pubkey := PK }} = Msg, _Vsn) -> +validate(#{ connect := #{ pubkey := PK + , protocols := Protocols + , versions := Versions + }} = Msg, _Vsn) -> + valid({list, protocol}, Protocols), + valid({list, version} , Versions), valid(pubkey, PK), Msg; -validate(#{ connect_ack := #{ pubkey := PK +validate(#{ connect_ack := #{ protocol := Protocol0 + , version := Version + , pubkey := PK , edge_bits := EB }} = Msg, _Vsn) -> + Protocol = binary_to_existing_atom(Protocol0, utf8), + valid(protocol, Protocol), + valid(version, Version), valid(pubkey, PK), valid(edgebits, EB), - Msg; + Msg#{ protocol => Protocol }; validate(#{ nonces := #{ seq := Seq , n := N } } = Msg, _Vsn) -> valid(seq, Seq), @@ -151,18 +161,23 @@ encode_request(Req0, Id, ?PROTOCOL_JSON, Vsn) -> , <<"method">> => Method , <<"args">> => Args }). -encode_reply(Reply0, Id, ?PROTOCOL_JSON, Vsn) -> +encode_reply(Reply0, Id, ?PROTOCOL_JSON, Vsn) when is_map(Reply0) -> Reply = validate(Reply0, Vsn), + Msg = #{ <<"jsonrpc">> => <<"2.0">> + , <<"id">> => Id + , <<"result">> => Reply }, + json:encode(Msg); +encode_reply(Reply, Id, ?PROTOCOL_JSON, _Vsn) -> Msg = case Reply of {error, Reason} -> #{ <<"jsonrpc">> => <<"2.0">> , <<"id">> => Id , <<"error">> => #{ <<"code">> => error_code(Reason) , <<"message">> => Reason }}; - Result -> + ok -> #{ <<"jsonrpc">> => <<"2.0">> , <<"id">> => Id - , <<"result">> => Result } + , <<"result">> => <<"ok">> } end, json:encode(Msg). @@ -197,7 +212,6 @@ decode_msg_(<<"solution">>, #{ <<"seq">> := Seq , <<"evidence">> := Evidence }) -> #{solution => #{ seq => Seq , nonce => Nonces - , seq => Seq , evidence => Evidence}}. valid(Type, Val) -> @@ -207,7 +221,10 @@ valid(Type, Val) -> false end. -valid_(pubkey, PK) -> ok_tuple(aeser_api:safe_decode(account_pubkey, PK)); +valid_({list,T}, Ps) -> lists:all(fun(X) -> valid_(T, X) end, Ps); +valid_(protocol, P) -> is_binary(P); +valid_(version, V) -> is_binary(V); +valid_(pubkey, PK) -> ok_tuple(gmser_api_encoder:safe_decode(account_pubkey, PK)); valid_(seq, Seq) -> pos_integer(Seq); valid_(edgebits, E) -> pos_integer(E); valid_(pos_int, I) -> pos_integer(I); @@ -218,7 +235,7 @@ valid_(nonces, Ns) -> _ -> false end; -valid_(candidate, C) -> ok_tuple(aeser_api:safe_decode(bytearray, C)). +valid_(candidate, C) -> ok_tuple(gmser_api_encoder:safe_decode(bytearray, C)). ok_tuple(V) -> case V of