From c3426f0e65aa91d5f3f9b3fe0ea2f82895cfeca4 Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Mon, 3 Feb 2020 12:52:00 +0100 Subject: [PATCH] Add AENS.lookup Also move Pointee-constructors inside AENS namespace. --- src/aeso_ast_infer_types.erl | 18 ++++++++++++------ src/aeso_ast_to_fcode.erl | 21 ++++++++++++--------- src/aeso_fcode_to_fate.erl | 3 +++ test/aeso_compiler_tests.erl | 3 ++- test/contracts/aens_update.aes | 17 +++++++++++++++++ 5 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test/contracts/aens_update.aes diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 5ee5641..802614c 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -390,6 +390,7 @@ global_env() -> SignFun = fun(Ts, T) -> {type_sig, [stateful|Ann], none, [Signature], Ts, T} end, TTL = {qid, Ann, ["Chain", "ttl"]}, Pointee = {qid, Ann, ["AENS", "pointee"]}, + AENSName = {qid, Ann, ["AENS", "name"]}, Fr = {qid, Ann, ["MCL_BLS12_381", "fr"]}, Fp = {qid, Ann, ["MCL_BLS12_381", "fp"]}, Fp2 = {tuple_t, Ann, [Fp, Fp]}, @@ -410,10 +411,6 @@ 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)}]) @@ -476,8 +473,17 @@ global_env() -> {"claim", SignFun([Address, String, Int, Int], Unit)}, {"transfer", SignFun([Address, Address, String], Unit)}, {"revoke", SignFun([Address, String], Unit)}, - {"update", SignFun([Address, String, Option(TTL), Option(Int), Option(Map(String, Pointee))], Unit)}]) - , types = MkDefs([{"pointee", 0}]) }, + {"update", SignFun([Address, String, Option(TTL), Option(Int), Option(Map(String, Pointee))], Unit)}, + {"lookup", Fun([String], option_t(Ann, AENSName))}, + %% AENS pointee constructors + {"AccountPt", Fun1(Address, Pointee)}, + {"OraclePt", Fun1(Address, Pointee)}, + {"ContractPt", Fun1(Address, Pointee)}, + {"ChannelPt", Fun1(Address, Pointee)}, + %% Name object constructor + {"Name", Fun([Address, TTL, Map(String, Pointee)], AENSName)} + ]) + , types = MkDefs([{"pointee", 0}, {"name", 0}]) }, MapScope = #scope { funs = MkDefs( diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index bd4c539..d4c1f0d 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -181,13 +181,15 @@ 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] }, - ["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] } + 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] }, + ["AENS", "AccountPt"] => #con_tag{ tag = 0, arities = [1, 1, 1, 1] }, + ["AENS", "OraclePt"] => #con_tag{ tag = 1, arities = [1, 1, 1, 1] }, + ["AENS", "ContractPt"] => #con_tag{ tag = 2, arities = [1, 1, 1, 1] }, + ["AENS", "ChannelPt"] => #con_tag{ tag = 3, arities = [1, 1, 1, 1] }, + ["AENS", "Name"] => #con_tag{ tag = 0, arities = [3] } }, options => Options, functions => #{} }. @@ -208,7 +210,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}, {"update", 6}]}, + {"revoke", 3}, {"update", 6}, {"lookup", 1}]}, {["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}, @@ -253,7 +255,8 @@ init_type_env() -> ["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]]}), + ["AENS", "pointee"] => ?type({variant, [[address], [address], [address], [address]]}), + ["AENS", "name"] => ?type({variant, [[address, {variant, [[integer], [integer]]}, {map, string, {variant, [[address], [address], [address], [address]]}}]]}), ["MCL_BLS12_381", "fr"] => ?type({bytes, 32}), ["MCL_BLS12_381", "fp"] => ?type({bytes, 48}) }. diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index 181c945..70cac15 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -541,6 +541,8 @@ builtin_to_scode(Env, aens_revoke, [_Sign, _Account, _Name] = 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, aens_lookup, [_Name] = Args) -> + call_to_scode(Env, aeb_fate_ops:aens_lookup(?a, ?a), Args); builtin_to_scode(_Env, auth_tx_hash, []) -> [aeb_fate_ops:auth_tx_hash(?a)]. @@ -889,6 +891,7 @@ attributes(I) -> {'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]); + {'AENS_LOOKUP', A, B} -> Impure(A, [B]); {'BLS12_381_G1_NEG', A, B} -> Pure(A, [B]); {'BLS12_381_G1_NORM', A, B} -> Pure(A, [B]); {'BLS12_381_G1_VALID', A, B} -> Pure(A, [B]); diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index ea27c67..2e610b0 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -154,6 +154,7 @@ compilable_contracts() -> "bytes_to_x", "bytes_concat", "aens", + "aens_update", "tuple_match", "cyclic_include", "stdlib_include", @@ -170,7 +171,7 @@ compilable_contracts() -> ]. not_yet_compilable(fate) -> []; -not_yet_compilable(aevm) -> ["pairing_crypto"]. +not_yet_compilable(aevm) -> ["pairing_crypto", "aens_update"]. %% Contracts that should produce type errors diff --git a/test/contracts/aens_update.aes b/test/contracts/aens_update.aes new file mode 100644 index 0000000..5d65229 --- /dev/null +++ b/test/contracts/aens_update.aes @@ -0,0 +1,17 @@ +contract AENSUpdate = + stateful entrypoint update_name(owner : address, name : string) = + let p1 : AENS.pointee = AENS.AccountPt(Call.caller) + let p2 : AENS.pointee = AENS.OraclePt(Call.caller) + let p3 : AENS.pointee = AENS.ContractPt(Call.caller) + let p4 : AENS.pointee = AENS.ChannelPt(Call.caller) + AENS.update(owner, name, None, None, + Some({ ["account_pubkey"] = p1, ["oracle_pubkey"] = p2, + ["contract_pubkey"] = p3, ["misc"] = p4 })) + + entrypoint get_ttl(name : string) = + switch(AENS.lookup(name)) + Some(AENS.Name(_, FixedTTL(ttl), _)) => ttl + + entrypoint expiry(o : oracle(int, int)) : int = + Oracle.expiry(o) +