Compare commits

..

No commits in common. "master" and "uw-client-debugging" have entirely different histories.

3 changed files with 21 additions and 49 deletions

View File

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

View File

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

View File

@ -60,10 +60,9 @@ validate(#{ connect := #{ pubkey := PK
, versions := Versions
, pool_id := PoolId
, extra_pubkeys := Extra
, type := Type0
, type := Type
, nonces := Nonces
}} = Msg, _Vsn) ->
Type = to_atom(Type0),
valid({list, protocol}, Protocols),
valid({list, version} , Versions),
valid(pubkey, PK),
@ -92,21 +91,18 @@ validate(#{ candidate := #{ seq := Seq
validate(#{ get_nonces := #{ seq := Seq
, n := N }} = Msg, _Vsn) ->
valid(seq, Seq),
valid(pos_int, N),
Msg;
validate(#{ new_server := #{ host := Host, port := Port, keep := Keep }} = Msg, _Vsn) ->
valid(string, Host),
valid(pos_ind, Port),
valid(boolean, Keep),
valid(pos_integer, N),
Msg;
validate(#{ nonces := #{seq := Seq, nonces := Ns}} = Msg, _vsn) ->
valid(seq, Seq),
valid(nonces, Ns),
Msg;
validate(#{ solutions := #{ seq := Seq
, found := Solutions } } = Msg, _Vsn) ->
validate(#{ solution := #{ seq := Seq
, nonce := Nonce
, evidence := Evidence } } = Msg, _Vsn) ->
valid(seq, Seq),
valid(solutions, Solutions),
valid(nonce, Nonce),
valid(evidence, Evidence),
Msg;
validate(#{ solution_accepted := #{ seq := Seq }} = Msg, _Vsn) ->
valid(seq, Seq),
@ -151,7 +147,7 @@ decode(MsgBin, ?PROTOCOL_JSON, Vsn) ->
#{ <<"method">> := Method
, <<"params">> := Params } ->
%% JSON-RPC notification
#{ notification => validate(decode_msg_(Method, Params), Vsn) };
#{ notification => #{ msg => validate(decode_msg_(Method, Params), Vsn) }};
#{ <<"id">> := Id
, <<"result">> := Result } ->
#{ reply => #{ id => Id
@ -168,7 +164,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}, P, V) ->
encode(#{notification := #{msg := Msg}}, P, V) ->
encode_msg(Msg, P, V);
encode(#{reply := Reply0}, P, V) when is_map(Reply0) ->
{Id, Reply} = maps:take(id, Reply0),
@ -205,11 +201,7 @@ encode_reply(Reply, Id, ?PROTOCOL_JSON, _Vsn) ->
ok ->
#{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id
, <<"result">> => <<"ok">> };
continue ->
#{ <<"jsonrpc">> => <<"2.0">>
, <<"id">> => Id
, <<"result">> => <<"continue">> }
, <<"result">> => <<"ok">> }
end,
json_encode(Msg).
@ -236,7 +228,6 @@ 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
@ -264,10 +255,6 @@ 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}};
@ -281,15 +268,12 @@ decode_msg_(<<"candidate">>, #{ <<"candidate">> := C
, nonces => Nonces
, seq => Seq
, edge_bits => EdgeBits }};
decode_msg_(<<"solutions">>, #{ <<"seq">> := Seq
, <<"found">> := Found }) ->
Solutions = lists:map(
fun(#{ <<"nonce">> := Nonce
decode_msg_(<<"solution">>, #{ <<"seq">> := Seq
, <<"nonce">> := Nonce
, <<"evidence">> := Evidence }) ->
#{nonce => Nonce, evidence => Evidence}
end, Found),
#{solutions => #{ seq => Seq
, found => Solutions }};
#{solution => #{ seq => Seq
, nonce => Nonce
, evidence => Evidence}};
decode_msg_(<<"solution_accepted">>, #{<<"seq">> := Seq}) ->
#{solution_accepted => #{seq => Seq}};
decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
@ -298,10 +282,10 @@ decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
, nonce => Nonce }}.
valid(Type, Val) ->
try true = valid_(Type, Val)
try valid_(Type, Val)
catch
error:_ ->
error({invalid, {Type, Val}})
false
end.
valid_({list,T}, Ps) -> lists:all(fun(X) -> valid_(T, X) end, Ps);
@ -309,18 +293,11 @@ 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_(nonce, Nonce) -> pos_integer(Nonce);
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_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_(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);
@ -338,8 +315,3 @@ ok_tuple(V) ->
pos_integer(I) ->
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).