From 2db9ea613450116a330bf1e9447b77625ff8e2b3 Mon Sep 17 00:00:00 2001 From: Craig Everett Date: Wed, 22 Jan 2025 19:36:50 +0900 Subject: [PATCH] Package for zx (#45) This packages the library for deployment on zx (migrating from the old otpr-aeserialization-* line). It also adds a native Erlang fallback to invokation of the Blake2 algorithm, picking enacl if it is present, but proceeding with eblake2 if not. Reviewed-on: https://git.qpq.swiss/QPQ-AG/gmserialization/pulls/45 Reviewed-by: dimitar.p.ivanov Reviewed-by: Ulf Wiger Co-authored-by: Craig Everett Co-committed-by: Craig Everett --- .gitignore | 2 +- Emakefile | 1 + ebin/gmserialization.app | 11 +++++++++++ src/gmser_api_encoder.erl | 1 + src/gmser_chain_objects.erl | 1 + src/gmser_contract_code.erl | 27 ++++++++++++++++++++++----- src/gmser_delegation.erl | 1 + src/gmser_id.erl | 1 + src/gmser_rlp.erl | 3 +++ src/gmserialization.erl | 1 + zomp.meta | 17 +++++++++++++++++ 11 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 Emakefile create mode 100644 ebin/gmserialization.app create mode 100644 zomp.meta diff --git a/.gitignore b/.gitignore index f1c4554..2a6854a 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,7 @@ _* *.swp *.swo .erlang.cookie -ebin +ebin/*.beam log erl_crash.dump .rebar diff --git a/Emakefile b/Emakefile new file mode 100644 index 0000000..68c7b67 --- /dev/null +++ b/Emakefile @@ -0,0 +1 @@ +{"src/*", [debug_info, {i, "include/"}, {outdir, "ebin/"}]}. diff --git a/ebin/gmserialization.app b/ebin/gmserialization.app new file mode 100644 index 0000000..c1861e8 --- /dev/null +++ b/ebin/gmserialization.app @@ -0,0 +1,11 @@ +{application,gmserialization, + [{description,"Serialization of data for the Gajumaru"}, + {vsn,"0.1.2"}, + {registered,[]}, + {applications,[kernel,stdlib,crypto,base58]}, + {env,[]}, + {modules,[gmser_api_encoder,gmser_chain_objects, + gmser_contract_code,gmser_delegation,gmser_id, + gmser_rlp,gmserialization]}, + {licenses,[]}, + {links,[]}]}. diff --git a/src/gmser_api_encoder.erl b/src/gmser_api_encoder.erl index 25530ff..49f2f68 100644 --- a/src/gmser_api_encoder.erl +++ b/src/gmser_api_encoder.erl @@ -6,6 +6,7 @@ %%% @end %%%------------------------------------------------------------------- -module(gmser_api_encoder). +-vsn("0.1.2"). -export([encode/2, decode/1, diff --git a/src/gmser_chain_objects.erl b/src/gmser_chain_objects.erl index d93eaa5..e8bf58f 100644 --- a/src/gmser_chain_objects.erl +++ b/src/gmser_chain_objects.erl @@ -8,6 +8,7 @@ %%%------------------------------------------------------------------- -module(gmser_chain_objects). +-vsn("0.1.2"). -export([ serialize/4 , deserialize/4 diff --git a/src/gmser_contract_code.erl b/src/gmser_contract_code.erl index df1676d..c50efae 100644 --- a/src/gmser_contract_code.erl +++ b/src/gmser_contract_code.erl @@ -6,6 +6,7 @@ %%% @end %%%------------------------------------------------------------------- -module(gmser_contract_code). +-vsn("0.1.2"). -include("gmser_contract_code.hrl"). @@ -17,15 +18,16 @@ serialize(CodeMap) -> serialize(CodeMap, ?SOPHIA_CONTRACT_VSN_3). + -spec serialize(map(), non_neg_integer()) -> binary(). serialize(CodeMap = #{ byte_code := ByteCode , type_info := TypeInfo }, SophiaContractVersion) -> %% Source hash - SourceHash = case CodeMap of - #{ source_hash := SHash } -> SHash; - #{ contract_source := SrcStr } -> - enacl:generichash(32, list_to_binary(SrcStr)) - end, + SourceHash = + case CodeMap of + #{ source_hash := SHash } -> SHash; + #{ contract_source := SrcStr } -> blake2(32, list_to_binary(SrcStr)) + end, %% Compiler version Version = maps:get(compiler_version, CodeMap, <<"unknown">>), @@ -48,6 +50,21 @@ serialize(CodeMap = #{ byte_code := ByteCode serialization_template(SophiaContractVersion), Fields). +% NOTE: +% This form significantly favors the presence of enacl and slows fallback +% invokation of eblake2. `try' is really fast; the error throwing machinery +% is comparatively slow. The assumption here is that in cases where you want +% eblake2 performance isn't the problem you're solving (you're probably not +% syncing a new node, for example). +blake2(Size, Bin) -> + try + enacl:generichash(Size, Bin) + catch error:undef -> + {ok, Hash} = eblake2:blake2b(Size, Bin), + Hash + end. + + -spec deserialize(binary()) -> map(). deserialize(Binary) -> case gmser_chain_objects:deserialize_type_and_vsn(Binary) of diff --git a/src/gmser_delegation.erl b/src/gmser_delegation.erl index 7777cc8..18af0c9 100644 --- a/src/gmser_delegation.erl +++ b/src/gmser_delegation.erl @@ -6,6 +6,7 @@ %%% @end %%%------------------------------------------------------------------- -module(gmser_delegation). +-vsn("0.1.2"). -export([ aens_preclaim_sig/3 , aens_name_sig/4 diff --git a/src/gmser_id.erl b/src/gmser_id.erl index fa52302..04d0c46 100644 --- a/src/gmser_id.erl +++ b/src/gmser_id.erl @@ -8,6 +8,7 @@ %%%------------------------------------------------------------------- -module(gmser_id). +-vsn("0.1.2"). -export([ create/2 , specialize/1 diff --git a/src/gmser_rlp.erl b/src/gmser_rlp.erl index bf936be..817b756 100644 --- a/src/gmser_rlp.erl +++ b/src/gmser_rlp.erl @@ -4,12 +4,15 @@ %%% @doc %%% Implementation of the Recursive Length Prefix. %%% +%%% https://zxq9.com/archives/2749 %%% https://github.com/ethereum/wiki/wiki/RLP %%% %%% @end %%%------------------------------------------------------------------- -module(gmser_rlp). +-vsn("0.1.2"). + -export([ decode/1 , decode_one/1 , encode/1 diff --git a/src/gmserialization.erl b/src/gmserialization.erl index 9bbfef2..2c12e6b 100644 --- a/src/gmserialization.erl +++ b/src/gmserialization.erl @@ -7,6 +7,7 @@ %%%------------------------------------------------------------------- -module(gmserialization). +-vsn("0.1.2"). -export([ decode_fields/2 , deserialize/5 diff --git a/zomp.meta b/zomp.meta new file mode 100644 index 0000000..4528d1f --- /dev/null +++ b/zomp.meta @@ -0,0 +1,17 @@ +{name,"Gajumaru Serialization"}. +{type,lib}. +{modules,[]}. +{prefix,none}. +{author,"Hans Svensson"}. +{desc,"Serialization helpers for the Gajumaru."}. +{package_id,{"otpr","gmserialization",{0,1,2}}}. +{deps,[{"otpr","base58",{0,1,1}},{"otpr","eblake2",{1,0,0}}]}. +{key_name,none}. +{a_email,[]}. +{c_email,[]}. +{copyright,"QPQ AG"}. +{file_exts,[]}. +{license,skip}. +{repo_url,"https://git.qpq.swiss/QPQ-AG/gmserialization"}. +{tags,["blockchain","crypto","gm", "gajumaru"]}. +{ws_url,[]}.