diff --git a/prdoc/pr_5555.prdoc b/prdoc/pr_5555.prdoc new file mode 100644 index 000000000000..630345b9b5a1 --- /dev/null +++ b/prdoc/pr_5555.prdoc @@ -0,0 +1,15 @@ +title: Make salt optional + +doc: + - audience: Runtime Dev + description: | + Remove address_len and salt_len from uapi as both are now fixed size + +crates: + - name: pallet-revive + bump: patch + - name: pallet-revive-uapi + bump: patch + - name: pallet-revive-fixtures + bump: patch + diff --git a/substrate/frame/revive/fixtures/build.rs b/substrate/frame/revive/fixtures/build.rs index ed981a75467b..944ae246c1b8 100644 --- a/substrate/frame/revive/fixtures/build.rs +++ b/substrate/frame/revive/fixtures/build.rs @@ -159,6 +159,7 @@ mod build { fn post_process(input_path: &Path, output_path: &Path) -> Result<()> { let mut config = polkavm_linker::Config::default(); config.set_strip(true); + config.set_optimize(false); let orig = fs::read(input_path).with_context(|| format!("Failed to read {:?}", input_path))?; let linked = polkavm_linker::program_from_elf(config, orig.as_ref()) diff --git a/substrate/frame/revive/fixtures/contracts/call.rs b/substrate/frame/revive/fixtures/contracts/call.rs index 73f427650c20..93687441fa50 100644 --- a/substrate/frame/revive/fixtures/contracts/call.rs +++ b/substrate/frame/revive/fixtures/contracts/call.rs @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( callee_input: [u8; 4], - callee_addr: [u8; 20], + callee_addr: &[u8; 20], ); // Call the callee diff --git a/substrate/frame/revive/fixtures/contracts/call_return_code.rs b/substrate/frame/revive/fixtures/contracts/call_return_code.rs index e8f995cffc7f..29b77c343fe9 100644 --- a/substrate/frame/revive/fixtures/contracts/call_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/call_return_code.rs @@ -33,7 +33,7 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( 100, - callee_addr: [u8; 20], + callee_addr: &[u8; 20], input: [u8], ); diff --git a/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs b/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs index f3d2ece21327..7cd46849655f 100644 --- a/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs +++ b/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs @@ -31,7 +31,7 @@ pub extern "C" fn call() { input!( 512, callee_input: [u8; 4], - callee_addr: [u8; 20], + callee_addr: &[u8; 20], call: [u8], ); diff --git a/substrate/frame/revive/fixtures/contracts/call_with_flags_and_value.rs b/substrate/frame/revive/fixtures/contracts/call_with_flags_and_value.rs index 15c1124eeaee..c3204c29281c 100644 --- a/substrate/frame/revive/fixtures/contracts/call_with_flags_and_value.rs +++ b/substrate/frame/revive/fixtures/contracts/call_with_flags_and_value.rs @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( 256, - callee_addr: [u8; 20], + callee_addr: &[u8; 20], flags: u32, value: u64, forwarded_input: [u8], diff --git a/substrate/frame/revive/fixtures/contracts/call_with_limit.rs b/substrate/frame/revive/fixtures/contracts/call_with_limit.rs index 985df6724110..a941aa9a3421 100644 --- a/substrate/frame/revive/fixtures/contracts/call_with_limit.rs +++ b/substrate/frame/revive/fixtures/contracts/call_with_limit.rs @@ -32,7 +32,7 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( 256, - callee_addr: [u8; 20], + callee_addr: &[u8; 20], ref_time: u64, proof_size: u64, forwarded_input: [u8], diff --git a/substrate/frame/revive/fixtures/contracts/caller_contract.rs b/substrate/frame/revive/fixtures/contracts/caller_contract.rs index dceab813f88f..3b83f208d623 100644 --- a/substrate/frame/revive/fixtures/contracts/caller_contract.rs +++ b/substrate/frame/revive/fixtures/contracts/caller_contract.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - input!(code_hash: [u8; 32],); + input!(code_hash: &[u8; 32],); // The value to transfer on instantiation and calls. Chosen to be greater than existential // deposit. @@ -73,7 +73,6 @@ pub extern "C" fn call() { // Deploy the contract successfully. let mut callee = [0u8; 20]; - let callee = &mut &mut callee[..]; api::instantiate( code_hash, @@ -82,17 +81,16 @@ pub extern "C" fn call() { None, // No deposit limit. &value, &input, - Some(callee), + Some(&mut callee), None, &salt, ) .unwrap(); - assert_eq!(callee.len(), 20); // Call the new contract and expect it to return failing exit code. let res = api::call( uapi::CallFlags::empty(), - callee, + &callee, 0u64, // How much ref_time weight to devote for the execution. 0 = all. 0u64, // How much proof_size weight to devote for the execution. 0 = all. None, // No deposit limit. @@ -105,7 +103,7 @@ pub extern "C" fn call() { // Fail to call the contract due to insufficient ref_time weight. let res = api::call( uapi::CallFlags::empty(), - callee, + &callee, 1u64, // Too little ref_time weight. 0u64, // How much proof_size weight to devote for the execution. 0 = all. None, // No deposit limit. @@ -118,7 +116,7 @@ pub extern "C" fn call() { // Fail to call the contract due to insufficient proof_size weight. let res = api::call( uapi::CallFlags::empty(), - callee, + &callee, 0u64, // How much ref_time weight to devote for the execution. 0 = all. 1u64, // too little proof_size weight None, // No deposit limit. @@ -132,7 +130,7 @@ pub extern "C" fn call() { let mut output = [0u8; 4]; api::call( uapi::CallFlags::empty(), - callee, + &callee, 0u64, // How much ref_time weight to devote for the execution. 0 = all. 0u64, // How much proof_size weight to devote for the execution. 0 = all. None, // No deposit limit. diff --git a/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs b/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs index c7596e44dab3..bb5c1ccbc1d6 100644 --- a/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs +++ b/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs @@ -20,7 +20,7 @@ #![no_std] #![no_main] -use common::{input, output}; +use common::input; use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] @@ -47,12 +47,13 @@ pub extern "C" fn call() { input[8] = 1u8; // Read the contract address. - output!(addr, [0u8; 32], api::address,); + let mut addr = [0u8; 20]; + api::address(&mut addr); // call self api::call( uapi::CallFlags::ALLOW_REENTRY, - addr, + &addr, 0u64, // How much ref_time to devote for the execution. 0 = all. 0u64, // How much proof_size to devote for the execution. 0 = all. None, // No deposit limit. diff --git a/substrate/frame/revive/fixtures/contracts/common/src/lib.rs b/substrate/frame/revive/fixtures/contracts/common/src/lib.rs index 6631af8292fe..947247e9cf74 100644 --- a/substrate/frame/revive/fixtures/contracts/common/src/lib.rs +++ b/substrate/frame/revive/fixtures/contracts/common/src/lib.rs @@ -100,11 +100,23 @@ macro_rules! input { input!(@inner $input, $cursor + $n, $($rest)*); }; + // Match an array reference of the given size. + // e.g input!(var1: &[u8; 32], ); + (@inner $input:expr, $cursor:expr, $var:ident: &[u8; $n:expr], $($rest:tt)*) => { + let $var: &[u8; $n] = &$input[$cursor..$cursor+$n].try_into().unwrap(); + input!(@inner $input, $cursor + $n, $($rest)*); + }; + // Size of a u8 slice. (@size $size:expr, $var:ident: [u8; $n:expr], $($rest:tt)*) => { input!(@size $size + $n, $($rest)*) }; + // Size of an array reference. + (@size $size:expr, $var:ident: &[u8; $n:expr], $($rest:tt)*) => { + input!(@size $size + $n, $($rest)*) + }; + // Entry point, with the buffer and it's size specified first. // e.g input!(buffer, 512, var1: u32, var2: [u8], ); ($buffer:ident, $size:expr, $($rest:tt)*) => { diff --git a/substrate/frame/revive/fixtures/contracts/create_storage_and_call.rs b/substrate/frame/revive/fixtures/contracts/create_storage_and_call.rs index 7a0b497079cd..28d161791e5b 100644 --- a/substrate/frame/revive/fixtures/contracts/create_storage_and_call.rs +++ b/substrate/frame/revive/fixtures/contracts/create_storage_and_call.rs @@ -32,7 +32,7 @@ pub extern "C" fn call() { input!( buffer, input: [u8; 4], - callee: [u8; 20], + callee: &[u8; 20], deposit_limit: [u8; 8], ); diff --git a/substrate/frame/revive/fixtures/contracts/create_storage_and_instantiate.rs b/substrate/frame/revive/fixtures/contracts/create_storage_and_instantiate.rs index 53b9afba7783..d87c2e8cd35a 100644 --- a/substrate/frame/revive/fixtures/contracts/create_storage_and_instantiate.rs +++ b/substrate/frame/revive/fixtures/contracts/create_storage_and_instantiate.rs @@ -31,14 +31,13 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( input: [u8; 4], - code_hash: [u8; 32], + code_hash: &[u8; 32], deposit_limit: [u8; 8], ); let value = 10_000u64.to_le_bytes(); let salt = [0u8; 32]; let mut address = [0u8; 20]; - let address = &mut &mut address[..]; api::instantiate( code_hash, @@ -47,12 +46,12 @@ pub extern "C" fn call() { Some(deposit_limit), &value, input, - Some(address), + Some(&mut address), None, &salt, ) .unwrap(); // Return the deployed contract address. - api::return_value(uapi::ReturnFlags::empty(), address); + api::return_value(uapi::ReturnFlags::empty(), &address); } diff --git a/substrate/frame/revive/fixtures/contracts/create_transient_storage_and_call.rs b/substrate/frame/revive/fixtures/contracts/create_transient_storage_and_call.rs index 8788542a0c5a..753490cf26b7 100644 --- a/substrate/frame/revive/fixtures/contracts/create_transient_storage_and_call.rs +++ b/substrate/frame/revive/fixtures/contracts/create_transient_storage_and_call.rs @@ -35,7 +35,7 @@ pub extern "C" fn call() { buffer, len: u32, input: [u8; 4], - callee: [u8; 20], + callee: &[u8; 20], ); let rounds = len as usize / BUFFER.len(); diff --git a/substrate/frame/revive/fixtures/contracts/delegate_call.rs b/substrate/frame/revive/fixtures/contracts/delegate_call.rs index d03ddab1bc58..9fd155408af3 100644 --- a/substrate/frame/revive/fixtures/contracts/delegate_call.rs +++ b/substrate/frame/revive/fixtures/contracts/delegate_call.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - input!(code_hash: [u8; 32],); + input!(code_hash: &[u8; 32],); let mut key = [0u8; 32]; key[0] = 1u8; diff --git a/substrate/frame/revive/fixtures/contracts/delegate_call_lib.rs b/substrate/frame/revive/fixtures/contracts/delegate_call_lib.rs index 921543dced06..c5525423a9ee 100644 --- a/substrate/frame/revive/fixtures/contracts/delegate_call_lib.rs +++ b/substrate/frame/revive/fixtures/contracts/delegate_call_lib.rs @@ -44,6 +44,7 @@ pub extern "C" fn call() { assert_eq!(value_transferred, 1337); // Assert that ALICE is the caller of the contract. - output!(caller, [0u8; 20], api::caller,); - assert_eq!(&caller[..], &[1u8; 20]); + let mut caller = [0u8; 20]; + api::caller(&mut caller); + assert_eq!(caller, [1u8; 20]); } diff --git a/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs b/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs index cf3351c52fdc..20f8ec3364ee 100644 --- a/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs +++ b/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - input!(code_hash: [u8; 32],); + input!(code_hash: &[u8; 32],); // Delegate call into passed code hash. let input = [0u8; 0]; diff --git a/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs b/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs index b86e761d53e7..4959a5e2e0ce 100644 --- a/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs +++ b/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs @@ -27,11 +27,10 @@ const VALUE: [u8; 8] = [0, 0, 1u8, 0, 0, 0, 0, 0]; #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn deploy() { - input!(code_hash: [u8; 32],); + input!(code_hash: &[u8; 32],); let input = [0u8; 0]; let mut address = [0u8; 20]; - let address = &mut &mut address[..]; let salt = [47u8; 32]; api::instantiate( @@ -41,27 +40,28 @@ pub extern "C" fn deploy() { None, // No deposit limit. &VALUE, &input, - Some(address), + Some(&mut address), None, &salt, ) .unwrap(); // Return the deployed contract address. - api::set_storage(StorageFlags::empty(), &ADDRESS_KEY, address); + api::set_storage(StorageFlags::empty(), &ADDRESS_KEY, &address); } #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { let mut callee_addr = [0u8; 20]; - let callee_addr = &mut &mut callee_addr[..]; - api::get_storage(StorageFlags::empty(), &ADDRESS_KEY, callee_addr).unwrap(); + let callee = &mut &mut callee_addr[..]; + api::get_storage(StorageFlags::empty(), &ADDRESS_KEY, callee).unwrap(); + assert!(callee.len() == 20); // Calling the destination contract with non-empty input data should fail. let res = api::call( uapi::CallFlags::empty(), - callee_addr, + &callee_addr, 0u64, // How much ref_time weight to devote for the execution. 0 = all. 0u64, // How much proof_size weight to devote for the execution. 0 = all. None, // No deposit limit. @@ -74,7 +74,7 @@ pub extern "C" fn call() { // Call the destination contract regularly, forcing it to self-destruct. api::call( uapi::CallFlags::empty(), - callee_addr, + &callee_addr, 0u64, // How much ref_time weight to devote for the execution. 0 = all. 0u64, // How much proof_size weight to devote for the execution. 0 = all. None, // No deposit limit. diff --git a/substrate/frame/revive/fixtures/contracts/drain.rs b/substrate/frame/revive/fixtures/contracts/drain.rs index f5c8681c9382..b46d4f7c8418 100644 --- a/substrate/frame/revive/fixtures/contracts/drain.rs +++ b/substrate/frame/revive/fixtures/contracts/drain.rs @@ -39,6 +39,6 @@ pub extern "C" fn call() { // Try to self-destruct by sending more balance to the 0 address. // The call will fail because a contract transfer has a keep alive requirement. - let res = api::transfer(&[0u8; 32], &balance.to_le_bytes()); + let res = api::transfer(&[0u8; 20], &balance.to_le_bytes()); assert!(matches!(res, Err(uapi::ReturnErrorCode::TransferFailed))); } diff --git a/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs b/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs index 90884f1a2a68..a81ffea943d4 100644 --- a/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - input!(buffer, 36, code_hash: [u8; 32],); + input!(buffer, 36, code_hash: &[u8; 32],); let input = &buffer[32..]; let err_code = match api::instantiate( @@ -41,7 +41,7 @@ pub extern "C" fn call() { input, None, None, - &[0u8; 0], // Empty salt. + &[0u8; 32], // Salt. ) { Ok(_) => 0u32, Err(code) => code as u32, diff --git a/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs b/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs index 2f3e5ae148cb..2efacb4e683f 100644 --- a/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs +++ b/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs @@ -30,7 +30,7 @@ const ETH_ALICE: [u8; 20] = [1u8; 20]; fn load_input(delegate_call: bool) { input!( action: u32, - code_hash: [u8; 32], + code_hash: &[u8; 32], ); match action { diff --git a/substrate/frame/revive/fixtures/contracts/read_only_call.rs b/substrate/frame/revive/fixtures/contracts/read_only_call.rs index ef8bc95f0098..7476b7a8366d 100644 --- a/substrate/frame/revive/fixtures/contracts/read_only_call.rs +++ b/substrate/frame/revive/fixtures/contracts/read_only_call.rs @@ -31,7 +31,7 @@ pub extern "C" fn deploy() {} pub extern "C" fn call() { input!( 256, - callee_addr: [u8; 20], + callee_addr: &[u8; 20], callee_input: [u8], ); diff --git a/substrate/frame/revive/fixtures/contracts/recurse.rs b/substrate/frame/revive/fixtures/contracts/recurse.rs index f4dfd6c965d0..c15784b7f245 100644 --- a/substrate/frame/revive/fixtures/contracts/recurse.rs +++ b/substrate/frame/revive/fixtures/contracts/recurse.rs @@ -20,7 +20,7 @@ #![no_std] #![no_main] -use common::{input, output}; +use common::input; use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] @@ -33,7 +33,8 @@ pub extern "C" fn call() { input!(calls_left: u32, ); // own address - output!(addr, [0u8; 32], api::address,); + let mut addr = [0u8; 20]; + api::address(&mut addr); if calls_left == 0 { return @@ -41,7 +42,7 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::ALLOW_REENTRY, - addr, + &addr, 0u64, // How much ref_time to devote for the execution. 0 = all. 0u64, // How much deposit_limit to devote for the execution. 0 = all. None, // No deposit limit. diff --git a/substrate/frame/revive/fixtures/contracts/self_destruct.rs b/substrate/frame/revive/fixtures/contracts/self_destruct.rs index b0e004018f10..0e1e4d30e6f3 100644 --- a/substrate/frame/revive/fixtures/contracts/self_destruct.rs +++ b/substrate/frame/revive/fixtures/contracts/self_destruct.rs @@ -18,7 +18,7 @@ #![no_std] #![no_main] -use common::{input, output}; +use common::input; use uapi::{HostFn, HostFnImpl as api}; const ETH_DJANGO: [u8; 20] = [4u8; 20]; @@ -36,10 +36,12 @@ pub extern "C" fn call() { input!(input, 4,); if !input.is_empty() { - output!(addr, [0u8; 20], api::address,); + let mut addr = [0u8; 20]; + api::address(&mut addr); + api::call( uapi::CallFlags::ALLOW_REENTRY, - addr, + &addr, 0u64, // How much ref_time to devote for the execution. 0 = all. 0u64, // How much proof_size to devote for the execution. 0 = all. None, // No deposit limit. diff --git a/substrate/frame/revive/fixtures/contracts/self_destructing_constructor.rs b/substrate/frame/revive/fixtures/contracts/self_destructing_constructor.rs index 28bcef978231..3285aecbe780 100644 --- a/substrate/frame/revive/fixtures/contracts/self_destructing_constructor.rs +++ b/substrate/frame/revive/fixtures/contracts/self_destructing_constructor.rs @@ -24,7 +24,7 @@ use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn deploy() { - api::terminate(&[0u8; 32]); + api::terminate(&[0u8; 20]); } #[no_mangle] diff --git a/substrate/frame/revive/fixtures/contracts/set_code_hash.rs b/substrate/frame/revive/fixtures/contracts/set_code_hash.rs index e3cf4becfb97..75995d7bb8a2 100644 --- a/substrate/frame/revive/fixtures/contracts/set_code_hash.rs +++ b/substrate/frame/revive/fixtures/contracts/set_code_hash.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - input!(addr: [u8; 32],); + input!(addr: &[u8; 32],); api::set_code_hash(addr).unwrap(); // we return 1 after setting new code_hash diff --git a/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs b/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs index d3f6a1dd3a08..3e1f2757c27a 100644 --- a/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs @@ -28,7 +28,7 @@ pub extern "C" fn deploy() {} #[no_mangle] #[polkavm_derive::polkavm_export] pub extern "C" fn call() { - let ret_code = match api::transfer(&[0u8; 32], &100u64.to_le_bytes()) { + let ret_code = match api::transfer(&[0u8; 20], &100u64.to_le_bytes()) { Ok(_) => 0u32, Err(code) => code as u32, }; diff --git a/substrate/frame/revive/src/benchmarking/mod.rs b/substrate/frame/revive/src/benchmarking/mod.rs index 3ffd53e3561c..2c5285622843 100644 --- a/substrate/frame/revive/src/benchmarking/mod.rs +++ b/substrate/frame/revive/src/benchmarking/mod.rs @@ -532,17 +532,17 @@ mod benchmarks { #[benchmark(pov_mode = Measured)] fn seal_caller() { let len = H160::len_bytes(); - build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + build_runtime!(runtime, memory: [vec![0u8; len as _], ]); let result; #[block] { - result = runtime.bench_caller(memory.as_mut_slice(), 4, 0); + result = runtime.bench_caller(memory.as_mut_slice(), 0); } assert_ok!(result); assert_eq!( - ::decode(&mut &memory[4..]).unwrap(), + ::decode(&mut &memory[..]).unwrap(), T::AddressMapper::to_address(&runtime.ext().caller().account_id().unwrap()) ); } @@ -567,17 +567,17 @@ mod benchmarks { fn seal_code_hash() { let contract = Contract::::with_index(1, WasmModule::dummy(), vec![]).unwrap(); let len = ::max_encoded_len() as u32; - build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], contract.account_id.encode(), ]); + build_runtime!(runtime, memory: [vec![0u8; len as _], contract.account_id.encode(), ]); let result; #[block] { - result = runtime.bench_code_hash(memory.as_mut_slice(), 4 + len, 4, 0); + result = runtime.bench_code_hash(memory.as_mut_slice(), len, 0); } assert_ok!(result); assert_eq!( - ::decode(&mut &memory[4..]).unwrap(), + ::decode(&mut &memory[..]).unwrap(), contract.info().unwrap().code_hash ); } @@ -585,16 +585,16 @@ mod benchmarks { #[benchmark(pov_mode = Measured)] fn seal_own_code_hash() { let len = ::max_encoded_len() as u32; - build_runtime!(runtime, contract, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + build_runtime!(runtime, contract, memory: [vec![0u8; len as _], ]); let result; #[block] { - result = runtime.bench_own_code_hash(memory.as_mut_slice(), 4, 0); + result = runtime.bench_own_code_hash(memory.as_mut_slice(), 0); } assert_ok!(result); assert_eq!( - ::decode(&mut &memory[4..]).unwrap(), + ::decode(&mut &memory[..]).unwrap(), contract.info().unwrap().code_hash ); } @@ -629,15 +629,15 @@ mod benchmarks { #[benchmark(pov_mode = Measured)] fn seal_address() { let len = H160::len_bytes(); - build_runtime!(runtime, memory: [len.to_le_bytes(), vec![0u8; len as _], ]); + build_runtime!(runtime, memory: [vec![0u8; len as _], ]); let result; #[block] { - result = runtime.bench_address(memory.as_mut_slice(), 4, 0); + result = runtime.bench_address(memory.as_mut_slice(), 0); } assert_ok!(result); - assert_eq!(::decode(&mut &memory[4..]).unwrap(), runtime.ext().address()); + assert_eq!(::decode(&mut &memory[..]).unwrap(), runtime.ext().address()); } #[benchmark(pov_mode = Measured)] @@ -1581,11 +1581,9 @@ mod benchmarks { offset(value_len), // input_data_ptr i, // input_data_len SENTINEL, // address_ptr - 0, // address_len_ptr SENTINEL, // output_ptr 0, // output_len_ptr offset(i), // salt_ptr - 32, // salt_len ); } diff --git a/substrate/frame/revive/src/debug.rs b/substrate/frame/revive/src/debug.rs index 00e893b94f86..d1fc0823e03d 100644 --- a/substrate/frame/revive/src/debug.rs +++ b/substrate/frame/revive/src/debug.rs @@ -64,7 +64,7 @@ impl Tracing for () { type CallSpan = (); fn new_call_span(contract_address: &H160, entry_point: ExportedFunction, input_data: &[u8]) { - log::trace!(target: LOG_TARGET, "call {entry_point:?} account: {contract_address:?}, input_data: {input_data:?}") + log::trace!(target: LOG_TARGET, "call {entry_point:?} address: {contract_address:?}, input_data: {input_data:?}") } } diff --git a/substrate/frame/revive/src/lib.rs b/substrate/frame/revive/src/lib.rs index 9a99b01776c1..393acc8c9852 100644 --- a/substrate/frame/revive/src/lib.rs +++ b/substrate/frame/revive/src/lib.rs @@ -824,7 +824,7 @@ pub mod pallet { /// must be supplied. #[pallet::call_index(1)] #[pallet::weight( - T::WeightInfo::instantiate(data.len() as u32, 32).saturating_add(*gas_limit) + T::WeightInfo::instantiate(data.len() as u32).saturating_add(*gas_limit) )] pub fn instantiate( origin: OriginFor, @@ -855,7 +855,7 @@ pub mod pallet { dispatch_result( output.result.map(|result| result.result), output.gas_consumed, - T::WeightInfo::instantiate(data_len, 32), + T::WeightInfo::instantiate(data_len), ) } @@ -888,7 +888,7 @@ pub mod pallet { /// - The `deploy` function is executed in the context of the newly-created account. #[pallet::call_index(2)] #[pallet::weight( - T::WeightInfo::instantiate_with_code(code.len() as u32, data.len() as u32, 32) + T::WeightInfo::instantiate_with_code(code.len() as u32, data.len() as u32) .saturating_add(*gas_limit) )] pub fn instantiate_with_code( @@ -921,7 +921,7 @@ pub mod pallet { dispatch_result( output.result.map(|result| result.result), output.gas_consumed, - T::WeightInfo::instantiate_with_code(code_len, data_len, 32), + T::WeightInfo::instantiate_with_code(code_len, data_len), ) } diff --git a/substrate/frame/revive/src/storage.rs b/substrate/frame/revive/src/storage.rs index 91b7b904d2bb..ef7ce2db32cf 100644 --- a/substrate/frame/revive/src/storage.rs +++ b/substrate/frame/revive/src/storage.rs @@ -83,16 +83,16 @@ impl ContractInfo { /// This returns an `Err` if an contract with the supplied `account` already exists /// in storage. pub fn new( - account: &H160, + address: &H160, nonce: T::Nonce, code_hash: sp_core::H256, ) -> Result { - if >::contains_key(account) { + if >::contains_key(address) { return Err(Error::::DuplicateContract.into()) } let trie_id = { - let buf = ("bcontract_trie_v1", account, nonce).using_encoded(T::Hashing::hash); + let buf = ("bcontract_trie_v1", address, nonce).using_encoded(T::Hashing::hash); buf.as_ref() .to_vec() .try_into() diff --git a/substrate/frame/revive/src/tests/test_debug.rs b/substrate/frame/revive/src/tests/test_debug.rs index 7885d681e482..1e94d5cafb81 100644 --- a/substrate/frame/revive/src/tests/test_debug.rs +++ b/substrate/frame/revive/src/tests/test_debug.rs @@ -28,7 +28,7 @@ use std::cell::RefCell; #[derive(Clone, PartialEq, Eq, Debug)] struct DebugFrame { - contract_account: sp_core::H160, + contract_address: sp_core::H160, call: ExportedFunction, input: Vec, result: Option>, @@ -41,7 +41,7 @@ thread_local! { pub struct TestDebug; pub struct TestCallSpan { - contract_account: sp_core::H160, + contract_address: sp_core::H160, call: ExportedFunction, input: Vec, } @@ -50,20 +50,20 @@ impl Tracing for TestDebug { type CallSpan = TestCallSpan; fn new_call_span( - contract_account: &crate::H160, + contract_address: &crate::H160, entry_point: ExportedFunction, input_data: &[u8], ) -> TestCallSpan { DEBUG_EXECUTION_TRACE.with(|d| { d.borrow_mut().push(DebugFrame { - contract_account: *contract_account, + contract_address: *contract_address, call: entry_point, input: input_data.to_vec(), result: None, }) }); TestCallSpan { - contract_account: *contract_account, + contract_address: *contract_address, call: entry_point, input: input_data.to_vec(), } @@ -90,7 +90,7 @@ impl CallSpan for TestCallSpan { fn after_call(self, output: &ExecReturnValue) { DEBUG_EXECUTION_TRACE.with(|d| { d.borrow_mut().push(DebugFrame { - contract_account: self.contract_account, + contract_address: self.contract_address, call: self.call, input: self.input, result: Some(output.data.clone()), @@ -131,18 +131,18 @@ mod run_tests { .addr } - fn constructor_frame(contract_account: &H160, after: bool) -> DebugFrame { + fn constructor_frame(contract_address: &H160, after: bool) -> DebugFrame { DebugFrame { - contract_account: *contract_account, + contract_address: *contract_address, call: ExportedFunction::Constructor, input: vec![], result: if after { Some(vec![]) } else { None }, } } - fn call_frame(contract_account: &H160, args: Vec, after: bool) -> DebugFrame { + fn call_frame(contract_address: &H160, args: Vec, after: bool) -> DebugFrame { DebugFrame { - contract_account: *contract_account, + contract_address: *contract_address, call: ExportedFunction::Call, input: args, result: if after { Some(vec![]) } else { None }, diff --git a/substrate/frame/revive/src/wasm/runtime.rs b/substrate/frame/revive/src/wasm/runtime.rs index 533baf8d2c82..51c723493847 100644 --- a/substrate/frame/revive/src/wasm/runtime.rs +++ b/substrate/frame/revive/src/wasm/runtime.rs @@ -453,7 +453,7 @@ impl Token for RuntimeCosts { DelegateCallBase => T::WeightInfo::seal_delegate_call(), CallTransferSurcharge => cost_args!(seal_call, 1, 0), CallInputCloned(len) => cost_args!(seal_call, 0, len), - Instantiate { input_data_len } => T::WeightInfo::seal_instantiate(input_data_len, 32), + Instantiate { input_data_len } => T::WeightInfo::seal_instantiate(input_data_len), HashSha256(len) => T::WeightInfo::seal_hash_sha2_256(len), HashKeccak256(len) => T::WeightInfo::seal_hash_keccak_256(len), HashBlake256(len) => T::WeightInfo::seal_hash_blake2_256(len), @@ -661,6 +661,27 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { memory.write(out_len_ptr, &buf_len.encode()) } + /// Same as `write_sandbox_output` but for static size output. + pub fn write_fixed_sandbox_output( + &mut self, + memory: &mut M, + out_ptr: u32, + buf: &[u8], + allow_skip: bool, + create_token: impl FnOnce(u32) -> Option, + ) -> Result<(), DispatchError> { + if allow_skip && out_ptr == SENTINEL { + return Ok(()) + } + + let buf_len = buf.len() as u32; + if let Some(costs) = create_token(buf_len) { + self.charge_gas(costs)?; + } + + memory.write(out_ptr, buf) + } + /// Computes the given hash function on the supplied input. /// /// Reads from the sandboxed input buffer into an intermediate buffer. @@ -1010,7 +1031,6 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { input_data_ptr: u32, input_data_len: u32, address_ptr: u32, - address_len_ptr: u32, output_ptr: u32, output_len_ptr: u32, salt_ptr: u32, @@ -1041,11 +1061,10 @@ impl<'a, E: Ext, M: ?Sized + Memory> Runtime<'a, E, M> { ); if let Ok((address, output)) = &instantiate_outcome { if !output.flags.contains(ReturnFlags::REVERT) { - self.write_sandbox_output( + self.write_fixed_sandbox_output( memory, address_ptr, - address_len_ptr, - &address.encode(), + &address.as_bytes(), true, already_charged, )?; @@ -1169,12 +1188,12 @@ pub mod env { fn transfer( &mut self, memory: &mut M, - account_ptr: u32, + address_ptr: u32, value_ptr: u32, ) -> Result { self.charge_gas(RuntimeCosts::Transfer)?; let mut callee = H160::zero(); - memory.read_into_buf(account_ptr, callee.as_bytes_mut())?; + memory.read_into_buf(address_ptr, callee.as_bytes_mut())?; let value: BalanceOf<::T> = memory.read_as(value_ptr)?; let result = self.ext.transfer(&callee, value); match result { @@ -1258,11 +1277,9 @@ pub mod env { input_data_ptr: u32, input_data_len: u32, address_ptr: u32, - address_len_ptr: u32, output_ptr: u32, output_len_ptr: u32, salt_ptr: u32, - _salt_len: u32, ) -> Result { self.instantiate( memory, @@ -1273,7 +1290,6 @@ pub mod env { input_data_ptr, input_data_len, address_ptr, - address_len_ptr, output_ptr, output_len_ptr, salt_ptr, @@ -1320,13 +1336,12 @@ pub mod env { /// Stores the address of the caller into the supplied buffer. /// See [`pallet_revive_uapi::HostFn::caller`]. #[api_version(0)] - fn caller(&mut self, memory: &mut M, out_ptr: u32, out_len_ptr: u32) -> Result<(), TrapReason> { + fn caller(&mut self, memory: &mut M, out_ptr: u32) -> Result<(), TrapReason> { self.charge_gas(RuntimeCosts::Caller)?; let caller = ::AddressMapper::to_address(self.ext.caller().account_id()?); - Ok(self.write_sandbox_output( + Ok(self.write_fixed_sandbox_output( memory, out_ptr, - out_len_ptr, caller.as_bytes(), false, already_charged, @@ -1349,18 +1364,16 @@ pub mod env { fn code_hash( &mut self, memory: &mut M, - account_ptr: u32, + addr_ptr: u32, out_ptr: u32, - out_len_ptr: u32, ) -> Result { self.charge_gas(RuntimeCosts::CodeHash)?; let mut address = H160::zero(); - memory.read_into_buf(account_ptr, address.as_bytes_mut())?; + memory.read_into_buf(addr_ptr, address.as_bytes_mut())?; if let Some(value) = self.ext.code_hash(&address) { - self.write_sandbox_output( + self.write_fixed_sandbox_output( memory, out_ptr, - out_len_ptr, &value.encode(), false, already_charged, @@ -1374,18 +1387,12 @@ pub mod env { /// Retrieve the code hash of the currently executing contract. /// See [`pallet_revive_uapi::HostFn::own_code_hash`]. #[api_version(0)] - fn own_code_hash( - &mut self, - memory: &mut M, - out_ptr: u32, - out_len_ptr: u32, - ) -> Result<(), TrapReason> { + fn own_code_hash(&mut self, memory: &mut M, out_ptr: u32) -> Result<(), TrapReason> { self.charge_gas(RuntimeCosts::OwnCodeHash)?; let code_hash_encoded = &self.ext.own_code_hash().encode(); - Ok(self.write_sandbox_output( + Ok(self.write_fixed_sandbox_output( memory, out_ptr, - out_len_ptr, code_hash_encoded, false, already_charged, @@ -1411,18 +1418,12 @@ pub mod env { /// Stores the address of the current contract into the supplied buffer. /// See [`pallet_revive_uapi::HostFn::address`]. #[api_version(0)] - fn address( - &mut self, - memory: &mut M, - out_ptr: u32, - out_len_ptr: u32, - ) -> Result<(), TrapReason> { + fn address(&mut self, memory: &mut M, out_ptr: u32) -> Result<(), TrapReason> { self.charge_gas(RuntimeCosts::Address)?; let address = self.ext.address(); - Ok(self.write_sandbox_output( + Ok(self.write_fixed_sandbox_output( memory, out_ptr, - out_len_ptr, address.as_bytes(), false, already_charged, diff --git a/substrate/frame/revive/src/weights.rs b/substrate/frame/revive/src/weights.rs index 6a0d27529d87..7974cc1260e4 100644 --- a/substrate/frame/revive/src/weights.rs +++ b/substrate/frame/revive/src/weights.rs @@ -65,8 +65,8 @@ pub trait WeightInfo { fn on_runtime_upgrade_in_progress() -> Weight; fn on_runtime_upgrade() -> Weight; fn call_with_code_per_byte(c: u32, ) -> Weight; - fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight; - fn instantiate(i: u32, s: u32, ) -> Weight; + fn instantiate_with_code(c: u32, i: u32) -> Weight; + fn instantiate(i: u32) -> Weight; fn call() -> Weight; fn upload_code_determinism_enforced(c: u32, ) -> Weight; fn upload_code_determinism_relaxed(c: u32, ) -> Weight; @@ -115,7 +115,7 @@ pub trait WeightInfo { fn seal_transfer() -> Weight; fn seal_call(t: u32, i: u32, ) -> Weight; fn seal_delegate_call() -> Weight; - fn seal_instantiate(i: u32, s: u32, ) -> Weight; + fn seal_instantiate(i: u32) -> Weight; fn seal_hash_sha2_256(n: u32, ) -> Weight; fn seal_hash_keccak_256(n: u32, ) -> Weight; fn seal_hash_blake2_256(n: u32, ) -> Weight; @@ -382,7 +382,7 @@ impl WeightInfo for SubstrateWeight { /// The range of component `c` is `[0, 125952]`. /// The range of component `i` is `[0, 1048576]`. /// The range of component `s` is `[0, 1048576]`. - fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight { + fn instantiate_with_code(c: u32, i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `323` // Estimated: `6262` @@ -393,7 +393,6 @@ impl WeightInfo for SubstrateWeight { // Standard Error: 22 .saturating_add(Weight::from_parts(2_143, 0).saturating_mul(i.into())) // Standard Error: 22 - .saturating_add(Weight::from_parts(2_210, 0).saturating_mul(s.into())) .saturating_add(T::DbWeight::get().reads(8_u64)) .saturating_add(T::DbWeight::get().writes(7_u64)) } @@ -415,7 +414,7 @@ impl WeightInfo for SubstrateWeight { /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) /// The range of component `i` is `[0, 1048576]`. /// The range of component `s` is `[0, 1048576]`. - fn instantiate(i: u32, s: u32, ) -> Weight { + fn instantiate(i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `560` // Estimated: `4017` @@ -424,7 +423,6 @@ impl WeightInfo for SubstrateWeight { // Standard Error: 32 .saturating_add(Weight::from_parts(934, 0).saturating_mul(i.into())) // Standard Error: 32 - .saturating_add(Weight::from_parts(920, 0).saturating_mul(s.into())) .saturating_add(T::DbWeight::get().reads(8_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } @@ -978,7 +976,7 @@ impl WeightInfo for SubstrateWeight { /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) /// The range of component `i` is `[0, 983040]`. /// The range of component `s` is `[0, 983040]`. - fn seal_instantiate(i: u32, s: u32, ) -> Weight { + fn seal_instantiate(i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `676` // Estimated: `4132` @@ -987,7 +985,6 @@ impl WeightInfo for SubstrateWeight { // Standard Error: 24 .saturating_add(Weight::from_parts(581, 0).saturating_mul(i.into())) // Standard Error: 24 - .saturating_add(Weight::from_parts(915, 0).saturating_mul(s.into())) .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(3_u64)) } @@ -1375,7 +1372,7 @@ impl WeightInfo for () { /// The range of component `c` is `[0, 125952]`. /// The range of component `i` is `[0, 1048576]`. /// The range of component `s` is `[0, 1048576]`. - fn instantiate_with_code(c: u32, i: u32, s: u32, ) -> Weight { + fn instantiate_with_code(c: u32, i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `323` // Estimated: `6262` @@ -1386,7 +1383,6 @@ impl WeightInfo for () { // Standard Error: 22 .saturating_add(Weight::from_parts(2_143, 0).saturating_mul(i.into())) // Standard Error: 22 - .saturating_add(Weight::from_parts(2_210, 0).saturating_mul(s.into())) .saturating_add(RocksDbWeight::get().reads(8_u64)) .saturating_add(RocksDbWeight::get().writes(7_u64)) } @@ -1408,7 +1404,7 @@ impl WeightInfo for () { /// Proof: `Balances::Holds` (`max_values`: None, `max_size`: Some(193), added: 2668, mode: `Measured`) /// The range of component `i` is `[0, 1048576]`. /// The range of component `s` is `[0, 1048576]`. - fn instantiate(i: u32, s: u32, ) -> Weight { + fn instantiate(i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `560` // Estimated: `4017` @@ -1417,7 +1413,6 @@ impl WeightInfo for () { // Standard Error: 32 .saturating_add(Weight::from_parts(934, 0).saturating_mul(i.into())) // Standard Error: 32 - .saturating_add(Weight::from_parts(920, 0).saturating_mul(s.into())) .saturating_add(RocksDbWeight::get().reads(8_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } @@ -1971,7 +1966,7 @@ impl WeightInfo for () { /// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `Measured`) /// The range of component `i` is `[0, 983040]`. /// The range of component `s` is `[0, 983040]`. - fn seal_instantiate(i: u32, s: u32, ) -> Weight { + fn seal_instantiate(i: u32) -> Weight { // Proof Size summary in bytes: // Measured: `676` // Estimated: `4132` @@ -1980,7 +1975,6 @@ impl WeightInfo for () { // Standard Error: 24 .saturating_add(Weight::from_parts(581, 0).saturating_mul(i.into())) // Standard Error: 24 - .saturating_add(Weight::from_parts(915, 0).saturating_mul(s.into())) .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(3_u64)) } diff --git a/substrate/frame/revive/uapi/src/host.rs b/substrate/frame/revive/uapi/src/host.rs index 6eb662363f7c..f52ea9574025 100644 --- a/substrate/frame/revive/uapi/src/host.rs +++ b/substrate/frame/revive/uapi/src/host.rs @@ -40,12 +40,10 @@ pub enum HostFnImpl {} pub trait HostFn: private::Sealed { /// Stores the address of the current contract into the supplied buffer. /// - /// If the available space in `output` is less than the size of the value a trap is triggered. - /// /// # Parameters /// /// - `output`: A reference to the output data buffer to write the address. - fn address(output: &mut &mut [u8]); + fn address(output: &mut [u8; 20]); /// Lock a new delegate dependency to the contract. /// @@ -56,7 +54,7 @@ pub trait HostFn: private::Sealed { /// /// - `code_hash`: The code hash of the dependency. Should be decodable as an `T::Hash`. Traps /// otherwise. - fn lock_delegate_dependency(code_hash: &[u8]); + fn lock_delegate_dependency(code_hash: &[u8; 32]); /// Stores the *free* balance of the current account into the supplied buffer. /// @@ -105,7 +103,7 @@ pub trait HostFn: private::Sealed { /// - [NotCallable][`crate::ReturnErrorCode::NotCallable] fn call( flags: CallFlags, - callee: &[u8], + callee: &[u8; 20], ref_time_limit: u64, proof_size_limit: u64, deposit: Option<&[u8]>, @@ -166,8 +164,6 @@ pub trait HostFn: private::Sealed { /// Stores the address of the caller into the supplied buffer. /// - /// If the available space in `output` is less than the size of the value a trap is triggered. - /// /// If this is a top-level call (i.e. initiated by an extrinsic) the origin address of the /// extrinsic will be returned. Otherwise, if this call is initiated by another contract then /// the address of the contract will be returned. @@ -178,7 +174,7 @@ pub trait HostFn: private::Sealed { /// # Parameters /// /// - `output`: A reference to the output data buffer to write the caller address. - fn caller(output: &mut &mut [u8]); + fn caller(output: &mut [u8; 20]); /// Checks whether the caller of the current contract is the origin of the whole call stack. /// @@ -216,15 +212,13 @@ pub trait HostFn: private::Sealed { /// /// # Parameters /// - /// - `account_id`: The address of the contract.Should be decodable as an `T::AccountId`. Traps - /// otherwise. + /// - `addr`: The address of the contract. /// - `output`: A reference to the output data buffer to write the code hash. /// - /// /// # Errors /// /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] - fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result; + fn code_hash(addr: &[u8; 20], output: &mut [u8; 32]) -> Result; /// Checks whether there is a value stored under the given key. /// @@ -281,7 +275,7 @@ pub trait HostFn: private::Sealed { /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] fn delegate_call( flags: CallFlags, - code_hash: &[u8], + code_hash: &[u8; 32], input_data: &[u8], output: Option<&mut &mut [u8]>, ) -> Result; @@ -405,28 +399,27 @@ pub trait HostFn: private::Sealed { /// - [TransferFailed][`crate::ReturnErrorCode::TransferFailed] /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] fn instantiate( - code_hash: &[u8], + code_hash: &[u8; 32], ref_time_limit: u64, proof_size_limit: u64, deposit: Option<&[u8]>, value: &[u8], input: &[u8], - address: Option<&mut &mut [u8]>, + address: Option<&mut [u8; 20]>, output: Option<&mut &mut [u8]>, - salt: &[u8], + salt: &[u8; 32], ) -> Result; /// Checks whether a specified address belongs to a contract. /// /// # Parameters /// - /// - `account_id`: The address to check. Should be decodable as an `T::AccountId`. Traps - /// otherwise. + /// - `address`: The address to check /// /// # Return /// /// Returns `true` if the address belongs to a contract. - fn is_contract(account_id: &[u8]) -> bool; + fn is_contract(address: &[u8; 20]) -> bool; /// Stores the minimum balance (a.k.a. existential deposit) into the supplied buffer. /// The data is encoded as `T::Balance`. @@ -443,7 +436,7 @@ pub trait HostFn: private::Sealed { /// # Parameters /// /// - `output`: A reference to the output data buffer to write the code hash. - fn own_code_hash(output: &mut [u8]); + fn own_code_hash(output: &mut [u8; 32]); /// Load the latest block timestamp into the supplied buffer /// @@ -462,7 +455,7 @@ pub trait HostFn: private::Sealed { /// /// - `code_hash`: The code hash of the dependency. Should be decodable as an `T::Hash`. Traps /// otherwise. - fn unlock_delegate_dependency(code_hash: &[u8]); + fn unlock_delegate_dependency(code_hash: &[u8; 32]); /// Cease contract execution and save a data buffer as a result of the execution. /// @@ -510,7 +503,7 @@ pub trait HostFn: private::Sealed { /// # Errors /// /// - [CodeNotFound][`crate::ReturnErrorCode::CodeNotFound] - fn set_code_hash(code_hash: &[u8]) -> Result; + fn set_code_hash(code_hash: &[u8; 32]) -> Result; /// Set the value at the given key in the contract storage. /// @@ -554,14 +547,13 @@ pub trait HostFn: private::Sealed { /// /// # Parameters /// - /// - `account_id`: The address of the account to transfer funds to. Should be decodable as an - /// `T::AccountId`. Traps otherwise. + /// - `address`: The address of the account to transfer funds to. /// - `value`: The value to transfer. Should be decodable as a `T::Balance`. Traps otherwise. /// /// # Errors /// /// - [TransferFailed][`crate::ReturnErrorCode::TransferFailed] - fn transfer(account_id: &[u8], value: &[u8]) -> Result; + fn transfer(address: &[u8; 20], value: &[u8]) -> Result; /// Remove the calling account and transfer remaining **free** balance. /// @@ -571,15 +563,14 @@ pub trait HostFn: private::Sealed { /// /// # Parameters /// - /// - `beneficiary`: The address of the beneficiary account, Should be decodable as an - /// `T::AccountId`. + /// - `beneficiary`: The address of the beneficiary account /// /// # Traps /// /// - The contract is live i.e is already on the call stack. /// - Failed to send the balance to the beneficiary. /// - The deletion queue is full. - fn terminate(beneficiary: &[u8]) -> !; + fn terminate(beneficiary: &[u8; 20]) -> !; /// Stores the value transferred along with this call/instantiate into the supplied buffer. /// The data is encoded as `T::Balance`. diff --git a/substrate/frame/revive/uapi/src/host/riscv32.rs b/substrate/frame/revive/uapi/src/host/riscv32.rs index 0b7130015f13..c8218bb8f737 100644 --- a/substrate/frame/revive/uapi/src/host/riscv32.rs +++ b/substrate/frame/revive/uapi/src/host/riscv32.rs @@ -58,7 +58,7 @@ mod sys { out_ptr: *mut u8, out_len_ptr: *mut u32, ) -> ReturnCode; - pub fn transfer(account_ptr: *const u8, value_ptr: *const u8) -> ReturnCode; + pub fn transfer(address_ptr: *const u8, value_ptr: *const u8) -> ReturnCode; pub fn call(ptr: *const u8) -> ReturnCode; pub fn delegate_call( flags: u32, @@ -72,17 +72,13 @@ mod sys { pub fn terminate(beneficiary_ptr: *const u8); pub fn input(out_ptr: *mut u8, out_len_ptr: *mut u32); pub fn seal_return(flags: u32, data_ptr: *const u8, data_len: u32); - pub fn caller(out_ptr: *mut u8, out_len_ptr: *mut u32); + pub fn caller(out_ptr: *mut u8); pub fn is_contract(account_ptr: *const u8) -> ReturnCode; - pub fn code_hash( - account_ptr: *const u8, - out_ptr: *mut u8, - out_len_ptr: *mut u32, - ) -> ReturnCode; - pub fn own_code_hash(out_ptr: *mut u8, out_len_ptr: *mut u32); + pub fn code_hash(address_ptr: *const u8, out_ptr: *mut u8) -> ReturnCode; + pub fn own_code_hash(out_ptr: *mut u8); pub fn caller_is_origin() -> ReturnCode; pub fn caller_is_root() -> ReturnCode; - pub fn address(out_ptr: *mut u8, out_len_ptr: *mut u32); + pub fn address(out_ptr: *mut u8); pub fn weight_to_fee( ref_time: u64, proof_size: u64, @@ -198,17 +194,20 @@ fn ptr_or_sentinel(data: &Option<&[u8]>) -> *const u8 { impl HostFn for HostFnImpl { fn instantiate( - code_hash: &[u8], + code_hash: &[u8; 32], ref_time_limit: u64, proof_size_limit: u64, deposit_limit: Option<&[u8]>, value: &[u8], input: &[u8], - mut address: Option<&mut &mut [u8]>, + mut address: Option<&mut [u8; 20]>, mut output: Option<&mut &mut [u8]>, - salt: &[u8], + salt: &[u8; 32], ) -> Result { - let (address_ptr, mut address_len) = ptr_len_or_sentinel(&mut address); + let address = match address { + Some(ref mut data) => data.as_mut_ptr(), + None => crate::SENTINEL as _, + }; let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output); let deposit_limit_ptr = ptr_or_sentinel(&deposit_limit); #[repr(packed)] @@ -220,13 +219,11 @@ impl HostFn for HostFnImpl { deposit_limit: *const u8, value: *const u8, input: *const u8, - input_len: usize, + input_len: u32, address: *const u8, - address_len: *mut u32, output: *mut u8, output_len: *mut u32, salt: *const u8, - salt_len: usize, } let args = Args { code_hash: code_hash.as_ptr(), @@ -235,21 +232,15 @@ impl HostFn for HostFnImpl { deposit_limit: deposit_limit_ptr, value: value.as_ptr(), input: input.as_ptr(), - input_len: input.len(), - address: address_ptr, - address_len: &mut address_len as *mut _, + input_len: input.len() as _, + address, output: output_ptr, output_len: &mut output_len as *mut _, salt: salt.as_ptr(), - salt_len: salt.len(), }; let ret_code = { unsafe { sys::instantiate(&args as *const Args as *const _) } }; - if let Some(ref mut address) = address { - extract_from_slice(address, address_len as usize); - } - if let Some(ref mut output) = output { extract_from_slice(output, output_len as usize); } @@ -259,7 +250,7 @@ impl HostFn for HostFnImpl { fn call( flags: CallFlags, - callee: &[u8], + callee: &[u8; 20], ref_time_limit: u64, proof_size_limit: u64, deposit_limit: Option<&[u8]>, @@ -279,7 +270,7 @@ impl HostFn for HostFnImpl { deposit_limit: *const u8, value: *const u8, input: *const u8, - input_len: usize, + input_len: u32, output: *mut u8, output_len: *mut u32, } @@ -291,7 +282,7 @@ impl HostFn for HostFnImpl { deposit_limit: deposit_limit_ptr, value: value.as_ptr(), input: input.as_ptr(), - input_len: input.len(), + input_len: input.len() as _, output: output_ptr, output_len: &mut output_len as *mut _, }; @@ -311,7 +302,7 @@ impl HostFn for HostFnImpl { fn delegate_call( flags: CallFlags, - code_hash: &[u8], + code_hash: &[u8; 32], input: &[u8], mut output: Option<&mut &mut [u8]>, ) -> Result { @@ -336,8 +327,8 @@ impl HostFn for HostFnImpl { ret_code.into() } - fn transfer(account_id: &[u8], value: &[u8]) -> Result { - let ret_code = unsafe { sys::transfer(account_id.as_ptr(), value.as_ptr()) }; + fn transfer(address: &[u8; 20], value: &[u8]) -> Result { + let ret_code = unsafe { sys::transfer(address.as_ptr(), value.as_ptr()) }; ret_code.into() } @@ -415,7 +406,7 @@ impl HostFn for HostFnImpl { ret_code.into() } - fn terminate(beneficiary: &[u8]) -> ! { + fn terminate(beneficiary: &[u8; 20]) -> ! { unsafe { sys::terminate(beneficiary.as_ptr()) } panic!("terminate does not return"); } @@ -458,8 +449,16 @@ impl HostFn for HostFnImpl { ret_code.into() } + fn address(output: &mut [u8; 20]) { + unsafe { sys::address(output.as_mut_ptr()) } + } + + fn caller(output: &mut [u8; 20]) { + unsafe { sys::caller(output.as_mut_ptr()) } + } + impl_wrapper_for! { - caller, block_number, address, balance, + block_number, balance, value_transferred,now, minimum_balance, weight_left, } @@ -512,8 +511,8 @@ impl HostFn for HostFnImpl { ret_code.into() } - fn is_contract(account_id: &[u8]) -> bool { - let ret_val = unsafe { sys::is_contract(account_id.as_ptr()) }; + fn is_contract(address: &[u8; 20]) -> bool { + let ret_val = unsafe { sys::is_contract(address.as_ptr()) }; ret_val.into_bool() } @@ -522,28 +521,25 @@ impl HostFn for HostFnImpl { ret_val.into_bool() } - fn set_code_hash(code_hash: &[u8]) -> Result { + fn set_code_hash(code_hash: &[u8; 32]) -> Result { let ret_val = unsafe { sys::set_code_hash(code_hash.as_ptr()) }; ret_val.into() } - fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result { - let mut output_len = output.len() as u32; - let ret_val = - unsafe { sys::code_hash(account_id.as_ptr(), output.as_mut_ptr(), &mut output_len) }; + fn code_hash(address: &[u8; 20], output: &mut [u8; 32]) -> Result { + let ret_val = unsafe { sys::code_hash(address.as_ptr(), output.as_mut_ptr()) }; ret_val.into() } - fn own_code_hash(output: &mut [u8]) { - let mut output_len = output.len() as u32; - unsafe { sys::own_code_hash(output.as_mut_ptr(), &mut output_len) } + fn own_code_hash(output: &mut [u8; 32]) { + unsafe { sys::own_code_hash(output.as_mut_ptr()) } } - fn lock_delegate_dependency(code_hash: &[u8]) { + fn lock_delegate_dependency(code_hash: &[u8; 32]) { unsafe { sys::lock_delegate_dependency(code_hash.as_ptr()) } } - fn unlock_delegate_dependency(code_hash: &[u8]) { + fn unlock_delegate_dependency(code_hash: &[u8; 32]) { unsafe { sys::unlock_delegate_dependency(code_hash.as_ptr()) } }