Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(sdk): blanket impl Header for Block types #12660

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 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, MaybeSerde};
Expand Down Expand Up @@ -49,3 +49,53 @@ impl<T> BlockHeader for T where
+ MaybeSerde
{
}

/// Helper trait to implement [`BlockHeader`] functionality for all [`Block`](crate::Block) types.
pub trait Header {
Copy link
Collaborator

@klkvr klkvr Nov 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this identical to alloy_consensus::Header?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes. not possible to impl foreign trait for generic T: Block.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this really simplifies things a lot I'd prefer those as Block trait methods with default implementations

although doing block.header().number() is not that bad imo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you default impl these, you lose the ability to inline them afaik. would inline all getters, ref alloy-rs/alloy#1642

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but sure, can have identical trait methods on Block

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole point though is for rollups to have to avoid implementing lots of boilerplate code.

there is a reason we avoided calling .header() everywhere in the original code I believe.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

introducing a new trait just for this feels like weird to me

/// 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>;
}
127 changes: 123 additions & 4 deletions crates/primitives-traits/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
pub mod body;
pub mod header;

use alloc::fmt;
use core::fmt;

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

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

/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: Compact> + Compact {}
pub trait FullBlock: Block<Header: FullBlockHeader, Body: FullBlockBody> + Compact {}

impl<T> FullBlock for T where T: Block<Header: FullBlockHeader> + Compact {}
impl<T> FullBlock for T where T: Block<Header: FullBlockHeader, Body: FullBlockBody> + Compact {}

/// Abstraction of block data type.
// todo: make sealable super-trait, depends on <https://github.com/paradigmxyz/reth/issues/11449>
Expand All @@ -34,3 +36,120 @@ pub trait Block:
/// Returns reference to block body.
fn body(&self) -> &Self::Body;
}

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

#[inline]
fn ommers_hash(&self) -> B256 {
self.header().ommers_hash()
}

#[inline]
fn beneficiary(&self) -> Address {
self.header().beneficiary()
}

#[inline]
fn state_root(&self) -> B256 {
self.header().state_root()
}

#[inline]
fn transactions_root(&self) -> B256 {
self.header().transactions_root()
}

#[inline]
fn receipts_root(&self) -> B256 {
self.header().receipts_root()
}

#[inline]
fn withdrawals_root(&self) -> Option<B256> {
self.header().withdrawals_root()
}

#[inline]
fn logs_bloom(&self) -> Bloom {
self.header().logs_bloom()
}

#[inline]
fn difficulty(&self) -> U256 {
self.header().difficulty()
}

#[inline]
fn number(&self) -> BlockNumber {
self.header().number()
}

#[inline]
fn gas_limit(&self) -> u64 {
self.header().gas_limit()
}

#[inline]
fn gas_used(&self) -> u64 {
self.header().gas_used()
}

#[inline]
fn timestamp(&self) -> u64 {
self.header().timestamp()
}

#[inline]
fn mix_hash(&self) -> Option<B256> {
self.header().mix_hash()
}

#[inline]
fn nonce(&self) -> Option<B64> {
self.header().nonce()
}

#[inline]
fn base_fee_per_gas(&self) -> Option<u64> {
self.header().base_fee_per_gas()
}

#[inline]
fn blob_gas_used(&self) -> Option<u64> {
self.header().blob_gas_used()
}

#[inline]
fn excess_blob_gas(&self) -> Option<u64> {
self.header().excess_blob_gas()
}

#[inline]
fn parent_beacon_block_root(&self) -> Option<B256> {
self.header().parent_beacon_block_root()
}

#[inline]
fn requests_hash(&self) -> Option<B256> {
self.header().requests_hash()
}

#[inline]
fn extra_data(&self) -> &Bytes {
self.header().extra_data()
}

#[inline]
fn next_block_excess_blob_gas(&self) -> Option<u64> {
self.header().next_block_excess_blob_gas()
}

#[inline]
fn next_block_blob_fee(&self) -> Option<u128> {
self.header().next_block_blob_fee()
}
}
8 changes: 6 additions & 2 deletions crates/primitives-traits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
#[macro_use]
extern crate alloc;

/// Helper traits for calling block header and body methods directly on block type.
pub mod block_prelude {
pub use crate::{Block, Header};
}

/// Common constants.
pub mod constants;

pub use constants::gas_units::{format_gas, format_gas_throughput};

/// Minimal account
Expand All @@ -37,7 +41,7 @@ pub use integer_list::{IntegerList, IntegerListError};
pub mod block;
pub use block::{
body::{BlockBody, FullBlockBody},
header::{BlockHeader, FullBlockHeader},
header::{BlockHeader, FullBlockHeader, Header},
Block, FullBlock,
};

Expand Down
Loading