Skip to content

Commit

Permalink
Impl Header for all Block types
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane committed Nov 14, 2024
1 parent cac0655 commit ab284eb
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 101 deletions.
54 changes: 53 additions & 1 deletion crates/primitives-traits/src/block/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use core::fmt;

use alloy_primitives::Sealable;
use alloy_primitives::{Address, BlockNumber, Bloom, Bytes, Sealable, B256, B64, U256};
use reth_codecs::Compact;

use crate::InMemorySize;
Expand All @@ -23,6 +23,8 @@ pub trait BlockHeader:
+ fmt::Debug
+ PartialEq
+ Eq
+ serde::Serialize
+ for<'de> serde::Deserialize<'de>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ alloy_consensus::BlockHeader
Expand All @@ -49,3 +51,53 @@ impl<T> BlockHeader for T where
+ InMemorySize
{
}

/// Helper trait to implement [`BlockHeader`] functionality for all [`Block`](crate::Block) types.
pub trait Header {
/// See [`alloy_consensus::BlockHeader`].
fn parent_hash(&self) -> B256;
/// See [`alloy_consensus::BlockHeader`].
fn ommers_hash(&self) -> B256;
/// See [`alloy_consensus::BlockHeader`].
fn beneficiary(&self) -> Address;
/// See [`alloy_consensus::BlockHeader`].
fn state_root(&self) -> B256;
/// See [`alloy_consensus::BlockHeader`].
fn transactions_root(&self) -> B256;
/// See [`alloy_consensus::BlockHeader`].
fn receipts_root(&self) -> B256;
/// See [`alloy_consensus::BlockHeader`].
fn withdrawals_root(&self) -> Option<B256>;
/// See [`alloy_consensus::BlockHeader`].
fn logs_bloom(&self) -> Bloom;
/// See [`alloy_consensus::BlockHeader`].
fn difficulty(&self) -> U256;
/// See [`alloy_consensus::BlockHeader`].
fn number(&self) -> BlockNumber;
/// See [`alloy_consensus::BlockHeader`].
fn gas_limit(&self) -> u64;
/// See [`alloy_consensus::BlockHeader`].
fn gas_used(&self) -> u64;
/// See [`alloy_consensus::BlockHeader`].
fn timestamp(&self) -> u64;
/// See [`alloy_consensus::BlockHeader`].
fn mix_hash(&self) -> Option<B256>;
/// See [`alloy_consensus::BlockHeader`].
fn nonce(&self) -> Option<B64>;
/// See [`alloy_consensus::BlockHeader`].
fn base_fee_per_gas(&self) -> Option<u64>;
/// See [`alloy_consensus::BlockHeader`].
fn blob_gas_used(&self) -> Option<u64>;
/// See [`alloy_consensus::BlockHeader`].
fn excess_blob_gas(&self) -> Option<u64>;
/// See [`alloy_consensus::BlockHeader`].
fn parent_beacon_block_root(&self) -> Option<B256>;
/// See [`alloy_consensus::BlockHeader`].
fn requests_hash(&self) -> Option<B256>;
/// See [`alloy_consensus::BlockHeader`].
fn extra_data(&self) -> &Bytes;
/// See [`alloy_consensus::BlockHeader`].
fn next_block_excess_blob_gas(&self) -> Option<u64>;
/// See [`alloy_consensus::BlockHeader`].
fn next_block_blob_fee(&self) -> Option<u128>;
}
101 changes: 98 additions & 3 deletions crates/primitives-traits/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ pub mod header;

use alloc::fmt;

use alloy_consensus::BlockHeader as _;
use alloy_eips::eip7685::Requests;
use alloy_primitives::{Address, B256};
use alloy_primitives::{Address, BlockNumber, Bloom, Bytes, B256, B64, U256};
use reth_codecs::Compact;

use crate::{BlockBody, BlockHeader, Body, FullBlockBody, FullBlockHeader, InMemorySize};
use crate::{BlockBody, BlockHeader, Body, FullBlockBody, FullBlockHeader, Header, InMemorySize};

/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: FullBlockHeader, Body: FullBlockBody> + Compact {}
Expand All @@ -33,7 +34,7 @@ pub trait Block:
+ for<'a> serde::Deserialize<'a>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ alloy_consensus::BlockHeader
+ Header
+ Body<
Self::Header,
<Self::Body as BlockBody>::Transaction,
Expand All @@ -53,6 +54,100 @@ pub trait Block:
fn body(&self) -> &Self::Body;
}

impl<T: Block> Header for T {
fn parent_hash(&self) -> B256 {
self.header().parent_hash()
}

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

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

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

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

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

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

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

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

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

fn gas_limit(&self) -> u64 {
self.header().gas_limit()
}

fn gas_used(&self) -> u64 {
self.header().gas_used()
}

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

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

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

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

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

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

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

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

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

fn next_block_excess_blob_gas(&self) -> Option<u64> {
self.header().next_block_excess_blob_gas()
}

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

impl<T: Block>
Body<T::Header, <T::Body as BlockBody>::Transaction, <T::Body as BlockBody>::Withdrawals>
for T
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use integer_list::{IntegerList, IntegerListError};
pub mod block;
pub use block::{
body::{BlockBody, Body, FullBlockBody},
header::{BlockHeader, FullBlockHeader},
header::{BlockHeader, FullBlockHeader, Header},
Block, FullBlock,
};

Expand Down
88 changes: 1 addition & 87 deletions crates/primitives/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{GotExpected, SealedHeader, TransactionSigned, TransactionSignedEcRec
use alloc::vec::Vec;
use alloy_consensus::Header;
use alloy_eips::{eip2718::Encodable2718, eip4895::Withdrawals, eip7685::Requests};
use alloy_primitives::{Address, BlockNumber, Bloom, Bytes, B256, B64, U256};
use alloy_primitives::{Address, Bytes, B256};
use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
use derive_more::{Deref, DerefMut};
#[cfg(any(test, feature = "arbitrary"))]
Expand Down Expand Up @@ -99,92 +99,6 @@ impl reth_primitives_traits::Block for Block {
}
}

impl alloy_consensus::BlockHeader for Block {
fn parent_hash(&self) -> B256 {
self.header.parent_hash()
}

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

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

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

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

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

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

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

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

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

fn gas_limit(&self) -> u64 {
self.header.gas_limit()
}

fn gas_used(&self) -> u64 {
self.header.gas_used()
}

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

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

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

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

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

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

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

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

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

impl InMemorySize for Block {
/// Calculates a heuristic for the in-memory size of the [`Block`].
#[inline]
Expand Down
9 changes: 0 additions & 9 deletions crates/primitives/src/traits/mod.rs

This file was deleted.

0 comments on commit ab284eb

Please sign in to comment.