Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chain id abi calls #320

Merged
merged 4 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions src/as_execution/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ASEnv>) -> ABIResult<u64> {
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.
Expand Down
1 change: 1 addition & 0 deletions src/as_execution/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
};

Expand Down
4 changes: 4 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,10 @@ impl Interface for TestInterface {
println!("get_origin_operation_id");
Ok(Some(String::new()))
}

fn chain_id(&self) -> Result<u64> {
Ok(7)
}
}

#[cfg(feature = "gas_calibration")]
Expand Down
4 changes: 4 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<u64>;

fn native_amount_from_str_wasmv1(&self, amount: &str) -> Result<NativeAmount>;

fn native_amount_to_string_wasmv1(&self, amount: &NativeAmount) -> Result<String>;
Expand Down
23 changes: 22 additions & 1 deletion src/wasmv1_execution/abi/abis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down Expand Up @@ -1660,3 +1661,23 @@ pub fn abi_verify_signature(
},
)
}

#[named]
pub fn abi_chain_id(
store_env: FunctionEnvMut<ABIEnv>,
arg_offset: i32,
) -> Result<i32, WasmV1Error> {
handle_abi(
function_name!(),
store_env,
arg_offset,
|handler, _req: ChainIdRequest| -> Result<AbiResponse, WasmV1Error> {
match handler.interface.chain_id() {
Ok(chain_id) => {
resp_ok!(ChainIdResult, { id: chain_id })
}
Err(e) => resp_err!(e),
}
},
)
}
Loading