From 126e04ae427d3f68e5745bca46ca48251db82674 Mon Sep 17 00:00:00 2001 From: skkw Date: Tue, 10 Sep 2019 15:33:02 +0200 Subject: [PATCH] support for AENS.update call --- rebar.config | 2 +- rebar.lock | 2 +- src/aeso_ast_infer_types.erl | 10 +++++++- src/aeso_ast_to_fcode.erl | 42 ++++++++++++++++++---------------- src/aeso_ast_to_icode.erl | 10 ++++++++ src/aeso_fcode_to_fate.erl | 5 +++- src/aeso_icode.erl | 44 ++++++++++++++++++++---------------- test/contracts/aens.aes | 17 +++++++++++++- 8 files changed, 88 insertions(+), 44 deletions(-) diff --git a/rebar.config b/rebar.config index e5ca552..87edb36 100644 --- a/rebar.config +++ b/rebar.config @@ -2,7 +2,7 @@ {erl_opts, [debug_info]}. -{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"4f4d6d3"}}} +{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {ref,"38f4f0e"}}} , {getopt, "1.0.1"} , {eblake2, "1.0.0"} , {jsx, {git, "https://github.com/talentdeficit/jsx.git", diff --git a/rebar.lock b/rebar.lock index f5cf812..f4e93cb 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,7 +1,7 @@ {"1.1.0", [{<<"aebytecode">>, {git,"https://github.com/aeternity/aebytecode.git", - {ref,"4f4d6d30cd2c46b3830454d650a424d513f69134"}}, + {ref,"38f4f0ecd75db592f596b9687c37945d4fc807d7"}}, 0}, {<<"aeserialization">>, {git,"https://github.com/aeternity/aeserialization.git", diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 9376129..db5b39a 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -389,6 +389,8 @@ global_env() -> Signature = {named_arg_t, Ann, SignId, SignId, {typed, Ann, SignDef, SignId}}, SignFun = fun(Ts, T) -> {type_sig, [stateful|Ann], none, [Signature], Ts, T} end, TTL = {qid, Ann, ["Chain", "ttl"]}, + Pointee = {qid, Ann, ["AENS", "pointee"]}, + Fee = Int, [A, Q, R, K, V] = lists:map(TVar, ["a", "q", "r", "k", "v"]), @@ -402,6 +404,10 @@ global_env() -> %% TTL constructors {"RelativeTTL", Fun1(Int, TTL)}, {"FixedTTL", Fun1(Int, TTL)}, + %% AENS pointee constructors + {"AccountPointee", Fun1(Address, Pointee)}, + {"OraclePointee", Fun1(Address, Pointee)}, + {"ContractPointee", Fun1(Address, Pointee)}, %% Abort {"abort", Fun1(String, A)}, {"require", Fun([Bool, String], Unit)}]) @@ -462,7 +468,9 @@ global_env() -> {"preclaim", SignFun([Address, Hash], Unit)}, {"claim", SignFun([Address, String, Int, Int], Unit)}, {"transfer", SignFun([Address, Address, String], Unit)}, - {"revoke", SignFun([Address, String], Unit)}]) }, + {"revoke", SignFun([Address, String], Unit)}, + {"update", SignFun([Address, String, Option(TTL), Option(TTL), Option(Map(String, Pointee))], Unit)}]) + , types = MkDefs([{"pointee", 0}]) }, MapScope = #scope { funs = MkDefs( diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index 35d5d06..6fa163a 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -164,10 +164,13 @@ init_env(Options) -> #{ type_env => init_type_env(), fun_env => #{}, builtins => builtins(), - con_env => #{["None"] => #con_tag{ tag = 0, arities = [0, 1] }, - ["Some"] => #con_tag{ tag = 1, arities = [0, 1] }, - ["RelativeTTL"] => #con_tag{ tag = 0, arities = [1, 1] }, - ["FixedTTL"] => #con_tag{ tag = 1, arities = [1, 1] } + con_env => #{["None"] => #con_tag{ tag = 0, arities = [0, 1] }, + ["Some"] => #con_tag{ tag = 1, arities = [0, 1] }, + ["RelativeTTL"] => #con_tag{ tag = 0, arities = [1, 1] }, + ["FixedTTL"] => #con_tag{ tag = 1, arities = [1, 1] }, + ["AccountPointee"] => #con_tag{ tag = 0, arities = [1, 1, 1] }, + ["OraclePointee"] => #con_tag{ tag = 1, arities = [1, 1, 1] }, + ["ContractPointee"] => #con_tag{ tag = 2, arities = [1, 1, 1] } }, options => Options, functions => #{} }. @@ -188,7 +191,7 @@ builtins() -> {"respond", 4}, {"extend", 3}, {"get_answer", 2}, {"check", 1}, {"check_query", 2}]}, {["AENS"], [{"resolve", 2}, {"preclaim", 3}, {"claim", 5}, {"transfer", 4}, - {"revoke", 3}]}, + {"revoke", 3}, {"update", 6}]}, {["Map"], [{"from_list", 1}, {"to_list", 1}, {"lookup", 2}, {"lookup_default", 3}, {"delete", 2}, {"member", 2}, {"size", 1}]}, {["Crypto"], [{"verify_sig", 3}, {"verify_sig_secp256k1", 3}, @@ -212,20 +215,21 @@ builtins() -> -spec init_type_env() -> type_env(). init_type_env() -> - #{ ["int"] => ?type(integer), - ["bool"] => ?type(boolean), - ["bits"] => ?type(bits), - ["char"] => ?type(integer), - ["string"] => ?type(string), - ["address"] => ?type(address), - ["hash"] => ?type(hash), - ["signature"] => ?type(signature), - ["oracle"] => ?type(Q, R, {oracle, Q, R}), - ["oracle_query"] => ?type(_, _, oracle_query), %% TODO: not in Fate - ["list"] => ?type(T, {list, T}), - ["map"] => ?type(K, V, {map, K, V}), - ["option"] => ?type(T, {variant, [[], [T]]}), - ["Chain", "ttl"] => ?type({variant, [[integer], [integer]]}) + #{ ["int"] => ?type(integer), + ["bool"] => ?type(boolean), + ["bits"] => ?type(bits), + ["char"] => ?type(integer), + ["string"] => ?type(string), + ["address"] => ?type(address), + ["hash"] => ?type(hash), + ["signature"] => ?type(signature), + ["oracle"] => ?type(Q, R, {oracle, Q, R}), + ["oracle_query"] => ?type(_, _, oracle_query), %% TODO: not in Fate + ["list"] => ?type(T, {list, T}), + ["map"] => ?type(K, V, {map, K, V}), + ["option"] => ?type(T, {variant, [[], [T]]}), + ["Chain", "ttl"] => ?type({variant, [[integer], [integer]]}), + ["AENS", "pointee"] => ?type({variant, [[address], [address], [address]]}) }. is_no_code(Env) -> diff --git a/src/aeso_ast_to_icode.erl b/src/aeso_ast_to_icode.erl index acf68ea..0dc28fa 100644 --- a/src/aeso_ast_to_icode.erl +++ b/src/aeso_ast_to_icode.erl @@ -463,6 +463,7 @@ is_builtin_fun({qid, _, ["AENS", "preclaim"]}, _Icode) -> is_builtin_fun({qid, _, ["AENS", "claim"]}, _Icode) -> true; is_builtin_fun({qid, _, ["AENS", "transfer"]}, _Icode) -> true; is_builtin_fun({qid, _, ["AENS", "revoke"]}, _Icode) -> true; +is_builtin_fun({qid, _, ["AENS", "update"]}, _Icode) -> true; is_builtin_fun({qid, _, ["Map", "lookup"]}, _Icode) -> true; is_builtin_fun({qid, _, ["Map", "lookup_default"]}, _Icode) -> true; is_builtin_fun({qid, _, ["Map", "member"]}, _Icode) -> true; @@ -618,6 +619,12 @@ builtin_code(_, {qid, _, ["AENS", "revoke"]}, Args, _, _, Icode) -> [ast_body(Addr, Icode), ast_body(Name, Icode), ast_body(Sign, Icode)], [word, word, sign_t()], {tuple, []}); +builtin_code(_, {qid, _, ["AENS", "update"]}, Args, _, _, Icode) -> + {Sign, [Addr, Name, TTL, ClientTTL, Pointers]} = get_signature_arg(Args), + prim_call(?PRIM_CALL_AENS_UPDATE, #integer{value = 0}, + [ast_body(Addr, Icode), ast_body(Name, Icode), ast_body(TTL, Icode), ast_body(ClientTTL, Icode), ast_body(Pointers, Icode), ast_body(Sign, Icode)], + [word, string, word, word, word, sign_t()], {tuple, []}); + %% -- Maps %% -- lookup functions builtin_code(_, {qid, _, ["Map", "lookup"]}, [Key, Map], _, _, Icode) -> @@ -926,6 +933,9 @@ ast_typerep1({variant_t, Cons}, Icode) -> ttl_t(Icode) -> ast_typerep({qid, [], ["Chain", "ttl"]}, Icode). +%% pointee_t(Icode) -> +%% ast_typerep({qid, [], ["AENS", "pointee"]}, Icode). + sign_t() -> bytes_t(64). bytes_t(Len) when Len =< 32 -> word; bytes_t(Len) -> {tuple, lists:duplicate((31 + Len) div 32, word)}. diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index 7c399b9..8c93490 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -572,6 +572,9 @@ builtin_to_scode(Env, aens_transfer, [_Sign, _From, _To, _Name] = Args) -> builtin_to_scode(Env, aens_revoke, [_Sign, _Account, _Name] = Args) -> call_to_scode(Env, [aeb_fate_ops:aens_revoke(?a, ?a, ?a), tuple(0)], Args); +builtin_to_scode(Env, aens_update, [_Sign, _Account, _NameString, _TTL, _ClientTTL, _Pointers] = Args) -> + call_to_scode(Env, [aeb_fate_ops:aens_update(?a, ?a, ?a, ?a, ?a, ?a), + tuple(0)], Args); builtin_to_scode(_Env, auth_tx_hash, []) -> [aeb_fate_ops:auth_tx_hash(?a)]. @@ -896,7 +899,7 @@ attributes(I) -> {'AENS_RESOLVE', A, B, C, D} -> Impure(A, [B, C, D]); {'AENS_PRECLAIM', A, B, C} -> Impure(none, [A, B, C]); {'AENS_CLAIM', A, B, C, D, E} -> Impure(none, [A, B, C, D, E]); - 'AENS_UPDATE' -> Impure(none, []);%% TODO + {'AENS_UPDATE', A, B, C, D, E, F} -> Impure(none, [A, B, C, D, E, F]); {'AENS_TRANSFER', A, B, C, D} -> Impure(none, [A, B, C, D]); {'AENS_REVOKE', A, B, C} -> Impure(none, [A, B, C]); {'ABORT', A} -> Impure(pc, A); diff --git a/src/aeso_icode.erl b/src/aeso_icode.erl index e6d50f9..0b6357b 100644 --- a/src/aeso_icode.erl +++ b/src/aeso_icode.erl @@ -73,28 +73,33 @@ new(Options) -> builtin_types() -> Word = fun([]) -> word end, - #{ "bool" => Word - , "int" => Word - , "char" => Word - , "bits" => Word - , "string" => fun([]) -> string end - , "address" => Word - , "hash" => Word - , "unit" => fun([]) -> {tuple, []} end - , "signature" => fun([]) -> {tuple, [word, word]} end - , "oracle" => fun([_, _]) -> word end - , "oracle_query" => fun([_, _]) -> word end - , "list" => fun([A]) -> {list, A} end - , "option" => fun([A]) -> {variant, [[], [A]]} end - , "map" => fun([K, V]) -> map_typerep(K, V) end - , ["Chain", "ttl"] => fun([]) -> {variant, [[word], [word]]} end + #{ "bool" => Word + , "int" => Word + , "char" => Word + , "bits" => Word + , "string" => fun([]) -> string end + , "address" => Word + , "hash" => Word + , "unit" => fun([]) -> {tuple, []} end + , "signature" => fun([]) -> {tuple, [word, word]} end + , "oracle" => fun([_, _]) -> word end + , "oracle_query" => fun([_, _]) -> word end + , "list" => fun([A]) -> {list, A} end + , "option" => fun([A]) -> {variant, [[], [A]]} end + , "map" => fun([K, V]) -> map_typerep(K, V) end + , ["Chain", "ttl"] => fun([]) -> {variant, [[word], [word]]} end + , ["AENS", "pointee"] => fun([]) -> {variant, [[word], [word], [word]]} end }. builtin_constructors() -> - #{ ["RelativeTTL"] => 0 - , ["FixedTTL"] => 1 - , ["None"] => 0 - , ["Some"] => 1 }. + #{ ["RelativeTTL"] => 0 + , ["FixedTTL"] => 1 + , ["None"] => 0 + , ["Some"] => 1 + , ["AccountPointee"] => 0 + , ["OraclePointee"] => 1 + , ["ContractPointee"] => 2 + }. map_typerep(K, V) -> {map, K, V}. @@ -146,4 +151,3 @@ get_constructor_tag(Name, #{constructors := Constructors}) -> undefined -> error({undefined_constructor, Name}); Tag -> Tag end. - diff --git a/test/contracts/aens.aes b/test/contracts/aens.aes index ee1ba27..cb34e82 100644 --- a/test/contracts/aens.aes +++ b/test/contracts/aens.aes @@ -33,7 +33,22 @@ contract AENSTest = sign : signature) : unit = AENS.claim(addr, name, salt, name_fee, signature = sign) - // TODO: update() -- how to handle pointers? + + stateful entrypoint update(owner : address, + name : string, + ttl : option(Chain.ttl), + client_ttl : option(Chain.ttl), + pointers : option(map(string, AENS.pointee))) : unit = + AENS.update(owner, name, ttl, client_ttl, pointers) + + stateful entrypoint signedUpdate(owner : address, + name : string, + ttl : option(Chain.ttl), + client_ttl : option(Chain.ttl), + pointers : option(map(string, AENS.pointee)), + sign : signature) : unit = + AENS.update(owner, name, ttl, client_ttl, pointers, signature = sign) + stateful entrypoint transfer(owner : address, new_owner : address,