diff --git a/crates/optimism/primitives/Cargo.toml b/crates/optimism/primitives/Cargo.toml index ade6d4eb6bc5..555d02d5691b 100644 --- a/crates/optimism/primitives/Cargo.toml +++ b/crates/optimism/primitives/Cargo.toml @@ -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" diff --git a/crates/optimism/primitives/src/lib.rs b/crates/optimism/primitives/src/lib.rs index 5f6b1848e648..0c9159c1fba6 100644 --- a/crates/optimism/primitives/src/lib.rs +++ b/crates/optimism/primitives/src/lib.rs @@ -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; diff --git a/crates/primitives-traits/src/block/header.rs b/crates/primitives-traits/src/block/header.rs index 524835879f31..f15a76500bb4 100644 --- a/crates/primitives-traits/src/block/header.rs +++ b/crates/primitives-traits/src/block/header.rs @@ -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}; @@ -49,3 +49,53 @@ impl BlockHeader for T where + MaybeSerde { } + +/// 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; + /// 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; + /// See [`alloy_consensus::BlockHeader`]. + fn nonce(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn base_fee_per_gas(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn blob_gas_used(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn excess_blob_gas(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn parent_beacon_block_root(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn requests_hash(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn extra_data(&self) -> &Bytes; + /// See [`alloy_consensus::BlockHeader`]. + fn next_block_excess_blob_gas(&self) -> Option; + /// See [`alloy_consensus::BlockHeader`]. + fn next_block_blob_fee(&self) -> Option; +} diff --git a/crates/primitives-traits/src/block/mod.rs b/crates/primitives-traits/src/block/mod.rs index 67658c39e07d..e5779885054d 100644 --- a/crates/primitives-traits/src/block/mod.rs +++ b/crates/primitives-traits/src/block/mod.rs @@ -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 {} @@ -34,3 +36,120 @@ pub trait Block: /// Returns reference to block body. fn body(&self) -> &Self::Body; } + +impl 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 { + 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 { + self.header().mix_hash() + } + + #[inline] + fn nonce(&self) -> Option { + self.header().nonce() + } + + #[inline] + fn base_fee_per_gas(&self) -> Option { + self.header().base_fee_per_gas() + } + + #[inline] + fn blob_gas_used(&self) -> Option { + self.header().blob_gas_used() + } + + #[inline] + fn excess_blob_gas(&self) -> Option { + self.header().excess_blob_gas() + } + + #[inline] + fn parent_beacon_block_root(&self) -> Option { + self.header().parent_beacon_block_root() + } + + #[inline] + fn requests_hash(&self) -> Option { + self.header().requests_hash() + } + + #[inline] + fn extra_data(&self) -> &Bytes { + self.header().extra_data() + } + + #[inline] + fn next_block_excess_blob_gas(&self) -> Option { + self.header().next_block_excess_blob_gas() + } + + #[inline] + fn next_block_blob_fee(&self) -> Option { + self.header().next_block_blob_fee() + } +} diff --git a/crates/primitives-traits/src/header/sealed.rs b/crates/primitives-traits/src/header/sealed.rs index d9931fc95c5b..b1b36c90dc8c 100644 --- a/crates/primitives-traits/src/header/sealed.rs +++ b/crates/primitives-traits/src/header/sealed.rs @@ -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 { diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index 819825d635f1..5c1115bd2356 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -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 @@ -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, }; @@ -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. ///