Skip to content

Commit

Permalink
consensus: add BlockHeader getter trait (#1302)
Browse files Browse the repository at this point in the history
consensus: add BlockHeader getter trait
  • Loading branch information
tcoratger authored Sep 23, 2024
1 parent 789aee8 commit b021936
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 3 deletions.
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 {
/// 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

0 comments on commit b021936

Please sign in to comment.