From 29847ad5a6982147a43e1d11926cfe711e82e1f5 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 16:32:39 +0800 Subject: [PATCH 1/8] Enhance the weight info --- Cargo.lock | 4 +- frame/ethereum/src/lib.rs | 35 +++++++++-------- frame/evm/src/lib.rs | 3 -- frame/evm/src/runner/mod.rs | 14 +++---- frame/evm/src/runner/stack.rs | 58 +++++++++++----------------- primitives/evm/src/lib.rs | 3 ++ primitives/evm/src/validation.rs | 27 ++++++------- template/runtime/src/lib.rs | 66 +++++++++++++++----------------- 8 files changed, 94 insertions(+), 116 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb0da466bc..28a8885005 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6911,8 +6911,8 @@ checksum = "31b476131c3c86cb68032fdc5cb6d5a1045e3e42d96b69fa599fd77701e1f5bf" dependencies = [ "bitflags 2.4.0", "num-traits", - "rand", - "rand_chacha", + "rand 0.8.5", + "rand_chacha 0.3.1", "rand_xorshift", "unarray", ] diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 848960a51a..bae62cf6ba 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -361,16 +361,21 @@ pub mod pallet { } impl Pallet { - pub fn transaction_weight(transaction_data: &TransactionData) -> (Option, Option) { + pub fn transaction_weight(transaction_data: &TransactionData) -> Option { match ::GasWeightMapping::gas_to_weight( transaction_data.gas_limit.unique_saturated_into(), true, ) { - weight_limit if weight_limit.proof_size() > 0 => ( - Some(weight_limit), - Some(transaction_data.proof_size_base_cost()), - ), - _ => (None, None), + weight_limit if weight_limit.proof_size() > 0 => { + // todo: fix the unwrap() + let weight_info = fp_evm::WeightInfo::new_from_weight_limit( + Some(weight_limit), + Some(transaction_data.proof_size_base_cost()), + ) + .unwrap(); + weight_info + } + _ => None, } } @@ -493,7 +498,7 @@ impl Pallet { ) -> TransactionValidity { let transaction_data: TransactionData = transaction.into(); let transaction_nonce = transaction_data.nonce; - let (weight_limit, proof_size_base_cost) = Self::transaction_weight(&transaction_data); + let weight_info = Self::transaction_weight(&transaction_data); let (base_fee, _) = T::FeeCalculator::min_gas_price(); let (who, _) = pallet_evm::Pallet::::account_basic(&origin); @@ -506,8 +511,7 @@ impl Pallet { is_transactional: true, }, transaction_data.clone().into(), - weight_limit, - proof_size_base_cost, + weight_info, ) .validate_in_pool_for(&who) .and_then(|v| v.with_chain_id()) @@ -723,7 +727,7 @@ impl Pallet { config: Option, ) -> Result<(Option, Option, CallOrCreateInfo), DispatchErrorWithPostInfo> { let transaction_data: TransactionData = transaction.into(); - let (weight_limit, proof_size_base_cost) = Self::transaction_weight(&transaction_data); + let weight_info = Self::transaction_weight(&transaction_data); let is_transactional = true; let validate = false; @@ -801,8 +805,7 @@ impl Pallet { access_list, is_transactional, validate, - weight_limit, - proof_size_base_cost, + weight_info, config.as_ref().unwrap_or_else(|| T::config()), ) { Ok(res) => res, @@ -831,8 +834,7 @@ impl Pallet { access_list, is_transactional, validate, - weight_limit, - proof_size_base_cost, + weight_info, config.as_ref().unwrap_or_else(|| T::config()), ) { Ok(res) => res, @@ -861,7 +863,7 @@ impl Pallet { transaction: &Transaction, ) -> Result<(), TransactionValidityError> { let transaction_data: TransactionData = transaction.into(); - let (weight_limit, proof_size_base_cost) = Self::transaction_weight(&transaction_data); + let weight_info = Self::transaction_weight(&transaction_data); let (base_fee, _) = T::FeeCalculator::min_gas_price(); let (who, _) = pallet_evm::Pallet::::account_basic(&origin); @@ -874,8 +876,7 @@ impl Pallet { is_transactional: true, }, transaction_data.into(), - weight_limit, - proof_size_base_cost, + weight_info, ) .validate_in_block_for(&who) .and_then(|v| v.with_chain_id()) diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 81de8a366a..6041ffd9d7 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -246,7 +246,6 @@ pub mod pallet { is_transactional, validate, None, - None, T::config(), ) { Ok(info) => info, @@ -321,7 +320,6 @@ pub mod pallet { is_transactional, validate, None, - None, T::config(), ) { Ok(info) => info, @@ -409,7 +407,6 @@ pub mod pallet { is_transactional, validate, None, - None, T::config(), ) { Ok(info) => info, diff --git a/frame/evm/src/runner/mod.rs b/frame/evm/src/runner/mod.rs index 45ed14299e..6c679a084c 100644 --- a/frame/evm/src/runner/mod.rs +++ b/frame/evm/src/runner/mod.rs @@ -19,7 +19,7 @@ pub mod stack; use crate::{Config, Weight}; use alloc::vec::Vec; -use fp_evm::{CallInfo, CreateInfo}; +use fp_evm::{CallInfo, CreateInfo, WeightInfo}; use sp_core::{H160, H256, U256}; #[derive(Debug)] @@ -42,8 +42,7 @@ pub trait Runner { nonce: Option, access_list: Vec<(H160, Vec)>, is_transactional: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, evm_config: &evm::Config, ) -> Result<(), RunnerError>; @@ -59,8 +58,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; @@ -75,8 +73,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; @@ -92,8 +89,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; } diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 0707159cd2..39612f343a 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -75,8 +75,7 @@ where config: &'config evm::Config, precompiles: &'precompiles T::PrecompilesType, is_transactional: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, f: F, ) -> Result, RunnerError>> where @@ -112,8 +111,7 @@ where f, base_fee, weight, - weight_limit, - proof_size_base_cost, + weight_info, ); // Set IN_EVM to false @@ -137,8 +135,7 @@ where f: F, base_fee: U256, weight: Weight, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, ) -> Result, RunnerError>> where F: FnOnce( @@ -152,13 +149,15 @@ where R: Default, { // Used to record the external costs in the evm through the StackState implementation - let maybe_weight_info = - WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).map_err( - |_| RunnerError { - error: Error::::GasLimitTooLow, - weight, - }, - )?; + // let maybe_weight_info = + // WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).map_err( + // |_| RunnerError { + // error: Error::::GasLimitTooLow, + // weight, + // }, + // )?; + let maybe_weight_info = weight_info; + // The precompile check is only used for transactional invocations. However, here we always // execute the check, because the check has side effects. match precompiles.is_precompile(source, gas_limit) { @@ -366,8 +365,7 @@ where nonce: Option, access_list: Vec<(H160, Vec)>, is_transactional: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, evm_config: &evm::Config, ) -> Result<(), RunnerError> { let (base_fee, mut weight) = T::FeeCalculator::min_gas_price(); @@ -394,8 +392,7 @@ where value, access_list, }, - weight_limit, - proof_size_base_cost, + weight_info, ) .validate_in_block_for(&source_account) .and_then(|v| v.with_base_fee()) @@ -416,8 +413,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -432,8 +428,7 @@ where nonce, access_list.clone(), is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, config, )?; } @@ -447,8 +442,7 @@ where config, &precompiles, is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, |executor| executor.transact_call(source, target, value, input, gas_limit, access_list), ) } @@ -464,8 +458,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -480,8 +473,7 @@ where nonce, access_list.clone(), is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, config, )?; } @@ -495,8 +487,7 @@ where config, &precompiles, is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, |executor| { let address = executor.create_address(evm::CreateScheme::Legacy { caller: source }); T::OnCreate::on_create(source, address); @@ -519,8 +510,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -535,8 +525,7 @@ where nonce, access_list.clone(), is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, config, )?; } @@ -551,8 +540,7 @@ where config, &precompiles, is_transactional, - weight_limit, - proof_size_base_cost, + weight_info, |executor| { let address = executor.create_address(evm::CreateScheme::Create2 { caller: source, diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index efc94593a4..56f0c53d59 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -78,6 +78,7 @@ pub enum AccessedStorage { #[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct WeightInfo { + pub proof_size_base_cost: Option, pub ref_time_limit: Option, pub proof_size_limit: Option, pub ref_time_usage: Option, @@ -95,6 +96,7 @@ impl WeightInfo { if weight_limit.proof_size() >= proof_size_base_cost => { Some(WeightInfo { + proof_size_base_cost: Some(proof_size_base_cost), ref_time_limit: Some(weight_limit.ref_time()), proof_size_limit: Some(weight_limit.proof_size()), ref_time_usage: Some(0u64), @@ -102,6 +104,7 @@ impl WeightInfo { }) } (Some(weight_limit), None) => Some(WeightInfo { + proof_size_base_cost: None, ref_time_limit: Some(weight_limit.ref_time()), proof_size_limit: None, ref_time_usage: Some(0u64), diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index 4359dcaef1..0ac5ed7515 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -19,7 +19,7 @@ use alloc::vec::Vec; pub use evm::backend::Basic as Account; -use frame_support::{sp_runtime::traits::UniqueSaturatedInto, weights::Weight}; +use frame_support::sp_runtime::traits::UniqueSaturatedInto; use sp_core::{H160, H256, U256}; #[derive(Debug)] @@ -49,8 +49,7 @@ pub struct CheckEvmTransactionConfig<'config> { pub struct CheckEvmTransaction<'config, E: From> { pub config: CheckEvmTransactionConfig<'config>, pub transaction: CheckEvmTransactionInput, - pub weight_limit: Option, - pub proof_size_base_cost: Option, + pub weight_info: Option, _marker: core::marker::PhantomData, } @@ -87,14 +86,12 @@ impl<'config, E: From> CheckEvmTransaction<'config, pub fn new( config: CheckEvmTransactionConfig<'config>, transaction: CheckEvmTransactionInput, - weight_limit: Option, - proof_size_base_cost: Option, + weight_info: Option, ) -> Self { CheckEvmTransaction { config, transaction, - weight_limit, - proof_size_base_cost, + weight_info, _marker: Default::default(), } } @@ -203,14 +200,14 @@ impl<'config, E: From> CheckEvmTransaction<'config, if self.config.is_transactional { // Try to subtract the proof_size_base_cost from the Weight proof_size limit or fail. // Validate the weight limit can afford recording the proof size cost. - if let (Some(weight_limit), Some(proof_size_base_cost)) = - (self.weight_limit, self.proof_size_base_cost) - { - let _ = weight_limit - .proof_size() - .checked_sub(proof_size_base_cost) - .ok_or(TransactionValidationError::GasLimitTooLow)?; - } + // if let (Some(weight_limit), Some(proof_size_base_cost)) = + // (self.weight_limit, self.proof_size_base_cost) + // { + // let _ = weight_limit + // .proof_size() + // .checked_sub(proof_size_base_cost) + // .ok_or(TransactionValidationError::GasLimitTooLow)?; + // } // We must ensure a transaction can pay the cost of its data bytes. // If it can't it should not be included in a block. diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 4ed97e9c8f..06d416ef78 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -796,6 +796,7 @@ impl_runtime_apis! { access_list: Option)>>, ) -> Result { use pallet_evm::GasWeightMapping as _; + use fp_evm::WeightInfo; let config = if estimate { let mut config = ::config().clone(); @@ -805,45 +806,40 @@ impl_runtime_apis! { None }; - // Estimated encoded transaction size must be based on the heaviest transaction - // type (EIP1559Transaction) to be compatible with all transaction types. - let mut estimated_transaction_len = data.len() + - // pallet ethereum index: 1 - // transact call index: 1 - // Transaction enum variant: 1 - // chain_id 8 bytes - // nonce: 32 - // max_priority_fee_per_gas: 32 - // max_fee_per_gas: 32 - // gas_limit: 32 - // action: 21 (enum varianrt + call address) - // value: 32 - // access_list: 1 (empty vec size) - // 65 bytes signature - 258; - - if access_list.is_some() { - estimated_transaction_len += access_list.encoded_size(); - } - + // Estimated encoded transaction size must be based on the heaviest transaction + // type (EIP1559Transaction) to be compatible with all transaction types. + let mut estimated_transaction_len = data.len() + + // pallet ethereum index: 1 + // transact call index: 1 + // Transaction enum variant: 1 + // chain_id 8 bytes + // nonce: 32 + // max_priority_fee_per_gas: 32 + // max_fee_per_gas: 32 + // gas_limit: 32 + // action: 21 (enum varianrt + call address) + // value: 32 + // access_list: 1 (empty vec size) + // 65 bytes signature + 258; - let gas_limit = if gas_limit > U256::from(u64::MAX) { - u64::MAX - } else { - gas_limit.low_u64() - }; - let without_base_extrinsic_weight = true; + if access_list.is_some() { + estimated_transaction_len += access_list.encoded_size(); + } + let gas_limit = if gas_limit > U256::from(u64::MAX) { + u64::MAX + } else { + gas_limit.low_u64() + }; let (weight_limit, proof_size_base_cost) = - match ::GasWeightMapping::gas_to_weight( - gas_limit, - without_base_extrinsic_weight - ) { + match ::GasWeightMapping::gas_to_weight(gas_limit, true) { weight_limit if weight_limit.proof_size() > 0 => { (Some(weight_limit), Some(estimated_transaction_len as u64)) } _ => (None, None), }; + let weight_info = WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).unwrap(); ::Runner::call( from, @@ -857,8 +853,7 @@ impl_runtime_apis! { access_list.unwrap_or_default(), false, true, - weight_limit, - proof_size_base_cost, + weight_info, config.as_ref().unwrap_or(::config()), ).map_err(|err| err.error.into()) } @@ -875,6 +870,7 @@ impl_runtime_apis! { access_list: Option)>>, ) -> Result { use pallet_evm::GasWeightMapping as _; + use fp_evm::WeightInfo; let config = if estimate { let mut config = ::config().clone(); @@ -924,6 +920,7 @@ impl_runtime_apis! { _ => (None, None), }; + let weight_info = WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).unwrap(); ::Runner::create( from, data, @@ -935,8 +932,7 @@ impl_runtime_apis! { access_list.unwrap_or_default(), false, true, - weight_limit, - proof_size_base_cost, + weight_info, config.as_ref().unwrap_or(::config()), ).map_err(|err| err.error.into()) } From 0eb5db569c511dd422dc128d81975eace5ad70d2 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 17:18:02 +0800 Subject: [PATCH 2/8] Update the validation --- frame/ethereum/src/lib.rs | 18 +++++------------ primitives/evm/src/lib.rs | 34 ++++++++++---------------------- primitives/evm/src/validation.rs | 14 ++++--------- template/runtime/src/lib.rs | 29 +++++---------------------- 4 files changed, 24 insertions(+), 71 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index bae62cf6ba..4c9b60a094 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -362,21 +362,13 @@ pub mod pallet { impl Pallet { pub fn transaction_weight(transaction_data: &TransactionData) -> Option { - match ::GasWeightMapping::gas_to_weight( + let weight_limit = ::GasWeightMapping::gas_to_weight( transaction_data.gas_limit.unique_saturated_into(), true, - ) { - weight_limit if weight_limit.proof_size() > 0 => { - // todo: fix the unwrap() - let weight_info = fp_evm::WeightInfo::new_from_weight_limit( - Some(weight_limit), - Some(transaction_data.proof_size_base_cost()), - ) - .unwrap(); - weight_info - } - _ => None, - } + ); + let proof_size_base_cost = transaction_data.proof_size_base_cost(); + + fp_evm::WeightInfo::new(weight_limit, proof_size_base_cost) } fn recover_signer(transaction: &Transaction) -> Option { diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 56f0c53d59..29667aba18 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -86,32 +86,18 @@ pub struct WeightInfo { } impl WeightInfo { - pub fn new_from_weight_limit( - weight_limit: Option, - proof_size_base_cost: Option, - ) -> Result, &'static str> { - Ok(match (weight_limit, proof_size_base_cost) { - (None, _) => None, - (Some(weight_limit), Some(proof_size_base_cost)) - if weight_limit.proof_size() >= proof_size_base_cost => - { - Some(WeightInfo { - proof_size_base_cost: Some(proof_size_base_cost), - ref_time_limit: Some(weight_limit.ref_time()), - proof_size_limit: Some(weight_limit.proof_size()), - ref_time_usage: Some(0u64), - proof_size_usage: Some(proof_size_base_cost), - }) - } - (Some(weight_limit), None) => Some(WeightInfo { - proof_size_base_cost: None, + pub fn new(weight_limit: Weight, proof_size_base_cost: u64) -> Option { + if weight_limit.proof_size() >= proof_size_base_cost { + return Some(WeightInfo { + proof_size_base_cost: Some(proof_size_base_cost), ref_time_limit: Some(weight_limit.ref_time()), - proof_size_limit: None, + proof_size_limit: Some(weight_limit.proof_size()), ref_time_usage: Some(0u64), - proof_size_usage: None, - }), - _ => return Err("must provide Some valid weight limit or None"), - }) + proof_size_usage: Some(proof_size_base_cost), + }); + } + + None } fn try_consume(&self, cost: u64, limit: u64, usage: u64) -> Result { diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index 0ac5ed7515..d1952d7b01 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -198,16 +198,10 @@ impl<'config, E: From> CheckEvmTransaction<'config, pub fn validate_common(&self) -> Result<&Self, E> { if self.config.is_transactional { - // Try to subtract the proof_size_base_cost from the Weight proof_size limit or fail. - // Validate the weight limit can afford recording the proof size cost. - // if let (Some(weight_limit), Some(proof_size_base_cost)) = - // (self.weight_limit, self.proof_size_base_cost) - // { - // let _ = weight_limit - // .proof_size() - // .checked_sub(proof_size_base_cost) - // .ok_or(TransactionValidationError::GasLimitTooLow)?; - // } + // The weight_info is None if and only if the weight_limit's proof size is less than the proof_size_base_cost + if self.weight_info.is_none() { + return Err(TransactionValidationError::GasLimitTooLow.into()); + } // We must ensure a transaction can pay the cost of its data bytes. // If it can't it should not be included in a block. diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 06d416ef78..4fc041f970 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -832,15 +832,9 @@ impl_runtime_apis! { } else { gas_limit.low_u64() }; - let (weight_limit, proof_size_base_cost) = - match ::GasWeightMapping::gas_to_weight(gas_limit, true) { - weight_limit if weight_limit.proof_size() > 0 => { - (Some(weight_limit), Some(estimated_transaction_len as u64)) - } - _ => (None, None), - }; - let weight_info = WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).unwrap(); + let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); + let weight_info = WeightInfo::new(weight_limit, estimated_transaction_len as u64); ::Runner::call( from, to, @@ -900,27 +894,14 @@ impl_runtime_apis! { if access_list.is_some() { estimated_transaction_len += access_list.encoded_size(); } - - let gas_limit = if gas_limit > U256::from(u64::MAX) { u64::MAX } else { gas_limit.low_u64() }; - let without_base_extrinsic_weight = true; - - let (weight_limit, proof_size_base_cost) = - match ::GasWeightMapping::gas_to_weight( - gas_limit, - without_base_extrinsic_weight - ) { - weight_limit if weight_limit.proof_size() > 0 => { - (Some(weight_limit), Some(estimated_transaction_len as u64)) - } - _ => (None, None), - }; - - let weight_info = WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).unwrap(); + + let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); + let weight_info = WeightInfo::new(weight_limit, estimated_transaction_len as u64); ::Runner::create( from, data, From 60f4f3386dc0b57804b192a643ab40344684c989 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 17:47:15 +0800 Subject: [PATCH 3/8] Try fix tests --- frame/ethereum/src/tests/eip1559.rs | 2 +- frame/evm/src/benchmarking.rs | 2 - frame/evm/src/runner/stack.rs | 14 +---- frame/evm/src/tests.rs | 79 ++++++++++++++--------------- 4 files changed, 41 insertions(+), 56 deletions(-) diff --git a/frame/ethereum/src/tests/eip1559.rs b/frame/ethereum/src/tests/eip1559.rs index 2dd7f7c823..dc6fdb83d7 100644 --- a/frame/ethereum/src/tests/eip1559.rs +++ b/frame/ethereum/src/tests/eip1559.rs @@ -581,7 +581,7 @@ fn proof_size_weight_limit_validation_works() { // Execute assert!( - Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err() + Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_ok() ); }); } diff --git a/frame/evm/src/benchmarking.rs b/frame/evm/src/benchmarking.rs index 3cd4f4d672..eb9aa18bea 100644 --- a/frame/evm/src/benchmarking.rs +++ b/frame/evm/src/benchmarking.rs @@ -90,7 +90,6 @@ benchmarks! { is_transactional, validate, None, - None, T::config(), ); assert!(create_runner_results.is_ok(), "create() failed"); @@ -127,7 +126,6 @@ benchmarks! { is_transactional, validate, None, - None, T::config(), ); assert!(call_runner_results.is_ok(), "call() failed"); diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 39612f343a..b6f6528e48 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -148,16 +148,6 @@ where ) -> (ExitReason, R), R: Default, { - // Used to record the external costs in the evm through the StackState implementation - // let maybe_weight_info = - // WeightInfo::new_from_weight_limit(weight_limit, proof_size_base_cost).map_err( - // |_| RunnerError { - // error: Error::::GasLimitTooLow, - // weight, - // }, - // )?; - let maybe_weight_info = weight_info; - // The precompile check is only used for transactional invocations. However, here we always // execute the check, because the check has side effects. match precompiles.is_precompile(source, gas_limit) { @@ -172,7 +162,7 @@ where standard: gas_limit.into(), effective: gas_limit.into(), }, - weight_info: maybe_weight_info, + weight_info, logs: Default::default(), }) } @@ -239,7 +229,7 @@ where }; let metadata = StackSubstateMetadata::new(gas_limit, config); - let state = SubstrateStackState::new(&vicinity, metadata, maybe_weight_info); + let state = SubstrateStackState::new(&vicinity, metadata, weight_info); let mut executor = StackExecutor::new_with_precompiles(state, config, precompiles); let (reason, retv) = f(&mut executor); diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index a8c2405264..72d7af5e3a 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -90,7 +90,7 @@ mod proof_size_test { fn create_proof_size_test_callee_contract( gas_limit: u64, - weight_limit: Option, + weight_info: Option, ) -> Result>> { ::Runner::create( H160::default(), @@ -103,15 +103,14 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - weight_limit, - Some(0), + weight_info, &::config().clone(), ) } fn create_proof_size_test_contract( gas_limit: u64, - weight_limit: Option, + weight_info: Option, ) -> Result>> { ::Runner::create( H160::default(), @@ -124,8 +123,7 @@ mod proof_size_test { Vec::new(), true, // non-transactional true, // must be validated - weight_limit, - Some(0), + weight_info, &::config().clone(), ) } @@ -171,8 +169,8 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - - let result = create_proof_size_test_callee_contract(gas_limit, Some(weight_limit)) + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let result = create_proof_size_test_callee_contract(gas_limit, weight_info) .expect("create succeeds"); // Creating a new contract does not involve reading the code from storage. @@ -198,13 +196,15 @@ mod proof_size_test { // Create callee contract A let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let result = - create_proof_size_test_callee_contract(gas_limit, None).expect("create succeeds"); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let result = create_proof_size_test_callee_contract(gas_limit, weight_info) + .expect("create succeeds"); let subcall_contract_address = result.value; // Create proof size test contract B - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -226,8 +226,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -258,9 +257,11 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); // Create proof size test contract - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -281,8 +282,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -315,9 +315,11 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); // Create proof size test contract - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -335,8 +337,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -364,9 +365,11 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); // Create proof size test contract - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -384,8 +387,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -414,12 +416,14 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let mut weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); // Artifically set a lower proof size limit so we OOG this instead gas. *weight_limit.proof_size_mut() = weight_limit.proof_size() / 2; // Create proof size test contract - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -437,8 +441,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -471,8 +474,9 @@ mod proof_size_test { // Create callee contract A let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let result = - create_proof_size_test_callee_contract(gas_limit, None).expect("create succeeds"); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let result = create_proof_size_test_callee_contract(gas_limit, weight_info) + .expect("create succeeds"); let subcall_contract_address = result.value; @@ -484,7 +488,8 @@ mod proof_size_test { >::remove(subcall_contract_address); // Create proof size test contract B - let result = create_proof_size_test_contract(gas_limit, None).expect("create succeeds"); + let result = + create_proof_size_test_contract(gas_limit, weight_info).expect("create succeeds"); let call_contract_address = result.value; @@ -505,8 +510,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &::config().clone(), ) .expect("call succeeds"); @@ -550,6 +554,7 @@ mod proof_size_test { let gas_limit: u64 = 21_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); let result = ::Runner::call( H160::default(), @@ -563,8 +568,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &config, ) .expect("call succeeds"); @@ -591,6 +595,7 @@ mod proof_size_test { let gas_limit: u64 = 700_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); let result = ::Runner::call( H160::default(), @@ -604,8 +609,7 @@ mod proof_size_test { Vec::new(), true, // transactional true, // must be validated - Some(weight_limit), - Some(0), + weight_info, &config, ) .expect("call succeeds"); @@ -1066,7 +1070,6 @@ fn runner_non_transactional_calls_with_non_balance_accounts_is_ok_without_gas_pr false, // non-transactional true, // must be validated None, - None, &::config().clone(), ) .expect("Non transactional call succeeds"); @@ -1102,7 +1105,6 @@ fn runner_non_transactional_calls_with_non_balance_accounts_is_err_with_gas_pric false, // non-transactional true, // must be validated None, - None, &::config().clone(), ); assert!(res.is_err()); @@ -1126,7 +1128,6 @@ fn runner_transactional_call_with_zero_gas_price_fails() { true, // transactional true, // must be validated None, - None, &::config().clone(), ); assert!(res.is_err()); @@ -1150,7 +1151,6 @@ fn runner_max_fee_per_gas_gte_max_priority_fee_per_gas() { true, // transactional true, // must be validated None, - None, &::config().clone(), ); assert!(res.is_err()); @@ -1167,7 +1167,6 @@ fn runner_max_fee_per_gas_gte_max_priority_fee_per_gas() { false, // non-transactional true, // must be validated None, - None, &::config().clone(), ); assert!(res.is_err()); @@ -1192,7 +1191,6 @@ fn eip3607_transaction_from_contract() { true, // transactional false, // not sure be validated None, - None, &::config().clone(), ) { Err(RunnerError { @@ -1217,7 +1215,6 @@ fn eip3607_transaction_from_contract() { false, // non-transactional true, // must be validated None, - None, &::config().clone(), ) .is_ok()); From e92dc2e0dde5929b65ca2bde35a2d6ba5836d4b9 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 18:13:10 +0800 Subject: [PATCH 4/8] Fix evm pallet tests --- frame/evm/src/lib.rs | 12 +++++++++--- frame/evm/src/tests.rs | 5 +++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index 6041ffd9d7..b965d65074 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -233,6 +233,8 @@ pub mod pallet { let is_transactional = true; let validate = true; + let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); let info = match T::Runner::call( source, target, @@ -245,7 +247,7 @@ pub mod pallet { access_list, is_transactional, validate, - None, + weight_info, T::config(), ) { Ok(info) => info, @@ -308,6 +310,8 @@ pub mod pallet { let is_transactional = true; let validate = true; + let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); let info = match T::Runner::create( source, init, @@ -319,7 +323,7 @@ pub mod pallet { access_list, is_transactional, validate, - None, + weight_info, T::config(), ) { Ok(info) => info, @@ -394,6 +398,8 @@ pub mod pallet { let is_transactional = true; let validate = true; + let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); let info = match T::Runner::create2( source, init, @@ -406,7 +412,7 @@ pub mod pallet { access_list, is_transactional, validate, - None, + weight_info, T::config(), ) { Ok(info) => info, diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 72d7af5e3a..7338943175 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -416,10 +416,11 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let mut weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); - // Artifically set a lower proof size limit so we OOG this instead gas. *weight_limit.proof_size_mut() = weight_limit.proof_size() / 2; + + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + // Create proof size test contract let result = From 729d4ad350109596ee6fed76f952aa91d3dbdffe Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 19:54:15 +0800 Subject: [PATCH 5/8] Fix ethereum tests --- frame/ethereum/src/tests/eip1559.rs | 6 ++---- frame/ethereum/src/tests/eip2930.rs | 6 ++---- frame/ethereum/src/tests/legacy.rs | 6 ++---- frame/evm/src/tests.rs | 3 +-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/frame/ethereum/src/tests/eip1559.rs b/frame/ethereum/src/tests/eip1559.rs index dc6fdb83d7..652e7f77e2 100644 --- a/frame/ethereum/src/tests/eip1559.rs +++ b/frame/ethereum/src/tests/eip1559.rs @@ -579,10 +579,8 @@ fn proof_size_weight_limit_validation_works() { tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize]; let tx = tx.sign(&alice.private_key, None); - // Execute - assert!( - Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_ok() - ); + // Validate in Pool + assert!(Ethereum::validate_transaction_in_pool(alice.address, &tx).is_err()); }); } diff --git a/frame/ethereum/src/tests/eip2930.rs b/frame/ethereum/src/tests/eip2930.rs index 4cb6a13c4d..b6542dee4a 100644 --- a/frame/ethereum/src/tests/eip2930.rs +++ b/frame/ethereum/src/tests/eip2930.rs @@ -505,10 +505,8 @@ fn proof_size_weight_limit_validation_works() { tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize]; let tx = tx.sign(&alice.private_key, None); - // Execute - assert!( - Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err() - ); + // Validate in Pool + assert!(Ethereum::validate_transaction_in_pool(alice.address, &tx).is_err()); }); } diff --git a/frame/ethereum/src/tests/legacy.rs b/frame/ethereum/src/tests/legacy.rs index e90d6cb092..f9b2171213 100644 --- a/frame/ethereum/src/tests/legacy.rs +++ b/frame/ethereum/src/tests/legacy.rs @@ -505,10 +505,8 @@ fn proof_size_weight_limit_validation_works() { tx.input = vec![0u8; (weight_limit.proof_size() + 1) as usize]; let tx = tx.sign(&alice.private_key); - // Execute - assert!( - Ethereum::transact(RawOrigin::EthereumTransaction(alice.address).into(), tx,).is_err() - ); + // Validate in Pool + assert!(Ethereum::validate_transaction_in_pool(alice.address, &tx).is_err()); }); } diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 7338943175..56ce54abc3 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -418,9 +418,8 @@ mod proof_size_test { let mut weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); // Artifically set a lower proof size limit so we OOG this instead gas. *weight_limit.proof_size_mut() = weight_limit.proof_size() / 2; - - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); // Create proof size test contract let result = From ce1e4cc75fed9f85d3c2b77b2b34822704303f65 Mon Sep 17 00:00:00 2001 From: bear Date: Wed, 29 May 2024 20:45:27 +0800 Subject: [PATCH 6/8] Rename --- frame/ethereum/src/lib.rs | 7 ++++--- frame/evm/src/lib.rs | 8 ++++---- frame/evm/src/runner/mod.rs | 10 +++++----- frame/evm/src/runner/stack.rs | 24 ++++++++++++------------ frame/evm/src/tests.rs | 22 +++++++++++----------- primitives/evm/src/lib.rs | 10 ++++------ primitives/evm/src/validation.rs | 5 +++-- template/runtime/src/lib.rs | 8 ++++---- 8 files changed, 47 insertions(+), 47 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 4c9b60a094..337b8231a5 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -64,7 +64,8 @@ use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID}; pub use fp_ethereum::TransactionData; use fp_ethereum::ValidatedTransaction as ValidatedTransactionT; use fp_evm::{ - CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, TransactionValidationError, + CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, EvmWeightInfo, + TransactionValidationError, }; pub use fp_rpc::TransactionStatus; use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA}; @@ -361,14 +362,14 @@ pub mod pallet { } impl Pallet { - pub fn transaction_weight(transaction_data: &TransactionData) -> Option { + pub fn transaction_weight(transaction_data: &TransactionData) -> Option { let weight_limit = ::GasWeightMapping::gas_to_weight( transaction_data.gas_limit.unique_saturated_into(), true, ); let proof_size_base_cost = transaction_data.proof_size_base_cost(); - fp_evm::WeightInfo::new(weight_limit, proof_size_base_cost) + EvmWeightInfo::new(weight_limit, proof_size_base_cost) } fn recover_signer(transaction: &Transaction) -> Option { diff --git a/frame/evm/src/lib.rs b/frame/evm/src/lib.rs index b965d65074..68bb0d51c5 100644 --- a/frame/evm/src/lib.rs +++ b/frame/evm/src/lib.rs @@ -100,12 +100,12 @@ use sp_runtime::{ }; // Frontier use fp_account::AccountId20; -use fp_evm::GenesisAccount; pub use fp_evm::{ Account, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, FeeCalculator, IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet, TransactionValidationError, Vicinity, }; +use fp_evm::{EvmWeightInfo, GenesisAccount}; pub use self::{ pallet::*, @@ -234,7 +234,7 @@ pub mod pallet { let is_transactional = true; let validate = true; let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let info = match T::Runner::call( source, target, @@ -311,7 +311,7 @@ pub mod pallet { let is_transactional = true; let validate = true; let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let info = match T::Runner::create( source, init, @@ -399,7 +399,7 @@ pub mod pallet { let is_transactional = true; let validate = true; let weight_limit = T::GasWeightMapping::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let info = match T::Runner::create2( source, init, diff --git a/frame/evm/src/runner/mod.rs b/frame/evm/src/runner/mod.rs index 6c679a084c..1842bec9c7 100644 --- a/frame/evm/src/runner/mod.rs +++ b/frame/evm/src/runner/mod.rs @@ -19,7 +19,7 @@ pub mod stack; use crate::{Config, Weight}; use alloc::vec::Vec; -use fp_evm::{CallInfo, CreateInfo, WeightInfo}; +use fp_evm::{CallInfo, CreateInfo, EvmWeightInfo}; use sp_core::{H160, H256, U256}; #[derive(Debug)] @@ -42,7 +42,7 @@ pub trait Runner { nonce: Option, access_list: Vec<(H160, Vec)>, is_transactional: bool, - weight_info: Option, + weight_info: Option, evm_config: &evm::Config, ) -> Result<(), RunnerError>; @@ -58,7 +58,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; @@ -73,7 +73,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; @@ -89,7 +89,7 @@ pub trait Runner { access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result>; } diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index b6f6528e48..186b79c978 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -41,8 +41,8 @@ use sp_core::{H160, H256, U256}; use sp_runtime::traits::UniqueSaturatedInto; // Frontier use fp_evm::{ - AccessedStorage, CallInfo, CreateInfo, ExecutionInfoV2, IsPrecompileResult, Log, PrecompileSet, - Vicinity, WeightInfo, ACCOUNT_BASIC_PROOF_SIZE, ACCOUNT_CODES_METADATA_PROOF_SIZE, + AccessedStorage, CallInfo, CreateInfo, EvmWeightInfo, ExecutionInfoV2, IsPrecompileResult, Log, + PrecompileSet, Vicinity, ACCOUNT_BASIC_PROOF_SIZE, ACCOUNT_CODES_METADATA_PROOF_SIZE, ACCOUNT_STORAGE_PROOF_SIZE, IS_EMPTY_CHECK_PROOF_SIZE, WRITE_PROOF_SIZE, }; @@ -75,7 +75,7 @@ where config: &'config evm::Config, precompiles: &'precompiles T::PrecompilesType, is_transactional: bool, - weight_info: Option, + weight_info: Option, f: F, ) -> Result, RunnerError>> where @@ -135,7 +135,7 @@ where f: F, base_fee: U256, weight: Weight, - weight_info: Option, + weight_info: Option, ) -> Result, RunnerError>> where F: FnOnce( @@ -355,7 +355,7 @@ where nonce: Option, access_list: Vec<(H160, Vec)>, is_transactional: bool, - weight_info: Option, + weight_info: Option, evm_config: &evm::Config, ) -> Result<(), RunnerError> { let (base_fee, mut weight) = T::FeeCalculator::min_gas_price(); @@ -403,7 +403,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -448,7 +448,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -500,7 +500,7 @@ where access_list: Vec<(H160, Vec)>, is_transactional: bool, validate: bool, - weight_info: Option, + weight_info: Option, config: &evm::Config, ) -> Result> { if validate { @@ -655,7 +655,7 @@ pub struct SubstrateStackState<'vicinity, 'config, T> { substate: SubstrateStackSubstate<'config>, original_storage: BTreeMap<(H160, H256), H256>, recorded: Recorded, - weight_info: Option, + weight_info: Option, _marker: PhantomData, } @@ -664,7 +664,7 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> { pub fn new( vicinity: &'vicinity Vicinity, metadata: StackSubstateMetadata<'config>, - weight_info: Option, + weight_info: Option, ) -> Self { Self { vicinity, @@ -681,7 +681,7 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> { } } - pub fn weight_info(&self) -> Option { + pub fn weight_info(&self) -> Option { self.weight_info } @@ -689,7 +689,7 @@ impl<'vicinity, 'config, T: Config> SubstrateStackState<'vicinity, 'config, T> { &self.recorded } - pub fn info_mut(&mut self) -> (&mut Option, &mut Recorded) { + pub fn info_mut(&mut self) -> (&mut Option, &mut Recorded) { (&mut self.weight_info, &mut self.recorded) } } diff --git a/frame/evm/src/tests.rs b/frame/evm/src/tests.rs index 56ce54abc3..90fdf5040b 100644 --- a/frame/evm/src/tests.rs +++ b/frame/evm/src/tests.rs @@ -90,7 +90,7 @@ mod proof_size_test { fn create_proof_size_test_callee_contract( gas_limit: u64, - weight_info: Option, + weight_info: Option, ) -> Result>> { ::Runner::create( H160::default(), @@ -110,7 +110,7 @@ mod proof_size_test { fn create_proof_size_test_contract( gas_limit: u64, - weight_info: Option, + weight_info: Option, ) -> Result>> { ::Runner::create( H160::default(), @@ -169,7 +169,7 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let result = create_proof_size_test_callee_contract(gas_limit, weight_info) .expect("create succeeds"); @@ -196,7 +196,7 @@ mod proof_size_test { // Create callee contract A let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let result = create_proof_size_test_callee_contract(gas_limit, weight_info) .expect("create succeeds"); @@ -257,7 +257,7 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); // Create proof size test contract let result = @@ -315,7 +315,7 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); // Create proof size test contract let result = @@ -365,7 +365,7 @@ mod proof_size_test { new_test_ext().execute_with(|| { let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); // Create proof size test contract let result = @@ -419,7 +419,7 @@ mod proof_size_test { // Artifically set a lower proof size limit so we OOG this instead gas. *weight_limit.proof_size_mut() = weight_limit.proof_size() / 2; - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); // Create proof size test contract let result = @@ -474,7 +474,7 @@ mod proof_size_test { // Create callee contract A let gas_limit: u64 = 1_000_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let result = create_proof_size_test_callee_contract(gas_limit, weight_info) .expect("create succeeds"); @@ -554,7 +554,7 @@ mod proof_size_test { let gas_limit: u64 = 21_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let result = ::Runner::call( H160::default(), @@ -595,7 +595,7 @@ mod proof_size_test { let gas_limit: u64 = 700_000; let weight_limit = FixedGasWeightMapping::::gas_to_weight(gas_limit, true); - let weight_info = fp_evm::WeightInfo::new(weight_limit, 0); + let weight_info = EvmWeightInfo::new(weight_limit, 0); let result = ::Runner::call( H160::default(), diff --git a/primitives/evm/src/lib.rs b/primitives/evm/src/lib.rs index 29667aba18..7143e28b31 100644 --- a/primitives/evm/src/lib.rs +++ b/primitives/evm/src/lib.rs @@ -77,19 +77,17 @@ pub enum AccessedStorage { #[derive(Clone, Copy, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct WeightInfo { - pub proof_size_base_cost: Option, +pub struct EvmWeightInfo { pub ref_time_limit: Option, pub proof_size_limit: Option, pub ref_time_usage: Option, pub proof_size_usage: Option, } -impl WeightInfo { +impl EvmWeightInfo { pub fn new(weight_limit: Weight, proof_size_base_cost: u64) -> Option { if weight_limit.proof_size() >= proof_size_base_cost { - return Some(WeightInfo { - proof_size_base_cost: Some(proof_size_base_cost), + return Some(EvmWeightInfo { ref_time_limit: Some(weight_limit.ref_time()), proof_size_limit: Some(weight_limit.proof_size()), ref_time_usage: Some(0u64), @@ -182,7 +180,7 @@ pub struct ExecutionInfoV2 { pub exit_reason: ExitReason, pub value: T, pub used_gas: UsedGas, - pub weight_info: Option, + pub weight_info: Option, pub logs: Vec, } diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index d1952d7b01..8015d7326f 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -17,6 +17,7 @@ #![allow(clippy::comparison_chain)] +use crate::EvmWeightInfo; use alloc::vec::Vec; pub use evm::backend::Basic as Account; use frame_support::sp_runtime::traits::UniqueSaturatedInto; @@ -49,7 +50,7 @@ pub struct CheckEvmTransactionConfig<'config> { pub struct CheckEvmTransaction<'config, E: From> { pub config: CheckEvmTransactionConfig<'config>, pub transaction: CheckEvmTransactionInput, - pub weight_info: Option, + pub weight_info: Option, _marker: core::marker::PhantomData, } @@ -86,7 +87,7 @@ impl<'config, E: From> CheckEvmTransaction<'config, pub fn new( config: CheckEvmTransactionConfig<'config>, transaction: CheckEvmTransactionInput, - weight_info: Option, + weight_info: Option, ) -> Self { CheckEvmTransaction { config, diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 4fc041f970..95bf72bda2 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -796,7 +796,7 @@ impl_runtime_apis! { access_list: Option)>>, ) -> Result { use pallet_evm::GasWeightMapping as _; - use fp_evm::WeightInfo; + use fp_evm::EvmWeightInfo; let config = if estimate { let mut config = ::config().clone(); @@ -834,7 +834,7 @@ impl_runtime_apis! { }; let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); - let weight_info = WeightInfo::new(weight_limit, estimated_transaction_len as u64); + let weight_info = EvmWeightInfo::new(weight_limit, estimated_transaction_len as u64); ::Runner::call( from, to, @@ -864,7 +864,7 @@ impl_runtime_apis! { access_list: Option)>>, ) -> Result { use pallet_evm::GasWeightMapping as _; - use fp_evm::WeightInfo; + use fp_evm::EvmWeightInfo; let config = if estimate { let mut config = ::config().clone(); @@ -901,7 +901,7 @@ impl_runtime_apis! { }; let weight_limit = ::GasWeightMapping::gas_to_weight(gas_limit, true); - let weight_info = WeightInfo::new(weight_limit, estimated_transaction_len as u64); + let weight_info = EvmWeightInfo::new(weight_limit, estimated_transaction_len as u64); ::Runner::create( from, data, From eee7e947ed87f0ddefe0fe0b81cd3ec422822e14 Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 31 May 2024 16:13:33 +0800 Subject: [PATCH 7/8] Fix test --- primitives/evm/src/validation.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index 8015d7326f..7b3955916e 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -20,7 +20,7 @@ use crate::EvmWeightInfo; use alloc::vec::Vec; pub use evm::backend::Basic as Account; -use frame_support::sp_runtime::traits::UniqueSaturatedInto; +use frame_support::{sp_runtime::traits::UniqueSaturatedInto, weights::Weight}; use sp_core::{H160, H256, U256}; #[derive(Debug)] @@ -287,8 +287,7 @@ mod tests { pub max_fee_per_gas: Option, pub max_priority_fee_per_gas: Option, pub value: U256, - pub weight_limit: Option, - pub proof_size_base_cost: Option, + pub weight_info: Option, } impl Default for TestCase { @@ -305,8 +304,7 @@ mod tests { max_fee_per_gas: Some(U256::from(1_000_000_000u128)), max_priority_fee_per_gas: Some(U256::from(1_000_000_000u128)), value: U256::from(1u8), - weight_limit: None, - proof_size_base_cost: None, + weight_info: EvmWeightInfo::new(Weight::from_parts(1, 1), 1), } } } @@ -324,8 +322,7 @@ mod tests { max_fee_per_gas, max_priority_fee_per_gas, value, - weight_limit, - proof_size_base_cost, + weight_info, } = input; CheckEvmTransaction::::new( CheckEvmTransactionConfig { @@ -347,8 +344,7 @@ mod tests { value, access_list: vec![], }, - weight_limit, - proof_size_base_cost, + weight_info, ) } @@ -375,9 +371,9 @@ mod tests { fn transaction_gas_limit_low_proof_size<'config>( is_transactional: bool, ) -> CheckEvmTransaction<'config, TestError> { + let weight_info = EvmWeightInfo::new(Weight::from_parts(1, 1), 2); test_env(TestCase { - weight_limit: Some(Weight::from_parts(1, 1)), - proof_size_base_cost: Some(2), + weight_info, is_transactional, ..Default::default() }) From 08668e1a83162174bcef86f879ae2833d1494968 Mon Sep 17 00:00:00 2001 From: bear Date: Fri, 31 May 2024 16:57:52 +0800 Subject: [PATCH 8/8] Fix conflict --- frame/evm/src/runner/stack.rs | 3 --- primitives/evm/src/validation.rs | 10 +++++++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/frame/evm/src/runner/stack.rs b/frame/evm/src/runner/stack.rs index 186b79c978..de5c17e9fc 100644 --- a/frame/evm/src/runner/stack.rs +++ b/frame/evm/src/runner/stack.rs @@ -1183,7 +1183,6 @@ mod tests { &MockPrecompileSet, false, None, - None, |_| { let res = Runner::::execute( H160::default(), @@ -1195,7 +1194,6 @@ mod tests { &MockPrecompileSet, false, None, - None, |_| (ExitReason::Succeed(ExitSucceed::Stopped), ()), ); assert_matches!( @@ -1227,7 +1225,6 @@ mod tests { &MockPrecompileSet, false, None, - None, |_| (ExitReason::Succeed(ExitSucceed::Stopped), ()), ); assert!(res.is_ok()); diff --git a/primitives/evm/src/validation.rs b/primitives/evm/src/validation.rs index 7b3955916e..d5455144b0 100644 --- a/primitives/evm/src/validation.rs +++ b/primitives/evm/src/validation.rs @@ -17,11 +17,14 @@ #![allow(clippy::comparison_chain)] -use crate::EvmWeightInfo; -use alloc::vec::Vec; pub use evm::backend::Basic as Account; -use frame_support::{sp_runtime::traits::UniqueSaturatedInto, weights::Weight}; + +use alloc::vec::Vec; +// Substrate +use frame_support::sp_runtime::traits::UniqueSaturatedInto; use sp_core::{H160, H256, U256}; +// Frontier +use crate::EvmWeightInfo; #[derive(Debug)] pub struct CheckEvmTransactionInput { @@ -239,6 +242,7 @@ impl<'config, E: From> CheckEvmTransaction<'config, #[cfg(test)] mod tests { use super::*; + use frame_support::weights::Weight; #[derive(Debug, PartialEq)] pub enum TestError {