Package for zomp

This commit is contained in:
Craig Everett 2023-05-30 15:41:16 +09:00
parent 177bf604b2
commit 9124228ff4
3 changed files with 39 additions and 5 deletions

1
Emakefile Normal file
View File

@ -0,0 +1 @@
{"src/*", [debug_info, {i, "include/"}, {outdir, "ebin/"}]}.

View File

@ -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

17
zomp.meta Normal file
View File

@ -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,[]}.