Skip to content

Commit

Permalink
fix: re-introduce HeaderResponse trait (#1627)
Browse files Browse the repository at this point in the history
* fix: re-introduce HeaderResponse trait

* fmt
  • Loading branch information
klkvr authored Nov 6, 2024
1 parent 394b569 commit 0c4c5f5
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 5 deletions.
87 changes: 87 additions & 0 deletions crates/consensus/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,93 @@ impl BlockHeader for Header {
}
}

#[cfg(feature = "serde")]
impl<T: BlockHeader> BlockHeader for alloy_serde::WithOtherFields<T> {
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<B256> {
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<B256> {
self.inner.mix_hash()
}

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

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

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

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

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

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

/// Bincode-compatibl [`Header`] serde implementation.
#[cfg(all(feature = "serde", feature = "serde-bincode-compat"))]
pub(crate) mod serde_bincode_compat {
Expand Down
2 changes: 1 addition & 1 deletion crates/network-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
14 changes: 13 additions & 1 deletion crates/network-primitives/src/traits.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -237,3 +243,9 @@ impl<T: BlockResponse> BlockResponse for WithOtherFields<T> {
Some(&self.other)
}
}

impl<T: HeaderResponse> HeaderResponse for WithOtherFields<T> {
fn hash(&self) -> BlockHash {
self.inner.hash()
}
}
3 changes: 2 additions & 1 deletion crates/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self::Header>;
type HeaderResponse: RpcObject + HeaderResponse + AsRef<Self::Header>;

/// The JSON body of a block response.
type BlockResponse: RpcObject
Expand Down
98 changes: 96 additions & 2 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,6 +172,98 @@ impl<H: BlockHeader> Header<H> {
}
}

impl<H: BlockHeader> BlockHeader for Header<H> {
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<B256> {
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<B256> {
self.inner.mix_hash()
}

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

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

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

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

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

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

impl<H: BlockHeader> HeaderResponse for Header<H> {
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 {
Expand Down

0 comments on commit 0c4c5f5

Please sign in to comment.