Skip to content

Commit

Permalink
Add CallOrCreateInfo to ValidatedTransaction apply result. (#1099)
Browse files Browse the repository at this point in the history
* Add 'CallOrCreateInfo' to 'ValidatedTransaction' apply result.

* fmt
  • Loading branch information
shaunxw authored Jul 31, 2023
1 parent 9f1a73b commit 611a2b2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
59 changes: 32 additions & 27 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -53,7 +55,7 @@ use sp_runtime::{
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransactionBuilder,
},
DispatchErrorWithPostInfo, RuntimeDebug, SaturatedConversion,
RuntimeDebug, SaturatedConversion,
};
use sp_std::{marker::PhantomData, prelude::*};

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -557,14 +559,14 @@ impl<T: Config> Pallet<T> {
fn apply_validated_transaction(
source: H160,
transaction: Transaction,
) -> DispatchResultWithPostInfo {
) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo> {
let (to, _, info) = Self::execute(source, &transaction, None)?;

let pending = Pending::<T>::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 {
Expand Down Expand Up @@ -679,24 +681,27 @@ impl<T: Config> Pallet<T> {
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
Expand All @@ -709,10 +714,7 @@ impl<T: Config> Pallet<T> {
from: H160,
transaction: &Transaction,
config: Option<evm::Config>,
) -> Result<
(Option<H160>, Option<H160>, CallOrCreateInfo),
DispatchErrorWithPostInfo<PostDispatchInfo>,
> {
) -> Result<(Option<H160>, Option<H160>, CallOrCreateInfo), DispatchErrorWithPostInfo> {
let transaction_data: TransactionData = transaction.into();
let (weight_limit, proof_size_base_cost) = Self::transaction_weight(&transaction_data);
let is_transactional = true;
Expand Down Expand Up @@ -944,7 +946,10 @@ impl<T: Config> Pallet<T> {

pub struct ValidatedTransaction<T>(PhantomData<T>);
impl<T: Config> ValidatedTransactionT for ValidatedTransaction<T> {
fn apply(source: H160, transaction: Transaction) -> DispatchResultWithPostInfo {
fn apply(
source: H160,
transaction: Transaction,
) -> Result<(PostDispatchInfo, CallOrCreateInfo), DispatchErrorWithPostInfo> {
Pallet::<T>::apply_validated_transaction(source, transaction)
}
}
Expand Down
7 changes: 4 additions & 3 deletions primitives/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down

0 comments on commit 611a2b2

Please sign in to comment.