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: provide a parameter for the runtime config to choose what to include in the PostLog #1014

Merged
merged 3 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
54 changes: 45 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(PostLogContent::BlockAndTxnHashes),
koushiro marked this conversation as resolved.
Show resolved Hide resolved
},
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 Expand Up @@ -872,6 +894,20 @@ impl<T: Config> Get<H256> for IntermediateStateRoot<T> {
}
}

pub struct PostBlockAndTxnHashes;
koushiro marked this conversation as resolved.
Show resolved Hide resolved
impl Get<PostLogContent> for PostBlockAndTxnHashes {
fn get() -> PostLogContent {
PostLogContent::BlockAndTxnHashes
}
}

pub struct OnlyPostBlockHash;
impl Get<PostLogContent> for OnlyPostBlockHash {
fn get() -> PostLogContent {
PostLogContent::OnlyBlockHash
}
}

/// Returns the Ethereum block hash by number.
pub struct EthereumBlockHashMapping<T>(PhantomData<T>);
impl<T: Config> BlockHashMapping for EthereumBlockHashMapping<T> {
Expand Down
1 change: 1 addition & 0 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ impl pallet_evm::Config for Runtime {
impl pallet_ethereum::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type StateRoot = pallet_ethereum::IntermediateStateRoot<Self>;
type PostLogContent = pallet_ethereum::PostBlockAndTxnHashes;
}

parameter_types! {
Expand Down