From ab284eba685df3fe03ab866b6991969ecf5dc54c Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Thu, 14 Nov 2024 18:22:40 +0100 Subject: [PATCH] Impl Header for all Block types --- crates/primitives-traits/src/block/header.rs | 54 +++++++++- crates/primitives-traits/src/block/mod.rs | 101 ++++++++++++++++++- crates/primitives-traits/src/lib.rs | 2 +- crates/primitives/src/block.rs | 88 +--------------- crates/primitives/src/traits/mod.rs | 9 -- 5 files changed, 153 insertions(+), 101 deletions(-) delete mode 100644 crates/primitives/src/traits/mod.rs diff --git a/crates/primitives-traits/src/block/header.rs b/crates/primitives-traits/src/block/header.rs index 7ab76f249873..eeef177f9f20 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; @@ -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 @@ -49,3 +51,53 @@ impl 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; + /// 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 f19207930ea1..a6867e204c6d 100644 --- a/crates/primitives-traits/src/block/mod.rs +++ b/crates/primitives-traits/src/block/mod.rs @@ -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 + Compact {} @@ -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, ::Transaction, @@ -53,6 +54,100 @@ pub trait Block: fn body(&self) -> &Self::Body; } +impl 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 { + 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 { + self.header().mix_hash() + } + + fn nonce(&self) -> Option { + self.header().nonce() + } + + fn base_fee_per_gas(&self) -> Option { + self.header().base_fee_per_gas() + } + + fn blob_gas_used(&self) -> Option { + self.header().blob_gas_used() + } + + fn excess_blob_gas(&self) -> Option { + self.header().excess_blob_gas() + } + + fn parent_beacon_block_root(&self) -> Option { + self.header().parent_beacon_block_root() + } + + fn requests_hash(&self) -> Option { + self.header().requests_hash() + } + + fn extra_data(&self) -> &Bytes { + self.header().extra_data() + } + + fn next_block_excess_blob_gas(&self) -> Option { + self.header().next_block_excess_blob_gas() + } + + fn next_block_blob_fee(&self) -> Option { + self.header().next_block_blob_fee() + } +} + impl Body::Transaction, ::Withdrawals> for T diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index 31bb59a2875b..91273f3954d9 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -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, }; diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index af956d02a29c..0789e20d40ab 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -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"))] @@ -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 { - 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 { - self.header.mix_hash() - } - - fn nonce(&self) -> Option { - self.header.nonce() - } - - fn base_fee_per_gas(&self) -> Option { - self.header.base_fee_per_gas() - } - - fn blob_gas_used(&self) -> Option { - self.header.blob_gas_used() - } - - fn excess_blob_gas(&self) -> Option { - self.header.excess_blob_gas() - } - - fn parent_beacon_block_root(&self) -> Option { - self.header.parent_beacon_block_root() - } - - fn requests_hash(&self) -> Option { - 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] diff --git a/crates/primitives/src/traits/mod.rs b/crates/primitives/src/traits/mod.rs deleted file mode 100644 index 49fb73ea5555..000000000000 --- a/crates/primitives/src/traits/mod.rs +++ /dev/null @@ -1,9 +0,0 @@ -//! Abstractions of primitive data types - -pub mod block; -pub mod transaction; - -pub use block::{body::BlockBody, Block}; -pub use transaction::signed::SignedTransaction; - -pub use alloy_consensus::BlockHeader;