Skip to content

Commit

Permalink
Add TRN changes
Browse files Browse the repository at this point in the history
  • Loading branch information
surangap committed Mar 13, 2024
1 parent 301d7c0 commit 50660c0
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 119 deletions.
72 changes: 51 additions & 21 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,21 @@ mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

pub use ethereum::{
AccessListItem, BlockV2 as Block, LegacyTransactionMessage, Log, ReceiptV3 as Receipt,
TransactionAction, TransactionV2 as Transaction,
};
use ethereum_types::{Bloom, BloomInput, H160, H256, H64, U256};
use evm::ExitReason;
use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
use fp_ethereum::{
TransactionData, TransactionValidationError, ValidatedTransaction as ValidatedTransactionT,
};
use fp_evm::{
CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, InvalidEvmTransactionError,
CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, HandleTxValidation,
InvalidEvmTransactionError,
};
pub use fp_rpc::TransactionStatus;
use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA};
use frame_support::{
codec::{Decode, Encode, MaxEncodedLen},
Expand All @@ -59,12 +65,6 @@ use sp_runtime::{
};
use sp_std::{marker::PhantomData, prelude::*};

pub use ethereum::{
AccessListItem, BlockV2 as Block, LegacyTransactionMessage, Log, ReceiptV3 as Receipt,
TransactionAction, TransactionV2 as Transaction,
};
pub use fp_rpc::TransactionStatus;

#[derive(Clone, Eq, PartialEq, RuntimeDebug)]
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo)]
pub enum RawOrigin {
Expand Down Expand Up @@ -197,6 +197,24 @@ pub mod pallet {
type PostLogContent: Get<PostLogContent>;
/// The maximum length of the extra data in the Executed event.
type ExtraDataLength: Get<u32>;

type HandleTxValidation: HandleTxValidation<InvalidTransactionWrapper>;
}

impl<T: Config> From<InvalidEvmTransactionError> for Error<T> {
fn from(err: InvalidEvmTransactionError) -> Self {
match err {
InvalidEvmTransactionError::GasLimitTooLow => Error::<T>::GasLimitTooLow,
InvalidEvmTransactionError::GasLimitTooHigh => Error::<T>::GasLimitTooHigh,
InvalidEvmTransactionError::GasPriceTooLow => Error::<T>::GasPriceTooLow,
InvalidEvmTransactionError::PriorityFeeTooHigh => Error::<T>::GasPriceTooLow,
InvalidEvmTransactionError::BalanceTooLow => Error::<T>::BalanceLow,
InvalidEvmTransactionError::TxNonceTooLow => Error::<T>::InvalidNonce,
InvalidEvmTransactionError::TxNonceTooHigh => Error::<T>::InvalidNonce,
InvalidEvmTransactionError::InvalidPaymentInput => Error::<T>::GasPriceTooLow,
_ => Error::<T>::Undefined,
}
}
}

#[pallet::hooks]
Expand Down Expand Up @@ -313,6 +331,13 @@ pub mod pallet {
InvalidSignature,
/// Pre-log is present, therefore transact is not allowed.
PreLogExists,
// Invalid EVM Transaction compatibility errors
GasLimitTooLow,
GasLimitTooHigh,
GasPriceTooLow,
BalanceLow,
InvalidNonce,
Undefined,
}

/// Current building block's transactions and receipts.
Expand Down Expand Up @@ -502,7 +527,7 @@ impl<T: Config> Pallet<T> {
let (base_fee, _) = T::FeeCalculator::min_gas_price();
let (who, _) = pallet_evm::Pallet::<T>::account_basic(&origin);

let _ = CheckEvmTransaction::<InvalidTransactionWrapper>::new(
let evm_config = CheckEvmTransaction::<InvalidTransactionWrapper>::new(
CheckEvmTransactionConfig {
evm_config: T::config(),
block_gas_limit: T::BlockGasLimit::get(),
Expand All @@ -513,12 +538,15 @@ impl<T: Config> Pallet<T> {
transaction_data.clone().into(),
weight_limit,
proof_size_base_cost,
)
.validate_in_pool_for(&who)
.and_then(|v| v.with_chain_id())
.and_then(|v| v.with_base_fee())
.and_then(|v| v.with_balance_for(&who))
.map_err(|e| e.0)?;
);

<T as pallet::Config>::HandleTxValidation::validate_in_pool_for(&evm_config, &who)
.and_then(|_| <T as pallet::Config>::HandleTxValidation::with_chain_id(&evm_config))
.and_then(|_| <T as pallet::Config>::HandleTxValidation::with_base_fee(&evm_config))
.and_then(|_| {
<T as pallet::Config>::HandleTxValidation::with_balance_for(&evm_config, &who)
})
.map_err(|e| e.0)?;

let priority = match (
transaction_data.gas_price,
Expand Down Expand Up @@ -881,7 +909,7 @@ impl<T: Config> Pallet<T> {
_ => (None, None),
};

let _ = CheckEvmTransaction::<InvalidTransactionWrapper>::new(
let evm_config = CheckEvmTransaction::<InvalidTransactionWrapper>::new(
CheckEvmTransactionConfig {
evm_config: T::config(),
block_gas_limit: T::BlockGasLimit::get(),
Expand All @@ -892,13 +920,15 @@ impl<T: Config> Pallet<T> {
transaction_data.into(),
weight_limit,
proof_size_base_cost,
)
.validate_in_block_for(&who)
.and_then(|v| v.with_chain_id())
.and_then(|v| v.with_base_fee())
.and_then(|v| v.with_balance_for(&who))
.map_err(|e| TransactionValidityError::Invalid(e.0))?;
);

<T as pallet::Config>::HandleTxValidation::validate_in_block_for(&evm_config, &who)
.and_then(|_| <T as pallet::Config>::HandleTxValidation::with_chain_id(&evm_config))
.and_then(|_| <T as pallet::Config>::HandleTxValidation::with_base_fee(&evm_config))
.and_then(|_| {
<T as pallet::Config>::HandleTxValidation::with_balance_for(&evm_config, &who)
})
.map_err(|e| TransactionValidityError::Invalid(e.0))?;
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ impl pallet_evm::Config for Test {
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type Timestamp = Timestamp;
type WeightInfo = ();
type HandleTxValidation = ();
}

parameter_types! {
Expand All @@ -182,6 +183,7 @@ impl Config for Test {
type StateRoot = IntermediateStateRoot<Self>;
type PostLogContent = PostBlockAndTxnHashes;
type ExtraDataLength = ConstU32<30>;
type HandleTxValidation = ();
}

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down Expand Up @@ -230,6 +232,8 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
fn apply_self_contained(
self,
info: Self::SignedInfo,
_dispatch_info: &DispatchInfoOf<RuntimeCall>,
_len: usize,
) -> Option<sp_runtime::DispatchResultWithInfo<sp_runtime::traits::PostDispatchInfoOf<Self>>> {
match self {
call @ RuntimeCall::Ethereum(crate::Call::transact { .. }) => {
Expand Down
1 change: 1 addition & 0 deletions frame/evm/precompile/dispatch/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ impl pallet_evm::Config for Test {
type GasLimitPovSizeRatio = ();
type Timestamp = Timestamp;
type WeightInfo = ();
type HandleTxValidation = ();
}

pub(crate) struct MockHandle {
Expand Down
3 changes: 3 additions & 0 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub use self::{
#[frame_support::pallet]
pub mod pallet {
use super::*;
use fp_evm::HandleTxValidation;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

Expand Down Expand Up @@ -177,6 +178,8 @@ pub mod pallet {
fn config() -> &'static EvmConfig {
&SHANGHAI_CONFIG
}

type HandleTxValidation: HandleTxValidation<Error<Self>>;
}

#[pallet::call]
Expand Down
1 change: 1 addition & 0 deletions frame/evm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ impl crate::Config for Test {
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type Timestamp = Timestamp;
type WeightInfo = ();
type HandleTxValidation = ();
}

/// Example PrecompileSet with only Identity precompile.
Expand Down
20 changes: 11 additions & 9 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ use sp_std::{
};
// Frontier
use fp_evm::{
AccessedStorage, CallInfo, CreateInfo, ExecutionInfoV2, IsPrecompileResult, Log, PrecompileSet,
Vicinity, WeightInfo, ACCOUNT_BASIC_PROOF_SIZE, ACCOUNT_CODES_METADATA_PROOF_SIZE,
ACCOUNT_STORAGE_PROOF_SIZE, IS_EMPTY_CHECK_PROOF_SIZE, WRITE_PROOF_SIZE,
AccessedStorage, CallInfo, CreateInfo, ExecutionInfoV2, HandleTxValidation, IsPrecompileResult,
Log, PrecompileSet, Vicinity, WeightInfo, ACCOUNT_BASIC_PROOF_SIZE,
ACCOUNT_CODES_METADATA_PROOF_SIZE, ACCOUNT_STORAGE_PROOF_SIZE, IS_EMPTY_CHECK_PROOF_SIZE,
WRITE_PROOF_SIZE,
};

use crate::{
Expand Down Expand Up @@ -377,7 +378,7 @@ where
let (source_account, inner_weight) = Pallet::<T>::account_basic(&source);
weight = weight.saturating_add(inner_weight);

let _ = fp_evm::CheckEvmTransaction::<Self::Error>::new(
let evm_config = fp_evm::CheckEvmTransaction::<Self::Error>::new(
fp_evm::CheckEvmTransactionConfig {
evm_config,
block_gas_limit: T::BlockGasLimit::get(),
Expand All @@ -399,11 +400,12 @@ where
},
weight_limit,
proof_size_base_cost,
)
.validate_in_block_for(&source_account)
.and_then(|v| v.with_base_fee())
.and_then(|v| v.with_balance_for(&source_account))
.map_err(|error| RunnerError { error, weight })?;
);

T::HandleTxValidation::validate_in_block_for(&evm_config, &source_account)
.and_then(|_| T::HandleTxValidation::with_base_fee(&evm_config))
.and_then(|_| T::HandleTxValidation::with_balance_for(&evm_config, &source_account))
.map_err(|error| RunnerError { error, weight })?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use self::{
},
validation::{
CheckEvmTransaction, CheckEvmTransactionConfig, CheckEvmTransactionInput,
InvalidEvmTransactionError,
HandleTxValidation, InvalidEvmTransactionError,
},
};

Expand Down
Loading

0 comments on commit 50660c0

Please sign in to comment.