report multiple solutions
This commit is contained in:
parent
226c4bc53c
commit
ea7914048a
@ -93,16 +93,19 @@ validate(#{ get_nonces := #{ seq := Seq
|
|||||||
valid(seq, Seq),
|
valid(seq, Seq),
|
||||||
valid(pos_integer, N),
|
valid(pos_integer, N),
|
||||||
Msg;
|
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) ->
|
validate(#{ nonces := #{seq := Seq, nonces := Ns}} = Msg, _vsn) ->
|
||||||
valid(seq, Seq),
|
valid(seq, Seq),
|
||||||
valid(nonces, Ns),
|
valid(nonces, Ns),
|
||||||
Msg;
|
Msg;
|
||||||
validate(#{ solution := #{ seq := Seq
|
validate(#{ solutions := #{ seq := Seq
|
||||||
, nonce := Nonce
|
, found := Solutions } } = Msg, _Vsn) ->
|
||||||
, evidence := Evidence } } = Msg, _Vsn) ->
|
|
||||||
valid(seq, Seq),
|
valid(seq, Seq),
|
||||||
valid(nonce, Nonce),
|
valid(solutions, Solutions),
|
||||||
valid(evidence, Evidence),
|
|
||||||
Msg;
|
Msg;
|
||||||
validate(#{ solution_accepted := #{ seq := Seq }} = Msg, _Vsn) ->
|
validate(#{ solution_accepted := #{ seq := Seq }} = Msg, _Vsn) ->
|
||||||
valid(seq, Seq),
|
valid(seq, Seq),
|
||||||
@ -170,6 +173,17 @@ encode(#{reply := Reply0}, P, V) when is_map(Reply0) ->
|
|||||||
{Id, Reply} = maps:take(id, Reply0),
|
{Id, Reply} = maps:take(id, Reply0),
|
||||||
encode_reply(Reply, Id, P, V).
|
encode_reply(Reply, Id, P, V).
|
||||||
|
|
||||||
|
encode_msg(#{solutions := #{}} = Msg0, ?PROTOCOL_JSON, Vsn) ->
|
||||||
|
#{solutions := #{ seq := Seq
|
||||||
|
, found := Found }} = validate(Msg0, Vsn),
|
||||||
|
EncFound = lists:map(
|
||||||
|
fun(#{ nonce := Nonce
|
||||||
|
, evidence := Evidence }) ->
|
||||||
|
#{ <<"n">> => Nonce
|
||||||
|
, <<"e">> => Evidence }
|
||||||
|
end, Found),
|
||||||
|
json_encode(#{ <<"solutions">> => #{ <<"seq">> => Seq
|
||||||
|
, <<"found">> => EncFound }});
|
||||||
encode_msg(Msg0, ?PROTOCOL_JSON, Vsn) ->
|
encode_msg(Msg0, ?PROTOCOL_JSON, Vsn) ->
|
||||||
Msg = validate(Msg0, Vsn),
|
Msg = validate(Msg0, Vsn),
|
||||||
[{Method, Args}] = maps:to_list(Msg),
|
[{Method, Args}] = maps:to_list(Msg),
|
||||||
@ -255,6 +269,10 @@ decode_msg_(<<"connect">>, #{ <<"protocols">> := Protos
|
|||||||
, type => Type
|
, type => Type
|
||||||
, nonces => Nonces
|
, 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
|
decode_msg_(<<"get_nonces">>, #{ <<"seq">> := Seq
|
||||||
, <<"n">> := N }) ->
|
, <<"n">> := N }) ->
|
||||||
#{get_nonces => #{seq => Seq, n => N}};
|
#{get_nonces => #{seq => Seq, n => N}};
|
||||||
@ -268,12 +286,15 @@ decode_msg_(<<"candidate">>, #{ <<"candidate">> := C
|
|||||||
, nonces => Nonces
|
, nonces => Nonces
|
||||||
, seq => Seq
|
, seq => Seq
|
||||||
, edge_bits => EdgeBits }};
|
, edge_bits => EdgeBits }};
|
||||||
decode_msg_(<<"solution">>, #{ <<"seq">> := Seq
|
decode_msg_(<<"solutions">>, #{ <<"seq">> := Seq
|
||||||
, <<"nonce">> := Nonce
|
, <<"found">> := Found }) ->
|
||||||
, <<"evidence">> := Evidence }) ->
|
Solutions = lists:map(
|
||||||
|
fun(#{ <<"n">> := Nonce
|
||||||
|
, <<"e">> := Evidence }) ->
|
||||||
|
#{nonce => Nonce, evidence => Evidence}
|
||||||
|
end, Found),
|
||||||
#{solution => #{ seq => Seq
|
#{solution => #{ seq => Seq
|
||||||
, nonce => Nonce
|
, found => Solutions }};
|
||||||
, evidence => Evidence}};
|
|
||||||
decode_msg_(<<"solution_accepted">>, #{<<"seq">> := Seq}) ->
|
decode_msg_(<<"solution_accepted">>, #{<<"seq">> := Seq}) ->
|
||||||
#{solution_accepted => #{seq => Seq}};
|
#{solution_accepted => #{seq => Seq}};
|
||||||
decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
|
decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
|
||||||
@ -296,8 +317,14 @@ valid_(seq, Seq) -> pos_integer(Seq);
|
|||||||
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_(boolean, B) -> is_boolean(B);
|
||||||
valid_(contract, Id) -> ok_tuple(gmser_api_encoder:safe_defode(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_(pos_int, N),
|
||||||
|
valid_({list, pos_int}, Evd)
|
||||||
|
end, S);
|
||||||
valid_(nonces, Ns) ->
|
valid_(nonces, Ns) ->
|
||||||
case Ns of
|
case Ns of
|
||||||
[N] -> pos_integer(N);
|
[N] -> pos_integer(N);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user