formatters #14

Merged
zxq9 merged 15 commits from formatters into master 2025-12-22 11:14:20 +09:00
4 changed files with 140 additions and 13 deletions
Showing only changes of commit b542205c0e - Show all commits

View File

@ -66,6 +66,8 @@
contract_create/8, contract_create/8,
prepare_contract/1, prepare_contract/1,
prepare_aaci/1, prepare_aaci/1,
cache_aaci/2,
lookup_aaci/1,
aaci_lookup_spec/2, aaci_lookup_spec/2,
contract_call/5, contract_call/5,
contract_call/6, contract_call/6,
@ -1073,7 +1075,7 @@ contract_create2(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Source, Options,
InitArgs :: [string()], InitArgs :: [string()],
Result :: {ok, CreateTX} | {error, Reason}, Result :: {ok, CreateTX} | {error, Reason},
CreateTX :: binary(), CreateTX :: binary(),
Reason :: file:posix() | term(). Reason :: file:posix() | bad_fun_name | aaci_not_found | term().
%% @doc %% @doc
%% This function takes the compiler output (instead of starting from source), %% This function takes the compiler output (instead of starting from source),
%% and returns the unsigned create contract call data with default values. %% and returns the unsigned create contract call data with default values.
@ -1187,7 +1189,7 @@ read_aci(Path) ->
-spec contract_call(CallerID, AACI, ConID, Fun, Args) -> Result -spec contract_call(CallerID, AACI, ConID, Fun, Args) -> Result
when CallerID :: unicode:chardata(), when CallerID :: unicode:chardata(),
AACI :: aaci(), AACI :: aaci() | {aaci, Label :: term()},
ConID :: unicode:chardata(), ConID :: unicode:chardata(),
Fun :: string(), Fun :: string(),
Args :: [string()], Args :: [string()],
@ -1222,7 +1224,7 @@ contract_call(CallerID, AACI, ConID, Fun, Args) ->
-spec contract_call(CallerID, Gas, AACI, ConID, Fun, Args) -> Result -spec contract_call(CallerID, Gas, AACI, ConID, Fun, Args) -> Result
when CallerID :: unicode:chardata(), when CallerID :: unicode:chardata(),
Gas :: pos_integer(), Gas :: pos_integer(),
AACI :: aaci(), AACI :: aaci() | {aaci, Label :: term()},
ConID :: unicode:chardata(), ConID :: unicode:chardata(),
Fun :: string(), Fun :: string(),
Args :: [string()], Args :: [string()],
@ -1260,7 +1262,7 @@ contract_call(CallerID, Gas, AACI, ConID, Fun, Args) ->
GasPrice :: pos_integer(), GasPrice :: pos_integer(),
Amount :: non_neg_integer(), Amount :: non_neg_integer(),
TTL :: non_neg_integer(), TTL :: non_neg_integer(),
AACI :: aaci(), AACI :: aaci() | {aaci, Label :: term()},
ConID :: unicode:chardata(), ConID :: unicode:chardata(),
Fun :: string(), Fun :: string(),
Args :: [string()], Args :: [string()],
@ -2207,11 +2209,34 @@ zip_record_field({Name, Type}, {Remaining, Missing}) ->
{missing, {Remaining, [Name | Missing]}} {missing, {Remaining, [Name | Missing]}}
end. 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 lookup_aaci(Label) -> Result
when Label :: term(),
Result :: {ok, aaci()} | error.
%% @doc
%% Retrieve a previously prepared and cached AACI.
lookup_aaci(Label) ->
hz_man:lookup_aaci(Label).
-spec aaci_lookup_spec(AACI, Fun) -> {ok, Type} | {error, Reason} -spec aaci_lookup_spec(AACI, Fun) -> {ok, Type} | {error, Reason}
when AACI :: aaci(), when AACI :: aaci() | {aaci, Label :: term()},
Fun :: binary() | string(), Fun :: binary() | string(),
Type :: {term(), term()}, % FIXME Type :: {term(), term()}, % FIXME
Reason :: bad_fun_name. Reason :: bad_fun_name | aaci_not_found.
%% @doc %% @doc
%% Look up the type information of a given function, in the AACI provided by %% Look up the type information of a given function, in the AACI provided by
@ -2222,6 +2247,11 @@ aaci_lookup_spec({aaci, _, FunDefs, _}, Fun) ->
case maps:find(Fun, FunDefs) of case maps:find(Fun, FunDefs) of
A = {ok, _} -> A; A = {ok, _} -> A;
error -> {error, bad_fun_name} error -> {error, bad_fun_name}
end;
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. end.
-spec min_gas_price() -> integer(). -spec min_gas_price() -> integer().
@ -2251,7 +2281,12 @@ min_gas() ->
encode_call_data({aaci, _ContractName, FunDefs, _TypeDefs}, Fun, Args) -> encode_call_data({aaci, _ContractName, FunDefs, _TypeDefs}, Fun, Args) ->
case maps:find(Fun, FunDefs) of case maps:find(Fun, FunDefs) of
{ok, {ArgDef, _ResultDef}} -> encode_call_data2(ArgDef, Fun, Args); {ok, {ArgDef, _ResultDef}} -> encode_call_data2(ArgDef, Fun, Args);
error -> {error, bad_fun_name} error -> {error, bad_fun_name}
end;
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. end.
encode_call_data2(ArgDef, Fun, Args) -> encode_call_data2(ArgDef, Fun, Args) ->

View File

@ -38,14 +38,14 @@
-module(hz_grids). -module(hz_grids).
-vsn("0.7.0"). -vsn("0.7.0").
-export([url/2, parse/1, req/2, req/3]). -export([url/2, url/3, url/4, parse/1, req/2, req/3]).
-spec url(Instruction, HTTP) -> Result -spec url(Instruction, HTTP) -> Result
when Instruction :: spend | transfer | sign, when Instruction :: spend | transfer | sign,
HTTP :: uri_string:uri_string(), HTTP :: uri_string:uri_string(),
GRIDS :: uri_string:uri_string(), Result :: {ok, GRIDS} | uri_string:uri_error(),
Result :: {ok, GRIDS} | uri_string:uri_error(). GRIDS :: uri_string:uri_string().
%% @doc %% @doc
%% Takes %% Takes
@ -66,6 +66,63 @@ url2(Instruction, URL = #{path := Path}) ->
{ok, uri_string:recompose(GRIDS)}. {ok, uri_string:recompose(GRIDS)}.
-spec url(Instruction, Recipient, Amount) -> GRIDS
when Instruction :: {spend, Network} | {transfer, Node},
Network :: string(),
Node :: {inet:ip_address() | inet:hostname(), inet:port_number()}
| uri_string:uri_string(),
Recipient :: string(),
Amount :: non_neg_integer(),
GRIDS :: uri_string:uri_string().
%% @doc
%% Forms a GRIDS URL for spends or transfers.
%% @equiv uri(Instruction, Recipient, Amount, "").
url(Instruction, Recipient, Amount) ->
url(Instruction, Recipient, Amount, "").
-spec url(Instruction, Recipient, Amount, Payload) -> GRIDS
when Instruction :: {spend, Network} | {transfer, Node},
Network :: string(),
Node :: {inet:ip_address() | inet:hostname(), inet:port_number()}
| uri_string:uri_string(), % "http://..." | "https://..."
Recipient :: string(),
Amount :: non_neg_integer() | none,
Payload :: binary(),
GRIDS :: uri_string:uri_string().
%% @doc
%% Forms a GRIDS URL for spends or transfers.
url({spend, Network}, Recipient, Amount, Payload) ->
Elements = ["grids://", Network, "/1/s/", Recipient, qwargs(Amount, Payload)],
unicode:characters_to_list(Elements);
url({transfer, Node}, Recipient, Amount, Payload) ->
Prefix =
case Node of
{H, P} -> ["grid://", h_to_s(H), ":", integer_to_list(P)];
"https://" ++ H -> ["grids://", H];
"http://" ++ H -> ["grid://", H];
<<"https://", H/binary>> -> ["grids://", H];
<<"http://", H/binary>> -> ["grid://", H]
end,
unicode:characters_to_list([Prefix, "/1/t/", Recipient, qwargs(Amount, Payload)]).
h_to_s(Host) when is_list(Host) -> Host;
h_to_s(Host) when is_binary(Host) -> Host;
h_to_s(Host) when is_tuple(Host) -> inet:ntoa(Host);
h_to_s(Host) when is_atom(Host) -> atom_to_list(Host).
qwargs(none, "") ->
[];
qwargs(Amount, "") ->
["?a=", integer_to_list(Amount)];
qwargs(none, Payload) ->
[$? | uri_string:compose_query([{"p", Payload}])];
qwargs(Amount, Payload) ->
[$? | uri_string:compose_query([{"a", integer_to_list(Amount)}, {"p", Payload}])].
-spec parse(GRIDS) -> Result -spec parse(GRIDS) -> Result
when GRIDS :: string(), when GRIDS :: string(),
Result :: {ok, Instruction} | uri_string:error(), Result :: {ok, Instruction} | uri_string:error(),

View File

@ -20,6 +20,9 @@
chain_nodes/0, chain_nodes/1, chain_nodes/0, chain_nodes/1,
timeout/0, timeout/1]). timeout/0, timeout/1]).
%% Contract caching
-export([cache_aaci/2, lookup_aaci/1]).
%% The whole point of this module: %% The whole point of this module:
-export([request_sticky/1, request_sticky/2, request/1, request/2]). -export([request_sticky/1, request_sticky/2, request/1, request/2]).
@ -44,7 +47,8 @@
chain_nodes = {[], []} :: {[hz:chain_node()], [hz:chain_node()]}, chain_nodes = {[], []} :: {[hz:chain_node()], [hz:chain_node()]},
sticky = none :: none | hz:chain_node(), sticky = none :: none | hz:chain_node(),
fetchers = [] :: [#fetcher{}], fetchers = [] :: [#fetcher{}],
timeout = 5000 :: pos_integer()}). timeout = 5000 :: pos_integer(),
cache = #{} :: #{Label :: term() := AACI :: hz:aaci()}}).
-type state() :: #s{}. -type state() :: #s{}.
@ -94,6 +98,22 @@ timeout(Value) when 0 < Value, Value =< 120000 ->
gen_server:cast(?MODULE, {timeout, Value}). gen_server:cast(?MODULE, {timeout, Value}).
-spec cache_aaci(Label, AACI) -> ok
when Label :: term(),
AACI :: hz:aaci().
cache_aaci(Label, AACI) ->
gen_server:call(?MODULE, {cache, Label, AACI}).
-spec lookup_aaci(Label) -> Result
when Label :: term(),
Result :: {ok, hz:aaci()} | error.
lookup_aaci(Label) ->
gen_server:call(?MODULE, {lookup, Label}).
-spec request_sticky(Path) -> {ok, Value} | {error, Reason} -spec request_sticky(Path) -> {ok, Value} | {error, Reason}
when Path :: unicode:charlist(), when Path :: unicode:charlist(),
Value :: map(), Value :: map(),
@ -167,6 +187,12 @@ handle_call({request, Request}, From, State) ->
handle_call({request_sticky, Request}, From, State) -> handle_call({request_sticky, Request}, From, State) ->
NewState = do_request_sticky(Request, From, State), NewState = do_request_sticky(Request, From, State),
{noreply, NewState}; {noreply, NewState};
handle_call({lookup, Label}, _, State) ->
Result = do_lookup(Label, State),
{reply, Result, State};
handle_call({cache, Label, AACI}, _, State) ->
NewState = do_cache_aaci(Label, AACI, State),
{reply, ok, NewState};
handle_call(tls, _, State = #s{tls = TLS}) -> handle_call(tls, _, State = #s{tls = TLS}) ->
{reply, TLS, State}; {reply, TLS, State};
handle_call(chain_nodes, _, State) -> handle_call(chain_nodes, _, State) ->
@ -265,6 +291,15 @@ do_tls(_, State) ->
State. State.
do_cache_aaci(Label, AACI, State = #s{cache = Cache}) ->
NewCache = maps:put(Label, AACI, Cache),
State#s{cache = NewCache}.
do_lookup(Label, #s{cache = Cache}) ->
maps:find(Label, Cache).
do_request_sticky(_, From, State = #s{sticky = none}) -> do_request_sticky(_, From, State = #s{sticky = none}) ->
ok = gen_server:reply(From, {error, no_nodes}), ok = gen_server:reply(From, {error, no_nodes}),
State; State;

View File

@ -19,6 +19,6 @@
{copyright,"Craig Everett"}. {copyright,"Craig Everett"}.
{file_exts,[]}. {file_exts,[]}.
{license,"MIT"}. {license,"MIT"}.
{repo_url,"https://gitlab.com/ioecs/hakuzaru"}. {repo_url,"https://git.qpq.swiss/QPQ-AG/hakuzaru"}.
{tags,["qpq","gajumaru","blockchain","hakuzaru","crypto","defi"]}. {tags,["qpq","gajumaru","blockchain","hakuzaru","crypto","defi"]}.
{ws_url,"https://gitlab.com/ioecs/hakuzaru"}. {ws_url,"https://git.qpq.swiss/QPQ-AG/hakuzaru"}.