Skip to content

Commit 18aa99b

Browse files
Add TestDefaultConfig to pallet-evm & pallet-ethereum (polkadot-evm#1524)
* feat: Implement TestDefaultConfig for pallet-evm * feat: Implement TestDefaultConfig for pallet-ethereum * style: Reformat * Extend to the ethereum pallet * chore: Align ChainId in TestDefaultConfig with template runtime * chore: Clean up imports * test: Fix broken pallet-ethereum tests * chore: Fix clippy error * chore: Fix clippy error (2) --------- Co-authored-by: bear <boundless.forest@outlook.com>
1 parent 5a24856 commit 18aa99b

File tree

9 files changed

+180
-219
lines changed

9 files changed

+180
-219
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frame/ethereum/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ frame-support = { workspace = true }
2121
frame-system = { workspace = true }
2222
sp-io = { workspace = true }
2323
sp-runtime = { workspace = true }
24+
sp-version = { workspace = true }
2425
# Frontier
2526
fp-consensus = { workspace = true }
2627
fp-ethereum = { workspace = true }
@@ -54,6 +55,7 @@ std = [
5455
"frame-system/std",
5556
"sp-io/std",
5657
"sp-runtime/std",
58+
"sp-version/std",
5759
# Frontier
5860
"fp-consensus/std",
5961
"fp-ethereum/std",

frame/ethereum/src/lib.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use sp_runtime::{
5959
},
6060
RuntimeDebug, SaturatedConversion,
6161
};
62+
use sp_version::RuntimeVersion;
6263
// Frontier
6364
use fp_consensus::{PostLog, PreLog, FRONTIER_ENGINE_ID};
6465
pub use fp_ethereum::TransactionData;
@@ -192,9 +193,10 @@ pub mod pallet {
192193
#[pallet::origin]
193194
pub type Origin = RawOrigin;
194195

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

209+
pub mod config_preludes {
210+
use super::*;
211+
use frame_support::{derive_impl, parameter_types};
212+
213+
pub struct TestDefaultConfig;
214+
215+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig, no_aggregated_types)]
216+
impl frame_system::DefaultConfig for TestDefaultConfig {}
217+
218+
#[derive_impl(pallet_evm::config_preludes::TestDefaultConfig, no_aggregated_types)]
219+
impl pallet_evm::DefaultConfig for TestDefaultConfig {}
220+
221+
parameter_types! {
222+
pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes;
223+
}
224+
225+
#[register_default_impl(TestDefaultConfig)]
226+
impl DefaultConfig for TestDefaultConfig {
227+
#[inject_runtime_type]
228+
type RuntimeEvent = ();
229+
type StateRoot = IntermediateStateRoot<Self::Version>;
230+
type PostLogContent = PostBlockAndTxnHashes;
231+
type ExtraDataLength = ConstU32<30>;
232+
}
233+
}
234+
207235
#[pallet::hooks]
208236
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
209237
fn on_finalize(n: BlockNumberFor<T>) {
@@ -968,9 +996,9 @@ pub enum ReturnValue {
968996
}
969997

970998
pub struct IntermediateStateRoot<T>(PhantomData<T>);
971-
impl<T: Config> Get<H256> for IntermediateStateRoot<T> {
999+
impl<T: Get<RuntimeVersion>> Get<H256> for IntermediateStateRoot<T> {
9721000
fn get() -> H256 {
973-
let version = T::Version::get().state_version();
1001+
let version = T::get().state_version();
9741002
H256::decode(&mut &sp_io::storage::root(version)[..])
9751003
.expect("Node is configured to use the same hash; qed")
9761004
}

frame/ethereum/src/mock.rs

Lines changed: 10 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,16 @@
2020
use ethereum::{TransactionAction, TransactionSignature};
2121
use rlp::RlpStream;
2222
// Substrate
23-
use frame_support::{
24-
derive_impl, parameter_types,
25-
traits::{ConstU32, FindAuthor},
26-
weights::Weight,
27-
ConsensusEngineId, PalletId,
28-
};
23+
use frame_support::{derive_impl, parameter_types, traits::FindAuthor, ConsensusEngineId};
2924
use sp_core::{hashing::keccak_256, H160, H256, U256};
3025
use sp_runtime::{
31-
traits::{BlakeTwo256, Dispatchable, IdentityLookup},
26+
traits::{Dispatchable, IdentityLookup},
3227
AccountId32, BuildStorage,
3328
};
3429
// Frontier
35-
use pallet_evm::{AddressMapping, EnsureAddressTruncated, FeeCalculator};
30+
use pallet_evm::{config_preludes::ChainId, AddressMapping};
3631

3732
use super::*;
38-
use crate::IntermediateStateRoot;
3933

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

@@ -55,30 +49,11 @@ parameter_types! {
5549

5650
#[derive_impl(frame_system::config_preludes::TestDefaultConfig as frame_system::DefaultConfig)]
5751
impl frame_system::Config for Test {
58-
type RuntimeEvent = RuntimeEvent;
59-
type BaseCallFilter = frame_support::traits::Everything;
60-
type BlockWeights = ();
61-
type BlockLength = ();
62-
type RuntimeOrigin = RuntimeOrigin;
63-
type RuntimeCall = RuntimeCall;
64-
type RuntimeTask = RuntimeTask;
65-
type Nonce = u64;
66-
type Hash = H256;
67-
type Hashing = BlakeTwo256;
6852
type AccountId = AccountId32;
6953
type Lookup = IdentityLookup<Self::AccountId>;
7054
type Block = frame_system::mocking::MockBlock<Self>;
7155
type BlockHashCount = BlockHashCount;
72-
type DbWeight = ();
73-
type Version = ();
74-
type PalletInfo = PalletInfo;
7556
type AccountData = pallet_balances::AccountData<u64>;
76-
type OnNewAccount = ();
77-
type OnKilledAccount = ();
78-
type SystemWeightInfo = ();
79-
type SS58Prefix = ();
80-
type OnSetCode = ();
81-
type MaxConsumers = ConstU32<16>;
8257
}
8358

8459
parameter_types! {
@@ -89,39 +64,14 @@ parameter_types! {
8964
pub const MaxReserves: u32 = 50;
9065
}
9166

67+
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
9268
impl pallet_balances::Config for Test {
93-
type RuntimeEvent = RuntimeEvent;
94-
type RuntimeHoldReason = RuntimeHoldReason;
95-
type RuntimeFreezeReason = RuntimeFreezeReason;
96-
type WeightInfo = ();
97-
type Balance = u64;
98-
type DustRemoval = ();
9969
type ExistentialDeposit = ExistentialDeposit;
10070
type AccountStore = System;
101-
type ReserveIdentifier = [u8; 8];
102-
type FreezeIdentifier = RuntimeFreezeReason;
103-
type MaxLocks = MaxLocks;
104-
type MaxReserves = MaxReserves;
105-
type MaxFreezes = ConstU32<1>;
106-
}
107-
108-
parameter_types! {
109-
pub const MinimumPeriod: u64 = 6000 / 2;
11071
}
11172

112-
impl pallet_timestamp::Config for Test {
113-
type Moment = u64;
114-
type OnTimestampSet = ();
115-
type MinimumPeriod = MinimumPeriod;
116-
type WeightInfo = ();
117-
}
118-
119-
pub struct FixedGasPrice;
120-
impl FeeCalculator for FixedGasPrice {
121-
fn min_gas_price() -> (U256, Weight) {
122-
(1.into(), Weight::zero())
123-
}
124-
}
73+
#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
74+
impl pallet_timestamp::Config for Test {}
12575

12676
pub struct FindAuthorTruncated;
12777
impl FindAuthor<H160> for FindAuthorTruncated {
@@ -133,66 +83,24 @@ impl FindAuthor<H160> for FindAuthorTruncated {
13383
}
13484
}
13585

136-
const BLOCK_GAS_LIMIT: u64 = 150_000_000;
137-
const MAX_POV_SIZE: u64 = 5 * 1024 * 1024;
138-
13986
parameter_types! {
14087
pub const TransactionByteFee: u64 = 1;
141-
pub const ChainId: u64 = 42;
142-
pub const EVMModuleId: PalletId = PalletId(*b"py/evmpa");
143-
pub BlockGasLimit: U256 = U256::from(BLOCK_GAS_LIMIT);
144-
pub const GasLimitPovSizeRatio: u64 = BLOCK_GAS_LIMIT.saturating_div(MAX_POV_SIZE);
145-
pub const WeightPerGas: Weight = Weight::from_parts(20_000, 0);
146-
}
147-
148-
pub struct HashedAddressMapping;
149-
impl AddressMapping<AccountId32> for HashedAddressMapping {
150-
fn into_account_id(address: H160) -> AccountId32 {
151-
let mut data = [0u8; 32];
152-
data[0..20].copy_from_slice(&address[..]);
153-
AccountId32::from(Into::<[u8; 32]>::into(data))
154-
}
155-
}
156-
157-
parameter_types! {
158-
pub SuicideQuickClearLimit: u32 = 0;
15988
}
16089

90+
#[derive_impl(pallet_evm::config_preludes::TestDefaultConfig)]
16191
impl pallet_evm::Config for Test {
16292
type AccountProvider = pallet_evm::FrameSystemAccountProvider<Self>;
163-
type FeeCalculator = FixedGasPrice;
164-
type GasWeightMapping = pallet_evm::FixedGasWeightMapping<Self>;
165-
type WeightPerGas = WeightPerGas;
16693
type BlockHashMapping = crate::EthereumBlockHashMapping<Self>;
167-
type CallOrigin = EnsureAddressTruncated;
168-
type WithdrawOrigin = EnsureAddressTruncated;
169-
type AddressMapping = HashedAddressMapping;
17094
type Currency = Balances;
171-
type RuntimeEvent = RuntimeEvent;
17295
type PrecompilesType = ();
17396
type PrecompilesValue = ();
174-
type ChainId = ChainId;
175-
type BlockGasLimit = BlockGasLimit;
17697
type Runner = pallet_evm::runner::stack::Runner<Self>;
177-
type OnChargeTransaction = ();
178-
type OnCreate = ();
17998
type FindAuthor = FindAuthorTruncated;
180-
type GasLimitPovSizeRatio = GasLimitPovSizeRatio;
181-
type SuicideQuickClearLimit = SuicideQuickClearLimit;
18299
type Timestamp = Timestamp;
183-
type WeightInfo = ();
184-
}
185-
186-
parameter_types! {
187-
pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes;
188100
}
189101

190-
impl Config for Test {
191-
type RuntimeEvent = RuntimeEvent;
192-
type StateRoot = IntermediateStateRoot<Self>;
193-
type PostLogContent = PostBlockAndTxnHashes;
194-
type ExtraDataLength = ConstU32<30>;
195-
}
102+
#[derive_impl(crate::config_preludes::TestDefaultConfig)]
103+
impl Config for Test {}
196104

197105
impl fp_self_contained::SelfContainedCall for RuntimeCall {
198106
type SignedInfo = H160;
@@ -262,12 +170,9 @@ fn address_build(seed: u8) -> AccountInfo {
262170
let public_key = &libsecp256k1::PublicKey::from_secret_key(&secret_key).serialize()[1..65];
263171
let address = H160::from(H256::from(keccak_256(public_key)));
264172

265-
let mut data = [0u8; 32];
266-
data[0..20].copy_from_slice(&address[..]);
267-
268173
AccountInfo {
269174
private_key,
270-
account_id: AccountId32::from(Into::<[u8; 32]>::into(data)),
175+
account_id: <Test as pallet_evm::Config>::AddressMapping::into_account_id(address),
271176
address,
272177
}
273178
}
@@ -301,7 +206,6 @@ pub fn new_test_ext_with_initial_balance(
301206
accounts_len: usize,
302207
initial_balance: u64,
303208
) -> (Vec<AccountInfo>, sp_io::TestExternalities) {
304-
// sc_cli::init_logger("");
305209
let mut ext = frame_system::GenesisConfig::<Test>::default()
306210
.build_storage()
307211
.unwrap();

0 commit comments

Comments
 (0)