Skip to content

Commit

Permalink
Rename and code clean
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Aug 28, 2023
1 parent d005519 commit e24fd82
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 82 deletions.
1 change: 0 additions & 1 deletion client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ fc-api = { workspace = true }
fc-mapping-sync = { workspace = true }
fc-rpc-core = { workspace = true }
fc-storage = { workspace = true }
# fp-ethereum = { workspace = true, features = ["default"] }
fp-evm = { workspace = true }
fp-rpc = { workspace = true, features = ["default"] }
fp-storage = { workspace = true, features = ["default"] }
Expand Down
6 changes: 1 addition & 5 deletions client/rpc/src/eth/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
use sc_transaction_pool_api::error::{Error as PError, IntoPoolError};
use sp_runtime::transaction_validity::InvalidTransaction;
// Frontier
// use fp_ethereum::TransactionValidationError as VError;
use fp_evm::InvalidEvmTransactionError as VError;
use fp_evm::TransactionValidationError as VError;

// Formats the same way Geth node formats responses.
pub struct Geth;
Expand All @@ -45,9 +44,6 @@ impl Geth {
VError::GasLimitTooLow => "intrinsic gas too low".into(),
VError::GasLimitTooHigh => "exceeds block gas limit".into(),
VError::GasPriceTooLow => "gas price too low".into(),
// VError::MaxFeePerGasTooLow => {
// "max priority fee per gas higher than max fee per gas".into()
// }
_ => "unknown error".into(),
},
_ => "unknown error".into(),
Expand Down
46 changes: 23 additions & 23 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use evm::ExitReason;
use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
use fp_ethereum::ValidatedTransaction as ValidatedTransactionT;
use fp_evm::{
CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, InvalidEvmTransactionError,
CallOrCreateInfo, CheckEvmTransaction, CheckEvmTransactionConfig, TransactionValidationError,
};
use fp_storage::{EthereumStorageSchema, PALLET_ETHEREUM_SCHEMA};
use frame_support::{
Expand Down Expand Up @@ -113,7 +113,7 @@ where
if let Call::transact { transaction } = self {
let check = || {
let origin = Pallet::<T>::recover_signer(transaction).ok_or(
InvalidTransaction::Custom(InvalidEvmTransactionError::InvalidSignature as u8),
InvalidTransaction::Custom(TransactionValidationError::InvalidSignature as u8),
)?;

Ok(origin)
Expand Down Expand Up @@ -979,41 +979,41 @@ impl<T: Config> BlockHashMapping for EthereumBlockHashMapping<T> {

pub struct InvalidTransactionWrapper(InvalidTransaction);

impl From<InvalidEvmTransactionError> for InvalidTransactionWrapper {
fn from(validation_error: InvalidEvmTransactionError) -> Self {
impl From<TransactionValidationError> for InvalidTransactionWrapper {
fn from(validation_error: TransactionValidationError) -> Self {
match validation_error {
InvalidEvmTransactionError::GasLimitTooLow => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::GasLimitTooLow as u8),
TransactionValidationError::GasLimitTooLow => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::GasLimitTooLow as u8),
),
InvalidEvmTransactionError::GasLimitTooHigh => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::GasLimitTooHigh as u8),
TransactionValidationError::GasLimitTooHigh => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::GasLimitTooHigh as u8),
),
InvalidEvmTransactionError::GasPriceTooLow => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::GasPriceTooLow as u8),
TransactionValidationError::PriorityFeeTooHigh => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::PriorityFeeTooHigh as u8),
),
InvalidEvmTransactionError::PriorityFeeTooHigh => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::PriorityFeeTooHigh as u8),
),
InvalidEvmTransactionError::BalanceTooLow => {
TransactionValidationError::BalanceTooLow => {
InvalidTransactionWrapper(InvalidTransaction::Payment)
}
InvalidEvmTransactionError::TxNonceTooLow => {
TransactionValidationError::TxNonceTooLow => {
InvalidTransactionWrapper(InvalidTransaction::Stale)
}
InvalidEvmTransactionError::TxNonceTooHigh => {
TransactionValidationError::TxNonceTooHigh => {
InvalidTransactionWrapper(InvalidTransaction::Future)
}
InvalidEvmTransactionError::InvalidPaymentInput => {
TransactionValidationError::InvalidPaymentInput => {
InvalidTransactionWrapper(InvalidTransaction::Payment)
}
InvalidEvmTransactionError::InvalidChainId => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::InvalidChainId as u8),
TransactionValidationError::InvalidChainId => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::InvalidChainId as u8),
),
TransactionValidationError::InvalidSignature => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::InvalidSignature as u8),
),
InvalidEvmTransactionError::InvalidSignature => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::InvalidSignature as u8),
TransactionValidationError::GasPriceTooLow => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::GasPriceTooLow as u8),
),
InvalidEvmTransactionError::UnknownError => InvalidTransactionWrapper(
InvalidTransaction::Custom(InvalidEvmTransactionError::UnknownError as u8),
TransactionValidationError::UnknownError => InvalidTransactionWrapper(
InvalidTransaction::Custom(TransactionValidationError::UnknownError as u8),
),
}
}
Expand Down
25 changes: 12 additions & 13 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ use fp_account::AccountId20;
use fp_evm::GenesisAccount;
pub use fp_evm::{
Account, CallInfo, CreateInfo, ExecutionInfoV2 as ExecutionInfo, FeeCalculator,
InvalidEvmTransactionError, IsPrecompileResult, LinearCostPrecompile, Log, Precompile,
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult, PrecompileSet,
Vicinity,
IsPrecompileResult, LinearCostPrecompile, Log, Precompile, PrecompileFailure, PrecompileHandle,
PrecompileOutput, PrecompileResult, PrecompileSet, TransactionValidationError, Vicinity,
};

pub use self::{
Expand Down Expand Up @@ -495,17 +494,17 @@ pub mod pallet {
TransactionMustComeFromEOA,
}

impl<T> From<InvalidEvmTransactionError> for Error<T> {
fn from(validation_error: InvalidEvmTransactionError) -> Self {
impl<T> From<TransactionValidationError> for Error<T> {
fn from(validation_error: TransactionValidationError) -> Self {
match validation_error {
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,
TransactionValidationError::GasLimitTooLow => Error::<T>::GasLimitTooLow,
TransactionValidationError::GasLimitTooHigh => Error::<T>::GasLimitTooHigh,
TransactionValidationError::GasPriceTooLow => Error::<T>::GasPriceTooLow,
TransactionValidationError::PriorityFeeTooHigh => Error::<T>::GasPriceTooLow,
TransactionValidationError::BalanceTooLow => Error::<T>::BalanceLow,
TransactionValidationError::TxNonceTooLow => Error::<T>::InvalidNonce,
TransactionValidationError::TxNonceTooHigh => Error::<T>::InvalidNonce,
TransactionValidationError::InvalidPaymentInput => Error::<T>::GasPriceTooLow,
_ => Error::<T>::Undefined,
}
}
Expand Down
13 changes: 0 additions & 13 deletions primitives/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,6 @@ use frame_support::dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo};
use scale_codec::{Decode, Encode};
use sp_std::{result::Result, vec::Vec};

// #[repr(u8)]
// #[derive(num_enum::FromPrimitive, num_enum::IntoPrimitive)]
// pub enum TransactionValidationError {
// #[allow(dead_code)]
// #[num_enum(default)]
// UnknownError,
// InvalidChainId,
// InvalidSignature,
// GasLimitTooLow,
// GasLimitTooHigh,
// MaxFeePerGasTooLow,
// }

pub trait ValidatedTransaction {
fn apply(
source: H160,
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,
TransactionValidationError,
},
};

Expand Down
52 changes: 26 additions & 26 deletions primitives/evm/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ pub struct CheckEvmTransactionConfig<'config> {
}

#[derive(Debug)]
pub struct CheckEvmTransaction<'config, E: From<InvalidEvmTransactionError>> {
pub struct CheckEvmTransaction<'config, E: From<TransactionValidationError>> {
pub config: CheckEvmTransactionConfig<'config>,
pub transaction: CheckEvmTransactionInput,
pub weight_limit: Option<Weight>,
pub proof_size_base_cost: Option<u64>,
_marker: sp_std::marker::PhantomData<E>,
}

/// Transaction validate errors
/// Transaction validation errors
#[repr(u8)]
#[derive(num_enum::FromPrimitive, num_enum::IntoPrimitive, Debug)]
pub enum InvalidEvmTransactionError {
pub enum TransactionValidationError {
/// The transaction gas limit is too low
GasLimitTooLow,
/// The transaction gas limit is too hign
Expand All @@ -82,7 +82,7 @@ pub enum InvalidEvmTransactionError {
UnknownError,
}

impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config, E> {
impl<'config, E: From<TransactionValidationError>> CheckEvmTransaction<'config, E> {
pub fn new(
config: CheckEvmTransactionConfig<'config>,
transaction: CheckEvmTransactionInput,
Expand All @@ -100,16 +100,16 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,

pub fn validate_in_pool_for(&self, who: &Account) -> Result<&Self, E> {
if self.transaction.nonce < who.nonce {
return Err(InvalidEvmTransactionError::TxNonceTooLow.into());
return Err(TransactionValidationError::TxNonceTooLow.into());
}
self.validate_common()
}

pub fn validate_in_block_for(&self, who: &Account) -> Result<&Self, E> {
if self.transaction.nonce > who.nonce {
return Err(InvalidEvmTransactionError::TxNonceTooHigh.into());
return Err(TransactionValidationError::TxNonceTooHigh.into());
} else if self.transaction.nonce < who.nonce {
return Err(InvalidEvmTransactionError::TxNonceTooLow.into());
return Err(TransactionValidationError::TxNonceTooLow.into());
}
self.validate_common()
}
Expand All @@ -118,7 +118,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
// Chain id matches the one in the signature.
if let Some(chain_id) = self.transaction.chain_id {
if chain_id != self.config.chain_id {
return Err(InvalidEvmTransactionError::InvalidChainId.into());
return Err(TransactionValidationError::InvalidChainId.into());
}
}
Ok(self)
Expand All @@ -130,7 +130,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
if self.config.is_transactional || gas_price > U256::zero() {
// Transaction max fee is at least the current base fee.
if gas_price < self.config.base_fee {
return Err(InvalidEvmTransactionError::GasPriceTooLow.into());
return Err(TransactionValidationError::GasPriceTooLow.into());
}
}
Ok(self)
Expand All @@ -153,7 +153,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
if self.config.is_transactional || fee > U256::zero() {
let total_payment = self.transaction.value.saturating_add(fee);
if who.balance < total_payment {
return Err(InvalidEvmTransactionError::BalanceTooLow.into());
return Err(TransactionValidationError::BalanceTooLow.into());
}
}
Ok(self)
Expand All @@ -177,7 +177,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
// EIP-1559 tip.
(None, Some(max_fee_per_gas), Some(max_priority_fee_per_gas)) => {
if max_priority_fee_per_gas > max_fee_per_gas {
return Err(InvalidEvmTransactionError::PriorityFeeTooHigh.into());
return Err(TransactionValidationError::PriorityFeeTooHigh.into());
}
let effective_gas_price = self
.config
Expand All @@ -189,7 +189,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
}
_ => {
if self.config.is_transactional {
Err(InvalidEvmTransactionError::InvalidPaymentInput.into())
Err(TransactionValidationError::InvalidPaymentInput.into())
} else {
// Allow non-set fee input for non-transactional calls.
Ok((U256::zero(), None))
Expand All @@ -208,7 +208,7 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
let _ = weight_limit
.proof_size()
.checked_sub(proof_size_base_cost)
.ok_or(InvalidEvmTransactionError::GasLimitTooLow)?;
.ok_or(TransactionValidationError::GasLimitTooLow)?;
}

// We must ensure a transaction can pay the cost of its data bytes.
Expand All @@ -230,12 +230,12 @@ impl<'config, E: From<InvalidEvmTransactionError>> CheckEvmTransaction<'config,
};

if gasometer.record_transaction(transaction_cost).is_err() {
return Err(InvalidEvmTransactionError::GasLimitTooLow.into());
return Err(TransactionValidationError::GasLimitTooLow.into());
}

// Transaction gas limit is within the upper bound block gas limit.
if self.transaction.gas_limit > self.config.block_gas_limit {
return Err(InvalidEvmTransactionError::GasLimitTooHigh.into());
return Err(TransactionValidationError::GasLimitTooHigh.into());
}
}

Expand All @@ -262,18 +262,18 @@ mod tests {

static SHANGHAI_CONFIG: evm::Config = evm::Config::shanghai();

impl From<InvalidEvmTransactionError> for TestError {
fn from(e: InvalidEvmTransactionError) -> Self {
impl From<TransactionValidationError> for TestError {
fn from(e: TransactionValidationError) -> Self {
match e {
InvalidEvmTransactionError::GasLimitTooLow => TestError::GasLimitTooLow,
InvalidEvmTransactionError::GasLimitTooHigh => TestError::GasLimitTooHigh,
InvalidEvmTransactionError::GasPriceTooLow => TestError::GasPriceTooLow,
InvalidEvmTransactionError::PriorityFeeTooHigh => TestError::PriorityFeeTooHigh,
InvalidEvmTransactionError::BalanceTooLow => TestError::BalanceTooLow,
InvalidEvmTransactionError::TxNonceTooLow => TestError::TxNonceTooLow,
InvalidEvmTransactionError::TxNonceTooHigh => TestError::TxNonceTooHigh,
InvalidEvmTransactionError::InvalidPaymentInput => TestError::InvalidPaymentInput,
InvalidEvmTransactionError::InvalidChainId => TestError::InvalidChainId,
TransactionValidationError::GasLimitTooLow => TestError::GasLimitTooLow,
TransactionValidationError::GasLimitTooHigh => TestError::GasLimitTooHigh,
TransactionValidationError::GasPriceTooLow => TestError::GasPriceTooLow,
TransactionValidationError::PriorityFeeTooHigh => TestError::PriorityFeeTooHigh,
TransactionValidationError::BalanceTooLow => TestError::BalanceTooLow,
TransactionValidationError::TxNonceTooLow => TestError::TxNonceTooLow,
TransactionValidationError::TxNonceTooHigh => TestError::TxNonceTooHigh,
TransactionValidationError::InvalidPaymentInput => TestError::InvalidPaymentInput,
TransactionValidationError::InvalidChainId => TestError::InvalidChainId,
}
}
}
Expand Down

0 comments on commit e24fd82

Please sign in to comment.