Add AACI caching

This commit is contained in:
2025-12-18 09:03:21 +09:00
parent c713053efd
commit aeb78eab38
2 changed files with 63 additions and 4 deletions
+27 -3
View File
@@ -66,6 +66,7 @@
contract_create/8,
prepare_contract/1,
prepare_aaci/1,
cache_aaci/2,
aaci_lookup_spec/2,
contract_call/5,
contract_call/6,
@@ -1073,7 +1074,7 @@ contract_create2(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Source, Options,
InitArgs :: [string()],
Result :: {ok, CreateTX} | {error, Reason},
CreateTX :: binary(),
Reason :: file:posix() | term().
Reason :: file:posix() | bad_fun_name | aaci_not_found | term().
%% @doc
%% This function takes the compiler output (instead of starting from source),
%% and returns the unsigned create contract call data with default values.
@@ -2207,17 +2208,35 @@ zip_record_field({Name, Type}, {Remaining, Missing}) ->
{missing, {Remaining, [Name | Missing]}}
end.
-spec cache_aaci(Label, AACI) -> ok
when Label :: term(),
AACI :: aaci().
%% @doc
%% Caches an AACI for future reference in calls that would otherwise require
%% the AACI as an argument. Once cached, a pre-built AACI can be referenced in
%% later calls by substituting the AACI argument with `{aaci, Label}'.
cache_aaci(Label, AACI) ->
hz_man:cache_aaci(Label, AACI).
-spec aaci_lookup_spec(AACI, Fun) -> {ok, Type} | {error, Reason}
when AACI :: aaci(),
Fun :: binary() | string(),
Type :: {term(), term()}, % FIXME
Reason :: bad_fun_name.
Reason :: bad_fun_name | aaci_not_found.
%% @doc
%% Look up the type information of a given function, in the AACI provided by
%% prepare_contract/1. This type information, particularly the return type, is
%% useful for calling decode_bytearray/2.
aaci_lookup_spec({aaci, Label}, Fun) ->
case hz_man:lookup_aaci(Label) of
{ok, AACI} -> aaci_lookup_spec(AACI, Fun);
error -> {error, aaci_not_found}
end;
aaci_lookup_spec({aaci, _, FunDefs, _}, Fun) ->
case maps:find(Fun, FunDefs) of
A = {ok, _} -> A;
@@ -2248,10 +2267,15 @@ min_gas() ->
200000.
encode_call_data({aaci, Label}, Fun, Args) ->
case hz_man:lookup_aaci(Label) of
{ok, AACI} -> encode_call_data(AACI, Fun, Args);
error -> {error, aaci_not_found}
end;
encode_call_data({aaci, _ContractName, FunDefs, _TypeDefs}, Fun, Args) ->
case maps:find(Fun, FunDefs) of
{ok, {ArgDef, _ResultDef}} -> encode_call_data2(ArgDef, Fun, Args);
error -> {error, bad_fun_name}
error -> {error, bad_fun_name}
end.
encode_call_data2(ArgDef, Fun, Args) ->