3 Commits

Author SHA1 Message Date
Ulf Wiger 6fd678b420 Reduce notification nesting, at 'continue' result 2025-04-10 09:09:12 +02:00
uwiger abb1bef2b6 report multiple solutions, server change message (#2)
Co-authored-by: Ulf Wiger <ulf@wiger.net>
Reviewed-on: #2
2025-03-24 06:04:29 +09:00
uwiger 226c4bc53c Merge pull request 'completing the pool-client interaction' (#1) from uw-client-debugging into master
Reviewed-on: #1
2025-03-21 06:52:46 +09:00
+35 -14
View File
@@ -93,16 +93,19 @@ validate(#{ get_nonces := #{ seq := Seq
valid(seq, Seq),
valid(pos_integer, N),
Msg;
validate(#{ new_server := #{ host := Host, port := Port, keep := Keep }} = Msg, _Vsn) ->
valid(string, Host),
valid(pos_ind, Port),
valid(boolean, Keep),
Msg;
validate(#{ nonces := #{seq := Seq, nonces := Ns}} = Msg, _vsn) ->
valid(seq, Seq),
valid(nonces, Ns),
Msg;
validate(#{ solution := #{ seq := Seq
, nonce := Nonce
, evidence := Evidence } } = Msg, _Vsn) ->
validate(#{ solutions := #{ seq := Seq
, found := Solutions } } = Msg, _Vsn) ->
valid(seq, Seq),
valid(nonce, Nonce),
valid(evidence, Evidence),
valid(solutions, Solutions),
Msg;
validate(#{ solution_accepted := #{ seq := Seq }} = Msg, _Vsn) ->
valid(seq, Seq),
@@ -147,7 +150,7 @@ decode(MsgBin, ?PROTOCOL_JSON, Vsn) ->
#{ <<"method">> := Method
, <<"params">> := Params } ->
%% JSON-RPC notification
#{ notification => #{ msg => validate(decode_msg_(Method, Params), Vsn) }};
#{ notification => validate(decode_msg_(Method, Params), Vsn) };
#{ <<"id">> := Id
, <<"result">> := Result } ->
#{ reply => #{ id => Id
@@ -164,7 +167,7 @@ decode(MsgBin, ?PROTOCOL_JSON, Vsn) ->
encode(#{call := Req0}, P, V) ->
{Id, Req} = maps:take(id, Req0),
encode_request(Req, Id, P, V);
encode(#{notification := #{msg := Msg}}, P, V) ->
encode(#{notification := Msg}, P, V) ->
encode_msg(Msg, P, V);
encode(#{reply := Reply0}, P, V) when is_map(Reply0) ->
{Id, Reply} = maps:take(id, Reply0),
@@ -201,7 +204,11 @@ encode_reply(Reply, Id, ?PROTOCOL_JSON, _Vsn) ->
ok ->
#{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id
, <<"result">> => <<"ok">> }
, <<"result">> => <<"ok">> };
continue ->
#{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id
, <<"result">> => <<"continue">> }
end,
json_encode(Msg).
@@ -228,6 +235,7 @@ error_code(unknown_method ) -> -32601;
error_code(_ ) -> -32603. % internal error
decode_result(<<"ok">>, _) -> ok;
decode_result(<<"continue">>, _) -> continue;
decode_result(#{<<"connect_ack">> := #{ <<"protocol">> := P
, <<"version">> := V }}, Vsn) ->
Msg = #{connect_ack => #{ protocol => P
@@ -255,6 +263,10 @@ decode_msg_(<<"connect">>, #{ <<"protocols">> := Protos
, type => Type
, nonces => Nonces
}};
decode_msg_(<<"new_server">>, #{ <<"host">> := Host
, <<"port">> := Port
, <<"keep">> := Keep }) ->
#{new_server => #{host => Host, port => Port, keep => Keep}};
decode_msg_(<<"get_nonces">>, #{ <<"seq">> := Seq
, <<"n">> := N }) ->
#{get_nonces => #{seq => Seq, n => N}};
@@ -268,12 +280,15 @@ decode_msg_(<<"candidate">>, #{ <<"candidate">> := C
, nonces => Nonces
, seq => Seq
, edge_bits => EdgeBits }};
decode_msg_(<<"solution">>, #{ <<"seq">> := Seq
, <<"nonce">> := Nonce
, <<"evidence">> := Evidence }) ->
#{solution => #{ seq => Seq
, nonce => Nonce
, evidence => Evidence}};
decode_msg_(<<"solutions">>, #{ <<"seq">> := Seq
, <<"found">> := Found }) ->
Solutions = lists:map(
fun(#{ <<"nonce">> := Nonce
, <<"evidence">> := Evidence }) ->
#{nonce => Nonce, evidence => Evidence}
end, Found),
#{solutions => #{ seq => Seq
, found => Solutions }};
decode_msg_(<<"solution_accepted">>, #{<<"seq">> := Seq}) ->
#{solution_accepted => #{seq => Seq}};
decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
@@ -296,8 +311,14 @@ valid_(seq, Seq) -> pos_integer(Seq);
valid_(target, T) -> pos_integer(T);
valid_(edge_bits, E) -> pos_integer(E);
valid_(pos_int, I) -> pos_integer(I);
valid_(string, S) -> is_binary(S);
valid_(boolean, B) -> is_boolean(B);
valid_(contract, Id) -> ok_tuple(gmser_api_encoder:safe_defode(contract_pubkey, Id));
valid_(type, T) -> lists:member(T, [miner, monitor]);
valid_(solutions, S) -> lists:all(fun(#{nonce := N, evidence := Evd}) ->
valid_(pos_int, N),
valid_({list, pos_int}, Evd)
end, S);
valid_(nonces, Ns) ->
case Ns of
[N] -> pos_integer(N);