Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ members = [
"primitives/dynamic-fee",
"primitives/evm",
"primitives/rpc",
"primitives/self-contained",
"template/node",
"template/runtime",
"precompiles",
Expand Down Expand Up @@ -190,7 +189,6 @@ fp-dynamic-fee = { path = "primitives/dynamic-fee", default-features = false }
fp-ethereum = { path = "primitives/ethereum", default-features = false }
fp-evm = { path = "primitives/evm", default-features = false }
fp-rpc = { path = "primitives/rpc", default-features = false }
fp-self-contained = { path = "primitives/self-contained", default-features = false }
fp-storage = { path = "primitives/storage", default-features = false }
# Frontier FRAME
pallet-base-fee = { path = "frame/base-fee", default-features = false }
Expand Down
2 changes: 0 additions & 2 deletions frame/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ rlp = { workspace = true }
pallet-balances = { workspace = true, features = ["default", "insecure_zero_ed"] }
pallet-timestamp = { workspace = true, features = ["default"] }
sp-core = { workspace = true, features = ["default"] }
# Frontier
fp-self-contained = { workspace = true, features = ["default"] }

[features]
default = ["std"]
Expand Down
124 changes: 124 additions & 0 deletions frame/ethereum/src/extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
//! Extensions for unsigned general extrinsic for ethereum pallet

use crate::pallet::Call as EthereumCall;
use crate::{Config, Origin, Pallet as Ethereum, RawOrigin};
use ethereum_types::H160;
use fp_evm::TransactionValidationError;
use frame_support::pallet_prelude::{PhantomData, TypeInfo};
use frame_system::pallet_prelude::{OriginFor, RuntimeCallFor};
use scale_codec::{Decode, Encode};
use scale_info::prelude::fmt;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, Implication,
TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};

/// Trait to be implemented by the Runtime call for Ethereum call conversion and additional validations.
pub trait EthereumTransactionHook<Runtime>
where
Runtime: Config,
OriginFor<Runtime>: Into<Result<RawOrigin, OriginFor<Runtime>>>,
{
fn maybe_ethereum_call(&self) -> Option<&EthereumCall<Runtime>>;

fn additional_validation(
&self,
_signer: H160,
_source: TransactionSource,
) -> Result<(), TransactionValidityError> {
Ok(())
}
}

/// Extensions for pallet-ethereum unsigned extrinsics.
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
pub struct EthereumExtension<Runtime>(PhantomData<Runtime>);

impl<Runtime> EthereumExtension<Runtime> {
pub fn new() -> Self {
Self(PhantomData)
}
}

impl<Runtime> Default for EthereumExtension<Runtime> {
fn default() -> Self {
Self::new()
}
}

impl<T: Config> fmt::Debug for EthereumExtension<T> {
#[cfg(feature = "std")]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "EthereumExtension",)
}

#[cfg(not(feature = "std"))]
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
Ok(())
}
}

impl<Runtime> EthereumExtension<Runtime> where
Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync
{
}

impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>> for EthereumExtension<Runtime>
where
Runtime: Config + scale_info::TypeInfo + fmt::Debug + Send + Sync,
<RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin:
AsSystemOriginSigner<<Runtime as frame_system::Config>::AccountId> + From<Origin> + Clone,
OriginFor<Runtime>: Into<Result<RawOrigin, OriginFor<Runtime>>>,
RuntimeCallFor<Runtime>: EthereumTransactionHook<Runtime>,
{
const IDENTIFIER: &'static str = "EthereumExtension";
type Implicit = ();
type Val = ();
type Pre = ();

fn validate(
&self,
origin: DispatchOriginOf<RuntimeCallFor<Runtime>>,
call: &RuntimeCallFor<Runtime>,
_info: &DispatchInfoOf<RuntimeCallFor<Runtime>>,
_len: usize,
_self_implicit: Self::Implicit,
_inherited_implication: &impl Implication,
source: TransactionSource,
) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
// we only care about unsigned calls
if origin.as_system_origin_signer().is_some() {
return Ok((ValidTransaction::default(), (), origin));
};

let transaction = match call.maybe_ethereum_call() {
Some(EthereumCall::transact { transaction }) => transaction,
_ => return Ok((ValidTransaction::default(), (), origin)),
};

// check signer
let signer = Ethereum::<Runtime>::recover_signer(transaction).ok_or(
InvalidTransaction::Custom(TransactionValidationError::InvalidSignature as u8),
)?;

// validation on transactions based on pre_dispatch or mempool validation.
let pre_dispatch = source == TransactionSource::InBlock;
let validity = if pre_dispatch {
Ethereum::<Runtime>::validate_transaction_in_block(signer, transaction)
.map(|_| ValidTransaction::default())
} else {
Ethereum::<Runtime>::validate_transaction_in_pool(signer, transaction)
}?;

// do any additional validations
call.additional_validation(signer, source)?;

Ok((validity, (), Origin::EthereumTransaction(signer).into()))
}

impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare weight);
}
79 changes: 4 additions & 75 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

extern crate alloc;

pub mod extension;
#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
Expand All @@ -44,16 +45,14 @@ use scale_codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
// Substrate
use frame_support::{
dispatch::{
DispatchErrorWithPostInfo, DispatchInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo,
},
dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Pays, PostDispatchInfo},
traits::{EnsureOrigin, Get, PalletInfoAccess, Time},
weights::Weight,
};
use frame_system::{pallet_prelude::OriginFor, CheckWeight, WeightInfo};
use frame_system::{pallet_prelude::OriginFor, WeightInfo};
use sp_runtime::{
generic::DigestItem,
traits::{DispatchInfoOf, Dispatchable, One, Saturating, UniqueSaturatedInto, Zero},
traits::{One, Saturating, UniqueSaturatedInto, Zero},
transaction_validity::{
InvalidTransaction, TransactionValidity, TransactionValidityError, ValidTransactionBuilder,
},
Expand Down Expand Up @@ -104,76 +103,6 @@ impl<O: Into<Result<RawOrigin, O>> + From<RawOrigin>> EnsureOrigin<O>
}
}

impl<T> Call<T>
where
OriginFor<T>: Into<Result<RawOrigin, OriginFor<T>>>,
T: Send + Sync + Config,
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
{
pub fn is_self_contained(&self) -> bool {
matches!(self, Call::transact { .. })
}

pub fn check_self_contained(&self) -> Option<Result<H160, TransactionValidityError>> {
if let Call::transact { transaction } = self {
let check = || {
let origin = Pallet::<T>::recover_signer(transaction).ok_or(
InvalidTransaction::Custom(TransactionValidationError::InvalidSignature as u8),
)?;

Ok(origin)
};

Some(check())
} else {
None
}
}

pub fn pre_dispatch_self_contained(
&self,
origin: &H160,
dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
len: usize,
) -> Option<Result<(), TransactionValidityError>> {
if let Call::transact { transaction } = self {
if let Err(e) =
CheckWeight::<T>::do_validate(dispatch_info, len).and_then(|(_, next_len)| {
CheckWeight::<T>::do_prepare(dispatch_info, len, next_len)
}) {
return Some(Err(e));
}

Some(Pallet::<T>::validate_transaction_in_block(
*origin,
transaction,
))
} else {
None
}
}

pub fn validate_self_contained(
&self,
origin: &H160,
dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
len: usize,
) -> Option<TransactionValidity> {
if let Call::transact { transaction } = self {
if let Err(e) = CheckWeight::<T>::do_validate(dispatch_info, len) {
return Some(Err(e));
}

Some(Pallet::<T>::validate_transaction_in_pool(
*origin,
transaction,
))
} else {
None
}
}
}

#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub enum PostLogContent {
#[default]
Expand Down
Loading
Loading