From 108cb1f94871a309e741da67fbfc0d9fd472fe6f Mon Sep 17 00:00:00 2001 From: Hans Svensson Date: Mon, 3 Jul 2023 08:04:16 +0200 Subject: [PATCH] [Ceres]: Add Chain.network_id (#468) * Add Chain.network_id * Bump aebytecode version --- CHANGELOG.md | 1 + docs/sophia_stdlib.md | 8 ++++++++ rebar.config | 2 +- rebar.lock | 2 +- src/aeso_ast_infer_types.erl | 1 + src/aeso_ast_to_fcode.erl | 4 ++-- src/aeso_fcode_to_fate.erl | 3 +++ test/contracts/chain.aes | 6 +++++- 8 files changed, 22 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e4ff71..0a00358 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `Bytes.to_fixed_size`, `Bytes.to_any_size`, `Bytes.size`, `String.to_bytes`, and `Int.to_bytes`; and adjust `Bytes.concat` to allow both fixed and arbitrary sized byte arrays. +- `Chain.network_id` - a function to get hold of the Chain's network id. ### Changed ### Removed - `Bitwise.aes` standard library is removed - the builtin operations are superior. diff --git a/docs/sophia_stdlib.md b/docs/sophia_stdlib.md index feec8dd..b07e4a1 100644 --- a/docs/sophia_stdlib.md +++ b/docs/sophia_stdlib.md @@ -734,6 +734,14 @@ Chain.gas_limit : int The gas limit of the current block. +##### network\_id +``` +Chain.network\_id : string +``` + +The network id of the chain. + + ##### spend ``` Chain.spend(to : address, amount : int) : unit diff --git a/rebar.config b/rebar.config index 767729d..842f919 100644 --- a/rebar.config +++ b/rebar.config @@ -2,7 +2,7 @@ {erl_opts, [debug_info]}. -{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {tag, "v3.3.0"}}} +{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git", {tag, "v3.4.0"}}} , {eblake2, "1.0.0"} , {jsx, {git, "https://github.com/talentdeficit/jsx.git", {tag, "2.8.0"}}} ]}. diff --git a/rebar.lock b/rebar.lock index 7f13b93..58ce514 100644 --- a/rebar.lock +++ b/rebar.lock @@ -1,7 +1,7 @@ {"1.2.0", [{<<"aebytecode">>, {git,"https://github.com/aeternity/aebytecode.git", - {ref,"b38349274fc2bed98d7fe86877e6e1a2df302109"}}, + {ref,"009e0361922037f978f9c0ef357d4d1be8559928"}}, 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 abf4d24..12e51f4 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -653,6 +653,7 @@ global_env() -> {"block_height", Int}, {"difficulty", Int}, {"gas_limit", Int}, + {"network_id", String}, {"bytecode_hash",FunC1(bytecode_hash, A, Option(Hash))}, {"create", Stateful( FunN([ {named_arg_t, Ann, {id, Ann, "value"}, Int, {typed, Ann, {int, Ann, 0}, Int}} diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index 1bd2bcc..d2bb96d 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -274,8 +274,8 @@ builtins() -> end, Scopes = [{[], [{"abort", 1}, {"require", 2}, {"exit", 1}]}, {["Chain"], [{"spend", 2}, {"balance", 1}, {"block_hash", 1}, {"coinbase", none}, - {"timestamp", none}, {"block_height", none}, {"difficulty", none}, - {"gas_limit", none}, {"bytecode_hash", 1}, {"create", variable}, {"clone", variable}]}, + {"timestamp", none}, {"block_height", none}, {"difficulty", none}, {"gas_limit", none}, + {"network_id", none}, {"bytecode_hash", 1}, {"create", variable}, {"clone", variable}]}, {["Contract"], [{"address", none}, {"balance", none}, {"creator", none}]}, {["Call"], [{"origin", none}, {"caller", none}, {"value", none}, {"gas_price", none}, {"fee", none}, {"gas_left", 0}]}, diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index ba4506e..e218778 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -568,6 +568,8 @@ builtin_to_scode(_Env, chain_difficulty, []) -> [aeb_fate_ops:difficulty(?a)]; builtin_to_scode(_Env, chain_gas_limit, []) -> [aeb_fate_ops:gaslimit(?a)]; +builtin_to_scode(_Env, chain_network_id, []) -> + [aeb_fate_ops:network_id(?a)]; builtin_to_scode(_Env, contract_balance, []) -> [aeb_fate_ops:balance(?a)]; builtin_to_scode(_Env, contract_address, []) -> @@ -1090,6 +1092,7 @@ attributes(I) -> {'MICROBLOCK', A} -> Pure(A, []); {'DIFFICULTY', A} -> Pure(A, []); {'GASLIMIT', A} -> Pure(A, []); + {'NETWORK_ID', A} -> Pure(A, []); {'GAS', A} -> Pure(A, []); {'LOG0', A} -> Impure(none, [A]); {'LOG1', A, B} -> Impure(none, [A, B]); diff --git a/test/contracts/chain.aes b/test/contracts/chain.aes index 9063a5e..a43493d 100644 --- a/test/contracts/chain.aes +++ b/test/contracts/chain.aes @@ -2,7 +2,8 @@ contract ChainTest = - record state = { last_bf : address } + record state = { last_bf : address + , nw_id : string } function init() : state = {last_bf = Contract.address} @@ -11,3 +12,6 @@ contract ChainTest = function save_coinbase() = put(state{last_bf = Chain.coinbase}) + + function save_network_id() = + put(state{nw_id = Chain.network_id})