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/src/aeser_contract_code.erl b/src/aeser_contract_code.erl index 0605420..1fb05d8 100644 --- a/src/aeser_contract_code.erl +++ b/src/aeser_contract_code.erl @@ -16,15 +16,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">>), @@ -47,6 +48,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 aeser_chain_objects:deserialize_type_and_vsn(Binary) of diff --git a/zomp.meta b/zomp.meta new file mode 100644 index 0000000..6d13bc0 --- /dev/null +++ b/zomp.meta @@ -0,0 +1,17 @@ +{a_email,[]}. +{author,"Hans Svensson"}. +{c_email,[]}. +{copyright,"Hans Svensson"}. +{deps,[{"otpr","eblake2",{1,0,0}}]}. +{desc,"Serialization helpers for Aeternity node."}. +{file_exts,[]}. +{key_name,none}. +{license,skip}. +{modules,[]}. +{name,"AE Serialization"}. +{package_id,{"otpr","aeserialization",{0,1,0}}}. +{prefix,none}. +{repo_url,"https://github.com/aeternity/aeserialization"}. +{tags,["blockchain","crypto","ae"]}. +{type,lib}. +{ws_url,[]}.