From d8ca6194eebc45ea69e063ae3f868cec3cc351be Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 7 Mar 2023 14:32:06 +0800 Subject: [PATCH 1/3] feat: provide a parameter for the runtime config to choose what to include in the PostLog --- frame/ethereum/src/lib.rs | 54 ++++++++++++++++++++++++++++++------- template/runtime/src/lib.rs | 1 + 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index f351101510..701de5bda3 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -163,6 +163,13 @@ where } } +#[derive(Copy, Clone, Eq, PartialEq, Default)] +pub enum PostLogContent { + #[default] + BlockAndTxnHashes, + OnlyBlockHash, +} + pub use self::pallet::*; #[frame_support::pallet] @@ -185,13 +192,18 @@ pub mod pallet { type RuntimeEvent: From + IsType<::RuntimeEvent>; /// How Ethereum state root is calculated. type StateRoot: Get; + /// What's included in the PostLog. + type PostLogContent: Get; } #[pallet::hooks] impl Hooks> for Pallet { fn on_finalize(n: T::BlockNumber) { >::store_block( - fp_consensus::find_pre_log(&frame_system::Pallet::::digest()).is_err(), + match fp_consensus::find_pre_log(&frame_system::Pallet::::digest()) { + Ok(_) => None, + Err(_) => Some(PostLogContent::BlockAndTxnHashes), + }, U256::from(UniqueSaturatedInto::::unique_saturated_into( frame_system::Pallet::::block_number(), )), @@ -332,7 +344,7 @@ pub mod pallet { #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { - >::store_block(false, U256::zero()); + >::store_block(None, U256::zero()); frame_support::storage::unhashed::put::( PALLET_ETHEREUM_SCHEMA, &EthereumStorageSchema::V3, @@ -375,7 +387,7 @@ impl Pallet { Some(H160::from(H256::from(sp_io::hashing::keccak_256(&pubkey)))) } - fn store_block(post_log: bool, block_number: U256) { + fn store_block(post_log: Option, block_number: U256) { let mut transactions = Vec::new(); let mut statuses = Vec::new(); let mut receipts = Vec::new(); @@ -426,12 +438,22 @@ impl Pallet { CurrentTransactionStatuses::::put(statuses.clone()); BlockHash::::insert(block_number, block.header.hash()); - if post_log { - let digest = DigestItem::Consensus( - FRONTIER_ENGINE_ID, - PostLog::Hashes(fp_consensus::Hashes::from_block(block)).encode(), - ); - frame_system::Pallet::::deposit_log(digest); + match post_log { + Some(PostLogContent::BlockAndTxnHashes) => { + let digest = DigestItem::Consensus( + FRONTIER_ENGINE_ID, + PostLog::Hashes(fp_consensus::Hashes::from_block(block)).encode(), + ); + frame_system::Pallet::::deposit_log(digest); + } + Some(PostLogContent::OnlyBlockHash) => { + let digest = DigestItem::Consensus( + FRONTIER_ENGINE_ID, + PostLog::BlockHash(block.header.hash()).encode(), + ); + frame_system::Pallet::::deposit_log(digest); + } + None => { /* do nothing*/ } } } @@ -872,6 +894,20 @@ impl Get for IntermediateStateRoot { } } +pub struct PostBlockAndTxnHashes; +impl Get for PostBlockAndTxnHashes { + fn get() -> PostLogContent { + PostLogContent::BlockAndTxnHashes + } +} + +pub struct OnlyPostBlockHash; +impl Get for OnlyPostBlockHash { + fn get() -> PostLogContent { + PostLogContent::OnlyBlockHash + } +} + /// Returns the Ethereum block hash by number. pub struct EthereumBlockHashMapping(PhantomData); impl BlockHashMapping for EthereumBlockHashMapping { diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 2fbea2d6e0..024cd98bdd 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -348,6 +348,7 @@ impl pallet_evm::Config for Runtime { impl pallet_ethereum::Config for Runtime { type RuntimeEvent = RuntimeEvent; type StateRoot = pallet_ethereum::IntermediateStateRoot; + type PostLogContent = pallet_ethereum::PostBlockAndTxnHashes; } parameter_types! { From 59252e12f42df75248b37f5abdf92ba7e378b47d Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 7 Mar 2023 15:08:03 +0800 Subject: [PATCH 2/3] use parameter_types macro --- frame/ethereum/src/lib.rs | 14 -------------- frame/ethereum/src/mock.rs | 6 +++++- template/runtime/src/lib.rs | 8 ++++++-- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 701de5bda3..7c4168ec33 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -894,20 +894,6 @@ impl Get for IntermediateStateRoot { } } -pub struct PostBlockAndTxnHashes; -impl Get for PostBlockAndTxnHashes { - fn get() -> PostLogContent { - PostLogContent::BlockAndTxnHashes - } -} - -pub struct OnlyPostBlockHash; -impl Get for OnlyPostBlockHash { - fn get() -> PostLogContent { - PostLogContent::OnlyBlockHash - } -} - /// Returns the Ethereum block hash by number. pub struct EthereumBlockHashMapping(PhantomData); impl BlockHashMapping for EthereumBlockHashMapping { diff --git a/frame/ethereum/src/mock.rs b/frame/ethereum/src/mock.rs index 4928dca369..5e41224e9d 100644 --- a/frame/ethereum/src/mock.rs +++ b/frame/ethereum/src/mock.rs @@ -143,7 +143,6 @@ parameter_types! { } pub struct HashedAddressMapping; - impl AddressMapping for HashedAddressMapping { fn into_account_id(address: H160) -> AccountId32 { let mut data = [0u8; 32]; @@ -172,9 +171,14 @@ impl pallet_evm::Config for Test { type FindAuthor = FindAuthorTruncated; } +parameter_types! { + pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes; +} + impl Config for Test { type RuntimeEvent = RuntimeEvent; type StateRoot = IntermediateStateRoot; + type PostLogContent = PostBlockAndTxnHashes; } impl fp_self_contained::SelfContainedCall for RuntimeCall { diff --git a/template/runtime/src/lib.rs b/template/runtime/src/lib.rs index 024cd98bdd..4b0aec6759 100644 --- a/template/runtime/src/lib.rs +++ b/template/runtime/src/lib.rs @@ -44,7 +44,7 @@ use pallet_transaction_payment::CurrencyAdapter; // Frontier use fp_evm::weight_per_gas; use fp_rpc::TransactionStatus; -use pallet_ethereum::{Call::transact, Transaction as EthereumTransaction}; +use pallet_ethereum::{Call::transact, PostLogContent, Transaction as EthereumTransaction}; use pallet_evm::{ Account as EVMAccount, EnsureAddressTruncated, FeeCalculator, HashedAddressMapping, Runner, }; @@ -345,10 +345,14 @@ impl pallet_evm::Config for Runtime { type FindAuthor = FindAuthorTruncated; } +parameter_types! { + pub const PostBlockAndTxnHashes: PostLogContent = PostLogContent::BlockAndTxnHashes; +} + impl pallet_ethereum::Config for Runtime { type RuntimeEvent = RuntimeEvent; type StateRoot = pallet_ethereum::IntermediateStateRoot; - type PostLogContent = pallet_ethereum::PostBlockAndTxnHashes; + type PostLogContent = PostBlockAndTxnHashes; } parameter_types! { From bef8c1b478a48472fb76fc69c71eec89ed172785 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 7 Mar 2023 15:37:45 +0800 Subject: [PATCH 3/3] fix on_finalize --- frame/ethereum/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/ethereum/src/lib.rs b/frame/ethereum/src/lib.rs index 7c4168ec33..3a474bfdf5 100644 --- a/frame/ethereum/src/lib.rs +++ b/frame/ethereum/src/lib.rs @@ -202,7 +202,7 @@ pub mod pallet { >::store_block( match fp_consensus::find_pre_log(&frame_system::Pallet::::digest()) { Ok(_) => None, - Err(_) => Some(PostLogContent::BlockAndTxnHashes), + Err(_) => Some(T::PostLogContent::get()), }, U256::from(UniqueSaturatedInto::::unique_saturated_into( frame_system::Pallet::::block_number(),