From d1dce679951ce1d1dad0471b2ac6b8ef12b44e9d Mon Sep 17 00:00:00 2001 From: Sydhds Date: Fri, 22 Dec 2023 15:04:49 +0100 Subject: [PATCH] Add chain id abi calls (#320) * Add chain id abi calls * Fix chain id export * Update massa-proto-rs dependency + mock abi chain id call * Fix to return u64 and not i64 --------- Co-authored-by: sydhds --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/as_execution/abi.rs | 8 ++++++++ src/as_execution/context.rs | 1 + src/tests/mod.rs | 4 ++++ src/types.rs | 4 ++++ src/wasmv1_execution/abi/abis.rs | 23 ++++++++++++++++++++++- 7 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42ea7386..7a96c87e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -938,7 +938,7 @@ dependencies = [ [[package]] name = "massa-proto-rs" version = "0.1.0" -source = "git+https://github.com/massalabs/massa-proto-rs.git?rev=30b91d705b704287662b0e42457ece442005fa46#30b91d705b704287662b0e42457ece442005fa46" +source = "git+https://github.com/massalabs/massa-proto-rs.git?rev=84678fb77cf6d06d85d55e2c20502908ff292e61#84678fb77cf6d06d85d55e2c20502908ff292e61" dependencies = [ "glob", "prost", diff --git a/Cargo.toml b/Cargo.toml index 8224d991..5033ec8a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ chrono = { version = "=0.4", features = ["clock"], default-features = false } displaydoc = "0.2" function_name = "0.3" loupe = "0.1" -massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs.git", rev = "30b91d705b704287662b0e42457ece442005fa46" } +massa-proto-rs = { git = "https://github.com/massalabs/massa-proto-rs.git", rev = "84678fb77cf6d06d85d55e2c20502908ff292e61"} more-asserts = "0.3" num_enum = "0.7" parking_lot = "0.12" diff --git a/src/as_execution/abi.rs b/src/as_execution/abi.rs index 9a4b5633..ca95e446 100644 --- a/src/as_execution/abi.rs +++ b/src/as_execution/abi.rs @@ -1051,6 +1051,14 @@ pub fn assembly_script_function_exists( Ok(function_exists(&mut ctx, address, function)? as i32) } +/// Return current chain id +#[named] +pub(crate) fn assembly_script_chain_id(mut ctx: FunctionEnvMut) -> ABIResult { + let env = get_env(&ctx)?; + sub_remaining_gas_abi(&env, &mut ctx, function_name!())?; + Ok(env.get_interface().chain_id()? as u64) +} + /// Assembly script builtin `abort` function. /// /// It prints the origin filename, an error messag, the line and column. diff --git a/src/as_execution/context.rs b/src/as_execution/context.rs index b990ba60..4b244eb4 100644 --- a/src/as_execution/context.rs +++ b/src/as_execution/context.rs @@ -277,6 +277,7 @@ impl ASContext { "assembly_script_local_execution" => Function::new_typed_with_env(store, &fenv, assembly_script_local_execution), "assembly_script_caller_has_write_access" => Function::new_typed_with_env(store, &fenv, assembly_script_caller_has_write_access), "assembly_script_function_exists" => Function::new_typed_with_env(store, &fenv, assembly_script_function_exists), + "assembly_script_chain_id" => Function::new_typed_with_env(store, &fenv, assembly_script_chain_id), }, }; diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3f34bf56..1d4a8325 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -801,6 +801,10 @@ impl Interface for TestInterface { println!("get_origin_operation_id"); Ok(Some(String::new())) } + + fn chain_id(&self) -> Result { + Ok(7) + } } #[cfg(feature = "gas_calibration")] diff --git a/src/types.rs b/src/types.rs index 7245b91f..3da770f0 100644 --- a/src/types.rs +++ b/src/types.rs @@ -145,6 +145,7 @@ impl Default for GasCosts { abi_costs.insert(String::from("assembly_script_console_warn"), 36); abi_costs.insert(String::from("assembly_script_console_error"), 36); abi_costs.insert(String::from("assembly_script_trace"), 36); + abi_costs.insert(String::from("assembly_script_chain_id"), 9); Self { abi_costs, operator_cost: 1, @@ -434,6 +435,9 @@ pub trait Interface: Send + Sync + InterfaceClone { // Keccak256 hash bytes fn hash_keccak256(&self, bytes: &[u8]) -> Result<[u8; 32]>; + // Return the current chain id + fn chain_id(&self) -> Result; + fn native_amount_from_str_wasmv1(&self, amount: &str) -> Result; fn native_amount_to_string_wasmv1(&self, amount: &NativeAmount) -> Result; diff --git a/src/wasmv1_execution/abi/abis.rs b/src/wasmv1_execution/abi/abis.rs index 3d8cae31..2368f5a1 100644 --- a/src/wasmv1_execution/abi/abis.rs +++ b/src/wasmv1_execution/abi/abis.rs @@ -114,7 +114,8 @@ pub fn register_abis(store: &mut impl AsStoreMut, shared_abi_env: ABIEnv) -> Imp "abi_evm_verify_signature" => abi_evm_verify_signature, "abi_evm_get_address_from_pubkey" => abi_evm_get_address_from_pubkey, "abi_evm_get_pubkey_from_signature" => abi_evm_get_pubkey_from_signature, - "abi_is_address_eoa" => abi_is_address_eoa + "abi_is_address_eoa" => abi_is_address_eoa, + "abi_chain_id" => abi_chain_id ) } @@ -1660,3 +1661,23 @@ pub fn abi_verify_signature( }, ) } + +#[named] +pub fn abi_chain_id( + store_env: FunctionEnvMut, + arg_offset: i32, +) -> Result { + handle_abi( + function_name!(), + store_env, + arg_offset, + |handler, _req: ChainIdRequest| -> Result { + match handler.interface.chain_id() { + Ok(chain_id) => { + resp_ok!(ChainIdResult, { id: chain_id }) + } + Err(e) => resp_err!(e), + } + }, + ) +}