diff --git a/crates/consensus/src/block/header.rs b/crates/consensus/src/block/header.rs index 0a94dbdd1ac..9f95d30f100 100644 --- a/crates/consensus/src/block/header.rs +++ b/crates/consensus/src/block/header.rs @@ -720,6 +720,93 @@ impl BlockHeader for Header { } } +#[cfg(feature = "serde")] +impl BlockHeader for alloy_serde::WithOtherFields { + fn parent_hash(&self) -> B256 { + self.inner.parent_hash() + } + + fn ommers_hash(&self) -> B256 { + self.inner.ommers_hash() + } + + fn beneficiary(&self) -> Address { + self.inner.beneficiary() + } + + fn state_root(&self) -> B256 { + self.inner.state_root() + } + + fn transactions_root(&self) -> B256 { + self.inner.transactions_root() + } + + fn receipts_root(&self) -> B256 { + self.inner.receipts_root() + } + + fn withdrawals_root(&self) -> Option { + self.inner.withdrawals_root() + } + + fn logs_bloom(&self) -> Bloom { + self.inner.logs_bloom() + } + + fn difficulty(&self) -> U256 { + self.inner.difficulty() + } + + fn number(&self) -> u64 { + self.inner.number() + } + + fn gas_limit(&self) -> u64 { + self.inner.gas_limit() + } + + fn gas_used(&self) -> u64 { + self.inner.gas_used() + } + + fn timestamp(&self) -> u64 { + self.inner.timestamp() + } + + fn extra_data(&self) -> &Bytes { + self.inner.extra_data() + } + + fn mix_hash(&self) -> Option { + self.inner.mix_hash() + } + + fn nonce(&self) -> Option { + self.inner.nonce() + } + + fn base_fee_per_gas(&self) -> Option { + self.inner.base_fee_per_gas() + } + + fn blob_gas_used(&self) -> Option { + self.inner.blob_gas_used() + } + + fn excess_blob_gas(&self) -> Option { + self.inner.excess_blob_gas() + } + + fn parent_beacon_block_root(&self) -> Option { + self.inner.parent_beacon_block_root() + } + + fn requests_hash(&self) -> Option { + self.inner.requests_hash() + } +} + /// Bincode-compatibl [`Header`] serde implementation. #[cfg(all(feature = "serde", feature = "serde-bincode-compat"))] pub(crate) mod serde_bincode_compat { diff --git a/crates/network-primitives/src/lib.rs b/crates/network-primitives/src/lib.rs index a6303490722..81f234f1db8 100644 --- a/crates/network-primitives/src/lib.rs +++ b/crates/network-primitives/src/lib.rs @@ -10,7 +10,7 @@ extern crate alloc; mod traits; -pub use traits::{BlockResponse, ReceiptResponse, TransactionResponse}; +pub use traits::{BlockResponse, HeaderResponse, ReceiptResponse, TransactionResponse}; mod block; pub use block::{BlockTransactionHashes, BlockTransactions, BlockTransactionsKind}; diff --git a/crates/network-primitives/src/traits.rs b/crates/network-primitives/src/traits.rs index 53b42fe2262..bebfa841b44 100644 --- a/crates/network-primitives/src/traits.rs +++ b/crates/network-primitives/src/traits.rs @@ -1,4 +1,4 @@ -use alloy_consensus::Transaction; +use alloy_consensus::{BlockHeader, Transaction}; use alloy_eips::eip7702::SignedAuthorization; use alloy_primitives::{Address, BlockHash, TxHash, B256}; use alloy_serde::WithOtherFields; @@ -111,6 +111,12 @@ pub trait TransactionResponse: Transaction { } } +/// Header JSON-RPC response. +pub trait HeaderResponse: BlockHeader { + /// Block hash + fn hash(&self) -> BlockHash; +} + /// Block JSON-RPC response. pub trait BlockResponse { /// Header type @@ -237,3 +243,9 @@ impl BlockResponse for WithOtherFields { Some(&self.other) } } + +impl HeaderResponse for WithOtherFields { + fn hash(&self) -> BlockHash { + self.inner.hash() + } +} diff --git a/crates/network/src/lib.rs b/crates/network/src/lib.rs index 9a5c09be81c..f034d950b81 100644 --- a/crates/network/src/lib.rs +++ b/crates/network/src/lib.rs @@ -9,6 +9,7 @@ use alloy_consensus::{BlockHeader, TxReceipt}; use alloy_eips::eip2718::{Eip2718Envelope, Eip2718Error}; use alloy_json_rpc::RpcObject; +use alloy_network_primitives::HeaderResponse; use core::fmt::{Debug, Display}; mod transaction; @@ -90,7 +91,7 @@ pub trait Network: Debug + Clone + Copy + Sized + Send + Sync + 'static { type ReceiptResponse: RpcObject + ReceiptResponse; /// The JSON body of a header response. - type HeaderResponse: RpcObject + AsRef; + type HeaderResponse: RpcObject + HeaderResponse + AsRef; /// The JSON body of a block response. type BlockResponse: RpcObject diff --git a/crates/rpc-types-eth/src/block.rs b/crates/rpc-types-eth/src/block.rs index de29189b7bc..94f5b18e0ac 100644 --- a/crates/rpc-types-eth/src/block.rs +++ b/crates/rpc-types-eth/src/block.rs @@ -10,8 +10,10 @@ pub use alloy_eips::{ calc_blob_gasprice, calc_excess_blob_gas, BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, RpcBlockHash, }; -use alloy_network_primitives::{BlockResponse, BlockTransactions, TransactionResponse}; -use alloy_primitives::{Address, BlockHash, Bytes, Sealable, B256, U256}; +use alloy_network_primitives::{ + BlockResponse, BlockTransactions, HeaderResponse, TransactionResponse, +}; +use alloy_primitives::{Address, BlockHash, Bloom, Bytes, Sealable, B256, B64, U256}; use alloy_rlp::Encodable; /// Block representation @@ -170,6 +172,98 @@ impl Header { } } +impl BlockHeader for Header { + fn parent_hash(&self) -> B256 { + self.inner.parent_hash() + } + + fn ommers_hash(&self) -> B256 { + self.inner.ommers_hash() + } + + fn beneficiary(&self) -> Address { + self.inner.beneficiary() + } + + fn state_root(&self) -> B256 { + self.inner.state_root() + } + + fn transactions_root(&self) -> B256 { + self.inner.transactions_root() + } + + fn receipts_root(&self) -> B256 { + self.inner.receipts_root() + } + + fn withdrawals_root(&self) -> Option { + self.inner.withdrawals_root() + } + + fn logs_bloom(&self) -> Bloom { + self.inner.logs_bloom() + } + + fn difficulty(&self) -> U256 { + self.inner.difficulty() + } + + fn number(&self) -> u64 { + self.inner.number() + } + + fn gas_limit(&self) -> u64 { + self.inner.gas_limit() + } + + fn gas_used(&self) -> u64 { + self.inner.gas_used() + } + + fn timestamp(&self) -> u64 { + self.inner.timestamp() + } + + fn extra_data(&self) -> &Bytes { + self.inner.extra_data() + } + + fn mix_hash(&self) -> Option { + self.inner.mix_hash() + } + + fn nonce(&self) -> Option { + self.inner.nonce() + } + + fn base_fee_per_gas(&self) -> Option { + self.inner.base_fee_per_gas() + } + + fn blob_gas_used(&self) -> Option { + self.inner.blob_gas_used() + } + + fn excess_blob_gas(&self) -> Option { + self.inner.excess_blob_gas() + } + + fn parent_beacon_block_root(&self) -> Option { + self.inner.parent_beacon_block_root() + } + + fn requests_hash(&self) -> Option { + self.inner.requests_hash() + } +} + +impl HeaderResponse for Header { + fn hash(&self) -> BlockHash { + self.hash + } +} + /// Error that can occur when converting other types to blocks #[derive(Clone, Copy, Debug, derive_more::Display)] pub enum BlockError {