From 05bbf058beafbe81c7b2ce39ab635decc6639cb7 Mon Sep 17 00:00:00 2001 From: Ulf Wiger Date: Tue, 24 Mar 2026 21:23:20 +0900 Subject: [PATCH] Map 'hash' as known api type, add some more tests (#56) I noticed that the 'hash' serialization type wasn't in the `known_type()` set, although it was supported otherwise. This PR adds it to the `known_type()` and also exports the type `gmser_api_encoder:known_type()`. Also, some more tests are added to try to detect this sort of thing in the future. Co-authored-by: Ulf Wiger Reviewed-on: https://git.qpq.swiss/QPQ-AG/gmserialization/pulls/56 --- src/gmser_api_encoder.erl | 4 +- test/gmser_api_encoder_tests.erl | 68 +++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/src/gmser_api_encoder.erl b/src/gmser_api_encoder.erl index 49f2f68..4b1eedc 100644 --- a/src/gmser_api_encoder.erl +++ b/src/gmser_api_encoder.erl @@ -13,7 +13,8 @@ safe_decode/2, byte_size_for_type/1]). --export_type([encoded/0]). +-export_type([encoded/0, + known_type/0]). -type known_type() :: key_block_hash | micro_block_hash @@ -38,6 +39,7 @@ | native_token | commitment | peer_pubkey + | hash | state | poi | state_trees diff --git a/test/gmser_api_encoder_tests.erl b/test/gmser_api_encoder_tests.erl index 7ffee1c..6ec7c74 100644 --- a/test/gmser_api_encoder_tests.erl +++ b/test/gmser_api_encoder_tests.erl @@ -22,10 +22,39 @@ , {native_token , 32} , {commitment , 32} , {peer_pubkey , 32} + , {hash , 32} , {state , 32} , {poi , not_applicable}]). encode_decode_test_() -> + encode_decode_test_(?TYPES). + +encode_decode_known_types_test_() -> + KnownTypes = known_types(), + SizedTypes = [{T, ?TEST_MODULE:byte_size_for_type(T)} || T <- KnownTypes], + encode_decode_test_(SizedTypes). + +prefixes_are_known_types_test() -> + MappedPfxs = mapped_prefixes(), + KnownTypes = known_types(), + lists:foreach( + fun({Pfx, Type}) -> + case lists:member(Type, KnownTypes) of + true -> ok; + false -> + error({not_a_known_type, Pfx, Type}) + end + end, MappedPfxs), + lists:foreach( + fun(Type) -> + case lists:keyfind(Type, 2, MappedPfxs) of + {_, _} -> ok; + false -> + error({has_no_mapped_prefix, Type}) + end + end, KnownTypes). + +encode_decode_test_(Types) -> [{"Byte sizes are correct", fun() -> lists:foreach( @@ -33,7 +62,7 @@ encode_decode_test_() -> {_Type, _, ByteSize} = {Type, ByteSize, ?TEST_MODULE:byte_size_for_type(Type)} end, - ?TYPES) + Types) end }, {"Serialize/deserialize known types", @@ -50,7 +79,7 @@ encode_decode_test_() -> {Type, Key} = ?TEST_MODULE:decode(EncodedKey), {ok, Key} = ?TEST_MODULE:safe_decode(Type, EncodedKey) end, - ?TYPES) + Types) end }, {"Key size check works", @@ -68,7 +97,7 @@ encode_decode_test_() -> CheckIlligalSize(ByteSize - 1), CheckIlligalSize(ByteSize + 1) end, - ?TYPES) + Types) end }, {"Missing prefix", @@ -91,7 +120,7 @@ encode_decode_test_() -> <<_WholePrefix:3/unit:8, RestOfKey2/binary>> = EncodedKey, {error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey2) end, - ?TYPES) + Types) end }, {"Piece of encoded key", @@ -110,7 +139,7 @@ encode_decode_test_() -> {error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, HalfKey), {error, invalid_encoding} = ?TEST_MODULE:safe_decode(Type, RestOfKey) end, - ?TYPES) + Types) end }, {"Encode/decode binary with only zeros", @@ -131,8 +160,35 @@ encode_decode_test_() -> Encoded1 = base58:binary_to_base58(Bin), Decoded1 = base58:base58_to_binary(Encoded1), ?assertEqual(Bin, Decoded1) - end, ?TYPES) + end, Types) end, Bins) end} ]. + +known_types() -> + Forms = get_forms(), + [{type, _, union, Types}] = + [Def || {attribute, _, type, {known_type, Def, []}} <- Forms], + [Name || {atom,_, Name} <- Types]. + +mapped_prefixes() -> + Forms = get_forms(), + [Clauses] = [Cs || {function,_,pfx2type,1,Cs} <- Forms], + Abst = [{B, A} || {clause,_,[B],[],[A]} <- Clauses], + lists:map( + fun({B, A}) -> + {eval_expr(B), eval_expr(A)} + end, Abst). + +get_forms() -> + get_forms(code:which(?TEST_MODULE)). + +get_forms(Beam) -> + {ok, {_, [{abstract_code, {raw_abstract_v1, Forms}}]}} = + beam_lib:chunks(Beam, [abstract_code]), + Forms. + +eval_expr(Expr) -> + {value, Val, []} = erl_eval:expr(Expr, []), + Val.