Merge pull request #97 from aeternity/PT-166899532-static_hashing_for_events
Pre-compute and switch to Blake2b for event name hash
This commit is contained in:
commit
a47fa59f5b
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"c63ac88"}}}
|
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"c63ac88"}}}
|
||||||
, {getopt, "1.0.1"}
|
, {getopt, "1.0.1"}
|
||||||
|
, {eblake2, "1.0.0"}
|
||||||
, {jsx, {git, "https://github.com/talentdeficit/jsx.git",
|
, {jsx, {git, "https://github.com/talentdeficit/jsx.git",
|
||||||
{tag, "2.8.0"}}}
|
{tag, "2.8.0"}}}
|
||||||
]}.
|
]}.
|
||||||
|
@ -137,6 +137,7 @@
|
|||||||
vars => [var_name()],
|
vars => [var_name()],
|
||||||
functions := #{ fun_name() => fun_def() } }.
|
functions := #{ fun_name() => fun_def() } }.
|
||||||
|
|
||||||
|
-define(HASH_BYTES, 32).
|
||||||
%% -- Entrypoint -------------------------------------------------------------
|
%% -- Entrypoint -------------------------------------------------------------
|
||||||
|
|
||||||
%% Main entrypoint. Takes typed syntax produced by aeso_ast_infer_types:infer/1,2
|
%% Main entrypoint. Takes typed syntax produced by aeso_ast_infer_types:infer/1,2
|
||||||
@ -829,8 +830,8 @@ event_function(_Env = #{event_type := {variant_t, EventCons}}, EventType = {vari
|
|||||||
|| {I, {constr_t, Ann, {con, _, Name}, _}} <- indexed(EventCons) ],
|
|| {I, {constr_t, Ann, {con, _, Name}, _}} <- indexed(EventCons) ],
|
||||||
Arities = [length(Ts) || Ts <- FCons],
|
Arities = [length(Ts) || Ts <- FCons],
|
||||||
Case = fun({Name, Tag, Ixs}) ->
|
Case = fun({Name, Tag, Ixs}) ->
|
||||||
%% TODO: precompute (needs dependency)
|
{ok, HashValue} = eblake2:blake2b(?HASH_BYTES, list_to_binary(Name)),
|
||||||
Hash = {op, crypto_sha3, [{lit, {string, list_to_binary(Name)}}]},
|
Hash = {lit, {bytes, HashValue}},
|
||||||
Vars = [ "arg" ++ integer_to_list(I) || I <- lists:seq(1, length(Ixs)) ],
|
Vars = [ "arg" ++ integer_to_list(I) || I <- lists:seq(1, length(Ixs)) ],
|
||||||
IVars = lists:zip(Ixs, Vars),
|
IVars = lists:zip(Ixs, Vars),
|
||||||
Payload =
|
Payload =
|
||||||
|
@ -62,6 +62,8 @@ v(X) when is_list(X) -> #var_ref{name = X}.
|
|||||||
option_none() -> {tuple, [{integer, 0}]}.
|
option_none() -> {tuple, [{integer, 0}]}.
|
||||||
option_some(X) -> {tuple, [{integer, 1}, X]}.
|
option_some(X) -> {tuple, [{integer, 1}, X]}.
|
||||||
|
|
||||||
|
-define(HASH_BYTES, 32).
|
||||||
|
|
||||||
-define(call(Fun, Args), #funcall{ function = #var_ref{ name = {builtin, Fun} }, args = Args }).
|
-define(call(Fun, Args), #funcall{ function = #var_ref{ name = {builtin, Fun} }, args = Args }).
|
||||||
-define(I(X), {integer, X}).
|
-define(I(X), {integer, X}).
|
||||||
-define(V(X), v(X)).
|
-define(V(X), v(X)).
|
||||||
@ -93,12 +95,6 @@ operand(A) when is_atom(A) -> v(A);
|
|||||||
operand(I) when is_integer(I) -> {integer, I};
|
operand(I) when is_integer(I) -> {integer, I};
|
||||||
operand(T) -> T.
|
operand(T) -> T.
|
||||||
|
|
||||||
str_to_icode(String) when is_list(String) ->
|
|
||||||
str_to_icode(list_to_binary(String));
|
|
||||||
str_to_icode(BinStr) ->
|
|
||||||
Cpts = [size(BinStr) | aeb_memory:binary_to_words(BinStr)],
|
|
||||||
#tuple{ cpts = [ #integer{value = X} || X <- Cpts ] }.
|
|
||||||
|
|
||||||
check_event_type(Icode) ->
|
check_event_type(Icode) ->
|
||||||
case maps:get(event_type, Icode) of
|
case maps:get(event_type, Icode) of
|
||||||
{variant_t, Cons} ->
|
{variant_t, Cons} ->
|
||||||
@ -192,7 +188,8 @@ builtin_event(EventT) ->
|
|||||||
Types = [ T || {_Ix, T} <- IxTypes ],
|
Types = [ T || {_Ix, T} <- IxTypes ],
|
||||||
Indexed = [ Ix(Type, Var) || {Var, {indexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
|
Indexed = [ Ix(Type, Var) || {Var, {indexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
|
||||||
Data = [ {Type, Var} || {Var, {notindexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
|
Data = [ {Type, Var} || {Var, {notindexed, Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
|
||||||
EvtIndex = {unop, 'sha3', str_to_icode(Con)},
|
{ok, <<EvtIndexN:256>>} = eblake2:blake2b(?HASH_BYTES, list_to_binary(Con)),
|
||||||
|
EvtIndex = {integer, EvtIndexN},
|
||||||
{event, lists:reverse(Indexed) ++ [EvtIndex], Payload(Data)}
|
{event, lists:reverse(Indexed) ++ [EvtIndex], Payload(Data)}
|
||||||
end,
|
end,
|
||||||
Pat = fun(Tag, Types) -> {tuple, [{integer, Tag} | ArgPats(Types)]} end,
|
Pat = fun(Tag, Types) -> {tuple, [{integer, Tag} | ArgPats(Types)]} end,
|
||||||
|
@ -8,7 +8,8 @@
|
|||||||
jsx,
|
jsx,
|
||||||
syntax_tools,
|
syntax_tools,
|
||||||
getopt,
|
getopt,
|
||||||
aebytecode
|
aebytecode,
|
||||||
|
eblake2
|
||||||
]},
|
]},
|
||||||
{env,[]},
|
{env,[]},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user