Skip to content

Commit

Permalink
feat: provide a parameter for the runtime config to choose what to in…
Browse files Browse the repository at this point in the history
…clude in the PostLog (#1014)

* feat: provide a parameter for the runtime config to choose what to include in the PostLog

* use parameter_types macro

* fix on_finalize
  • Loading branch information
koushiro authored Mar 7, 2023
1 parent 0535842 commit 98aacf6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
40 changes: 31 additions & 9 deletions frame/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ where
}
}

#[derive(Copy, Clone, Eq, PartialEq, Default)]
pub enum PostLogContent {
#[default]
BlockAndTxnHashes,
OnlyBlockHash,
}

pub use self::pallet::*;

#[frame_support::pallet]
Expand All @@ -185,13 +192,18 @@ pub mod pallet {
type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// How Ethereum state root is calculated.
type StateRoot: Get<H256>;
/// What's included in the PostLog.
type PostLogContent: Get<PostLogContent>;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_finalize(n: T::BlockNumber) {
<Pallet<T>>::store_block(
fp_consensus::find_pre_log(&frame_system::Pallet::<T>::digest()).is_err(),
match fp_consensus::find_pre_log(&frame_system::Pallet::<T>::digest()) {
Ok(_) => None,
Err(_) => Some(T::PostLogContent::get()),
},
U256::from(UniqueSaturatedInto::<u128>::unique_saturated_into(
frame_system::Pallet::<T>::block_number(),
)),
Expand Down Expand Up @@ -332,7 +344,7 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
<Pallet<T>>::store_block(false, U256::zero());
<Pallet<T>>::store_block(None, U256::zero());
frame_support::storage::unhashed::put::<EthereumStorageSchema>(
PALLET_ETHEREUM_SCHEMA,
&EthereumStorageSchema::V3,
Expand Down Expand Up @@ -375,7 +387,7 @@ impl<T: Config> Pallet<T> {
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<PostLogContent>, block_number: U256) {
let mut transactions = Vec::new();
let mut statuses = Vec::new();
let mut receipts = Vec::new();
Expand Down Expand Up @@ -426,12 +438,22 @@ impl<T: Config> Pallet<T> {
CurrentTransactionStatuses::<T>::put(statuses.clone());
BlockHash::<T>::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::<T>::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::<T>::deposit_log(digest);
}
Some(PostLogContent::OnlyBlockHash) => {
let digest = DigestItem::Consensus(
FRONTIER_ENGINE_ID,
PostLog::BlockHash(block.header.hash()).encode(),
);
frame_system::Pallet::<T>::deposit_log(digest);
}
None => { /* do nothing*/ }
}
}

Expand Down
6 changes: 5 additions & 1 deletion frame/ethereum/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ parameter_types! {
}

pub struct HashedAddressMapping;

impl AddressMapping<AccountId32> for HashedAddressMapping {
fn into_account_id(address: H160) -> AccountId32 {
let mut data = [0u8; 32];
Expand Down Expand Up @@ -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<Self>;
type PostLogContent = PostBlockAndTxnHashes;
}

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down
7 changes: 6 additions & 1 deletion template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -345,9 +345,14 @@ impl pallet_evm::Config for Runtime {
type FindAuthor = FindAuthorTruncated<Aura>;
}

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

impl pallet_ethereum::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type StateRoot = pallet_ethereum::IntermediateStateRoot<Self>;
type PostLogContent = PostBlockAndTxnHashes;
}

parameter_types! {
Expand Down

0 comments on commit 98aacf6

Please sign in to comment.