Skip to content

Commit

Permalink
Add 'CallOrCreateInfo' to 'ValidatedTransaction' apply result; Make '…
Browse files Browse the repository at this point in the history
…Pending' public.
  • Loading branch information
shaunxw authored and Dinonard committed Jul 10, 2023
1 parent d74a159 commit 46cd859
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
57 changes: 31 additions & 26 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -55,7 +57,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 @@ -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());
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -317,7 +319,7 @@ pub mod pallet {

/// Current building block's transactions and receipts.
#[pallet::storage]
pub(super) type Pending<T: Config> =
pub type Pending<T: Config> =
StorageValue<_, Vec<(Transaction, TransactionStatus, Receipt)>, ValueQuery>;

/// The current Ethereum block.
Expand Down Expand Up @@ -558,14 +560,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 @@ -680,21 +682,24 @@ impl<T: Config> Pallet<T> {
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
Expand All @@ -707,10 +712,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 (
input,
value,
Expand Down Expand Up @@ -963,7 +965,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::prelude::*;

#[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 46cd859

Please sign in to comment.