From 611a2b233ba912f83c83c973687db6b28c817de8 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Tue, 1 Aug 2023 01:48:31 +1200 Subject: [PATCH] Add `CallOrCreateInfo` to `ValidatedTransaction` apply result. (#1099) * Add 'CallOrCreateInfo' to 'ValidatedTransaction' apply result. * fmt --- frame/ethereum/src/lib.rs | 59 ++++++++++++++++++---------------- primitives/ethereum/src/lib.rs | 7 ++-- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 5e2e6f977b..cbbdaab434 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -40,7 +40,9 @@ use fp_evm::{ use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA}; use frame_support::{ codec::{Decode, Encode, MaxEncodedLen}, - dispatch::{DispatchInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo}, + dispatch::{ + DispatchErrorWithPostInfo, DispatchInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo, + }, scale_info::TypeInfo, traits::{EnsureOrigin, Get, PalletInfoAccess, Time}, weights::Weight, @@ -53,7 +55,7 @@ use sp_runtime::{ transaction_validity::{ InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransactionBuilder, }, - DispatchErrorWithPostInfo, RuntimeDebug, SaturatedConversion, + RuntimeDebug, SaturatedConversion, }; use sp_std::{marker::PhantomData, prelude::*}; @@ -240,7 +242,7 @@ pub mod pallet { Self::validate_transaction_in_block(source, &transaction).expect( "pre-block transaction verification failed; the block cannot be built", ); - let r = Self::apply_validated_transaction(source, transaction) + let (r, _) = Self::apply_validated_transaction(source, transaction) .expect("pre-block apply transaction failed; the block cannot be built"); weight = weight.saturating_add(r.actual_weight.unwrap_or_default()); @@ -289,7 +291,7 @@ pub mod pallet { "pre log already exists; block is invalid", ); - Self::apply_validated_transaction(source, transaction) + Self::apply_validated_transaction(source, transaction).map(|(post_info, _)| post_info) } } @@ -557,14 +559,14 @@ impl Pallet { fn apply_validated_transaction( source: H160, transaction: Transaction, - ) -> DispatchResultWithPostInfo { + ) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo> { let (to, _, info) = Self::execute(source, &transaction, None)?; let pending = Pending::::get(); let transaction_hash = transaction.hash(); let transaction_index = pending.len() as u32; - let (reason, status, weight_info, used_gas, dest, extra_data) = match info { + let (reason, status, weight_info, used_gas, dest, extra_data) = match info.clone() { CallOrCreateInfo::Call(info) => ( info.exit_reason.clone(), TransactionStatus { @@ -679,24 +681,27 @@ impl Pallet { extra_data, }); - Ok(PostDispatchInfo { - actual_weight: { - let mut gas_to_weight = T::GasWeightMapping::gas_to_weight( - sp_std::cmp::max( - used_gas.standard.unique_saturated_into(), - used_gas.effective.unique_saturated_into(), - ), - true, - ); - if let Some(weight_info) = weight_info { - if let Some(proof_size_usage) = weight_info.proof_size_usage { - *gas_to_weight.proof_size_mut() = proof_size_usage; + Ok(( + PostDispatchInfo { + actual_weight: { + let mut gas_to_weight = T::GasWeightMapping::gas_to_weight( + sp_std::cmp::max( + used_gas.standard.unique_saturated_into(), + used_gas.effective.unique_saturated_into(), + ), + true, + ); + if let Some(weight_info) = weight_info { + if let Some(proof_size_usage) = weight_info.proof_size_usage { + *gas_to_weight.proof_size_mut() = proof_size_usage; + } } - } - Some(gas_to_weight) + Some(gas_to_weight) + }, + pays_fee: Pays::No, }, - pays_fee: Pays::No, - }) + info, + )) } /// Get current block hash @@ -709,10 +714,7 @@ impl Pallet { from: H160, transaction: &Transaction, config: Option, - ) -> Result< - (Option, Option, CallOrCreateInfo), - DispatchErrorWithPostInfo, - > { + ) -> Result<(Option, Option, CallOrCreateInfo), DispatchErrorWithPostInfo> { let transaction_data: TransactionData = transaction.into(); let (weight_limit, proof_size_base_cost) = Self::transaction_weight(&transaction_data); let is_transactional = true; @@ -944,7 +946,10 @@ impl Pallet { pub struct ValidatedTransaction(PhantomData); impl ValidatedTransactionT for ValidatedTransaction { - fn apply(source: H160, transaction: Transaction) -> DispatchResultWithPostInfo { + fn apply( + source: H160, + transaction: Transaction, + ) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo> { Pallet::::apply_validated_transaction(source, transaction) } } diff --git a/primitives/ethereum/src/lib.rs b/primitives/ethereum/src/lib.rs index b9e9726cba..3a15999d1c 100644 --- a/primitives/ethereum/src/lib.rs +++ b/primitives/ethereum/src/lib.rs @@ -23,9 +23,10 @@ pub use ethereum::{ TransactionAction, TransactionV2 as Transaction, }; use ethereum_types::{H160, H256, U256}; -use fp_evm::CheckEvmTransactionInput; +use fp_evm::{CallOrCreateInfo, CheckEvmTransactionInput}; +use frame_support::dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo}; use scale_codec::{Decode, Encode}; -use sp_std::vec::Vec; +use sp_std::{result::Result, vec::Vec}; #[repr(u8)] #[derive(num_enum::FromPrimitive, num_enum::IntoPrimitive)] @@ -44,7 +45,7 @@ pub trait ValidatedTransaction { fn apply( source: H160, transaction: Transaction, - ) -> frame_support::dispatch::DispatchResultWithPostInfo; + ) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo>; } #[derive(Clone, Debug, Eq, PartialEq, Encode, Decode)]