Package for zx #45

Merged
zxq9 merged 9 commits from zomp into master 2025-01-22 19:36:51 +09:00
11 changed files with 60 additions and 6 deletions

2
.gitignore vendored
View File

@ -7,7 +7,7 @@ _*
*.swp
*.swo
.erlang.cookie
ebin
ebin/*.beam
log
erl_crash.dump
.rebar

1
Emakefile Normal file
View File

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

11
ebin/gmserialization.app Normal file
View File

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

View File

@ -6,6 +6,7 @@
%%% @end
%%%-------------------------------------------------------------------
-module(gmser_api_encoder).
-vsn("0.1.2").
Review

I am wondering if we plan keeping all of those versions in sync somehow, ex. when we change the encoder version, do we also bump the chain objects one? How would we test for compatibility?

I am wondering if we plan keeping all of those versions in sync somehow, ex. when we change the encoder version, do we also bump the chain objects one? How would we test for compatibility?
Review

Those are added by zx but could be omitted. The versions are clearly not in sync across projects (the version of serialization has never been in sync with bytecode or the compiler, for example).

Those are added by zx but could be omitted. The versions are clearly not in sync across projects (the version of serialization has never been in sync with bytecode or the compiler, for example).
-export([encode/2,
decode/1,

View File

@ -8,6 +8,7 @@
%%%-------------------------------------------------------------------
-module(gmser_chain_objects).
-vsn("0.1.2").
-export([ serialize/4
, deserialize/4

View File

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

Actually, the slowest part of the error throwing machinery isn't the catch (it's actually very fast if not capturing stacktraces, and pretty fast even then), but the code:ensure_loaded/1 call needed before throwing an undef error.

If we can know (through initialization logic) that enacl will be loaded at this point, if present at all, we can use erlang:function_exported(enacl, generichash, 2).

Actually, the slowest part of the error throwing machinery isn't the `catch` (it's actually very fast if not capturing stacktraces, and pretty fast even then), but the `code:ensure_loaded/1` call needed before throwing an `undef` error. If we can know (through initialization logic) that `enacl` _will_ be loaded at this point, if present at all, we can use `erlang:function_exported(enacl, generichash, 2)`.
Review

Does that make the failed case more efficient?
I'm not sure where in initialization we could discover which case we're in, so I left it at call time.

Does that make the failed case more efficient? I'm not sure *where* in initialization we could discover which case we're in, so I left it at call time.
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

View File

@ -6,6 +6,7 @@
%%% @end
%%%-------------------------------------------------------------------
-module(gmser_delegation).
-vsn("0.1.2").
-export([ aens_preclaim_sig/3
, aens_name_sig/4

View File

@ -8,6 +8,7 @@
%%%-------------------------------------------------------------------
-module(gmser_id).
-vsn("0.1.2").
-export([ create/2
, specialize/1

View File

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

View File

@ -7,6 +7,7 @@
%%%-------------------------------------------------------------------
-module(gmserialization).
-vsn("0.1.2").
-export([ decode_fields/2
, deserialize/5

17
zomp.meta Normal file
View File

@ -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"}.
zxq9 marked this conversation as resolved Outdated

Is this correct?

Is this correct?
Outdated
Review

Ah, copyright, no. I think.

Ah, copyright, no. I think.
{file_exts,[]}.
{license,skip}.
{repo_url,"https://git.qpq.swiss/QPQ-AG/gmserialization"}.
{tags,["blockchain","crypto","gm", "gajumaru"]}.
{ws_url,[]}.