Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add TestDefaultConfig to pallet-evm & pallet-ethereum #1524

Merged
merged 9 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions frame/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ frame-support = { workspace = true }
frame-system = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-version = { workspace = true }
# Frontier
fp-consensus = { workspace = true }
fp-ethereum = { workspace = true }
Expand Down Expand Up @@ -54,6 +55,7 @@ std = [
"frame-system/std",
"sp-io/std",
"sp-runtime/std",
"sp-version/std",
# Frontier
"fp-consensus/std",
"fp-ethereum/std",
Expand Down
34 changes: 31 additions & 3 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ use sp_runtime::{
},
RuntimeDebug, SaturatedConversion,
};
use sp_version::RuntimeVersion;
// Frontier
use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
pub use fp_ethereum::TransactionData;
Expand Down Expand Up @@ -192,9 +193,10 @@ pub mod pallet {
#[pallet::origin]
pub type Origin = RawOrigin;

#[pallet::config]
#[pallet::config(with_default)]
pub trait Config: frame_system::Config + pallet_evm::Config {
/// The overarching event type.
#[pallet::no_default_bounds]
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// How Ethereum state root is calculated.
type StateRoot: Get<H256>;
Expand All @@ -204,6 +206,32 @@ pub mod pallet {
type ExtraDataLength: Get<u32>;
}

pub mod config_preludes {
use super::*;
use frame_support::{derive_impl, parameter_types};

pub struct TestDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
impl frame_system::DefaultConfig for TestDefaultConfig {}

#[derive_impl(pallet_evm::config_preludes::TestDefaultConfig, no_aggregated_types)]
impl pallet_evm::DefaultConfig for TestDefaultConfig {}

parameter_types! {
pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes;
}

#[register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
#[inject_runtime_type]
type RuntimeEvent = ();
type StateRoot = IntermediateStateRoot<Self::Version>;
type PostLogContent = PostBlockAndTxnHashes;
type ExtraDataLength = ConstU32<30>;
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: BlockNumberFor<T>) {
Expand Down Expand Up @@ -968,9 +996,9 @@ pub enum ReturnValue {
}

pub struct IntermediateStateRoot<T>(PhantomData<T>);
impl<T: Config> Get<H256> for IntermediateStateRoot<T> {
impl<T: Get<RuntimeVersion>> Get<H256> for IntermediateStateRoot<T> {
fn get() -> H256 {
let version = T::Version::get().state_version();
let version = T::get().state_version();
H256::decode(&mut &sp_io::storage::root(version)[..])
.expect("Node is configured to use the same hash; qed")
}
Expand Down
13 changes: 2 additions & 11 deletions frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use sp_runtime::{
use pallet_evm::{AddressMapping, EnsureAddressTruncated, FeeCalculator};

use super::*;
use crate::IntermediateStateRoot;

pub type SignedExtra = (frame_system::CheckSpecVersion<Test>,);

Expand Down Expand Up @@ -183,16 +182,8 @@ impl pallet_evm::Config for Test {
type WeightInfo = ();
}

parameter_types! {
pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes;
}

impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type StateRoot = IntermediateStateRoot<Self>;
type PostLogContent = PostBlockAndTxnHashes;
type ExtraDataLength = ConstU32<30>;
}
#[derive_impl(crate::config_preludes::TestDefaultConfig)]
impl Config for Test {}
boundless-forest marked this conversation as resolved.
Show resolved Hide resolved

impl fp_self_contained::SelfContainedCall for RuntimeCall {
type SignedInfo = H160;
Expand Down
101 changes: 99 additions & 2 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,10 @@ pub mod pallet {
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::config]
#[pallet::config(with_default)]
pub trait Config: frame_system::Config {
/// Account info provider.
#[pallet::no_default]
type AccountProvider: AccountProvider;

/// Calculator for current gas price.
Expand All @@ -139,19 +140,25 @@ pub mod pallet {
type WeightPerGas: Get<Weight>;

/// Block number to block hash.
#[pallet::no_default]
type BlockHashMapping: BlockHashMapping;

/// Allow the origin to call on behalf of given address.
#[pallet::no_default_bounds]
type CallOrigin: EnsureAddressOrigin<Self::RuntimeOrigin>;
/// Allow the origin to withdraw on behalf of given address.
#[pallet::no_default_bounds]
type WithdrawOrigin: EnsureAddressOrigin<Self::RuntimeOrigin, Success = AccountIdOf<Self>>;

/// Mapping from address to account id.
#[pallet::no_default_bounds]
type AddressMapping: AddressMapping<AccountIdOf<Self>>;
/// Currency type for withdraw and balance storage.
#[pallet::no_default]
type Currency: Currency<AccountIdOf<Self>> + Inspect<AccountIdOf<Self>>;

/// The overarching event type.
#[pallet::no_default_bounds]
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Precompiles associated with this EVM engine.
type PrecompilesType: PrecompileSet;
Expand All @@ -161,14 +168,17 @@ pub mod pallet {
/// The block gas limit. Can be a simple constant, or an adjustment algorithm in another pallet.
type BlockGasLimit: Get<U256>;
/// EVM execution runner.
#[pallet::no_default]
type Runner: Runner<Self>;

/// To handle fee deduction for EVM transactions. An example is this pallet being used by `pallet_ethereum`
/// where the chain implementing `pallet_ethereum` should be able to configure what happens to the fees
/// Similar to `OnChargeTransaction` of `pallet_transaction_payment`
#[pallet::no_default_bounds]
type OnChargeTransaction: OnChargeEVMTransaction<Self>;

/// Called on create calls, used to record owner
#[pallet::no_default_bounds]
type OnCreate: OnCreate<Self>;

/// Find author for the current block.
Expand All @@ -181,6 +191,7 @@ pub mod pallet {
type SuicideQuickClearLimit: Get<u32>;

/// Get the timestamp for the current block.
#[pallet::no_default]
type Timestamp: Time;

/// Weight information for extrinsics in this pallet.
Expand All @@ -192,6 +203,77 @@ pub mod pallet {
}
}

pub mod config_preludes {
use super::*;
use core::str::FromStr;
use frame_support::{derive_impl, parameter_types, ConsensusEngineId};
use sp_runtime::traits::IdentityLookup;

pub struct TestDefaultConfig;

#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
impl frame_system::DefaultConfig for TestDefaultConfig {
conr2d marked this conversation as resolved.
Show resolved Hide resolved
type AccountId = H160;
type Lookup = IdentityLookup<Self::AccountId>;
}

const BLOCK_GAS_LIMIT: u64 = 150_000_000;
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;

parameter_types! {
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
pub WeightPerGas: Weight = Weight::from_parts(20_000, 0);
pub SuicideQuickClearLimit: u32 = 0;
}

#[register_default_impl(TestDefaultConfig)]
impl DefaultConfig for TestDefaultConfig {
type FeeCalculator = FixedGasPrice;
type GasWeightMapping = FixedGasWeightMapping<Self>;
type WeightPerGas = WeightPerGas;
type CallOrigin = EnsureAddressRoot<Self::AccountId>;
type WithdrawOrigin = EnsureAddressNever<Self::AccountId>;
type AddressMapping = IdentityAddressMapping;
#[inject_runtime_type]
type RuntimeEvent = ();
type PrecompilesType = ();
type PrecompilesValue = ();
type ChainId = ();
conr2d marked this conversation as resolved.
Show resolved Hide resolved
type BlockGasLimit = BlockGasLimit;
type OnChargeTransaction = ();
type OnCreate = ();
type FindAuthor = FindAuthorTruncated;
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
type SuicideQuickClearLimit = SuicideQuickClearLimit;
type WeightInfo = ();
}

impl FixedGasWeightMappingAssociatedTypes for TestDefaultConfig {
type WeightPerGas = <Self as DefaultConfig>::WeightPerGas;
type BlockWeights = <Self as frame_system::DefaultConfig>::BlockWeights;
type GasLimitPovSizeRatio = <Self as DefaultConfig>::GasLimitPovSizeRatio;
}

pub struct FixedGasPrice;
impl FeeCalculator for FixedGasPrice {
fn min_gas_price() -> (U256, Weight) {
// Return some meaningful gas price and weight
(1_000_000_000u128.into(), Weight::from_parts(7u64, 0))
}
}

pub struct FindAuthorTruncated;
impl FindAuthor<H160> for FindAuthorTruncated {
fn find_author<'a, I>(_digests: I) -> Option<H160>
where
I: 'a + IntoIterator<Item = (ConsensusEngineId, &'a [u8])>,
{
Some(H160::from_str("1234500000000000000000000000000000000000").unwrap())
}
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Withdraw balance from EVM into currency/balances pallet.
Expand Down Expand Up @@ -772,8 +854,23 @@ pub trait GasWeightMapping {
fn weight_to_gas(weight: Weight) -> u64;
}

pub trait FixedGasWeightMappingAssociatedTypes {
type WeightPerGas: Get<Weight>;
type BlockWeights: Get<frame_system::limits::BlockWeights>;
type GasLimitPovSizeRatio: Get<u64>;
}

impl<T: Config> FixedGasWeightMappingAssociatedTypes for T {
type WeightPerGas = T::WeightPerGas;
type BlockWeights = T::BlockWeights;
type GasLimitPovSizeRatio = T::GasLimitPovSizeRatio;
}

pub struct FixedGasWeightMapping<T>(core::marker::PhantomData<T>);
impl<T: Config> GasWeightMapping for FixedGasWeightMapping<T> {
impl<T> GasWeightMapping for FixedGasWeightMapping<T>
where
T: FixedGasWeightMappingAssociatedTypes,
{
fn gas_to_weight(gas: u64, without_base_weight: bool) -> Weight {
let mut weight = T::WeightPerGas::get().saturating_mul(gas);
if without_base_weight {
Expand Down
Loading
Loading