Compare commits

..

3 Commits

Author SHA1 Message Date
Ulf Wiger
1619e3fb38 update deps 2025-04-07 11:36:15 +02:00
Ulf Wiger
2e2804c6aa fix encoding of solutions msg 2025-03-23 22:00:20 +01:00
Ulf Wiger
ea7914048a report multiple solutions 2025-03-22 12:00:03 +01:00
3 changed files with 10 additions and 22 deletions

View File

@ -3,7 +3,7 @@
{plugins, [rebar3_hex]}. {plugins, [rebar3_hex]}.
{deps, [ {deps, [
{gmserialization, {git, "https://git.qpq.swiss/QPQ-AG/gmserialization.git", {gmserialization, {git, "https://git.qpq.swiss/QPQ-AG/gmserialization.git",
{ref, "0288719ae1"}}} {ref, "bff07885fb"}}}
]}. ]}.
{xref_checks, [undefined_function_calls, undefined_functions, {xref_checks, [undefined_function_calls, undefined_functions,

View File

@ -8,5 +8,5 @@
1}, 1},
{<<"gmserialization">>, {<<"gmserialization">>,
{git,"https://git.qpq.swiss/QPQ-AG/gmserialization.git", {git,"https://git.qpq.swiss/QPQ-AG/gmserialization.git",
{ref,"0288719ae15814f3a53114c657502a24376bebfa"}}, {ref,"bff07885fb9fd328db4d1ae2cd27b118b57dd929"}},
0}]. 0}].

View File

@ -60,10 +60,9 @@ validate(#{ connect := #{ pubkey := PK
, versions := Versions , versions := Versions
, pool_id := PoolId , pool_id := PoolId
, extra_pubkeys := Extra , extra_pubkeys := Extra
, type := Type0 , type := Type
, nonces := Nonces , nonces := Nonces
}} = Msg, _Vsn) -> }} = Msg, _Vsn) ->
Type = to_atom(Type0),
valid({list, protocol}, Protocols), valid({list, protocol}, Protocols),
valid({list, version} , Versions), valid({list, version} , Versions),
valid(pubkey, PK), valid(pubkey, PK),
@ -92,7 +91,7 @@ validate(#{ candidate := #{ seq := Seq
validate(#{ get_nonces := #{ seq := Seq validate(#{ get_nonces := #{ seq := Seq
, n := N }} = Msg, _Vsn) -> , n := N }} = Msg, _Vsn) ->
valid(seq, Seq), valid(seq, Seq),
valid(pos_int, N), valid(pos_integer, N),
Msg; Msg;
validate(#{ new_server := #{ host := Host, port := Port, keep := Keep }} = Msg, _Vsn) -> validate(#{ new_server := #{ host := Host, port := Port, keep := Keep }} = Msg, _Vsn) ->
valid(string, Host), valid(string, Host),
@ -151,7 +150,7 @@ decode(MsgBin, ?PROTOCOL_JSON, Vsn) ->
#{ <<"method">> := Method #{ <<"method">> := Method
, <<"params">> := Params } -> , <<"params">> := Params } ->
%% JSON-RPC notification %% JSON-RPC notification
#{ notification => validate(decode_msg_(Method, Params), Vsn) }; #{ notification => #{ msg => validate(decode_msg_(Method, Params), Vsn) }};
#{ <<"id">> := Id #{ <<"id">> := Id
, <<"result">> := Result } -> , <<"result">> := Result } ->
#{ reply => #{ id => Id #{ reply => #{ id => Id
@ -168,7 +167,7 @@ decode(MsgBin, ?PROTOCOL_JSON, Vsn) ->
encode(#{call := Req0}, P, V) -> encode(#{call := Req0}, P, V) ->
{Id, Req} = maps:take(id, Req0), {Id, Req} = maps:take(id, Req0),
encode_request(Req, Id, P, V); encode_request(Req, Id, P, V);
encode(#{notification := Msg}, P, V) -> encode(#{notification := #{msg := Msg}}, P, V) ->
encode_msg(Msg, P, V); encode_msg(Msg, P, V);
encode(#{reply := Reply0}, P, V) when is_map(Reply0) -> encode(#{reply := Reply0}, P, V) when is_map(Reply0) ->
{Id, Reply} = maps:take(id, Reply0), {Id, Reply} = maps:take(id, Reply0),
@ -205,11 +204,7 @@ encode_reply(Reply, Id, ?PROTOCOL_JSON, _Vsn) ->
ok -> ok ->
#{ <<"jsonrpc">> => <<"2.0">> #{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id , <<"id">> => Id
, <<"result">> => <<"ok">> }; , <<"result">> => <<"ok">> }
continue ->
#{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id
, <<"result">> => <<"continue">> }
end, end,
json_encode(Msg). json_encode(Msg).
@ -236,7 +231,6 @@ error_code(unknown_method ) -> -32601;
error_code(_ ) -> -32603. % internal error error_code(_ ) -> -32603. % internal error
decode_result(<<"ok">>, _) -> ok; decode_result(<<"ok">>, _) -> ok;
decode_result(<<"continue">>, _) -> continue;
decode_result(#{<<"connect_ack">> := #{ <<"protocol">> := P decode_result(#{<<"connect_ack">> := #{ <<"protocol">> := P
, <<"version">> := V }}, Vsn) -> , <<"version">> := V }}, Vsn) ->
Msg = #{connect_ack => #{ protocol => P Msg = #{connect_ack => #{ protocol => P
@ -298,10 +292,10 @@ decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
, nonce => Nonce }}. , nonce => Nonce }}.
valid(Type, Val) -> valid(Type, Val) ->
try true = valid_(Type, Val) try valid_(Type, Val)
catch catch
error:_ -> error:_ ->
error({invalid, {Type, Val}}) false
end. end.
valid_({list,T}, Ps) -> lists:all(fun(X) -> valid_(T, X) end, Ps); valid_({list,T}, Ps) -> lists:all(fun(X) -> valid_(T, X) end, Ps);
@ -309,13 +303,12 @@ valid_(protocol, P) -> is_binary(P);
valid_(version, V) -> is_binary(V); valid_(version, V) -> is_binary(V);
valid_(pubkey, PK) -> ok_tuple(gmser_api_encoder:safe_decode(account_pubkey, PK)); valid_(pubkey, PK) -> ok_tuple(gmser_api_encoder:safe_decode(account_pubkey, PK));
valid_(seq, Seq) -> pos_integer(Seq); valid_(seq, Seq) -> pos_integer(Seq);
valid_(nonce, Nonce) -> pos_integer(Nonce);
valid_(target, T) -> pos_integer(T); valid_(target, T) -> pos_integer(T);
valid_(edge_bits, E) -> pos_integer(E); valid_(edge_bits, E) -> pos_integer(E);
valid_(pos_int, I) -> pos_integer(I); valid_(pos_int, I) -> pos_integer(I);
valid_(string, S) -> is_binary(S); valid_(string, S) -> is_binary(S);
valid_(boolean, B) -> is_boolean(B); valid_(boolean, B) -> is_boolean(B);
valid_(contract, Id) -> ok_tuple(gmser_api_encoder:safe_decode(contract_pubkey, Id)); valid_(contract, Id) -> ok_tuple(gmser_api_encoder:safe_defode(contract_pubkey, Id));
valid_(type, T) -> lists:member(T, [miner, monitor]); valid_(type, T) -> lists:member(T, [miner, monitor]);
valid_(solutions, S) -> lists:all(fun(#{nonce := N, evidence := Evd}) -> valid_(solutions, S) -> lists:all(fun(#{nonce := N, evidence := Evd}) ->
valid_(pos_int, N), valid_(pos_int, N),
@ -338,8 +331,3 @@ ok_tuple(V) ->
pos_integer(I) -> pos_integer(I) ->
is_integer(I) andalso I >= 0. is_integer(I) andalso I >= 0.
to_atom(A) when is_atom(A) ->
A;
to_atom(B) when is_binary(B) ->
binary_to_existing_atom(B, utf8).