Merge pull request 'Validation results were ignored, and also partly broken' (#4) from uw-fix-validation into master

Reviewed-on: #4
This commit is contained in:
Ulf Wiger 2025-04-11 17:26:36 +09:00
commit fe47d3715c

View File

@ -60,9 +60,10 @@ validate(#{ connect := #{ pubkey := PK
, versions := Versions
, pool_id := PoolId
, extra_pubkeys := Extra
, type := Type
, type := Type0
, nonces := Nonces
}} = Msg, _Vsn) ->
Type = to_atom(Type0),
valid({list, protocol}, Protocols),
valid({list, version} , Versions),
valid(pubkey, PK),
@ -91,7 +92,7 @@ validate(#{ candidate := #{ seq := Seq
validate(#{ get_nonces := #{ seq := Seq
, n := N }} = Msg, _Vsn) ->
valid(seq, Seq),
valid(pos_integer, N),
valid(pos_int, N),
Msg;
validate(#{ new_server := #{ host := Host, port := Port, keep := Keep }} = Msg, _Vsn) ->
valid(string, Host),
@ -297,10 +298,10 @@ decode_msg_(<<"no_solution">>, #{ <<"seq">> := Seq
, nonce => Nonce }}.
valid(Type, Val) ->
try valid_(Type, Val)
try true = valid_(Type, Val)
catch
error:_ ->
false
error({invalid, {Type, Val}})
end.
valid_({list,T}, Ps) -> lists:all(fun(X) -> valid_(T, X) end, Ps);
@ -308,12 +309,13 @@ 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_defode(contract_pubkey, Id));
valid_(contract, Id) -> ok_tuple(gmser_api_encoder:safe_decode(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),
@ -336,3 +338,8 @@ 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).