Compare commits
10 Commits
f54a33a293
...
558c4879fa
Author | SHA1 | Date | |
---|---|---|---|
![]() |
558c4879fa | ||
![]() |
6ebfc0d8b9 | ||
![]() |
532431cc36 | ||
![]() |
fb21e7f106 | ||
![]() |
d0da6c4beb | ||
![]() |
c28185082d | ||
![]() |
5fcece2c8e | ||
3a444daa6d | |||
1dc686215e | |||
c8670ae1b9 |
486
src/hz.erl
486
src/hz.erl
@ -76,6 +76,7 @@
|
|||||||
|
|
||||||
-export_type([chain_node/0, network_id/0, chain_error/0]).
|
-export_type([chain_node/0, network_id/0, chain_error/0]).
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
-type chain_node() :: {inet:ip_address(), inet:port_number()}.
|
-type chain_node() :: {inet:ip_address(), inet:port_number()}.
|
||||||
-type network_id() :: string().
|
-type network_id() :: string().
|
||||||
@ -656,13 +657,13 @@ dry_run_map(Map) ->
|
|||||||
|
|
||||||
%% @doc
|
%% @doc
|
||||||
%% Decode the "cb_XXXX" string that came out of a tx_info or dry_run, to
|
%% Decode the "cb_XXXX" string that came out of a tx_info or dry_run, to
|
||||||
%% the Erlang representation of FATE objects used by aeb_fate_encoding. See
|
%% the Erlang representation of FATE objects used by gmb_fate_encoding. See
|
||||||
%% decode_bytearray/2 for an alternative that provides simpler outputs based on
|
%% decode_bytearray/2 for an alternative that provides simpler outputs based on
|
||||||
%% information provided by an AACI.
|
%% information provided by an AACI.
|
||||||
|
|
||||||
decode_bytearray_fate(EncodedStr) ->
|
decode_bytearray_fate(EncodedStr) ->
|
||||||
Encoded = unicode:characters_to_binary(EncodedStr),
|
Encoded = unicode:characters_to_binary(EncodedStr),
|
||||||
{contract_bytearray, Binary} = aeser_api_encoder:decode(Encoded),
|
{contract_bytearray, Binary} = gmser_api_encoder:decode(Encoded),
|
||||||
case Binary of
|
case Binary of
|
||||||
<<>> -> {ok, none};
|
<<>> -> {ok, none};
|
||||||
<<"Out of gas">> -> {error, out_of_gas};
|
<<"Out of gas">> -> {error, out_of_gas};
|
||||||
@ -670,7 +671,7 @@ decode_bytearray_fate(EncodedStr) ->
|
|||||||
% FIXME there may be other errors that are encoded directly into
|
% FIXME there may be other errors that are encoded directly into
|
||||||
% the byte array. We could try and catch to at least return
|
% the byte array. We could try and catch to at least return
|
||||||
% *something* for cases that we don't already detect.
|
% *something* for cases that we don't already detect.
|
||||||
Object = aeb_fate_encoding:deserialize(Binary),
|
Object = gmb_fate_encoding:deserialize(Binary),
|
||||||
{ok, Object}
|
{ok, Object}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -952,7 +953,7 @@ contract_create(CreatorID, Path, InitArgs) ->
|
|||||||
%% </li>
|
%% </li>
|
||||||
%% <li>
|
%% <li>
|
||||||
%% <b>GasPrice:</b>
|
%% <b>GasPrice:</b>
|
||||||
%% This is a factor that is used calculate a value in aettos (the smallest unit of
|
%% This is a factor that is used calculate a value in pucks (the smallest unit of
|
||||||
%% Gajumaru's currency value) for the gas consumed. In times of high contention
|
%% Gajumaru's currency value) for the gas consumed. In times of high contention
|
||||||
%% in the mempool increasing the gas price increases the value of mining a given
|
%% in the mempool increasing the gas price increases the value of mining a given
|
||||||
%% transaction, thus making miners more likely to prioritize the high value ones.
|
%% transaction, thus making miners more likely to prioritize the high value ones.
|
||||||
@ -1058,14 +1059,14 @@ contract_create3(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, InitArg
|
|||||||
contract_create4(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
|
contract_create4(CreatorID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
|
||||||
PK = unicode:characters_to_binary(CreatorID),
|
PK = unicode:characters_to_binary(CreatorID),
|
||||||
try
|
try
|
||||||
{account_pubkey, OwnerID} = aeser_api_encoder:decode(PK),
|
{account_pubkey, OwnerID} = gmser_api_encoder:decode(PK),
|
||||||
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
|
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
|
||||||
catch
|
catch
|
||||||
Error:Reason -> {Error, Reason}
|
Error:Reason -> {Error, Reason}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
|
contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData) ->
|
||||||
Code = aeser_contract_code:serialize(Compiled),
|
Code = gmser_contract_code:serialize(Compiled),
|
||||||
Source = maps:get(contract_source, Compiled, <<>>),
|
Source = maps:get(contract_source, Compiled, <<>>),
|
||||||
VM = 1,
|
VM = 1,
|
||||||
ABI = 1,
|
ABI = 1,
|
||||||
@ -1073,7 +1074,7 @@ contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
|
|||||||
ContractCreateVersion = 1,
|
ContractCreateVersion = 1,
|
||||||
Type = contract_create_tx,
|
Type = contract_create_tx,
|
||||||
Fields =
|
Fields =
|
||||||
[{owner_id, aeser_id:create(account, OwnerID)},
|
[{owner_id, gmser_id:create(account, OwnerID)},
|
||||||
{nonce, Nonce},
|
{nonce, Nonce},
|
||||||
{code, Code},
|
{code, Code},
|
||||||
{source, Source},
|
{source, Source},
|
||||||
@ -1096,9 +1097,9 @@ contract_create5(OwnerID, Nonce, Amount, TTL, Gas, GasPrice, Compiled, CallData)
|
|||||||
{gas_price, int},
|
{gas_price, int},
|
||||||
{gas, int},
|
{gas, int},
|
||||||
{call_data, binary}],
|
{call_data, binary}],
|
||||||
TXB = aeser_chain_objects:serialize(Type, ContractCreateVersion, Template, Fields),
|
TXB = gmser_chain_objects:serialize(Type, ContractCreateVersion, Template, Fields),
|
||||||
try
|
try
|
||||||
{ok, aeser_api_encoder:encode(transaction, TXB)}
|
{ok, gmser_api_encoder:encode(transaction, TXB)}
|
||||||
catch
|
catch
|
||||||
error:Reason -> {error, Reason}
|
error:Reason -> {error, Reason}
|
||||||
end.
|
end.
|
||||||
@ -1271,7 +1272,7 @@ contract_call(CallerID, Gas, AACI, ConID, Fun, Args) ->
|
|||||||
%% </li>
|
%% </li>
|
||||||
%% <li>
|
%% <li>
|
||||||
%% <b>GasPrice:</b>
|
%% <b>GasPrice:</b>
|
||||||
%% This is a factor that is used calculate a value in aettos (the smallest unit of
|
%% This is a factor that is used calculate a value in pucks (the smallest unit of
|
||||||
%% Gajumaru's currency value) for the gas consumed. In times of high contention
|
%% Gajumaru's currency value) for the gas consumed. In times of high contention
|
||||||
%% in the mempool increasing the gas price increases the value of mining a given
|
%% in the mempool increasing the gas price increases the value of mining a given
|
||||||
%% transaction, thus making miners more likely to prioritize the high value ones.
|
%% transaction, thus making miners more likely to prioritize the high value ones.
|
||||||
@ -1329,7 +1330,7 @@ contract_call(CallerID, Nonce, Gas, GP, Amount, TTL, AACI, ConID, Fun, Args) ->
|
|||||||
contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
|
contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
|
||||||
CallerBin = unicode:characters_to_binary(CallerID),
|
CallerBin = unicode:characters_to_binary(CallerID),
|
||||||
try
|
try
|
||||||
{account_pubkey, PK} = aeser_api_encoder:decode(CallerBin),
|
{account_pubkey, PK} = gmser_api_encoder:decode(CallerBin),
|
||||||
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData)
|
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData)
|
||||||
catch
|
catch
|
||||||
Error:Reason -> {Error, Reason}
|
Error:Reason -> {Error, Reason}
|
||||||
@ -1338,7 +1339,7 @@ contract_call2(CallerID, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
|
|||||||
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
|
contract_call3(PK, Nonce, Gas, GasPrice, Amount, TTL, ConID, CallData) ->
|
||||||
ConBin = unicode:characters_to_binary(ConID),
|
ConBin = unicode:characters_to_binary(ConID),
|
||||||
try
|
try
|
||||||
{contract_pubkey, CK} = aeser_api_encoder:decode(ConBin),
|
{contract_pubkey, CK} = gmser_api_encoder:decode(ConBin),
|
||||||
contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData)
|
contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData)
|
||||||
catch
|
catch
|
||||||
Error:Reason -> {Error, Reason}
|
Error:Reason -> {Error, Reason}
|
||||||
@ -1349,9 +1350,9 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
|
|||||||
CallVersion = 1,
|
CallVersion = 1,
|
||||||
Type = contract_call_tx,
|
Type = contract_call_tx,
|
||||||
Fields =
|
Fields =
|
||||||
[{caller_id, aeser_id:create(account, PK)},
|
[{caller_id, gmser_id:create(account, PK)},
|
||||||
{nonce, Nonce},
|
{nonce, Nonce},
|
||||||
{contract_id, aeser_id:create(contract, CK)},
|
{contract_id, gmser_id:create(contract, CK)},
|
||||||
{abi_version, ABI},
|
{abi_version, ABI},
|
||||||
{ttl, TTL},
|
{ttl, TTL},
|
||||||
{amount, Amount},
|
{amount, Amount},
|
||||||
@ -1368,9 +1369,9 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
|
|||||||
{gas_price, int},
|
{gas_price, int},
|
||||||
{gas, int},
|
{gas, int},
|
||||||
{call_data, binary}],
|
{call_data, binary}],
|
||||||
TXB = aeser_chain_objects:serialize(Type, CallVersion, Template, Fields),
|
TXB = gmser_chain_objects:serialize(Type, CallVersion, Template, Fields),
|
||||||
try
|
try
|
||||||
{ok, aeser_api_encoder:encode(transaction, TXB)}
|
{ok, gmser_api_encoder:encode(transaction, TXB)}
|
||||||
catch
|
catch
|
||||||
error:Reason -> {error, Reason}
|
error:Reason -> {error, Reason}
|
||||||
end.
|
end.
|
||||||
@ -1385,101 +1386,106 @@ contract_call4(PK, Nonce, Gas, GasPrice, Amount, TTL, CK, CallData) ->
|
|||||||
%% of calldata
|
%% of calldata
|
||||||
|
|
||||||
prepare_contract(File) ->
|
prepare_contract(File) ->
|
||||||
case aeso_compiler:file(File, [{aci, json}]) of
|
case gmso_compiler:file(File, [{aci, json}]) of
|
||||||
{ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)};
|
{ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
prepare_aaci(ACI) ->
|
prepare_aaci(ACI) ->
|
||||||
% NOTE this will also pick up the main contract; as a result the main
|
% We want to take the types represented by the ACI, things like N1.T(N2.T),
|
||||||
% contract extraction later on shouldn't bother with typedefs.
|
% and dereference them down to concrete types like
|
||||||
Contracts = [ContractDef || #{contract := ContractDef} <- ACI],
|
% {tuple, [integer, string]}. Our type dereferencing algorithms
|
||||||
Types = simplify_contract_types(Contracts, #{}),
|
% shouldn't act directly on the JSON-based structures that the compiler
|
||||||
|
% gives us, though, though, so before we do the analysis, we should strip
|
||||||
|
% the ACI down to a list of 'opaque' type defintions and function specs.
|
||||||
|
{Name, OpaqueSpecs, TypeDefs} = convert_aci_types(ACI),
|
||||||
|
|
||||||
|
% Now that we have the opaque types, we can dereference the function specs
|
||||||
|
% down to the concrete types they actually represent. We annotate each
|
||||||
|
% subexpression of this concrete type with other info too, in case it helps
|
||||||
|
% make error messages easier to understand.
|
||||||
|
Specs = annotate_function_specs(OpaqueSpecs, TypeDefs, #{}),
|
||||||
|
|
||||||
|
{aaci, Name, Specs, TypeDefs}.
|
||||||
|
|
||||||
|
convert_aci_types(ACI) ->
|
||||||
|
% Find the main contract, so we can get the specifications of its
|
||||||
|
% entrypoints.
|
||||||
[{NameBin, SpecDefs}] =
|
[{NameBin, SpecDefs}] =
|
||||||
[{N, F}
|
[{N, F}
|
||||||
|| #{contract := #{kind := contract_main,
|
|| #{contract := #{kind := contract_main,
|
||||||
functions := F,
|
functions := F,
|
||||||
name := N}} <- ACI],
|
name := N}} <- ACI],
|
||||||
Name = binary_to_list(NameBin),
|
Name = binary_to_list(NameBin),
|
||||||
Specs = simplify_specs(SpecDefs, #{}, Types),
|
% Turn these specifications into opaque types that we can reason about.
|
||||||
{aaci, Name, Specs, Types}.
|
Specs = lists:map(fun convert_function_spec/1, SpecDefs),
|
||||||
|
|
||||||
simplify_contract_types([], Types) ->
|
% These specifications can reference other type definitions from the main
|
||||||
Types;
|
% contract and any other namespaces, so extract these types and convert
|
||||||
simplify_contract_types([Next | Rest], Types) ->
|
% them too.
|
||||||
TypeDefs = maps:get(typedefs, Next),
|
TypeDefTree = lists:map(fun convert_namespace_typedefs/1, ACI),
|
||||||
NameBin = maps:get(name, Next),
|
% The tree structure of the ACI naturally leads to a tree of opaque types,
|
||||||
|
% but we want a map, so flatten it out before we continue.
|
||||||
|
TypeDefMap = collect_opaque_types(TypeDefTree, #{}),
|
||||||
|
|
||||||
|
% This is all the information we actually need from the ACI, the rest is
|
||||||
|
% just pre-compute and acceleration.
|
||||||
|
{Name, Specs, TypeDefMap}.
|
||||||
|
|
||||||
|
convert_function_spec(#{name := NameBin, arguments := Args, returns := Result}) ->
|
||||||
Name = binary_to_list(NameBin),
|
Name = binary_to_list(NameBin),
|
||||||
Types2 = maps:put(Name, {[], contract}, Types),
|
ArgTypes = lists:map(fun convert_arg/1, Args),
|
||||||
Types3 = case maps:find(state, Next) of
|
ResultType = opaque_type([], Result),
|
||||||
{ok, StateDefACI} ->
|
{Name, ArgTypes, ResultType}.
|
||||||
StateDefOpaque = opaque_type([], StateDefACI),
|
|
||||||
maps:put(Name ++ ".state", {[], StateDefOpaque}, Types2);
|
|
||||||
error ->
|
|
||||||
Types2
|
|
||||||
end,
|
|
||||||
Types4 = simplify_typedefs(TypeDefs, Types3, Name ++ "."),
|
|
||||||
simplify_contract_types(Rest, Types4).
|
|
||||||
|
|
||||||
simplify_typedefs([], Types, _NamePrefix) ->
|
convert_arg(#{name := NameBin, type := TypeDef}) ->
|
||||||
Types;
|
|
||||||
simplify_typedefs([Next | Rest], Types, NamePrefix) ->
|
|
||||||
#{name := NameBin, vars := ParamDefs, typedef := T} = Next,
|
|
||||||
Name = NamePrefix ++ binary_to_list(NameBin),
|
|
||||||
Params = [binary_to_list(Param) || #{name := Param} <- ParamDefs],
|
|
||||||
Type = opaque_type(Params, T),
|
|
||||||
NewTypes = maps:put(Name, {Params, Type}, Types),
|
|
||||||
simplify_typedefs(Rest, NewTypes, NamePrefix).
|
|
||||||
|
|
||||||
simplify_specs([], Specs, _Types) ->
|
|
||||||
Specs;
|
|
||||||
simplify_specs([Next | Rest], Specs, Types) ->
|
|
||||||
#{name := NameBin, arguments := ArgDefs, returns := ResultDef} = Next,
|
|
||||||
Name = binary_to_list(NameBin),
|
Name = binary_to_list(NameBin),
|
||||||
ArgTypes = [simplify_args(Arg, Types) || Arg <- ArgDefs],
|
{ok, Type} = opaque_type([], TypeDef),
|
||||||
{ok, ResultType} = type(ResultDef, Types),
|
|
||||||
NewSpecs = maps:put(Name, {ArgTypes, ResultType}, Specs),
|
|
||||||
simplify_specs(Rest, NewSpecs, Types).
|
|
||||||
|
|
||||||
simplify_args(#{name := NameBin, type := TypeDef}, Types) ->
|
|
||||||
Name = binary_to_list(NameBin),
|
|
||||||
% FIXME We should make this error more informative, and continue
|
|
||||||
% propogating it up, so that the user can provide their own ACI and find
|
|
||||||
% out whether it worked or not. At that point ACI -> AACI could almost be a
|
|
||||||
% module or package of its own.
|
|
||||||
{ok, Type} = type(TypeDef, Types),
|
|
||||||
{Name, Type}.
|
{Name, Type}.
|
||||||
|
|
||||||
% Type preparation has two goals. First, we need a data structure that can be
|
convert_namespace_typedefs(#{namespace := NS}) ->
|
||||||
% traversed quickly, to take sophia-esque erlang expressions and turn them into
|
Name = namespace_name(NS),
|
||||||
% fate-esque erlang expressions that aebytecode can serialize. Second, we need
|
convert_typedefs(NS, Name);
|
||||||
% partially substituted names, so that error messages can be generated for why
|
convert_namespace_typedefs(#{contract := NS}) ->
|
||||||
% "foobar" is not valid as the third field of a `bazquux`, because the third
|
Name = namespace_name(NS),
|
||||||
% field is supposed to be `option(integer)`, not `string`.
|
ImplicitTypes = convert_implicit_types(NS, Name),
|
||||||
%
|
ExplicitTypes = convert_typedefs(NS, Name),
|
||||||
% To achieve this we need three representations of each type expression, which
|
[ImplicitTypes, ExplicitTypes].
|
||||||
% together form an 'annotated type'. First, we need the fully opaque name,
|
|
||||||
% "bazquux", then we need the normalized name, which is an opaque name with the
|
|
||||||
% bare-minimum substitution needed to make the outer-most type-constructor an
|
|
||||||
% identifiable built-in, ADT, or record type, and then we need the flattened
|
|
||||||
% type, which is the raw {variant, [{Name, Fields}, ...]} or
|
|
||||||
% {record, [{Name, Type}]} expression that can be used in actual Sophia->FATE
|
|
||||||
% coercion. The type sub-expressions in these flattened types will each be
|
|
||||||
% fully annotated as well, i.e. they will each contain *all three* of the above
|
|
||||||
% representations, so that coercion of subexpressions remains fast AND
|
|
||||||
% informative.
|
|
||||||
%
|
|
||||||
% In a lot of cases the opaque type given will already be normalized, in which
|
|
||||||
% case either the normalized field or the non-normalized field of an annotated
|
|
||||||
% type can simple be the atom `already_normalized`, which means error messages
|
|
||||||
% can simply render the normalized type expression and know that the error will
|
|
||||||
% make sense.
|
|
||||||
|
|
||||||
type(T, Types) ->
|
namespace_name(#{name := NameBin}) ->
|
||||||
O = opaque_type([], T),
|
binary_to_list(NameBin).
|
||||||
flatten_opaque_type(O, Types).
|
|
||||||
|
|
||||||
|
convert_implicit_types(#{state := StateDefACI}, Name) ->
|
||||||
|
StateDefOpaque = opaque_type([], StateDefACI),
|
||||||
|
[{Name, [], contract},
|
||||||
|
{Name ++ ".state", [], StateDefOpaque}];
|
||||||
|
convert_implicit_types(_, Name) ->
|
||||||
|
[{Name, [], contract}].
|
||||||
|
|
||||||
|
convert_typedefs(#{typedefs := TypeDefs}, Name) ->
|
||||||
|
convert_typedefs_loop(TypeDefs, Name ++ ".", []).
|
||||||
|
|
||||||
|
% Take a namespace that has already had a period appended, and use that as a
|
||||||
|
% prefix to convert and annotate a list of types.
|
||||||
|
convert_typedefs_loop([], _NamePrefix, Converted) ->
|
||||||
|
Converted;
|
||||||
|
convert_typedefs_loop([Next | Rest], NamePrefix, Converted) ->
|
||||||
|
#{name := NameBin, vars := ParamDefs, typedef := DefACI} = Next,
|
||||||
|
Name = NamePrefix ++ binary_to_list(NameBin),
|
||||||
|
Params = [binary_to_list(Param) || #{name := Param} <- ParamDefs],
|
||||||
|
Def = opaque_type(Params, DefACI),
|
||||||
|
convert_typedefs_loop(Rest, NamePrefix, [Converted, {Name, Params, Def}]).
|
||||||
|
|
||||||
|
collect_opaque_types([], Types) ->
|
||||||
|
Types;
|
||||||
|
collect_opaque_types([L | R], Types) ->
|
||||||
|
NewTypes = collect_opaque_types(L, Types),
|
||||||
|
collect_opaque_types(R, NewTypes);
|
||||||
|
collect_opaque_types({Name, Params, Def}, Types) ->
|
||||||
|
maps:put(Name, {Params, Def}, Types).
|
||||||
|
|
||||||
|
% Convert an ACI type defintion/spec into the 'opaque type' representation that
|
||||||
|
% our dereferencing algorithms can reason about.
|
||||||
opaque_type(Params, NameBin) when is_binary(NameBin) ->
|
opaque_type(Params, NameBin) when is_binary(NameBin) ->
|
||||||
Name = opaque_type_name(NameBin),
|
Name = opaque_type_name(NameBin),
|
||||||
case not is_atom(Name) and lists:member(Name, Params) of
|
case not is_atom(Name) and lists:member(Name, Params) of
|
||||||
@ -1503,7 +1509,7 @@ opaque_type(Params, Pair) when is_map(Pair) ->
|
|||||||
[{Name, TypeArgs}] = maps:to_list(Pair),
|
[{Name, TypeArgs}] = maps:to_list(Pair),
|
||||||
{opaque_type_name(Name), [opaque_type(Params, Arg) || Arg <- TypeArgs]}.
|
{opaque_type_name(Name), [opaque_type(Params, Arg) || Arg <- TypeArgs]}.
|
||||||
|
|
||||||
% atoms for builtins, lists for user defined types
|
% atoms for builtins, strings (lists) for user-defined types
|
||||||
opaque_type_name(<<"int">>) -> integer;
|
opaque_type_name(<<"int">>) -> integer;
|
||||||
opaque_type_name(<<"address">>) -> address;
|
opaque_type_name(<<"address">>) -> address;
|
||||||
opaque_type_name(<<"contract">>) -> contract;
|
opaque_type_name(<<"contract">>) -> contract;
|
||||||
@ -1514,16 +1520,49 @@ opaque_type_name(<<"map">>) -> map;
|
|||||||
opaque_type_name(<<"string">>) -> string;
|
opaque_type_name(<<"string">>) -> string;
|
||||||
opaque_type_name(Name) -> binary_to_list(Name).
|
opaque_type_name(Name) -> binary_to_list(Name).
|
||||||
|
|
||||||
flatten_opaque_type(T, Types) ->
|
% Type preparation has two goals. First, we need a data structure that can be
|
||||||
|
% traversed quickly, to take sophia-esque erlang expressions and turn them into
|
||||||
|
% fate-esque erlang expressions that gmbytecode can serialize. Second, we need
|
||||||
|
% partially substituted names, so that error messages can be generated for why
|
||||||
|
% "foobar" is not valid as the third field of a `bazquux`, because the third
|
||||||
|
% field is supposed to be `option(integer)`, not `string`.
|
||||||
|
%
|
||||||
|
% To achieve this we need three representations of each type expression, which
|
||||||
|
% together form an 'annotated type'. First, we need the fully opaque name,
|
||||||
|
% "bazquux", then we need the normalized name, which is an opaque name with the
|
||||||
|
% bare-minimum substitution needed to make the outer-most type-constructor an
|
||||||
|
% identifiable built-in, ADT, or record type, and then we need the dereferenced
|
||||||
|
% type, which is the raw {variant, [{Name, Fields}, ...]} or
|
||||||
|
% {record, [{Name, Type}]} expression that can be used in actual Sophia->FATE
|
||||||
|
% coercion. The type sub-expressions in these dereferenced types will each be
|
||||||
|
% fully annotated as well, i.e. they will each contain *all three* of the above
|
||||||
|
% representations, so that coercion of subexpressions remains fast and
|
||||||
|
% informative.
|
||||||
|
%
|
||||||
|
% In a lot of cases the opaque type given will already be normalized, in which
|
||||||
|
% case either the normalized field or the non-normalized field of an annotated
|
||||||
|
% type can simple be the atom `already_normalized`, which means error messages
|
||||||
|
% can simply render the normalized type expression and know that the error will
|
||||||
|
% make sense.
|
||||||
|
|
||||||
|
annotate_function_specs([], _Types, Specs) ->
|
||||||
|
Specs;
|
||||||
|
annotate_function_specs([{Name, ArgsOpaque, ResultOpaque} | Rest], Types, Specs) ->
|
||||||
|
{ok, Args} = annotate_types(ArgsOpaque, Types, []),
|
||||||
|
{ok, Result} = annotate_type(ResultOpaque, Types),
|
||||||
|
NewSpecs = maps:put(Name, {Args, Result}, Specs),
|
||||||
|
annotate_function_specs(Rest, Types, NewSpecs).
|
||||||
|
|
||||||
|
annotate_type(T, Types) ->
|
||||||
case normalize_opaque_type(T, Types) of
|
case normalize_opaque_type(T, Types) of
|
||||||
{ok, AlreadyNormalized, NOpaque, NExpanded} ->
|
{ok, AlreadyNormalized, NOpaque, NExpanded} ->
|
||||||
flatten_opaque_type2(T, AlreadyNormalized, NOpaque, NExpanded, Types);
|
annotate_type2(T, AlreadyNormalized, NOpaque, NExpanded, Types);
|
||||||
Error ->
|
Error ->
|
||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
flatten_opaque_type2(T, AlreadyNormalized, NOpaque, NExpanded, Types) ->
|
annotate_type2(T, AlreadyNormalized, NOpaque, NExpanded, Types) ->
|
||||||
case flatten_normalized_type(NExpanded, Types) of
|
case annotate_type_subexpressions(NExpanded, Types) of
|
||||||
{ok, Flat} ->
|
{ok, Flat} ->
|
||||||
case AlreadyNormalized of
|
case AlreadyNormalized of
|
||||||
true -> {ok, {T, already_normalized, Flat}};
|
true -> {ok, {T, already_normalized, Flat}};
|
||||||
@ -1533,48 +1572,48 @@ flatten_opaque_type2(T, AlreadyNormalized, NOpaque, NExpanded, Types) ->
|
|||||||
Error
|
Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
flatten_opaque_types([T | Rest], Types, Acc) ->
|
annotate_types([T | Rest], Types, Acc) ->
|
||||||
case flatten_opaque_type(T, Types) of
|
case annotate_type(T, Types) of
|
||||||
{ok, Type} -> flatten_opaque_types(Rest, Types, [Type | Acc]);
|
{ok, Type} -> annotate_types(Rest, Types, [Type | Acc]);
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
flatten_opaque_types([], _Types, Acc) ->
|
annotate_types([], _Types, Acc) ->
|
||||||
{ok, lists:reverse(Acc)}.
|
{ok, lists:reverse(Acc)}.
|
||||||
|
|
||||||
flatten_opaque_bindings([{Name, T} | Rest], Types, Acc) ->
|
annotate_type_subexpressions(PrimitiveType, _Types) when is_atom(PrimitiveType) ->
|
||||||
case flatten_opaque_type(T, Types) of
|
|
||||||
{ok, Type} -> flatten_opaque_bindings(Rest, Types, [{Name, Type} | Acc]);
|
|
||||||
Error -> Error
|
|
||||||
end;
|
|
||||||
flatten_opaque_bindings([], _Types, Acc) ->
|
|
||||||
{ok, lists:reverse(Acc)}.
|
|
||||||
|
|
||||||
flatten_opaque_variants([{Name, Elems} | Rest], Types, Acc) ->
|
|
||||||
case flatten_opaque_types(Elems, Types, []) of
|
|
||||||
{ok, ElemsFlat} -> flatten_opaque_variants(Rest, Types, [{Name, ElemsFlat} | Acc]);
|
|
||||||
Error -> Error
|
|
||||||
end;
|
|
||||||
flatten_opaque_variants([], _Types, Acc) ->
|
|
||||||
{ok, lists:reverse(Acc)}.
|
|
||||||
|
|
||||||
flatten_normalized_type(PrimitiveType, _Types) when is_atom(PrimitiveType) ->
|
|
||||||
{ok, PrimitiveType};
|
{ok, PrimitiveType};
|
||||||
flatten_normalized_type({variant, VariantsOpaque}, Types) ->
|
annotate_type_subexpressions({variant, VariantsOpaque}, Types) ->
|
||||||
case flatten_opaque_variants(VariantsOpaque, Types, []) of
|
case annotate_variants(VariantsOpaque, Types, []) of
|
||||||
{ok, Variants} -> {ok, {variant, Variants}};
|
{ok, Variants} -> {ok, {variant, Variants}};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
flatten_normalized_type({record, FieldsOpaque}, Types) ->
|
annotate_type_subexpressions({record, FieldsOpaque}, Types) ->
|
||||||
case flatten_opaque_bindings(FieldsOpaque, Types, []) of
|
case annotate_bindings(FieldsOpaque, Types, []) of
|
||||||
{ok, Fields} -> {ok, {record, Fields}};
|
{ok, Fields} -> {ok, {record, Fields}};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
flatten_normalized_type({T, ElemsOpaque}, Types) ->
|
annotate_type_subexpressions({T, ElemsOpaque}, Types) ->
|
||||||
case flatten_opaque_types(ElemsOpaque, Types, []) of
|
case annotate_types(ElemsOpaque, Types, []) of
|
||||||
{ok, Elems} -> {ok, {T, Elems}};
|
{ok, Elems} -> {ok, {T, Elems}};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
annotate_bindings([{Name, T} | Rest], Types, Acc) ->
|
||||||
|
case annotate_type(T, Types) of
|
||||||
|
{ok, Type} -> annotate_bindings(Rest, Types, [{Name, Type} | Acc]);
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
|
annotate_bindings([], _Types, Acc) ->
|
||||||
|
{ok, lists:reverse(Acc)}.
|
||||||
|
|
||||||
|
annotate_variants([{Name, Elems} | Rest], Types, Acc) ->
|
||||||
|
case annotate_types(Elems, Types, []) of
|
||||||
|
{ok, ElemsFlat} -> annotate_variants(Rest, Types, [{Name, ElemsFlat} | Acc]);
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
|
annotate_variants([], _Types, Acc) ->
|
||||||
|
{ok, lists:reverse(Acc)}.
|
||||||
|
|
||||||
normalize_opaque_type(T, Types) ->
|
normalize_opaque_type(T, Types) ->
|
||||||
case type_is_expanded(T) of
|
case type_is_expanded(T) of
|
||||||
false -> normalize_opaque_type(T, Types, true);
|
false -> normalize_opaque_type(T, Types, true);
|
||||||
@ -1644,12 +1683,39 @@ substitute_opaque_type(Bindings, {var, VarName}) ->
|
|||||||
false -> {error, invalid_aci};
|
false -> {error, invalid_aci};
|
||||||
{_, TypeArg} -> {ok, TypeArg}
|
{_, TypeArg} -> {ok, TypeArg}
|
||||||
end;
|
end;
|
||||||
|
substitute_opaque_type(Bindings, {variant, Args}) ->
|
||||||
|
case substitute_variant_types(Bindings, Args, []) of
|
||||||
|
{ok, Result} -> {ok, {variant, Result}};
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
|
substitute_opaque_type(Bindings, {record, Args}) ->
|
||||||
|
case substitute_record_types(Bindings, Args, []) of
|
||||||
|
{ok, Result} -> {ok, {record, Result}};
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
substitute_opaque_type(Bindings, {Connective, Args}) ->
|
substitute_opaque_type(Bindings, {Connective, Args}) ->
|
||||||
case substitute_opaque_types(Bindings, Args, []) of
|
case substitute_opaque_types(Bindings, Args, []) of
|
||||||
{ok, Result} -> {ok, {Connective, Result}};
|
{ok, Result} -> {ok, {Connective, Result}};
|
||||||
Error -> Error
|
Error -> Error
|
||||||
end;
|
end;
|
||||||
substitute_opaque_type(_Bindings, Type) -> {ok, Type}.
|
substitute_opaque_type(_Bindings, Type) ->
|
||||||
|
{ok, Type}.
|
||||||
|
|
||||||
|
substitute_variant_types(Bindings, [{VariantName, Elements} | Rest], Acc) ->
|
||||||
|
case substitute_opaque_types(Bindings, Elements, []) of
|
||||||
|
{ok, Result} -> substitute_variant_types(Bindings, Rest, [{VariantName, Result} | Acc]);
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
|
substitute_variant_types(_Bindings, [], Acc) ->
|
||||||
|
{ok, lists:reverse(Acc)}.
|
||||||
|
|
||||||
|
substitute_record_types(Bindings, [{ElementName, Type} | Rest], Acc) ->
|
||||||
|
case substitute_opaque_type(Bindings, Type) of
|
||||||
|
{ok, Result} -> substitute_record_types(Bindings, Rest, [{ElementName, Result} | Acc]);
|
||||||
|
Error -> Error
|
||||||
|
end;
|
||||||
|
substitute_record_types(_Bindings, [], Acc) ->
|
||||||
|
{ok, lists:reverse(Acc)}.
|
||||||
|
|
||||||
substitute_opaque_types(Bindings, [Next | Rest], Acc) ->
|
substitute_opaque_types(Bindings, [Next | Rest], Acc) ->
|
||||||
case substitute_opaque_type(Bindings, Next) of
|
case substitute_opaque_type(Bindings, Next) of
|
||||||
@ -1708,7 +1774,7 @@ coerce({O, N, integer}, S, to_fate) when is_list(S) ->
|
|||||||
end;
|
end;
|
||||||
coerce({O, N, address}, S, to_fate) ->
|
coerce({O, N, address}, S, to_fate) ->
|
||||||
try
|
try
|
||||||
case aeser_api_encoder:decode(unicode:characters_to_binary(S)) of
|
case gmser_api_encoder:decode(unicode:characters_to_binary(S)) of
|
||||||
{account_pubkey, Key} -> {ok, {address, Key}};
|
{account_pubkey, Key} -> {ok, {address, Key}};
|
||||||
_ -> single_error({invalid, O, N, S})
|
_ -> single_error({invalid, O, N, S})
|
||||||
end
|
end
|
||||||
@ -1716,11 +1782,11 @@ coerce({O, N, address}, S, to_fate) ->
|
|||||||
error:_ -> single_error({invalid, O, N, S})
|
error:_ -> single_error({invalid, O, N, S})
|
||||||
end;
|
end;
|
||||||
coerce({_, _, address}, {address, Bin}, from_fate) ->
|
coerce({_, _, address}, {address, Bin}, from_fate) ->
|
||||||
Address = aeser_api_encoder:encode(account_pubkey, Bin),
|
Address = gmser_api_encoder:encode(account_pubkey, Bin),
|
||||||
{ok, unicode:characters_to_list(Address)};
|
{ok, unicode:characters_to_list(Address)};
|
||||||
coerce({O, N, contract}, S, to_fate) ->
|
coerce({O, N, contract}, S, to_fate) ->
|
||||||
try
|
try
|
||||||
case aeser_api_encoder:decode(unicode:characters_to_binary(S)) of
|
case gmser_api_encoder:decode(unicode:characters_to_binary(S)) of
|
||||||
{contract_pubkey, Key} -> {ok, {contract, Key}};
|
{contract_pubkey, Key} -> {ok, {contract, Key}};
|
||||||
_ -> single_error({invalid, O, N, S})
|
_ -> single_error({invalid, O, N, S})
|
||||||
end
|
end
|
||||||
@ -1728,7 +1794,7 @@ coerce({O, N, contract}, S, to_fate) ->
|
|||||||
error:_ -> single_error({invalid, O, N, S})
|
error:_ -> single_error({invalid, O, N, S})
|
||||||
end;
|
end;
|
||||||
coerce({_, _, contract}, {contract, Bin}, from_fate) ->
|
coerce({_, _, contract}, {contract, Bin}, from_fate) ->
|
||||||
Address = aeser_api_encoder:encode(contract_pubkey, Bin),
|
Address = gmser_api_encoder:encode(contract_pubkey, Bin),
|
||||||
{ok, unicode:characters_to_list(Address)};
|
{ok, unicode:characters_to_list(Address)};
|
||||||
coerce({_, _, boolean}, true, _) ->
|
coerce({_, _, boolean}, true, _) ->
|
||||||
{ok, true};
|
{ok, true};
|
||||||
@ -1995,8 +2061,7 @@ aaci_lookup_spec({aaci, _, FunDefs, _}, Fun) ->
|
|||||||
%% @doc
|
%% @doc
|
||||||
%% This function always returns 1,000,000,000 in the current version.
|
%% This function always returns 1,000,000,000 in the current version.
|
||||||
%%
|
%%
|
||||||
%% This is the minimum gas price returned by aec_tx_pool:minimum_miner_gas_price(),
|
%% This is the minimum gas price returned by aec_tx_pool:minimum_miner_gas_price()
|
||||||
%% (the default set in aeternity_config_schema.json).
|
|
||||||
%%
|
%%
|
||||||
%% Surely there can be some more nuance to this, but until a "gas station" type
|
%% Surely there can be some more nuance to this, but until a "gas station" type
|
||||||
%% market/chain survey service exists we will use this naive value as a default
|
%% market/chain survey service exists we will use this naive value as a default
|
||||||
@ -2030,7 +2095,7 @@ encode_call_data({aaci, _ContractName, FunDefs, _TypeDefs}, Fun, Args) ->
|
|||||||
|
|
||||||
encode_call_data2(ArgDef, Fun, Args) ->
|
encode_call_data2(ArgDef, Fun, Args) ->
|
||||||
case coerce_bindings(ArgDef, Args, to_fate) of
|
case coerce_bindings(ArgDef, Args, to_fate) of
|
||||||
{ok, Coerced} -> aeb_fate_abi:create_calldata(Fun, Coerced);
|
{ok, Coerced} -> gmb_fate_abi:create_calldata(Fun, Coerced);
|
||||||
Errors -> Errors
|
Errors -> Errors
|
||||||
end.
|
end.
|
||||||
|
|
||||||
@ -2050,7 +2115,7 @@ encode_call_data2(ArgDef, Fun, Args) ->
|
|||||||
%% check failed before verification was able to pass or fail (bad key encoding or similar).
|
%% check failed before verification was able to pass or fail (bad key encoding or similar).
|
||||||
|
|
||||||
verify_signature(Sig, Message, PubKey) ->
|
verify_signature(Sig, Message, PubKey) ->
|
||||||
case aeser_api_encoder:decode(PubKey) of
|
case gmser_api_encoder:decode(PubKey) of
|
||||||
{account_pubkey, PK} -> verify_signature2(Sig, Message, PK);
|
{account_pubkey, PK} -> verify_signature2(Sig, Message, PK);
|
||||||
Other -> {error, {bad_key, Other}}
|
Other -> {error, {bad_key, Other}}
|
||||||
end.
|
end.
|
||||||
@ -2059,20 +2124,11 @@ verify_signature2(Sig, Message, PK) ->
|
|||||||
% Gajumaru signatures require messages to be salted and hashed, then
|
% Gajumaru signatures require messages to be salted and hashed, then
|
||||||
% the hash is what gets signed in order to protect
|
% the hash is what gets signed in order to protect
|
||||||
% the user from accidentally signing a transaction disguised as a message.
|
% the user from accidentally signing a transaction disguised as a message.
|
||||||
%
|
|
||||||
% Salt the message then hash with blake2b. See:
|
|
||||||
% 1. Erlang Blake2 blake2b/2 function:
|
|
||||||
% https://gitlab.com/ioecs/eblake2/blob/60a079f00d72d1bfcc25de8e6996d28f912db3fd/src/eblake2.erl#L23-L25
|
|
||||||
% 2. SDK salting step:
|
|
||||||
% https://gitlab.com/ioecs/aepp-sdk-js/blob/370f1e30064ad0239ba59931908d9aba0a2e86b6/src/utils/crypto.ts#L171-L175
|
|
||||||
% 3. SDK hashing:
|
|
||||||
% https://gitlab.com/ioecs/aepp-sdk-js/blob/370f1e30064ad0239ba59931908d9aba0a2e86b6/src/utils/crypto.ts#L83-L85
|
|
||||||
Prefix = <<"Gajumaru Signed Message:\n">>,
|
Prefix = <<"Gajumaru Signed Message:\n">>,
|
||||||
{ok, PSize} = vencode(byte_size(Prefix)),
|
{ok, PSize} = vencode(byte_size(Prefix)),
|
||||||
{ok, MSize} = vencode(byte_size(Message)),
|
{ok, MSize} = vencode(byte_size(Message)),
|
||||||
Smashed = iolist_to_binary([PSize, Prefix, MSize, Message]),
|
Smashed = iolist_to_binary([PSize, Prefix, MSize, Message]),
|
||||||
{ok, Hashed} = eblake2:blake2b(32, Smashed),
|
{ok, Hashed} = eblake2:blake2b(32, Smashed),
|
||||||
% Signature = <<(binary_to_integer(Sig, 16)):(64 * 8)>>,
|
|
||||||
Signature = base64:decode(Sig),
|
Signature = base64:decode(Sig),
|
||||||
Result = ecu_eddsa:sign_verify_detached(Signature, Hashed, PK),
|
Result = ecu_eddsa:sign_verify_detached(Signature, Hashed, PK),
|
||||||
{ok, Result}.
|
{ok, Result}.
|
||||||
@ -2140,3 +2196,151 @@ eu(N, Size) ->
|
|||||||
% /v3/debug/check-tx/pool/{hash}
|
% /v3/debug/check-tx/pool/{hash}
|
||||||
% /v3/debug/token-supply/height/{height}
|
% /v3/debug/token-supply/height/{height}
|
||||||
% /v3/debug/crash
|
% /v3/debug/crash
|
||||||
|
|
||||||
|
|
||||||
|
%%% Simple coerce/3 tests
|
||||||
|
|
||||||
|
try_coerce(Type, Sophia, Fate) ->
|
||||||
|
FateActual = coerce(Type, Sophia, to_fate),
|
||||||
|
SophiaActual = coerce(Type, Fate, from_fate),
|
||||||
|
case {ok, Fate} == FateActual of
|
||||||
|
true ->
|
||||||
|
ok;
|
||||||
|
false ->
|
||||||
|
erlang:error({to_fate_failed, Fate, FateActual})
|
||||||
|
end,
|
||||||
|
case {ok, Sophia} == SophiaActual of
|
||||||
|
true ->
|
||||||
|
ok;
|
||||||
|
false ->
|
||||||
|
erlang:error({from_fate_failed, Sophia, SophiaActual})
|
||||||
|
end,
|
||||||
|
ok.
|
||||||
|
|
||||||
|
coerce_int_test() ->
|
||||||
|
{ok, Type} = annotate_type(integer, #{}),
|
||||||
|
try_coerce(Type, 123, 123).
|
||||||
|
|
||||||
|
coerce_address_test() ->
|
||||||
|
{ok, Type} = annotate_type(address, #{}),
|
||||||
|
try_coerce(Type,
|
||||||
|
"ak_2FTnrGfV8qsfHpaSEHpBrziioCpwwzLqSevHqfxQY3PaAAdARx",
|
||||||
|
{address, <<164,136,155,90,124,22,40,206,255,76,213,56,238,123,
|
||||||
|
167,208,53,78,40,235,2,163,132,36,47,183,228,151,9,
|
||||||
|
210,39,214>>}).
|
||||||
|
|
||||||
|
coerce_contract_test() ->
|
||||||
|
{ok, Type} = annotate_type(contract, #{}),
|
||||||
|
try_coerce(Type,
|
||||||
|
"ct_2FTnrGfV8qsfHpaSEHpBrziioCpwwzLqSevHqfxQY3PaAAdARx",
|
||||||
|
{contract, <<164,136,155,90,124,22,40,206,255,76,213,56,238,123,
|
||||||
|
167,208,53,78,40,235,2,163,132,36,47,183,228,151,9,
|
||||||
|
210,39,214>>}).
|
||||||
|
|
||||||
|
coerce_bool_test() ->
|
||||||
|
{ok, Type} = annotate_type(boolean, #{}),
|
||||||
|
try_coerce(Type, true, true),
|
||||||
|
try_coerce(Type, false, false).
|
||||||
|
|
||||||
|
coerce_string_test() ->
|
||||||
|
{ok, Type} = annotate_type(string, #{}),
|
||||||
|
try_coerce(Type, "hello world", <<"hello world">>).
|
||||||
|
|
||||||
|
coerce_list_test() ->
|
||||||
|
{ok, Type} = annotate_type({list, [string]}, #{}),
|
||||||
|
try_coerce(Type, ["hello world", [65, 32, 65]], [<<"hello world">>, <<65, 32, 65>>]).
|
||||||
|
|
||||||
|
coerce_map_test() ->
|
||||||
|
{ok, Type} = annotate_type({map, [string, {list, [integer]}]}, #{}),
|
||||||
|
try_coerce(Type, #{"a" => "a", "b" => "b"}, #{<<"a">> => "a", <<"b">> => "b"}).
|
||||||
|
|
||||||
|
coerce_tuple_test() ->
|
||||||
|
{ok, Type} = annotate_type({tuple, [integer, string]}, #{}),
|
||||||
|
try_coerce(Type, {123, "456"}, {tuple, {123, <<"456">>}}).
|
||||||
|
|
||||||
|
coerce_variant_test() ->
|
||||||
|
{ok, Type} = annotate_type({variant, [{"A", [integer]},
|
||||||
|
{"B", [integer, integer]}]},
|
||||||
|
#{}),
|
||||||
|
try_coerce(Type, {"A", 123}, {variant, [1, 2], 0, {123}}),
|
||||||
|
try_coerce(Type, {"B", 456, 789}, {variant, [1, 2], 1, {456, 789}}).
|
||||||
|
|
||||||
|
coerce_record_test() ->
|
||||||
|
{ok, Type} = annotate_type({record, [{"a", integer}, {"b", integer}]}, #{}),
|
||||||
|
try_coerce(Type, #{"a" => 123, "b" => 456}, {tuple, {123, 456}}).
|
||||||
|
|
||||||
|
|
||||||
|
%%% Complex AACI paramter and namespace tests
|
||||||
|
|
||||||
|
aaci_from_string(String) ->
|
||||||
|
case aeso_compiler:from_string(String, [{aci, json}]) of
|
||||||
|
{ok, #{aci := ACI}} -> {ok, prepare_aaci(ACI)};
|
||||||
|
Error -> Error
|
||||||
|
end.
|
||||||
|
|
||||||
|
namespace_coerce_test() ->
|
||||||
|
Contract = "
|
||||||
|
namespace N =
|
||||||
|
record pair = { a : int, b : int }
|
||||||
|
|
||||||
|
contract C =
|
||||||
|
entrypoint f(): N.pair = { a = 1, b = 2 }
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "f"),
|
||||||
|
try_coerce(Output, #{"a" => 123, "b" => 456}, {tuple, {123, 456}}).
|
||||||
|
|
||||||
|
record_substitution_test() ->
|
||||||
|
Contract = "
|
||||||
|
contract C =
|
||||||
|
record pair('t) = { a : 't, b : 't }
|
||||||
|
entrypoint f(): pair(int) = { a = 1, b = 2 }
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "f"),
|
||||||
|
try_coerce(Output, #{"a" => 123, "b" => 456}, {tuple, {123, 456}}).
|
||||||
|
|
||||||
|
tuple_substitution_test() ->
|
||||||
|
Contract = "
|
||||||
|
contract C =
|
||||||
|
type triple('t1, 't2) = int * 't1 * 't2
|
||||||
|
entrypoint f(): triple(int, string) = (1, 2, \"hello\")
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "f"),
|
||||||
|
try_coerce(Output, {1, 2, "hello"}, {tuple, {1, 2, <<"hello">>}}).
|
||||||
|
|
||||||
|
variant_substitution_test() ->
|
||||||
|
Contract = "
|
||||||
|
contract C =
|
||||||
|
datatype adt('a, 'b) = Left('a, 'b) | Right('b, int)
|
||||||
|
entrypoint f(): adt(string, int) = Left(\"hi\", 1)
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "f"),
|
||||||
|
try_coerce(Output, {"Left", "hi", 1}, {variant, [2, 2], 0, {<<"hi">>, 1}}),
|
||||||
|
try_coerce(Output, {"Right", 2, 3}, {variant, [2, 2], 1, {2, 3}}).
|
||||||
|
|
||||||
|
nested_coerce_test() ->
|
||||||
|
Contract = "
|
||||||
|
contract C =
|
||||||
|
type pair('t) = 't * 't
|
||||||
|
record r = { f1 : pair(int), f2: pair(string) }
|
||||||
|
entrypoint f(): r = { f1 = (1, 2), f2 = (\"a\", \"b\") }
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "f"),
|
||||||
|
try_coerce(Output,
|
||||||
|
#{ "f1" => {1, 2}, "f2" => {"a", "b"}},
|
||||||
|
{tuple, {{tuple, {1, 2}}, {tuple, {<<"a">>, <<"b">>}}}}).
|
||||||
|
|
||||||
|
state_coerce_test() ->
|
||||||
|
Contract = "
|
||||||
|
contract C =
|
||||||
|
type state = int
|
||||||
|
entrypoint init(): state = 0
|
||||||
|
",
|
||||||
|
{ok, AACI} = aaci_from_string(Contract),
|
||||||
|
{ok, {[], Output}} = aaci_lookup_spec(AACI, "init"),
|
||||||
|
try_coerce(Output, 0, 0).
|
||||||
|
|
||||||
|
@ -6,8 +6,6 @@
|
|||||||
|
|
||||||
-export([connect/4, slowly_connect/4]).
|
-export([connect/4, slowly_connect/4]).
|
||||||
|
|
||||||
-include("$zx_include/zx_logger.hrl").
|
|
||||||
|
|
||||||
|
|
||||||
connect(Node = {Host, Port}, Request, From, Timeout) ->
|
connect(Node = {Host, Port}, Request, From, Timeout) ->
|
||||||
Timer = erlang:send_after(Timeout, self(), timeout),
|
Timer = erlang:send_after(Timeout, self(), timeout),
|
||||||
@ -236,3 +234,10 @@ url({Node, Port}, Path) when is_list(Node) ->
|
|||||||
["https://", Node, ":", integer_to_list(Port), Path];
|
["https://", Node, ":", integer_to_list(Port), Path];
|
||||||
url({Node, Port}, Path) when is_tuple(Node) ->
|
url({Node, Port}, Path) when is_tuple(Node) ->
|
||||||
["https://", inet:ntoa(Node), ":", integer_to_list(Port), Path].
|
["https://", inet:ntoa(Node), ":", integer_to_list(Port), Path].
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
log(Level, Format, Args) ->
|
||||||
|
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
|
||||||
|
Entry = unicode:characters_to_list(Raw),
|
||||||
|
logger:log(Level, Entry).
|
||||||
|
@ -29,8 +29,6 @@
|
|||||||
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
|
||||||
code_change/3, terminate/2]).
|
code_change/3, terminate/2]).
|
||||||
|
|
||||||
%% TODO: Make logging more flexible
|
|
||||||
-include("$zx_include/zx_logger.hrl").
|
|
||||||
|
|
||||||
|
|
||||||
%%% Type and Record Definitions
|
%%% Type and Record Definitions
|
||||||
@ -197,7 +195,7 @@ handle_info({'DOWN', Mon, process, PID, Info}, State) ->
|
|||||||
NewState = handle_down(PID, Mon, Info, State),
|
NewState = handle_down(PID, Mon, Info, State),
|
||||||
{noreply, NewState};
|
{noreply, NewState};
|
||||||
handle_info(Unexpected, State) ->
|
handle_info(Unexpected, State) ->
|
||||||
ok = log("Unexpected info: ~tp~n", [Unexpected]),
|
ok = log(warning, "Unexpected info: ~tp~n", [Unexpected]),
|
||||||
{noreply, State}.
|
{noreply, State}.
|
||||||
|
|
||||||
|
|
||||||
@ -287,3 +285,11 @@ do_request(Request,
|
|||||||
do_request(Request, From, State = #s{chain_nodes = {[], Used}}) ->
|
do_request(Request, From, State = #s{chain_nodes = {[], Used}}) ->
|
||||||
Fresh = lists:reverse(Used),
|
Fresh = lists:reverse(Used),
|
||||||
do_request(Request, From, State#s{chain_nodes = {Fresh, []}}).
|
do_request(Request, From, State#s{chain_nodes = {Fresh, []}}).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
log(Level, Format, Args) ->
|
||||||
|
Raw = io_lib:format("~w ~w: " ++ Format, [?MODULE, self() | Args]),
|
||||||
|
Entry = unicode:characters_to_list(Raw),
|
||||||
|
logger:log(Level, Entry).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user