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

consensus: add BlockHeader getter trait #1302

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
152 changes: 152 additions & 0 deletions crates/consensus/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,158 @@ impl<'a> arbitrary::Arbitrary<'a> for Header {
}
}

/// Trait for extracting specific Ethereum block data from a header
pub trait BlockHeader {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from reth pov there's an argument for limiting these functions to the absolute necessary fields, or introduce sub traits, for example for evm you only need certain fields.
but since this doesn't clash with rpc, I think all of those are fine, and we could still do sub traits

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you think we should split in subtraits now or if we should do that in a follow up if needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do in a followup if needed, realistically we only need this trait for one type really, the chain's header type and for op we can even reuse the L1 type

/// Retrieves the parent hash of the block
fn parent_hash(&self) -> B256;

/// Retrieves the ommers hash of the block
fn ommers_hash(&self) -> B256;

/// Retrieves the beneficiary (miner) of the block
fn beneficiary(&self) -> Address;

/// Retrieves the state root hash of the block
fn state_root(&self) -> B256;

/// Retrieves the transactions root hash of the block
fn transactions_root(&self) -> B256;

/// Retrieves the receipts root hash of the block
fn receipts_root(&self) -> B256;

/// Retrieves the withdrawals root hash of the block, if available
fn withdrawals_root(&self) -> Option<B256>;

/// Retrieves the logs bloom filter of the block
fn logs_bloom(&self) -> Bloom;

/// Retrieves the difficulty of the block
fn difficulty(&self) -> U256;

/// Retrieves the block number
fn number(&self) -> BlockNumber;

/// Retrieves the gas limit of the block
fn gas_limit(&self) -> u128;

/// Retrieves the gas used by the block
fn gas_used(&self) -> u128;

/// Retrieves the timestamp of the block
fn timestamp(&self) -> u64;

/// Retrieves the mix hash of the block
fn mix_hash(&self) -> B256;

/// Retrieves the nonce of the block
fn nonce(&self) -> B64;

/// Retrieves the base fee per gas of the block, if available
fn base_fee_per_gas(&self) -> Option<u128>;

/// Retrieves the blob gas used by the block, if available
fn blob_gas_used(&self) -> Option<u128>;

/// Retrieves the excess blob gas of the block, if available
fn excess_blob_gas(&self) -> Option<u128>;

/// Retrieves the parent beacon block root of the block, if available
fn parent_beacon_block_root(&self) -> Option<B256>;

/// Retrieves the requests root of the block, if available
fn requests_root(&self) -> Option<B256>;

/// Retrieves the block's extra data field
fn extra_data(&self) -> &Bytes;
}

impl BlockHeader for Header {
fn parent_hash(&self) -> B256 {
self.parent_hash
}

fn ommers_hash(&self) -> B256 {
self.ommers_hash
}

fn beneficiary(&self) -> Address {
self.beneficiary
}

fn state_root(&self) -> B256 {
self.state_root
}

fn transactions_root(&self) -> B256 {
self.transactions_root
}

fn receipts_root(&self) -> B256 {
self.receipts_root
}

fn withdrawals_root(&self) -> Option<B256> {
self.withdrawals_root
}

fn logs_bloom(&self) -> Bloom {
self.logs_bloom
}

fn difficulty(&self) -> U256 {
self.difficulty
}

fn number(&self) -> BlockNumber {
self.number
}

fn gas_limit(&self) -> u128 {
self.gas_limit
}

fn gas_used(&self) -> u128 {
self.gas_used
}

fn timestamp(&self) -> u64 {
self.timestamp
}

fn mix_hash(&self) -> B256 {
self.mix_hash
}

fn nonce(&self) -> B64 {
self.nonce
}

fn base_fee_per_gas(&self) -> Option<u128> {
self.base_fee_per_gas
}

fn blob_gas_used(&self) -> Option<u128> {
self.blob_gas_used
}

fn excess_blob_gas(&self) -> Option<u128> {
self.excess_blob_gas
}

fn parent_beacon_block_root(&self) -> Option<B256> {
self.parent_beacon_block_root
}

fn requests_root(&self) -> Option<B256> {
self.requests_root
}

fn extra_data(&self) -> &Bytes {
&self.extra_data
}
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod encodable_signature;
pub use encodable_signature::EncodableSignature;

mod header;
pub use header::{Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};
pub use header::{BlockHeader, Header, EMPTY_OMMER_ROOT_HASH, EMPTY_ROOT_HASH};

mod receipt;
pub use receipt::{
Expand Down
4 changes: 2 additions & 2 deletions crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy_consensus::TxReceipt;
use alloy_consensus::{BlockHeader, TxReceipt};
use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error};
use alloy_json_rpc::RpcObject;
use core::fmt::{Debug, Display};
Expand Down Expand Up @@ -66,7 +66,7 @@ pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static {
type ReceiptEnvelope: Eip2718Envelope + TxReceipt;

/// The network header type.
type Header;
type Header: BlockHeader;

// -- JSON RPC types --

Expand Down