From a5481542518ec420352d263adcb2f78835ac9bc2 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Mon, 10 Jul 2023 17:52:49 +1200 Subject: [PATCH] Add 'CallOrCreateInfo' to 'ValidatedTransaction' apply result; Make 'Pending' public. --- frame/ethereum/src/lib.rs | 57 ++++++++++++++++++---------------- primitives/ethereum/src/lib.rs | 7 +++-- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index e667341c92..859be26891 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -42,7 +42,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, @@ -55,7 +57,7 @@ use sp_runtime::{ transaction_validity::{ InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransactionBuilder, }, - DispatchErrorWithPostInfo, RuntimeDebug, SaturatedConversion, + RuntimeDebug, SaturatedConversion, }; use sp_std::{marker::PhantomData, prelude::*}; @@ -241,7 +243,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()); @@ -290,7 +292,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) } } @@ -317,7 +319,7 @@ pub mod pallet { /// Current building block's transactions and receipts. #[pallet::storage] - pub(super) type Pending = + pub type Pending = StorageValue<_, Vec<(Transaction, TransactionStatus, Receipt)>, ValueQuery>; /// The current Ethereum block. @@ -558,14 +560,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 { @@ -680,21 +682,24 @@ impl Pallet { extra_data, }); - Ok(PostDispatchInfo { - actual_weight: { - let mut gas_to_weight = T::GasWeightMapping::gas_to_weight( - used_gas.standard.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( + used_gas.standard.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) - }, - pays_fee: Pays::No, - }) + Some(gas_to_weight) + }, + pays_fee: Pays::No, + }), + info, + )) } /// Get current block hash @@ -707,10 +712,7 @@ impl Pallet { from: H160, transaction: &Transaction, config: Option, - ) -> Result< - (Option, Option, CallOrCreateInfo), - DispatchErrorWithPostInfo, - > { + ) -> Result<(Option, Option, CallOrCreateInfo), DispatchErrorWithPostInfo> { let ( input, value, @@ -963,7 +965,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 03d7092ea3..69bcec62bc 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::prelude::*; #[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)]