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 all commits
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
12 changes: 11 additions & 1 deletion crates/optimism/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ reth-codecs = { workspace = true, features = ["test-utils"] }
rstest.workspace = true

[features]
default = ["reth-codec"]
default = ["std", "reth-codec"]
std = [
"reth-primitives-traits/std",
"reth-primitives/std",
"reth-node-types/std",
"reth-codecs/std",
"alloy-consensus/std",
"alloy-eips/std",
"alloy-primitives/std",
"serde/std",
]
reth-codec = [
"dep:reth-codecs",
"reth-primitives/reth-codec"
Expand Down
1 change: 1 addition & 0 deletions crates/optimism/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod bedrock;
pub mod tx_type;
Expand Down
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>;
}
123 changes: 121 additions & 2 deletions crates/primitives-traits/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
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, FullBlockHeader, Header, InMemorySize, MaybeSerde};

/// Helper trait that unifies all behaviour required by block to support full node operations.
pub trait FullBlock: Block<Header: Compact> {}
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()
}
}
7 changes: 5 additions & 2 deletions crates/primitives-traits/src/header/sealed.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use crate::InMemorySize;
pub use alloy_consensus::Header;

use core::mem;

use alloy_consensus::Sealed;
use alloy_eips::BlockNumHash;
use alloy_primitives::{keccak256, BlockHash, Sealable, B256};
use alloy_rlp::{Decodable, Encodable};
use bytes::BufMut;
use core::mem;
use derive_more::{AsRef, Deref};
use reth_codecs::add_arbitrary_tests;
use serde::{Deserialize, Serialize};

use crate::InMemorySize;

/// A helper struct to store the block number/hash and its parent hash.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct BlockWithParent {
Expand Down
10 changes: 7 additions & 3 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 as _};
}

/// 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 All @@ -60,7 +64,7 @@ pub use tx_type::{FullTxType, TxType};
pub mod header;
#[cfg(any(test, feature = "arbitrary", feature = "test-utils"))]
pub use header::test_utils;
pub use header::{BlockWithParent, Header, HeaderError, SealedHeader};
pub use header::{BlockWithParent, HeaderError, SealedHeader};

/// Bincode-compatible serde implementations for common abstracted types in Reth.
///
Expand Down
Loading