From d30278adc300249a00569f22aeebeb3a40cf3eda Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Tue, 17 Sep 2024 23:24:10 +0200 Subject: [PATCH] consensus: add BlockHeader getter trait --- crates/consensus/src/header.rs | 152 +++++++++++++++++++++++++++++++++ crates/consensus/src/lib.rs | 2 +- crates/network/src/lib.rs | 4 +- 3 files changed, 155 insertions(+), 3 deletions(-) diff --git a/crates/consensus/src/header.rs b/crates/consensus/src/header.rs index 3109d9044e3..a234a158036 100644 --- a/crates/consensus/src/header.rs +++ b/crates/consensus/src/header.rs @@ -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; + + /// 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; + + /// Retrieves the blob gas used by the block, if available + fn blob_gas_used(&self) -> Option; + + /// Retrieves the excess blob gas of the block, if available + fn excess_blob_gas(&self) -> Option; + + /// Retrieves the parent beacon block root of the block, if available + fn parent_beacon_block_root(&self) -> Option; + + /// Retrieves the requests root of the block, if available + fn requests_root(&self) -> Option; + + /// 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 { + 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 { + self.base_fee_per_gas + } + + fn blob_gas_used(&self) -> Option { + self.blob_gas_used + } + + fn excess_blob_gas(&self) -> Option { + self.excess_blob_gas + } + + fn parent_beacon_block_root(&self) -> Option { + self.parent_beacon_block_root + } + + fn requests_root(&self) -> Option { + self.requests_root + } + + fn extra_data(&self) -> &Bytes { + &self.extra_data + } +} + #[cfg(all(test, feature = "serde"))] mod tests { use super::*; diff --git a/crates/consensus/src/lib.rs b/crates/consensus/src/lib.rs index 65ec35a908f..b2a09c2c31a 100644 --- a/crates/consensus/src/lib.rs +++ b/crates/consensus/src/lib.rs @@ -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::{ diff --git a/crates/network/src/lib.rs b/crates/network/src/lib.rs index 1ae519fdef0..6822654445e 100644 --- a/crates/network/src/lib.rs +++ b/crates/network/src/lib.rs @@ -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}; @@ -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 --