From 3e4bbe8ca1746e8f4f98b7c7660bc39f1fad9f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20Bald=C3=A9?= Date: Thu, 9 Jan 2025 20:46:53 +0000 Subject: [PATCH] Remove `U256_MAX` const, use `U256::MAX` --- .../frame/revive/fixtures/contracts/call.rs | 10 ++-- .../contracts/call_diverging_out_len.rs | 6 +-- .../fixtures/contracts/call_return_code.rs | 10 ++-- .../contracts/call_runtime_and_call.rs | 10 ++-- .../contracts/call_with_flags_and_value.rs | 10 ++-- .../fixtures/contracts/call_with_limit.rs | 4 +- .../fixtures/contracts/caller_contract.rs | 50 +++++++++---------- .../contracts/chain_extension_temp_storage.rs | 10 ++-- .../fixtures/contracts/create1_with_value.rs | 14 +++++- .../create_transient_storage_and_call.rs | 10 ++-- .../fixtures/contracts/delegate_call.rs | 4 +- .../contracts/delegate_call_simple.rs | 4 +- .../contracts/destroy_and_transfer.rs | 20 ++++---- .../frame/revive/fixtures/contracts/drain.rs | 4 +- .../contracts/instantiate_return_code.rs | 4 +- .../contracts/locking_delegate_dependency.rs | 12 ++++- .../frame/revive/fixtures/contracts/origin.rs | 4 +- .../fixtures/contracts/read_only_call.rs | 10 ++-- .../revive/fixtures/contracts/recurse.rs | 10 ++-- .../fixtures/contracts/return_data_api.rs | 8 +-- .../fixtures/contracts/self_destruct.rs | 10 ++-- .../contracts/transfer_return_code.rs | 4 +- substrate/frame/revive/src/exec.rs | 32 +++++------- substrate/frame/revive/src/tests.rs | 10 ++-- substrate/frame/revive/uapi/src/lib.rs | 21 -------- 25 files changed, 140 insertions(+), 151 deletions(-) diff --git a/substrate/frame/revive/fixtures/contracts/call.rs b/substrate/frame/revive/fixtures/contracts/call.rs index 035db56a5b41..7c4c0882c6b8 100644 --- a/substrate/frame/revive/fixtures/contracts/call.rs +++ b/substrate/frame/revive/fixtures/contracts/call.rs @@ -20,7 +20,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -38,10 +38,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::empty(), callee_addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. callee_input, None, ) diff --git a/substrate/frame/revive/fixtures/contracts/call_diverging_out_len.rs b/substrate/frame/revive/fixtures/contracts/call_diverging_out_len.rs index 069ec0a483e1..9a8fe5f5f6cc 100644 --- a/substrate/frame/revive/fixtures/contracts/call_diverging_out_len.rs +++ b/substrate/frame/revive/fixtures/contracts/call_diverging_out_len.rs @@ -28,7 +28,7 @@ extern crate common; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; const BUF_SIZE: usize = 8; static DATA: [u8; BUF_SIZE] = [1, 2, 3, 4, 5, 6, 7, 8]; @@ -44,7 +44,7 @@ fn assert_call(callee_address: &[u8; 20], expected_output: [u8; callee_address, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &[0u8; 32], &[], Some(output_buf_capped), @@ -69,7 +69,7 @@ fn assert_instantiate(expected_output: [u8; BUF_SIZE]) { &code_hash, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &[0; 32], &[0; 32], None, diff --git a/substrate/frame/revive/fixtures/contracts/call_return_code.rs b/substrate/frame/revive/fixtures/contracts/call_return_code.rs index 00ac4e51f5fe..19b3ae3fdb26 100644 --- a/substrate/frame/revive/fixtures/contracts/call_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/call_return_code.rs @@ -22,7 +22,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -42,10 +42,10 @@ pub extern "C" fn call() { let err_code = match api::call( uapi::CallFlags::empty(), callee_addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. - value, // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. + value, // Value transferred to the contract. input, None, ) { 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 5fc3242cf3ba..78b275459f0e 100644 --- a/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs +++ b/substrate/frame/revive/fixtures/contracts/call_runtime_and_call.rs @@ -19,7 +19,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -42,10 +42,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::empty(), callee_addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. callee_input, None, ) 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 d90b71aa73a5..155a4b41bd95 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 @@ -20,7 +20,7 @@ #![no_main] use common::{input, u256_bytes}; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -40,10 +40,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::from_bits(flags).unwrap(), callee_addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. - &u256_bytes(value), // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. + &u256_bytes(value), // Value transferred to the contract. forwarded_input, None, ) diff --git a/substrate/frame/revive/fixtures/contracts/call_with_limit.rs b/substrate/frame/revive/fixtures/contracts/call_with_limit.rs index 4ff0352dfe51..af5c301a353c 100644 --- a/substrate/frame/revive/fixtures/contracts/call_with_limit.rs +++ b/substrate/frame/revive/fixtures/contracts/call_with_limit.rs @@ -21,7 +21,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -43,7 +43,7 @@ pub extern "C" fn call() { callee_addr, ref_time, proof_size, - &U256_MAX, // No deposit limit. + &[u8::MAX; 32], // No deposit limit. &[0u8; 32], // value transferred to the contract. forwarded_input, None, diff --git a/substrate/frame/revive/fixtures/contracts/caller_contract.rs b/substrate/frame/revive/fixtures/contracts/caller_contract.rs index 99a821ebc0e8..d042dc2c22a2 100644 --- a/substrate/frame/revive/fixtures/contracts/caller_contract.rs +++ b/substrate/frame/revive/fixtures/contracts/caller_contract.rs @@ -19,7 +19,7 @@ #![no_main] use common::{input, u256_bytes}; -use uapi::{HostFn, HostFnImpl as api, ReturnErrorCode, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api, ReturnErrorCode}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -42,9 +42,9 @@ pub extern "C" fn call() { // Fail to deploy the contract since it returns a non-zero exit status. let res = api::instantiate( code_hash, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &reverted_input, None, @@ -56,9 +56,9 @@ pub extern "C" fn call() { // Fail to deploy the contract due to insufficient ref_time weight. let res = api::instantiate( code_hash, - 1u64, // too little ref_time weight - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + 1u64, // too little ref_time weight + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &input, None, @@ -70,9 +70,9 @@ pub extern "C" fn call() { // Fail to deploy the contract due to insufficient proof_size weight. let res = api::instantiate( code_hash, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - 1u64, // Too little proof_size weight - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + 1u64, // Too little proof_size weight + &[u8::MAX; 32], // No deposit limit. &value, &input, None, @@ -86,9 +86,9 @@ pub extern "C" fn call() { api::instantiate( code_hash, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &input, Some(&mut callee), @@ -101,9 +101,9 @@ pub extern "C" fn call() { let res = api::call( uapi::CallFlags::empty(), &callee, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &reverted_input, None, @@ -114,9 +114,9 @@ pub extern "C" fn call() { let res = api::call( uapi::CallFlags::empty(), &callee, - 1u64, // Too little ref_time weight. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + 1u64, // Too little ref_time weight. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &input, None, @@ -127,9 +127,9 @@ pub extern "C" fn call() { let res = api::call( uapi::CallFlags::empty(), &callee, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - 1u64, // too little proof_size weight - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + 1u64, // too little proof_size weight + &[u8::MAX; 32], // No deposit limit. &value, &input, None, @@ -141,9 +141,9 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::empty(), &callee, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &value, &input, Some(&mut &mut output[..]), 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 8c3e897df813..9b76b9d39ee9 100644 --- a/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs +++ b/substrate/frame/revive/fixtures/contracts/chain_extension_temp_storage.rs @@ -21,7 +21,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -54,10 +54,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::ALLOW_REENTRY, &addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. input, None, ) diff --git a/substrate/frame/revive/fixtures/contracts/create1_with_value.rs b/substrate/frame/revive/fixtures/contracts/create1_with_value.rs index 22de069db5e6..3554f8f620a2 100644 --- a/substrate/frame/revive/fixtures/contracts/create1_with_value.rs +++ b/substrate/frame/revive/fixtures/contracts/create1_with_value.rs @@ -19,7 +19,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -34,6 +34,16 @@ pub extern "C" fn call() { api::value_transferred(&mut value); // Deploy the contract with no salt (equivalent to create1). - let ret = api::instantiate(code_hash, u64::MAX, u64::MAX, &U256_MAX, &value, &[], None, None, None); + let ret = api::instantiate( + code_hash, + u64::MAX, + u64::MAX, + &[u8::MAX; 32], + &value, + &[], + None, + None, + None + ); assert!(ret.is_ok()); } 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 390b6e0735e3..660db84028db 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 @@ -20,7 +20,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, StorageFlags, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api, StorageFlags}; static BUFFER: [u8; 448] = [0u8; 448]; @@ -49,10 +49,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::empty(), callee, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. input, None, ) diff --git a/substrate/frame/revive/fixtures/contracts/delegate_call.rs b/substrate/frame/revive/fixtures/contracts/delegate_call.rs index 686c459b9da9..0dedd5f704cb 100644 --- a/substrate/frame/revive/fixtures/contracts/delegate_call.rs +++ b/substrate/frame/revive/fixtures/contracts/delegate_call.rs @@ -19,7 +19,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, StorageFlags, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api, StorageFlags}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -51,7 +51,7 @@ pub extern "C" fn call() { address, ref_time, proof_size, - &U256_MAX, + &[u8::MAX; 32], &input, None ).unwrap(); diff --git a/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs b/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs index 116d648bb60d..b7bdb792c76c 100644 --- a/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs +++ b/substrate/frame/revive/fixtures/contracts/delegate_call_simple.rs @@ -19,7 +19,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -37,7 +37,7 @@ pub extern "C" fn call() { address, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &input, None ).unwrap(); diff --git a/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs b/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs index e921b1b9f336..c2c7da528ba7 100644 --- a/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs +++ b/substrate/frame/revive/fixtures/contracts/destroy_and_transfer.rs @@ -19,7 +19,7 @@ #![no_main] use common::{input, u256_bytes}; -use uapi::{HostFn, HostFnImpl as api, StorageFlags, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api, StorageFlags}; const ADDRESS_KEY: [u8; 32] = [0u8; 32]; const VALUE: [u8; 32] = u256_bytes(65536); @@ -35,9 +35,9 @@ pub extern "C" fn deploy() { api::instantiate( code_hash, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &VALUE, &input, Some(&mut address), @@ -62,9 +62,9 @@ pub extern "C" fn call() { let res = api::call( uapi::CallFlags::empty(), &callee_addr, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &VALUE, &[0u8; 1], None, @@ -75,9 +75,9 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::empty(), &callee_addr, - u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. - u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. + u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. + &[u8::MAX; 32], // No deposit limit. &VALUE, &[0u8; 0], None, diff --git a/substrate/frame/revive/fixtures/contracts/drain.rs b/substrate/frame/revive/fixtures/contracts/drain.rs index 23bd6e0a4704..53fb213143c4 100644 --- a/substrate/frame/revive/fixtures/contracts/drain.rs +++ b/substrate/frame/revive/fixtures/contracts/drain.rs @@ -19,7 +19,7 @@ #![no_main] use common::{u256_bytes, u64_output}; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -41,7 +41,7 @@ pub extern "C" fn call() { &[0u8; 20], 0, 0, - &U256_MAX, + &[u8::MAX; 32], &u256_bytes(balance), &[], None, diff --git a/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs b/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs index 6b43f9fed9cd..f7cbd75be5aa 100644 --- a/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/instantiate_return_code.rs @@ -19,7 +19,7 @@ #![no_main] use common::{input, u256_bytes}; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -35,7 +35,7 @@ pub extern "C" fn call() { code_hash, u64::MAX, // How much ref_time weight to devote for the execution. u64::MAX = use all. u64::MAX, // How much proof_size weight to devote for the execution. u64::MAX = use all. - &U256_MAX, // No deposit limit. + &[u8::MAX; 32], // No deposit limit. &u256_bytes(10_000u64), // Value to transfer. input, None, diff --git a/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs b/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs index 9d0bbe5c8b9a..6be5d5c72f9a 100644 --- a/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs +++ b/substrate/frame/revive/fixtures/contracts/locking_delegate_dependency.rs @@ -21,7 +21,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; const ALICE_FALLBACK: [u8; 20] = [1u8; 20]; @@ -52,7 +52,15 @@ fn load_input(delegate_call: bool) { } if delegate_call { - api::delegate_call(uapi::CallFlags::empty(), address, u64::MAX, u64::MAX, &U256_MAX, &[], None).unwrap(); + api::delegate_call( + uapi::CallFlags::empty(), + address, + u64::MAX, + u64::MAX, + &[u8::MAX; 32], + &[], + None + ).unwrap(); } } diff --git a/substrate/frame/revive/fixtures/contracts/origin.rs b/substrate/frame/revive/fixtures/contracts/origin.rs index b12a45b114f8..151ca3da77cd 100644 --- a/substrate/frame/revive/fixtures/contracts/origin.rs +++ b/substrate/frame/revive/fixtures/contracts/origin.rs @@ -23,7 +23,7 @@ #![no_main] extern crate common; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -51,7 +51,7 @@ pub extern "C" fn call() { &addr, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &[0; 32], &[], Some(&mut &mut buf[..]), diff --git a/substrate/frame/revive/fixtures/contracts/read_only_call.rs b/substrate/frame/revive/fixtures/contracts/read_only_call.rs index 96786cf9e370..0a87ecbb9b14 100644 --- a/substrate/frame/revive/fixtures/contracts/read_only_call.rs +++ b/substrate/frame/revive/fixtures/contracts/read_only_call.rs @@ -20,7 +20,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -39,10 +39,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::READ_ONLY, callee_addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = all. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = all. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. callee_input, None, ) diff --git a/substrate/frame/revive/fixtures/contracts/recurse.rs b/substrate/frame/revive/fixtures/contracts/recurse.rs index 64b4358e5ffb..ead565c01459 100644 --- a/substrate/frame/revive/fixtures/contracts/recurse.rs +++ b/substrate/frame/revive/fixtures/contracts/recurse.rs @@ -21,7 +21,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -43,10 +43,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::ALLOW_REENTRY, &addr, - u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all resources. - u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all resources. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value transferred to the contract. + u64::MAX, // How much ref_time to devote for the execution. u64::MAX = use all resources. + u64::MAX, // How much proof_size to devote for the execution. u64::MAX = use all resources. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value transferred to the contract. &(calls_left - 1).to_le_bytes(), None, ) diff --git a/substrate/frame/revive/fixtures/contracts/return_data_api.rs b/substrate/frame/revive/fixtures/contracts/return_data_api.rs index 0ce89bc71ca6..1407e5323ea1 100644 --- a/substrate/frame/revive/fixtures/contracts/return_data_api.rs +++ b/substrate/frame/revive/fixtures/contracts/return_data_api.rs @@ -27,7 +27,7 @@ #![no_main] use common::{input, u256_bytes}; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; const INPUT_BUF_SIZE: usize = 128; static INPUT_DATA: [u8; INPUT_BUF_SIZE] = [0xFF; INPUT_BUF_SIZE]; @@ -85,7 +85,7 @@ fn assert_balance_transfer_does_reset() { &[0u8; 20], u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &u256_bytes(128), &[], None @@ -121,7 +121,7 @@ pub extern "C" fn call() { code_hash, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &[0; 32], &construct_input(exit_flag), Some(&mut address_buf), @@ -135,7 +135,7 @@ pub extern "C" fn call() { address_buf, u64::MAX, u64::MAX, - &U256_MAX, + &[u8::MAX; 32], &[0; 32], &construct_input(exit_flag), None, diff --git a/substrate/frame/revive/fixtures/contracts/self_destruct.rs b/substrate/frame/revive/fixtures/contracts/self_destruct.rs index ea3eedf9ee0c..053e545deb19 100644 --- a/substrate/frame/revive/fixtures/contracts/self_destruct.rs +++ b/substrate/frame/revive/fixtures/contracts/self_destruct.rs @@ -19,7 +19,7 @@ #![no_main] use common::input; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; const DJANGO_FALLBACK: [u8; 20] = [4u8; 20]; @@ -42,10 +42,10 @@ pub extern "C" fn call() { api::call( uapi::CallFlags::ALLOW_REENTRY, &addr, - u64::MAX, // How much ref_time to devote for the execution. u64 = all. - u64::MAX, // How much proof_size to devote for the execution. u64 = all. - &U256_MAX, // No deposit limit. - &[0u8; 32], // Value to transfer. + u64::MAX, // How much ref_time to devote for the execution. u64 = all. + u64::MAX, // How much proof_size to devote for the execution. u64 = all. + &[u8::MAX; 32], // No deposit limit. + &[0u8; 32], // Value to transfer. &[0u8; 0], None, ) diff --git a/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs b/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs index cbfe34f96964..053f97feda4a 100644 --- a/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs +++ b/substrate/frame/revive/fixtures/contracts/transfer_return_code.rs @@ -19,7 +19,7 @@ #![no_main] use common::u256_bytes; -use uapi::{HostFn, HostFnImpl as api, U256_MAX}; +use uapi::{HostFn, HostFnImpl as api}; #[no_mangle] #[polkavm_derive::polkavm_export] @@ -33,7 +33,7 @@ pub extern "C" fn call() { &[0u8; 20], 0, 0, - &U256_MAX, + &[u8::MAX; 32], &u256_bytes(100u64), &[], None, diff --git a/substrate/frame/revive/src/exec.rs b/substrate/frame/revive/src/exec.rs index f99f5eb5ae40..a8d0da88ff40 100644 --- a/substrate/frame/revive/src/exec.rs +++ b/substrate/frame/revive/src/exec.rs @@ -1463,7 +1463,7 @@ where FrameArgs::Call { dest: dest.clone(), cached_info, delegated_call: None }, value, gas_limit, - deposit_limit.try_into().map_err(|_| Error::::BalanceConversionFailed)?, + deposit_limit.saturated_into::>(), // Enable read-only access if requested; cannot disable it if already set. read_only || self.is_read_only(), )? { @@ -1519,7 +1519,7 @@ where }, value, gas_limit, - deposit_limit.try_into().map_err(|_| Error::::BalanceConversionFailed)?, + deposit_limit.saturated_into::>(), self.is_read_only(), )?; self.run(executable.expect(FRAME_ALWAYS_EXISTS_ON_INSTANTIATE), input_data) @@ -1549,7 +1549,7 @@ where }, value.try_into().map_err(|_| Error::::BalanceConversionFailed)?, gas_limit, - deposit_limit.try_into().map_err(|_| Error::::BalanceConversionFailed)?, + deposit_limit.saturated_into::>(), self.is_read_only(), )?; let address = T::AddressMapper::to_address(&self.top_frame().account_id); @@ -1958,7 +1958,7 @@ mod tests { use assert_matches::assert_matches; use frame_support::{assert_err, assert_noop, assert_ok, parameter_types}; use frame_system::{AccountInfo, EventRecord, Phase}; - use pallet_revive_uapi::{ReturnFlags, U256_MAX}; + use pallet_revive_uapi::ReturnFlags; use pretty_assertions::assert_eq; use sp_io::hashing::keccak_256; use sp_runtime::{traits::Hash, DispatchError}; @@ -3099,7 +3099,7 @@ mod tests { .ext .instantiate( Weight::MAX, - U256::from_little_endian(&U256_MAX), + U256::MAX, dummy_ch, ::Currency::minimum_balance().into(), vec![], @@ -3803,7 +3803,7 @@ mod tests { ctx.ext .instantiate( Weight::MAX, - U256::from_little_endian(&U256_MAX), + U256::MAX, fail_code, ctx.ext.minimum_balance() * 100, vec![], @@ -3820,7 +3820,7 @@ mod tests { .ext .instantiate( Weight::MAX, - U256::from_little_endian(&U256_MAX), + U256::MAX, success_code, ctx.ext.minimum_balance() * 100, vec![], @@ -4597,7 +4597,7 @@ mod tests { // Successful instantiation should set the output let address = ctx .ext - .instantiate(Weight::MAX, U256::from_little_endian(&U256_MAX), ok_ch, value, vec![], None) + .instantiate(Weight::MAX, U256::MAX, ok_ch, value, vec![], None) .unwrap(); assert_eq!( ctx.ext.last_frame_output(), @@ -4606,15 +4606,7 @@ mod tests { // Balance transfers should reset the output ctx.ext - .call( - Weight::MAX, - U256::from_little_endian(&U256_MAX), - &address, - U256::from(1), - vec![], - true, - false, - ) + .call(Weight::MAX, U256::MAX, &address, U256::from(1), vec![], true, false) .unwrap(); assert_eq!(ctx.ext.last_frame_output(), &Default::default()); @@ -4827,7 +4819,7 @@ mod tests { // Constructors can not access the immutable data ctx.ext - .instantiate(Weight::MAX, U256::from_little_endian(&U256_MAX), dummy_ch, value, vec![], None) + .instantiate(Weight::MAX, U256::MAX, dummy_ch, value, vec![], None) .unwrap(); exec_success() @@ -4944,7 +4936,7 @@ mod tests { move |ctx, _| { let value = ::Currency::minimum_balance().into(); ctx.ext - .instantiate(Weight::MAX, U256::from_little_endian(&U256_MAX), dummy_ch, value, vec![], None) + .instantiate(Weight::MAX, U256::MAX, dummy_ch, value, vec![], None) .unwrap(); exec_success() @@ -4989,7 +4981,7 @@ mod tests { move |ctx, _| { let value = ::Currency::minimum_balance().into(); ctx.ext - .instantiate(Weight::MAX, U256::from_little_endian(&U256_MAX), dummy_ch, value, vec![], None) + .instantiate(Weight::MAX, U256::MAX, dummy_ch, value, vec![], None) .unwrap(); exec_success() diff --git a/substrate/frame/revive/src/tests.rs b/substrate/frame/revive/src/tests.rs index c4407eb56ff6..04a6c4416062 100644 --- a/substrate/frame/revive/src/tests.rs +++ b/substrate/frame/revive/src/tests.rs @@ -61,7 +61,7 @@ use frame_support::{ }; use frame_system::{EventRecord, Phase}; use pallet_revive_fixtures::{bench::dummy_unique, compile_module}; -use pallet_revive_uapi::{ReturnErrorCode as RuntimeReturnCode, U256_MAX}; +use pallet_revive_uapi::ReturnErrorCode as RuntimeReturnCode; use pallet_transaction_payment::{ConstFeeMultiplier, Multiplier}; use pretty_assertions::{assert_eq, assert_ne}; use sp_core::U256; @@ -3459,7 +3459,7 @@ fn deposit_limit_in_nested_calls() { assert_err_ignore_postinfo!( builder::call(addr_caller) .storage_deposit_limit(13) - .data((100u32, &addr_callee, U256_MAX).encode()) + .data((100u32, &addr_callee, U256::MAX).encode()) .build(), >::StorageDepositLimitExhausted, ); @@ -3473,7 +3473,7 @@ fn deposit_limit_in_nested_calls() { assert_err_ignore_postinfo!( builder::call(addr_caller) .storage_deposit_limit(14) - .data((101u32, &addr_callee, U256::from_little_endian(&U256_MAX),).encode()) + .data((101u32, &addr_callee, &U256::MAX).encode()) .build(), >::StorageDepositLimitExhausted, ); @@ -3495,7 +3495,7 @@ fn deposit_limit_in_nested_calls() { assert_err_ignore_postinfo!( builder::call(addr_caller) .storage_deposit_limit(0) - .data((87u32, &addr_callee, U256::from_little_endian(&U256_MAX),).encode()) + .data((87u32, &addr_callee, &U256::MAX.to_little_endian()).encode()) .build(), >::StorageDepositLimitExhausted, ); @@ -3554,7 +3554,7 @@ fn deposit_limit_in_nested_instantiate() { let ret = builder::bare_call(addr_caller) .origin(RuntimeOrigin::signed(BOB)) .storage_deposit_limit(DepositLimit::Balance(callee_info_len + 2 + ED + 1)) - .data((0u32, &code_hash_callee, U256::from_little_endian(&U256_MAX),).encode()) + .data((0u32, &code_hash_callee, &U256::MAX.to_little_endian()).encode()) .build_and_unwrap_result(); assert_return_code!(ret, RuntimeReturnCode::OutOfResources); // The charges made on instantiation should be rolled back. diff --git a/substrate/frame/revive/uapi/src/lib.rs b/substrate/frame/revive/uapi/src/lib.rs index 0715532c61e7..ef1798b4bf61 100644 --- a/substrate/frame/revive/uapi/src/lib.rs +++ b/substrate/frame/revive/uapi/src/lib.rs @@ -116,27 +116,6 @@ pub struct ReturnCode(u32); /// make sense for a memory location or length. const SENTINEL: u32 = u32::MAX; -/// Converts a `u64` to a 32-byte array, encoded little-endian. -/// -/// The `u64` value will be placed in the first 8 bytes, -/// and the remaining bytes are set to zero. -const fn u64_to_u256_bytes(value: u64) -> [u8; 32] { - let mut bytes = [0u8; 32]; - let value_bytes = value.to_le_bytes(); - let mut i = 0; - while i < 8 { - bytes[i] = value_bytes[i]; - i += 1; - } - bytes -} - -/// A constant representing `u64::MAX` as a 32-byte array, in little-endian encoding. -/// -/// Deposit limits are `U256`, but balances are represented as `u64`. -/// To represent no deposit limits on an operation, this should be used. -pub const U256_MAX: [u8; 32] = u64_to_u256_bytes(u64::MAX); - impl From for Option { fn from(code: ReturnCode) -> Self { (code.0 < SENTINEL).then_some(code.0)