From d0668fc29335c1f216c420719276c48df51befe8 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 4 Jan 2025 08:51:18 +0700 Subject: [PATCH 01/10] chore: make SealedBlock.header field private --- crates/evm/execution-types/src/chain.rs | 3 ++- crates/primitives/src/block.rs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index e207209a997a..96f0d37701a3 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -6,6 +6,7 @@ use alloy_consensus::BlockHeader; use alloy_eips::{eip1898::ForkBlock, eip2718::Encodable2718, BlockNumHash}; use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash}; use core::{fmt, ops::RangeInclusive}; +use std::ops::Deref; use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError}; use reth_primitives::{ transaction::SignedTransactionIntoRecoveredExt, RecoveredTx, SealedBlockFor, @@ -91,7 +92,7 @@ impl Chain { /// Returns an iterator over all headers in the block with increasing block numbers. pub fn headers(&self) -> impl Iterator> + '_ { - self.blocks.values().map(|block| block.header.clone()) + self.blocks.values().map(|block| block.deref().clone()) } /// Get cached trie updates for this chain. diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index acf76b1e591e..73a7ecb4de97 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -167,7 +167,7 @@ pub struct SealedBlock { /// Locked block header. #[deref] #[deref_mut] - pub header: SealedHeader, + header: SealedHeader, /// Block body. body: B, } From 93e21aabd1407d5305b98a08501e46f9d55ef670 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 4 Jan 2025 19:34:39 +0700 Subject: [PATCH 02/10] bet --- .../provider/src/providers/blockchain_provider.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 7c8648fe8451..b48377152fbc 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -818,6 +818,7 @@ mod tests { sync::Arc, time::Instant, }; +be use std::ops::Deref; const TEST_BLOCKS_COUNT: usize = 5; @@ -1564,8 +1565,8 @@ mod tests { Some(database_block.header.clone().unseal()) ); assert_eq!( - provider.sealed_header_by_number_or_tag(block_number.into()).unwrap(), - Some(database_block.header) + provider.sealed_header_by_number_or_tag(block_number.into())?, + Some(database_block.deref()) ); assert_eq!( @@ -1574,7 +1575,7 @@ mod tests { ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.header) + Some(canonical_block.deref()) ); assert_eq!( @@ -1583,7 +1584,7 @@ mod tests { ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.header) + Some(safe_block.deref()) ); assert_eq!( From 87dd29c99b6741bff1352fa66d80c3e2b064c7be Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 4 Jan 2025 20:00:25 +0700 Subject: [PATCH 03/10] bet --- crates/evm/execution-types/src/chain.rs | 2 +- crates/net/p2p/src/bodies/response.rs | 3 ++- crates/primitives-traits/src/header/sealed.rs | 7 +++++++ .../storage/provider/src/providers/blockchain_provider.rs | 8 ++++---- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index 96f0d37701a3..4ceef41fa8c0 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -92,7 +92,7 @@ impl Chain { /// Returns an iterator over all headers in the block with increasing block numbers. pub fn headers(&self) -> impl Iterator> + '_ { - self.blocks.values().map(|block| block.deref().clone()) + self.blocks.values().map(|block| block.as_sealed_header().clone()) } /// Get cached trie updates for this chain. diff --git a/crates/net/p2p/src/bodies/response.rs b/crates/net/p2p/src/bodies/response.rs index 956057d98bff..5f8ed5ca1c27 100644 --- a/crates/net/p2p/src/bodies/response.rs +++ b/crates/net/p2p/src/bodies/response.rs @@ -1,3 +1,4 @@ +use std::ops::Deref; use alloy_consensus::BlockHeader; use alloy_primitives::{BlockNumber, U256}; use reth_primitives::{BlockBody, SealedBlock, SealedHeader}; @@ -19,7 +20,7 @@ where /// Return the reference to the response header pub const fn header(&self) -> &SealedHeader { match self { - Self::Full(block) => &block.header, + Self::Full(block) => &block.as_sealed_header().clone(), Self::Empty(header) => header, } } diff --git a/crates/primitives-traits/src/header/sealed.rs b/crates/primitives-traits/src/header/sealed.rs index ef8b5fde5e91..d88d59f4a997 100644 --- a/crates/primitives-traits/src/header/sealed.rs +++ b/crates/primitives-traits/src/header/sealed.rs @@ -58,6 +58,13 @@ impl SealedHeader { pub fn split(self) -> (H, BlockHash) { (self.header, self.hash) } + + /// Returns a reference to self. + /// This is useful when you need to convert from a dereferenced header back to SealedHeader. + #[inline] + pub const fn as_sealed_header(&self) -> &SealedHeader { + self + } } impl SealedHeader { diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index b48377152fbc..e581dfe4949f 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -818,7 +818,7 @@ mod tests { sync::Arc, time::Instant, }; -be use std::ops::Deref; + use std::ops::Deref; const TEST_BLOCKS_COUNT: usize = 5; @@ -1566,7 +1566,7 @@ be use std::ops::Deref; ); assert_eq!( provider.sealed_header_by_number_or_tag(block_number.into())?, - Some(database_block.deref()) + Some(database_block.header.clone()) ); assert_eq!( @@ -1575,7 +1575,7 @@ be use std::ops::Deref; ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.deref()) + Some(canonical_block.header.clone()) ); assert_eq!( @@ -1584,7 +1584,7 @@ be use std::ops::Deref; ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.deref()) + Some(safe_block.header.clone()) ); assert_eq!( From 3ae156cf148f7bc855ca8130bc82998d523677bb Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 4 Jan 2025 21:12:26 +0700 Subject: [PATCH 04/10] bet --- crates/chain-state/src/in_memory.rs | 6 +- crates/net/downloaders/src/bodies/request.rs | 3 +- crates/net/downloaders/src/test_utils/mod.rs | 3 +- crates/net/p2p/src/bodies/response.rs | 19 +++++- .../src/providers/blockchain_provider.rs | 59 ++++++++++--------- .../provider/src/providers/consistent.rs | 12 ++-- .../src/providers/database/provider.rs | 18 +++--- .../storage/provider/src/test_utils/blocks.rs | 3 +- 8 files changed, 70 insertions(+), 53 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 06d228a8f82e..f55ae8f74aa8 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -228,8 +228,8 @@ impl CanonicalInMemoryState { } /// Returns the header corresponding to the given hash. - pub fn header_by_hash(&self, hash: B256) -> Option> { - self.state_by_hash(hash).map(|block| block.block_ref().block.header.clone()) + pub fn header_by_hash(&self, hash: B256) -> Option::Block>> { + self.state_by_hash(hash).map(|block| block.block_ref().block.deref().clone()) } /// Clears all entries in the in memory state. @@ -1321,7 +1321,7 @@ mod tests { assert_eq!(state.pending_header().unwrap(), block2.block().header().clone()); // Check the pending sealed header - assert_eq!(state.pending_sealed_header().unwrap(), block2.block().header.clone()); + assert_eq!(state.pending_sealed_header().unwrap(), block2.block().deref().clone()); // Check the pending block with senders assert_eq!( diff --git a/crates/net/downloaders/src/bodies/request.rs b/crates/net/downloaders/src/bodies/request.rs index a3ad1f3b9dc2..20e3e94e0026 100644 --- a/crates/net/downloaders/src/bodies/request.rs +++ b/crates/net/downloaders/src/bodies/request.rs @@ -18,6 +18,7 @@ use std::{ sync::Arc, task::{ready, Context, Poll}, }; +use std::ops::Deref; /// Body request implemented as a [Future]. /// @@ -194,7 +195,7 @@ where // Body is invalid, put the header back and return an error let hash = block.hash(); let number = block.number(); - self.pending_headers.push_front(block.header); + self.pending_headers.push_front(block.deref().clone()); return Err(DownloadError::BodyValidation { hash, number, diff --git a/crates/net/downloaders/src/test_utils/mod.rs b/crates/net/downloaders/src/test_utils/mod.rs index 0529b78a2b20..44f6f9315c32 100644 --- a/crates/net/downloaders/src/test_utils/mod.rs +++ b/crates/net/downloaders/src/test_utils/mod.rs @@ -8,6 +8,7 @@ use futures::SinkExt; use reth_primitives::{BlockBody, SealedHeader}; use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams}; use std::{collections::HashMap, io::SeekFrom, ops::RangeInclusive}; +use std::ops::Deref; use tokio::{fs::File, io::AsyncSeekExt}; use tokio_util::codec::FramedWrite; @@ -28,7 +29,7 @@ pub(crate) fn generate_bodies( BlockRangeParams { parent: Some(B256::ZERO), tx_count: 0..2, ..Default::default() }, ); - let headers = blocks.iter().map(|block| block.header.clone()).collect(); + let headers = blocks.iter().map(|block| block.deref().clone()).collect(); let bodies = blocks.into_iter().map(|block| (block.hash(), block.into_body())).collect(); (headers, bodies) diff --git a/crates/net/p2p/src/bodies/response.rs b/crates/net/p2p/src/bodies/response.rs index 5f8ed5ca1c27..6fb06dc2ca60 100644 --- a/crates/net/p2p/src/bodies/response.rs +++ b/crates/net/p2p/src/bodies/response.rs @@ -5,7 +5,7 @@ use reth_primitives::{BlockBody, SealedBlock, SealedHeader}; use reth_primitives_traits::InMemorySize; /// The block response -#[derive(PartialEq, Eq, Debug, Clone)] +#[derive(Debug, Clone)] pub enum BlockResponse { /// Full block response (with transactions or ommers) Full(SealedBlock), @@ -13,14 +13,27 @@ pub enum BlockResponse { Empty(SealedHeader), } +impl PartialEq for BlockResponse +where + SealedBlock: PartialEq, +{ + fn eq(&self, other: &Self) -> bool { + match (self, other) { + (Self::Full(a), Self::Full(b)) => a == b, + (Self::Empty(a), Self::Empty(b)) => a == b, + _ => false, + } + } +} + impl BlockResponse where H: BlockHeader, { /// Return the reference to the response header - pub const fn header(&self) -> &SealedHeader { + pub fn header(&self) -> &SealedHeader { match self { - Self::Full(block) => &block.as_sealed_header().clone(), + Self::Full(block) => &block.deref(), Self::Empty(header) => header, } } diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 11a393971429..e3124f26f5dc 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -953,9 +953,9 @@ mod tests { let finalized_block = blocks.get(block_count - 3).unwrap(); // Set the canonical head, safe, and finalized blocks - provider.set_canonical_head(canonical_block.header.clone()); - provider.set_safe(safe_block.header.clone()); - provider.set_finalized(finalized_block.header.clone()); + provider.set_canonical_head(canonical_block.as_sealed_header().clone()); + provider.set_safe(safe_block.as_sealed_header().clone()); + provider.set_finalized(finalized_block.as_sealed_header().clone()); Ok((provider, database_blocks.clone(), in_memory_blocks.clone(), receipts)) } @@ -1362,7 +1362,7 @@ mod tests { let in_memory_block = in_memory_blocks.last().unwrap().clone(); // make sure that the finalized block is on db let finalized_block = database_blocks.get(database_blocks.len() - 3).unwrap(); - provider.set_finalized(finalized_block.header.clone()); + provider.set_finalized(finalized_block.deref().clone()); let blocks = [database_blocks, in_memory_blocks].concat(); @@ -1381,7 +1381,7 @@ mod tests { blocks .iter() .take_while(|header| header.number <= 8) - .map(|b| b.header.clone()) + .map(|b| b.deref().clone()) .collect::>() ); @@ -1562,38 +1562,38 @@ mod tests { let block_number = database_block.number; assert_eq!( provider.header_by_number_or_tag(block_number.into()).unwrap(), - Some(database_block.header.clone().unseal()) + Some(database_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_number_or_tag(block_number.into())?, - Some(database_block.header.clone()) + Some(database_block.deref().clone()) ); assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.header.clone().unseal()) + Some(canonical_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.header.clone()) + Some(canonical_block.deref().clone()) ); assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.header.clone().unseal()) + Some(safe_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.header.clone()) + Some(safe_block.deref().clone()) ); assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Finalized).unwrap(), - Some(finalized_block.header.clone().unseal()) + Some(finalized_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Finalized).unwrap(), - Some(finalized_block.header) + Some(finalized_block.deref().clone()) ); Ok(()) @@ -1617,20 +1617,20 @@ mod tests { assert_eq!( provider.header_by_id(block_number.into()).unwrap(), - Some(database_block.header.clone().unseal()) + Some(database_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_id(block_number.into()).unwrap(), - Some(database_block.header.clone()) + Some(database_block.deref().clone()) ); assert_eq!( provider.header_by_id(block_hash.into()).unwrap(), - Some(database_block.header.clone().unseal()) + Some(database_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_id(block_hash.into()).unwrap(), - Some(database_block.header) + Some(database_block.deref().clone()) ); let block_number = in_memory_block.number; @@ -1638,20 +1638,20 @@ mod tests { assert_eq!( provider.header_by_id(block_number.into()).unwrap(), - Some(in_memory_block.header.clone().unseal()) + Some(in_memory_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_id(block_number.into()).unwrap(), - Some(in_memory_block.header.clone()) + Some(in_memory_block.deref().clone()) ); assert_eq!( provider.header_by_id(block_hash.into()).unwrap(), - Some(in_memory_block.header.clone().unseal()) + Some(in_memory_block.deref().clone().unseal()) ); assert_eq!( provider.sealed_header_by_id(block_hash.into()).unwrap(), - Some(in_memory_block.header) + Some(in_memory_block.deref().clone()) ); Ok(()) @@ -2037,7 +2037,7 @@ mod tests { ); // test state by block tag for safe block let safe_block = in_memory_blocks[in_memory_blocks.len() - 2].clone(); - in_memory_provider.canonical_in_memory_state.set_safe(safe_block.header.clone()); + in_memory_provider.canonical_in_memory_state.set_safe(safe_block.deref().clone()); assert_eq!( safe_block.hash(), in_memory_provider @@ -2047,7 +2047,7 @@ mod tests { ); // test state by block tag for finalized block let finalized_block = in_memory_blocks[in_memory_blocks.len() - 3].clone(); - in_memory_provider.canonical_in_memory_state.set_finalized(finalized_block.header.clone()); + in_memory_provider.canonical_in_memory_state.set_finalized(finalized_block.deref().clone()); assert_eq!( finalized_block.hash(), in_memory_provider @@ -2120,11 +2120,11 @@ mod tests { // Set the safe block in memory let safe_block = in_memory_blocks[in_memory_blocks.len() - 2].clone(); - provider.canonical_in_memory_state.set_safe(safe_block.header.clone()); + provider.canonical_in_memory_state.set_safe(safe_block.deref().clone()); // Set the finalized block in memory let finalized_block = in_memory_blocks[in_memory_blocks.len() - 3].clone(); - provider.canonical_in_memory_state.set_finalized(finalized_block.header.clone()); + provider.canonical_in_memory_state.set_finalized(finalized_block.deref().clone()); // Verify the pending block number and hash assert_eq!( @@ -2338,8 +2338,9 @@ mod tests { // todo(joshie) add canonical_hashes_range below after changing its interface into range // instead start end test_by_block_range!([ - (headers_range, |block: &SealedBlock| block.header().clone()), - (sealed_headers_range, |block: &SealedBlock| block.header.clone()), + // For headers_range, we need to compare the raw header fields since Header may not implement PartialEq + (headers_range, |block: &SealedBlock| block.header().clone().into()), + (sealed_headers_range, |block: &SealedBlock| block.deref().clone()), (block_range, |block: &SealedBlock| block.clone().unseal()), (block_with_senders_range, |block: &SealedBlock| block .clone() @@ -2472,7 +2473,7 @@ mod tests { header_by_number, |block: &SealedBlock, _: TxNumber, _: B256, _: &Vec>| ( block.number, - Some(block.header.header().clone()) + Some(block.as_sealed_header().clone()) ), u64::MAX ), @@ -2481,7 +2482,7 @@ mod tests { sealed_header, |block: &SealedBlock, _: TxNumber, _: B256, _: &Vec>| ( block.number, - Some(block.header.clone()) + Some(block.as_sealed_header().clone()) ), u64::MAX ), diff --git a/crates/storage/provider/src/providers/consistent.rs b/crates/storage/provider/src/providers/consistent.rs index 46f7d7a9c49e..b47d8d5c5e9d 100644 --- a/crates/storage/provider/src/providers/consistent.rs +++ b/crates/storage/provider/src/providers/consistent.rs @@ -632,7 +632,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block( (*block_hash).into(), |db_provider| db_provider.header(block_hash), - |block_state| Ok(Some(block_state.block_ref().block().header.header().clone())), + |block_state| Ok(Some(block_state.block_ref().block().header().clone())), ) } @@ -640,7 +640,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block( num.into(), |db_provider| db_provider.header_by_number(num), - |block_state| Ok(Some(block_state.block_ref().block().header.header().clone())), + |block_state| Ok(Some(block_state.block_ref().block().header().clone())), ) } @@ -682,7 +682,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block_range_while( range, |db_provider, range, _| db_provider.headers_range(range), - |block_state, _| Some(block_state.block_ref().block().header.header().clone()), + |block_state, _| Some(block_state.block_ref().block().header().clone()), |_| true, ) } @@ -694,7 +694,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block( number.into(), |db_provider| db_provider.sealed_header(number), - |block_state| Ok(Some(block_state.block_ref().block().header.clone())), + |block_state| Ok(Some(block_state.block_ref().block().header().clone())), ) } @@ -705,7 +705,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block_range_while( range, |db_provider, range, _| db_provider.sealed_headers_range(range), - |block_state, _| Some(block_state.block_ref().block().header.clone()), + |block_state, _| Some(block_state.block_ref().block().header().clone()), |_| true, ) } @@ -719,7 +719,7 @@ impl HeaderProvider for ConsistentProvider { range, |db_provider, range, predicate| db_provider.sealed_headers_while(range, predicate), |block_state, predicate| { - let header = &block_state.block_ref().block().header; + let header = &block_state.block_ref().block().header(); predicate(header).then(|| header.clone()) }, predicate, diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 16c62f7c367e..7042a101cbb1 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -430,12 +430,12 @@ impl< &self, block: SealedBlockWithSenders<::Block>, ) -> ProviderResult { - let ttd = if block.number == 0 { - block.difficulty + let ttd = if block.number() == 0 { + block.difficulty() } else { - let parent_block_number = block.number - 1; + let parent_block_number = block.number() - 1; let parent_ttd = self.header_td_by_number(parent_block_number)?.unwrap_or_default(); - parent_ttd + block.difficulty + parent_ttd + block.difficulty() }; let mut writer = self.static_file_provider.latest_writer(StaticFileSegment::Headers)?; @@ -443,14 +443,14 @@ impl< // Backfill: some tests start at a forward block number, but static files require no gaps. let segment_header = writer.user_header(); if segment_header.block_end().is_none() && segment_header.expected_block_start() == 0 { - for block_number in 0..block.number { - let mut prev = block.header.clone().unseal(); + for block_number in 0..block.number() { + let mut prev = block.as_sealed_header().clone().unseal(); prev.number = block_number; writer.append_header(&prev, U256::ZERO, &B256::ZERO)?; } } - writer.append_header(block.header.as_ref(), ttd, &block.hash())?; + writer.append_header(block.deref().as_ref(), ttd, &block.hash())?; self.insert_block(block, StorageLocation::Database) } @@ -2780,7 +2780,7 @@ impl BlockWrite // Put header with canonical hashes. self.tx - .put::>>(block_number, block.header.as_ref().clone())?; + .put::>>(block_number, block.deref().as_ref().clone())?; durations_recorder.record_relative(metrics::Action::InsertHeaders); self.tx.put::(block_number, ttd.into())?; @@ -2790,7 +2790,7 @@ impl BlockWrite if write_to.static_files() { let mut writer = self.static_file_provider.get_writer(block_number, StaticFileSegment::Headers)?; - writer.append_header(&block.header, ttd, &block.hash())?; + writer.append_header(&block.header(), ttd, &block.hash())?; } self.tx.put::(block.hash(), block_number)?; diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 5ed8b09ee0b8..2e7aea841120 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -18,6 +18,7 @@ use reth_primitives::{ use reth_trie::root::{state_root_unhashed, storage_root_unhashed}; use revm::{db::BundleState, primitives::AccountInfo}; use std::{str::FromStr, sync::LazyLock}; +use std::ops::Deref; /// Assert genesis block pub fn assert_genesis_block( @@ -29,7 +30,7 @@ pub fn assert_genesis_block( let tx = provider; // check if all tables are empty - assert_eq!(tx.table::().unwrap(), vec![(g.number, g.header.clone().unseal())]); + assert_eq!(tx.table::().unwrap(), vec![(g.number, g.deref().clone().unseal())]); assert_eq!(tx.table::().unwrap(), vec![(h, n)]); assert_eq!(tx.table::().unwrap(), vec![(n, h)]); From 3d2a02b6f4c648f9a0a6fe5032189d31914e0e83 Mon Sep 17 00:00:00 2001 From: Khanh Hoa Date: Sat, 4 Jan 2025 22:05:27 +0700 Subject: [PATCH 05/10] bet --- crates/chain-state/src/in_memory.rs | 5 ++++- crates/evm/execution-types/src/chain.rs | 2 +- crates/net/downloaders/src/bodies/request.rs | 4 ++-- crates/net/downloaders/src/test_utils/mod.rs | 7 +++++-- crates/net/p2p/src/bodies/response.rs | 19 ++----------------- .../src/providers/blockchain_provider.rs | 6 +++--- .../provider/src/providers/consistent.rs | 2 +- .../src/providers/database/provider.rs | 6 ++++-- .../storage/provider/src/test_utils/blocks.rs | 8 +++++--- 9 files changed, 27 insertions(+), 32 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index f55ae8f74aa8..1c1e5b0ff172 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -228,7 +228,10 @@ impl CanonicalInMemoryState { } /// Returns the header corresponding to the given hash. - pub fn header_by_hash(&self, hash: B256) -> Option::Block>> { + pub fn header_by_hash( + &self, + hash: B256, + ) -> Option::Block>> { self.state_by_hash(hash).map(|block| block.block_ref().block.deref().clone()) } diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index 4ceef41fa8c0..8149bd4aaca0 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -6,7 +6,6 @@ use alloy_consensus::BlockHeader; use alloy_eips::{eip1898::ForkBlock, eip2718::Encodable2718, BlockNumHash}; use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash}; use core::{fmt, ops::RangeInclusive}; -use std::ops::Deref; use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError}; use reth_primitives::{ transaction::SignedTransactionIntoRecoveredExt, RecoveredTx, SealedBlockFor, @@ -15,6 +14,7 @@ use reth_primitives::{ use reth_primitives_traits::{Block, BlockBody, NodePrimitives, SignedTransaction}; use reth_trie::updates::TrieUpdates; use revm::db::BundleState; +use std::ops::Deref; /// A chain of blocks and their final state. /// diff --git a/crates/net/downloaders/src/bodies/request.rs b/crates/net/downloaders/src/bodies/request.rs index 20e3e94e0026..6ebae95569a6 100644 --- a/crates/net/downloaders/src/bodies/request.rs +++ b/crates/net/downloaders/src/bodies/request.rs @@ -14,11 +14,11 @@ use reth_primitives_traits::InMemorySize; use std::{ collections::VecDeque, mem, + ops::Deref, pin::Pin, sync::Arc, task::{ready, Context, Poll}, }; -use std::ops::Deref; /// Body request implemented as a [Future]. /// @@ -195,7 +195,7 @@ where // Body is invalid, put the header back and return an error let hash = block.hash(); let number = block.number(); - self.pending_headers.push_front(block.deref().clone()); + self.pending_headers.push_front(block.deref()); return Err(DownloadError::BodyValidation { hash, number, diff --git a/crates/net/downloaders/src/test_utils/mod.rs b/crates/net/downloaders/src/test_utils/mod.rs index 44f6f9315c32..67c35e1a26ed 100644 --- a/crates/net/downloaders/src/test_utils/mod.rs +++ b/crates/net/downloaders/src/test_utils/mod.rs @@ -7,8 +7,11 @@ use alloy_primitives::B256; use futures::SinkExt; use reth_primitives::{BlockBody, SealedHeader}; use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams}; -use std::{collections::HashMap, io::SeekFrom, ops::RangeInclusive}; -use std::ops::Deref; +use std::{ + collections::HashMap, + io::SeekFrom, + ops::{Deref, RangeInclusive}, +}; use tokio::{fs::File, io::AsyncSeekExt}; use tokio_util::codec::FramedWrite; diff --git a/crates/net/p2p/src/bodies/response.rs b/crates/net/p2p/src/bodies/response.rs index 6fb06dc2ca60..708c9d7ccfb8 100644 --- a/crates/net/p2p/src/bodies/response.rs +++ b/crates/net/p2p/src/bodies/response.rs @@ -1,11 +1,9 @@ -use std::ops::Deref; use alloy_consensus::BlockHeader; use alloy_primitives::{BlockNumber, U256}; use reth_primitives::{BlockBody, SealedBlock, SealedHeader}; use reth_primitives_traits::InMemorySize; - /// The block response -#[derive(Debug, Clone)] +#[derive(PartialEq, Eq, Debug, Clone)] pub enum BlockResponse { /// Full block response (with transactions or ommers) Full(SealedBlock), @@ -13,19 +11,6 @@ pub enum BlockResponse { Empty(SealedHeader), } -impl PartialEq for BlockResponse -where - SealedBlock: PartialEq, -{ - fn eq(&self, other: &Self) -> bool { - match (self, other) { - (Self::Full(a), Self::Full(b)) => a == b, - (Self::Empty(a), Self::Empty(b)) => a == b, - _ => false, - } - } -} - impl BlockResponse where H: BlockHeader, @@ -33,7 +18,7 @@ where /// Return the reference to the response header pub fn header(&self) -> &SealedHeader { match self { - Self::Full(block) => &block.deref(), + Self::Full(block) => block.as_sealed_header(), Self::Empty(header) => header, } } diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index e3124f26f5dc..f0e4276563d1 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -814,11 +814,10 @@ mod tests { }; use revm::db::BundleState; use std::{ - ops::{Bound, Range, RangeBounds}, + ops::{Bound, Deref, Range, RangeBounds}, sync::Arc, time::Instant, }; - use std::ops::Deref; const TEST_BLOCKS_COUNT: usize = 5; @@ -2338,7 +2337,8 @@ mod tests { // todo(joshie) add canonical_hashes_range below after changing its interface into range // instead start end test_by_block_range!([ - // For headers_range, we need to compare the raw header fields since Header may not implement PartialEq + // For headers_range, we need to compare the raw header fields since Header may not + // implement PartialEq (headers_range, |block: &SealedBlock| block.header().clone().into()), (sealed_headers_range, |block: &SealedBlock| block.deref().clone()), (block_range, |block: &SealedBlock| block.clone().unseal()), diff --git a/crates/storage/provider/src/providers/consistent.rs b/crates/storage/provider/src/providers/consistent.rs index b47d8d5c5e9d..24503fc3fa89 100644 --- a/crates/storage/provider/src/providers/consistent.rs +++ b/crates/storage/provider/src/providers/consistent.rs @@ -694,7 +694,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block( number.into(), |db_provider| db_provider.sealed_header(number), - |block_state| Ok(Some(block_state.block_ref().block().header().clone())), + |block_state| Ok(Some(block_state.block_ref().block().clone())), ) } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 7042a101cbb1..5a270be21ef3 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -2779,8 +2779,10 @@ impl BlockWrite durations_recorder.record_relative(metrics::Action::InsertCanonicalHeaders); // Put header with canonical hashes. - self.tx - .put::>>(block_number, block.deref().as_ref().clone())?; + self.tx.put::>>( + block_number, + block.deref().as_ref().clone(), + )?; durations_recorder.record_relative(metrics::Action::InsertHeaders); self.tx.put::(block_number, ttd.into())?; diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 2e7aea841120..739c2b47d87f 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -17,8 +17,7 @@ use reth_primitives::{ }; use reth_trie::root::{state_root_unhashed, storage_root_unhashed}; use revm::{db::BundleState, primitives::AccountInfo}; -use std::{str::FromStr, sync::LazyLock}; -use std::ops::Deref; +use std::{ops::Deref, str::FromStr, sync::LazyLock}; /// Assert genesis block pub fn assert_genesis_block( @@ -30,7 +29,10 @@ pub fn assert_genesis_block( let tx = provider; // check if all tables are empty - assert_eq!(tx.table::().unwrap(), vec![(g.number, g.deref().clone().unseal())]); + assert_eq!( + tx.table::().unwrap(), + vec![(g.number, g.deref().clone().unseal())] + ); assert_eq!(tx.table::().unwrap(), vec![(h, n)]); assert_eq!(tx.table::().unwrap(), vec![(n, h)]); From d965673da12fff21995e6114e70a7b80d7a4b6b1 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 7 Jan 2025 18:37:40 +0100 Subject: [PATCH 06/10] wip --- bin/reth-bench/src/bench/new_payload_fcu.rs | 2 +- crates/blockchain-tree/src/blockchain_tree.rs | 25 ++++++++++--------- crates/blockchain-tree/src/shareable.rs | 4 +-- crates/consensus/beacon/src/engine/mod.rs | 4 +-- crates/ethereum/payload/src/lib.rs | 2 +- crates/evm/execution-types/src/chain.rs | 7 +++--- crates/exex/exex/src/manager.rs | 24 +++++++++--------- crates/net/downloaders/src/bodies/bodies.rs | 2 +- crates/net/downloaders/src/bodies/request.rs | 3 +-- crates/net/p2p/src/bodies/response.rs | 4 +-- crates/optimism/payload/src/builder.rs | 2 +- crates/primitives-traits/src/header/sealed.rs | 7 ------ crates/primitives/src/block.rs | 10 ++++++++ crates/stages/stages/src/stages/bodies.rs | 2 +- .../stages/stages/src/test_utils/test_db.rs | 2 +- .../src/providers/blockchain_provider.rs | 12 ++++----- .../provider/src/providers/consistent.rs | 6 ++--- .../src/providers/database/provider.rs | 4 +-- 18 files changed, 61 insertions(+), 61 deletions(-) diff --git a/bin/reth-bench/src/bench/new_payload_fcu.rs b/bin/reth-bench/src/bench/new_payload_fcu.rs index e3d388b37ece..2866cf8fb45c 100644 --- a/bin/reth-bench/src/bench/new_payload_fcu.rs +++ b/bin/reth-bench/src/bench/new_payload_fcu.rs @@ -75,7 +75,7 @@ impl Command { while let Some((block, head, safe, finalized)) = receiver.recv().await { // just put gas used here let gas_used = block.gas_used; - let block_number = block.header.number; + let block_number = block.number; let versioned_hashes: Vec = block.body().blob_versioned_hashes_iter().copied().collect(); diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 3964ea53b7e2..1e5bf5bd242c 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -1199,7 +1199,8 @@ where durations_recorder.record_relative(MakeCanonicalAction::ClearTrieUpdatesForOtherChildren); // Send notification about new canonical chain and return outcome of canonicalization. - let outcome = CanonicalOutcome::Committed { head: chain_notification.tip().header.clone() }; + let outcome = + CanonicalOutcome::Committed { head: chain_notification.tip().sealed_header().clone() }; let _ = self.canon_state_notification_sender.send(chain_notification); Ok(outcome) } @@ -1434,8 +1435,8 @@ mod tests { ) { // insert genesis to db. - genesis.header.set_block_number(10); - genesis.header.set_state_root(EMPTY_ROOT_HASH); + genesis.set_block_number(10); + genesis.set_state_root(EMPTY_ROOT_HASH); let provider = factory.provider_rw().unwrap(); provider @@ -1669,7 +1670,7 @@ mod tests { assert_eq!( tree.make_canonical(fork_block.hash()).unwrap(), - CanonicalOutcome::Committed { head: fork_block.header.clone() } + CanonicalOutcome::Committed { head: fork_block.sealed_header().clone() } ); assert_eq!( @@ -1679,7 +1680,7 @@ mod tests { assert_eq!( tree.make_canonical(canonical_block_1.hash()).unwrap(), - CanonicalOutcome::Committed { head: canonical_block_1.header.clone() } + CanonicalOutcome::Committed { head: canonical_block_1.sealed_header().clone() } ); assert_eq!( @@ -1694,12 +1695,12 @@ mod tests { assert_eq!( tree.make_canonical(sidechain_block_1.hash()).unwrap(), - CanonicalOutcome::Committed { head: sidechain_block_1.header.clone() } + CanonicalOutcome::Committed { head: sidechain_block_1.sealed_header().clone() } ); assert_eq!( tree.make_canonical(canonical_block_1.hash()).unwrap(), - CanonicalOutcome::Committed { head: canonical_block_1.header.clone() } + CanonicalOutcome::Committed { head: canonical_block_1.sealed_header().clone() } ); assert_eq!( @@ -1709,7 +1710,7 @@ mod tests { assert_eq!( tree.make_canonical(sidechain_block_2.hash()).unwrap(), - CanonicalOutcome::Committed { head: sidechain_block_2.header.clone() } + CanonicalOutcome::Committed { head: sidechain_block_2.sealed_header().clone() } ); assert_eq!( @@ -1719,7 +1720,7 @@ mod tests { assert_eq!( tree.make_canonical(canonical_block_3.hash()).unwrap(), - CanonicalOutcome::Committed { head: canonical_block_3.header.clone() } + CanonicalOutcome::Committed { head: canonical_block_3.sealed_header().clone() } ); } @@ -1841,7 +1842,7 @@ mod tests { assert_eq!( tree.make_canonical(block2.hash()).unwrap(), - CanonicalOutcome::Committed { head: block2.header.clone() } + CanonicalOutcome::Committed { head: block2.sealed_header().clone() } ); assert_eq!( @@ -1854,7 +1855,7 @@ mod tests { assert_eq!( tree.make_canonical(block3.hash()).unwrap(), - CanonicalOutcome::Committed { head: block3.header.clone() } + CanonicalOutcome::Committed { head: block3.sealed_header().clone() } ); assert_eq!( @@ -1876,7 +1877,7 @@ mod tests { assert_eq!( tree.make_canonical(block5.hash()).unwrap(), - CanonicalOutcome::Committed { head: block5.header.clone() } + CanonicalOutcome::Committed { head: block5.sealed_header().clone() } ); let provider = tree.externals.provider_factory.provider().unwrap(); diff --git a/crates/blockchain-tree/src/shareable.rs b/crates/blockchain-tree/src/shareable.rs index e668f4e2dac0..6cb36cfab7cc 100644 --- a/crates/blockchain-tree/src/shareable.rs +++ b/crates/blockchain-tree/src/shareable.rs @@ -114,7 +114,7 @@ where { fn header_by_hash(&self, hash: BlockHash) -> Option { trace!(target: "blockchain_tree", ?hash, "Returning header by hash"); - self.tree.read().sidechain_block_by_hash(hash).map(|b| b.header.clone()) + self.tree.read().sidechain_block_by_hash(hash).map(|b| b.sealed_header().clone()) } fn block_by_hash(&self, block_hash: BlockHash) -> Option { @@ -128,7 +128,7 @@ where } fn buffered_header_by_hash(&self, block_hash: BlockHash) -> Option { - self.tree.read().get_buffered_block(&block_hash).map(|b| b.header.clone()) + self.tree.read().get_buffered_block(&block_hash).map(|b| b.sealed_header().clone()) } fn is_canonical(&self, hash: BlockHash) -> Result { diff --git a/crates/consensus/beacon/src/engine/mod.rs b/crates/consensus/beacon/src/engine/mod.rs index 2dc139acedb4..c91e0719a06b 100644 --- a/crates/consensus/beacon/src/engine/mod.rs +++ b/crates/consensus/beacon/src/engine/mod.rs @@ -1670,7 +1670,7 @@ where self.latest_valid_hash_for_invalid_payload(block.parent_hash)? }; // keep track of the invalid header - self.invalid_headers.insert(block.header.block_with_parent()); + self.invalid_headers.insert(block.sealed_header().block_with_parent()); PayloadStatus::new( PayloadStatusEnum::Invalid { validation_error: error.to_string() }, latest_valid_hash, @@ -1779,7 +1779,7 @@ where let (block, err) = err.split(); warn!(target: "consensus::engine", invalid_number=?block.number, invalid_hash=?block.hash(), %err, "Marking block as invalid"); - self.invalid_headers.insert(block.header.block_with_parent()); + self.invalid_headers.insert(block.sealed_header().block_with_parent()); } } } diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index 66ca1662b97e..a4a02c3ef768 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -490,7 +490,7 @@ where }; let sealed_block = Arc::new(block.seal_slow()); - debug!(target: "payload_builder", id=%attributes.id, sealed_block_header = ?sealed_block.header, "sealed built block"); + debug!(target: "payload_builder", id=%attributes.id, sealed_block_header = ?sealed_block.sealed_header(), "sealed built block"); // create the executed block data let executed = ExecutedBlock { diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index 4b1e47a20765..7e6ba2046043 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -14,7 +14,6 @@ use reth_primitives::{ use reth_primitives_traits::{Block, BlockBody, NodePrimitives, SignedTransaction}; use reth_trie::updates::TrieUpdates; use revm::db::BundleState; -use std::ops::Deref; /// A chain of blocks and their final state. /// @@ -92,7 +91,7 @@ impl Chain { /// Returns an iterator over all headers in the block with increasing block numbers. pub fn headers(&self) -> impl Iterator> + '_ { - self.blocks.values().map(|block| block.as_sealed_header().clone()) + self.blocks.values().map(|block| block.sealed_header().clone()) } /// Get cached trie updates for this chain. @@ -859,8 +858,8 @@ mod tests { let mut block2 = block; // Set the hashes of block1 and block2 - block1.block.header.set_hash(block1_hash); - block2.block.header.set_hash(block2_hash); + block1.block.set_hash(block1_hash); + block2.block.set_hash(block2_hash); // Create a random receipt object, receipt1 let receipt1 = Receipt { diff --git a/crates/exex/exex/src/manager.rs b/crates/exex/exex/src/manager.rs index 16a930526141..3edc9d8d3708 100644 --- a/crates/exex/exex/src/manager.rs +++ b/crates/exex/exex/src/manager.rs @@ -767,8 +767,8 @@ mod tests { // Define the notification for testing let mut block1: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(B256::new([0x01; 32])); - block1.block.header.set_block_number(10); + block1.block.set_hash(B256::new([0x01; 32])); + block1.block.set_block_number(10); let notification1 = ExExNotification::ChainCommitted { new: Arc::new(Chain::new(vec![block1.clone()], Default::default(), Default::default())), @@ -785,8 +785,8 @@ mod tests { // Push another notification let mut block2: SealedBlockWithSenders = Default::default(); - block2.block.header.set_hash(B256::new([0x02; 32])); - block2.block.header.set_block_number(20); + block2.block.set_hash(B256::new([0x02; 32])); + block2.block.set_block_number(20); let notification2 = ExExNotification::ChainCommitted { new: Arc::new(Chain::new(vec![block2.clone()], Default::default(), Default::default())), @@ -828,8 +828,8 @@ mod tests { // Push some notifications to fill part of the buffer let mut block1: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(B256::new([0x01; 32])); - block1.block.header.set_block_number(10); + block1.set_hash(B256::new([0x01; 32])); + block1.set_block_number(10); let notification1 = ExExNotification::ChainCommitted { new: Arc::new(Chain::new(vec![block1.clone()], Default::default(), Default::default())), @@ -1117,12 +1117,12 @@ mod tests { // Setup two blocks for the chain commit notification let mut block1: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(B256::new([0x01; 32])); - block1.block.header.set_block_number(10); + block1.block.set_hash(B256::new([0x01; 32])); + block1.block.set_block_number(10); let mut block2: SealedBlockWithSenders = Default::default(); - block2.block.header.set_hash(B256::new([0x02; 32])); - block2.block.header.set_block_number(11); + block2.block.set_hash(B256::new([0x02; 32])); + block2.block.set_block_number(11); // Setup a notification let notification = ExExNotification::ChainCommitted { @@ -1170,8 +1170,8 @@ mod tests { exex_handle.finished_height = Some(BlockNumHash::new(15, B256::random())); let mut block1: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(B256::new([0x01; 32])); - block1.block.header.set_block_number(10); + block1.block.set_hash(B256::new([0x01; 32])); + block1.block.set_block_number(10); let notification = ExExNotification::ChainCommitted { new: Arc::new(Chain::new(vec![block1.clone()], Default::default(), Default::default())), diff --git a/crates/net/downloaders/src/bodies/bodies.rs b/crates/net/downloaders/src/bodies/bodies.rs index 47a816f4ce6b..e8be8da033dc 100644 --- a/crates/net/downloaders/src/bodies/bodies.rs +++ b/crates/net/downloaders/src/bodies/bodies.rs @@ -677,7 +677,7 @@ mod tests { BlockRangeParams { parent: Some(B256::ZERO), tx_count: 1..2, ..Default::default() }, ); - let headers = blocks.iter().map(|block| block.header.clone()).collect::>(); + let headers = blocks.iter().map(|block| block.sealed_header().clone()).collect::>(); let bodies = blocks .into_iter() .map(|block| (block.hash(), block.into_body())) diff --git a/crates/net/downloaders/src/bodies/request.rs b/crates/net/downloaders/src/bodies/request.rs index 6ebae95569a6..ea690f0806ce 100644 --- a/crates/net/downloaders/src/bodies/request.rs +++ b/crates/net/downloaders/src/bodies/request.rs @@ -14,7 +14,6 @@ use reth_primitives_traits::InMemorySize; use std::{ collections::VecDeque, mem, - ops::Deref, pin::Pin, sync::Arc, task::{ready, Context, Poll}, @@ -195,7 +194,7 @@ where // Body is invalid, put the header back and return an error let hash = block.hash(); let number = block.number(); - self.pending_headers.push_front(block.deref()); + self.pending_headers.push_front(block.into_sealed_header()); return Err(DownloadError::BodyValidation { hash, number, diff --git a/crates/net/p2p/src/bodies/response.rs b/crates/net/p2p/src/bodies/response.rs index 708c9d7ccfb8..517c5b879835 100644 --- a/crates/net/p2p/src/bodies/response.rs +++ b/crates/net/p2p/src/bodies/response.rs @@ -16,9 +16,9 @@ where H: BlockHeader, { /// Return the reference to the response header - pub fn header(&self) -> &SealedHeader { + pub const fn header(&self) -> &SealedHeader { match self { - Self::Full(block) => block.as_sealed_header(), + Self::Full(block) => block.sealed_header(), Self::Empty(header) => header, } } diff --git a/crates/optimism/payload/src/builder.rs b/crates/optimism/payload/src/builder.rs index b87b01b37b2b..7bf3f8015b71 100644 --- a/crates/optimism/payload/src/builder.rs +++ b/crates/optimism/payload/src/builder.rs @@ -434,7 +434,7 @@ where }; let sealed_block = Arc::new(block.seal_slow()); - debug!(target: "payload_builder", id=%ctx.attributes().payload_id(), sealed_block_header = ?sealed_block.header, "sealed built block"); + debug!(target: "payload_builder", id=%ctx.attributes().payload_id(), sealed_block_header = ?sealed_block.header(), "sealed built block"); // create the executed block data let executed: ExecutedBlock = ExecutedBlock { diff --git a/crates/primitives-traits/src/header/sealed.rs b/crates/primitives-traits/src/header/sealed.rs index d88d59f4a997..ef8b5fde5e91 100644 --- a/crates/primitives-traits/src/header/sealed.rs +++ b/crates/primitives-traits/src/header/sealed.rs @@ -58,13 +58,6 @@ impl SealedHeader { pub fn split(self) -> (H, BlockHash) { (self.header, self.hash) } - - /// Returns a reference to self. - /// This is useful when you need to convert from a dereferenced header back to SealedHeader. - #[inline] - pub const fn as_sealed_header(&self) -> &SealedHeader { - self - } } impl SealedHeader { diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index 5bf4df63ac8e..bcf5ad71abed 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -190,6 +190,16 @@ impl SealedBlock { &self.body } + /// Returns the Sealed header. + pub const fn sealed_header(&self) -> &SealedHeader { + &self.header + } + + /// Consumes the block and returns the sealed header. + pub fn into_sealed_header(self) -> SealedHeader { + self.header + } + /// Consumes the block and returns the header. pub fn into_header(self) -> H { self.header.unseal() diff --git a/crates/stages/stages/src/stages/bodies.rs b/crates/stages/stages/src/stages/bodies.rs index b17ad3562a09..51941183953e 100644 --- a/crates/stages/stages/src/stages/bodies.rs +++ b/crates/stages/stages/src/stages/bodies.rs @@ -582,7 +582,7 @@ mod tests { ..Default::default() }, ); - self.db.insert_headers_with_td(blocks.iter().map(|block| &block.header))?; + self.db.insert_headers_with_td(blocks.iter().map(|block| block.sealed_header()))?; if let Some(progress) = blocks.get(start as usize) { // Insert last progress data { diff --git a/crates/stages/stages/src/test_utils/test_db.rs b/crates/stages/stages/src/test_utils/test_db.rs index 5e4c61b6fd36..5db57da54a4c 100644 --- a/crates/stages/stages/src/test_utils/test_db.rs +++ b/crates/stages/stages/src/test_utils/test_db.rs @@ -235,7 +235,7 @@ impl TestStageDB { .then(|| provider.latest_writer(StaticFileSegment::Headers).unwrap()); blocks.iter().try_for_each(|block| { - Self::insert_header(headers_writer.as_mut(), &tx, &block.header, U256::ZERO) + Self::insert_header(headers_writer.as_mut(), &tx, block.sealed_header(), U256::ZERO) })?; if let Some(mut writer) = headers_writer { diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index ae98dde99fef..6587047f1c39 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -948,9 +948,9 @@ mod tests { let finalized_block = blocks.get(block_count - 3).unwrap(); // Set the canonical head, safe, and finalized blocks - provider.set_canonical_head(canonical_block.as_sealed_header().clone()); - provider.set_safe(safe_block.as_sealed_header().clone()); - provider.set_finalized(finalized_block.as_sealed_header().clone()); + provider.set_canonical_head(canonical_block.sealed_header().clone()); + provider.set_safe(safe_block.sealed_header().clone()); + provider.set_finalized(finalized_block.sealed_header().clone()); Ok((provider, database_blocks.clone(), in_memory_blocks.clone(), receipts)) } @@ -2324,8 +2324,6 @@ mod tests { // todo(joshie) add canonical_hashes_range below after changing its interface into range // instead start end test_by_block_range!([ - // For headers_range, we need to compare the raw header fields since Header may not - // implement PartialEq (headers_range, |block: &SealedBlock| block.header().clone().into()), (sealed_headers_range, |block: &SealedBlock| block.deref().clone()), (block_range, |block: &SealedBlock| block.clone().unseal()), @@ -2460,7 +2458,7 @@ mod tests { header_by_number, |block: &SealedBlock, _: TxNumber, _: B256, _: &Vec>| ( block.number, - Some(block.as_sealed_header().clone()) + Some(block.sealed_header().clone()) ), u64::MAX ), @@ -2469,7 +2467,7 @@ mod tests { sealed_header, |block: &SealedBlock, _: TxNumber, _: B256, _: &Vec>| ( block.number, - Some(block.as_sealed_header().clone()) + Some(block.sealed_header().clone()) ), u64::MAX ), diff --git a/crates/storage/provider/src/providers/consistent.rs b/crates/storage/provider/src/providers/consistent.rs index 79ac42effda5..02ad7549439e 100644 --- a/crates/storage/provider/src/providers/consistent.rs +++ b/crates/storage/provider/src/providers/consistent.rs @@ -693,7 +693,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block( number.into(), |db_provider| db_provider.sealed_header(number), - |block_state| Ok(Some(block_state.block_ref().block().clone())), + |block_state| Ok(Some(block_state.block_ref().block().sealed_header().clone())), ) } @@ -704,7 +704,7 @@ impl HeaderProvider for ConsistentProvider { self.get_in_memory_or_storage_by_block_range_while( range, |db_provider, range, _| db_provider.sealed_headers_range(range), - |block_state, _| Some(block_state.block_ref().block().header().clone()), + |block_state, _| Some(block_state.block_ref().block().sealed_header().clone()), |_| true, ) } @@ -718,7 +718,7 @@ impl HeaderProvider for ConsistentProvider { range, |db_provider, range, predicate| db_provider.sealed_headers_while(range, predicate), |block_state, predicate| { - let header = &block_state.block_ref().block().header(); + let header = block_state.block_ref().block().sealed_header(); predicate(header).then(|| header.clone()) }, predicate, diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index aaabf91dca78..3dc760620c83 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -440,7 +440,7 @@ impl< let segment_header = writer.user_header(); if segment_header.block_end().is_none() && segment_header.expected_block_start() == 0 { for block_number in 0..block.number() { - let mut prev = block.as_sealed_header().clone().unseal(); + let mut prev = block.sealed_header().clone().unseal(); prev.number = block_number; writer.append_header(&prev, U256::ZERO, &B256::ZERO)?; } @@ -2782,7 +2782,7 @@ impl BlockWrite if write_to.static_files() { let mut writer = self.static_file_provider.get_writer(block_number, StaticFileSegment::Headers)?; - writer.append_header(&block.header(), ttd, &block.hash())?; + writer.append_header(block.header(), ttd, &block.hash())?; } self.tx.put::(block.hash(), block_number)?; From b755d202823e5ab51c0edec46cd7711c69cd5f8c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 7 Jan 2025 19:50:07 +0100 Subject: [PATCH 07/10] make it compile --- crates/blockchain-tree/src/blockchain_tree.rs | 2 +- crates/blockchain-tree/src/state.rs | 34 +++++++------- crates/chain-state/src/in_memory.rs | 19 ++++---- crates/consensus/beacon/src/engine/mod.rs | 8 ++-- crates/engine/tree/src/tree/mod.rs | 17 ++++--- crates/exex/exex/src/manager.rs | 8 ++-- crates/net/downloaders/src/test_utils/mod.rs | 8 +--- crates/node/events/src/node.rs | 12 ++--- crates/rpc/rpc-eth-api/src/helpers/call.rs | 5 +-- crates/rpc/rpc-eth-api/src/helpers/trace.rs | 4 +- crates/rpc/rpc/src/trace.rs | 10 ++--- crates/rpc/rpc/src/validation.rs | 15 +++---- .../stages/src/stages/hashing_storage.rs | 6 +-- .../static-file/src/static_file_producer.rs | 2 +- .../src/providers/blockchain_provider.rs | 44 +++++++++---------- 15 files changed, 92 insertions(+), 102 deletions(-) diff --git a/crates/blockchain-tree/src/blockchain_tree.rs b/crates/blockchain-tree/src/blockchain_tree.rs index 1e5bf5bd242c..ee73eb34f529 100644 --- a/crates/blockchain-tree/src/blockchain_tree.rs +++ b/crates/blockchain-tree/src/blockchain_tree.rs @@ -2338,7 +2338,7 @@ mod tests { assert_eq!( tree.insert_block(block2b.clone(), BlockValidationKind::Exhaustive).unwrap(), InsertPayloadOk::Inserted(BlockStatus::Disconnected { - head: block2.header.num_hash(), + head: block2.num_hash(), missing_ancestor: block2b.parent_num_hash() }) ); diff --git a/crates/blockchain-tree/src/state.rs b/crates/blockchain-tree/src/state.rs index 2d01293e20f8..762ced6bf486 100644 --- a/crates/blockchain-tree/src/state.rs +++ b/crates/blockchain-tree/src/state.rs @@ -191,10 +191,10 @@ mod tests { let mut block1 = block.clone(); let mut block2 = block; - block1.block.header.set_hash(block1_hash); - block1.block.header.set_block_number(9); - block2.block.header.set_hash(block2_hash); - block2.block.header.set_block_number(10); + block1.block.set_hash(block1_hash); + block1.block.set_block_number(9); + block2.block.set_hash(block2_hash); + block2.block.set_block_number(10); let chain = AppendableChain::new(Chain::new( [block1, block2], @@ -257,10 +257,10 @@ mod tests { let mut block1: SealedBlockWithSenders = Default::default(); let mut block2: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(block1_hash); - block1.block.header.set_block_number(9); - block2.block.header.set_hash(block2_hash); - block2.block.header.set_block_number(10); + block1.block.set_hash(block1_hash); + block1.block.set_block_number(9); + block2.block.set_hash(block2_hash); + block2.block.set_block_number(10); // Create an chain with these blocks let chain = AppendableChain::new(Chain::new( @@ -299,10 +299,10 @@ mod tests { let mut block1: SealedBlockWithSenders = Default::default(); let mut block2: SealedBlockWithSenders = Default::default(); - block1.block.header.set_hash(block1_hash); - block1.block.header.set_block_number(9); - block2.block.header.set_hash(block2_hash); - block2.block.header.set_block_number(10); + block1.block.set_hash(block1_hash); + block1.block.set_block_number(9); + block2.block.set_hash(block2_hash); + block2.block.set_block_number(10); // Create a chain with these blocks let chain = AppendableChain::new(Chain::new( @@ -337,7 +337,7 @@ mod tests { // Create a block with a random hash and add it to the buffer let block_hash = B256::random(); let mut block: SealedBlockWithSenders = Default::default(); - block.block.header.set_hash(block_hash); + block.block.set_hash(block_hash); // Add the block to the buffered blocks in the TreeState tree_state.buffered_blocks.insert_block(block.clone()); @@ -366,9 +366,9 @@ mod tests { let mut ancestor_block: SealedBlockWithSenders = Default::default(); let mut descendant_block: SealedBlockWithSenders = Default::default(); - ancestor_block.block.header.set_hash(ancestor_hash); - descendant_block.block.header.set_hash(descendant_hash); - descendant_block.block.header.set_parent_hash(ancestor_hash); + ancestor_block.block.set_hash(ancestor_hash); + descendant_block.block.set_hash(descendant_hash); + descendant_block.block.set_parent_hash(ancestor_hash); // Insert the blocks into the buffer tree_state.buffered_blocks.insert_block(ancestor_block.clone()); @@ -398,7 +398,7 @@ mod tests { let receipt2 = Receipt::default(); let mut block: SealedBlockWithSenders = Default::default(); - block.block.header.set_hash(block_hash); + block.block.set_hash(block_hash); let receipts = vec![receipt1, receipt2]; diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 1c1e5b0ff172..49c20bacdc2b 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -18,7 +18,7 @@ use reth_primitives::{ use reth_primitives_traits::{Block, BlockBody as _, SignedTransaction}; use reth_storage_api::StateProviderBox; use reth_trie::{updates::TrieUpdates, HashedPostState}; -use std::{collections::BTreeMap, ops::Deref, sync::Arc, time::Instant}; +use std::{collections::BTreeMap, sync::Arc, time::Instant}; use tokio::sync::{broadcast, watch}; /// Size of the broadcast channel used to notify canonical state events. @@ -181,9 +181,9 @@ impl CanonicalInMemoryState { safe: Option>, ) -> Self { let in_memory_state = InMemoryState::new(blocks, numbers, pending); - let header = in_memory_state - .head_state() - .map_or_else(SealedHeader::default, |state| state.block_ref().block().deref().clone()); + let header = in_memory_state.head_state().map_or_else(SealedHeader::default, |state| { + state.block_ref().block().sealed_header().clone() + }); let chain_info_tracker = ChainInfoTracker::new(header, finalized, safe); let (canon_state_notification_sender, _) = broadcast::channel(CANON_STATE_NOTIFICATION_CHANNEL_SIZE); @@ -228,11 +228,8 @@ impl CanonicalInMemoryState { } /// Returns the header corresponding to the given hash. - pub fn header_by_hash( - &self, - hash: B256, - ) -> Option::Block>> { - self.state_by_hash(hash).map(|block| block.block_ref().block.deref().clone()) + pub fn header_by_hash(&self, hash: B256) -> Option> { + self.state_by_hash(hash).map(|block| block.block_ref().block.sealed_header().clone()) } /// Clears all entries in the in memory state. @@ -465,7 +462,7 @@ impl CanonicalInMemoryState { /// Returns the `SealedHeader` corresponding to the pending state. pub fn pending_sealed_header(&self) -> Option> { - self.pending_state().map(|h| h.block_ref().block().deref().clone()) + self.pending_state().map(|h| h.block_ref().block().sealed_header().clone()) } /// Returns the `Header` corresponding to the pending state. @@ -1324,7 +1321,7 @@ mod tests { assert_eq!(state.pending_header().unwrap(), block2.block().header().clone()); // Check the pending sealed header - assert_eq!(state.pending_sealed_header().unwrap(), block2.block().deref().clone()); + assert_eq!(state.pending_sealed_header().unwrap(), block2.block().sealed_header().clone()); // Check the pending block with senders assert_eq!( diff --git a/crates/consensus/beacon/src/engine/mod.rs b/crates/consensus/beacon/src/engine/mod.rs index c91e0719a06b..9c1adb2bd783 100644 --- a/crates/consensus/beacon/src/engine/mod.rs +++ b/crates/consensus/beacon/src/engine/mod.rs @@ -2485,7 +2485,7 @@ mod tests { ..Default::default() }, ); - block1.header.set_difficulty(U256::from(1)); + block1.set_difficulty(U256::from(1)); // a second pre-merge block let mut block2 = random_block( @@ -2497,7 +2497,7 @@ mod tests { ..Default::default() }, ); - block2.header.set_difficulty(U256::from(1)); + block2.set_difficulty(U256::from(1)); // a transition block let mut block3 = random_block( @@ -2509,7 +2509,7 @@ mod tests { ..Default::default() }, ); - block3.header.set_difficulty(U256::from(1)); + block3.set_difficulty(U256::from(1)); let (_static_dir, static_dir_path) = create_test_static_files_dir(); insert_blocks( @@ -2883,7 +2883,7 @@ mod tests { async fn payload_pre_merge() { let data = BlockchainTestData::default(); let mut block1 = data.blocks[0].0.block.clone(); - block1.header.set_difficulty( + block1.set_difficulty( MAINNET.fork(EthereumHardfork::Paris).ttd().unwrap() - U256::from(1), ); block1 = block1.unseal::().seal_slow(); diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index fea93c8a5348..17128de83bc9 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -936,7 +936,7 @@ where while current_canonical_number > current_number { if let Some(block) = self.executed_block_by_hash(old_hash)? { old_chain.push(block.clone()); - old_hash = block.block.header.parent_hash(); + old_hash = block.block.parent_hash(); current_canonical_number -= 1; } else { // This shouldn't happen as we're walking back the canonical chain @@ -952,7 +952,7 @@ where // a common ancestor (fork block) is reached. while old_hash != current_hash { if let Some(block) = self.executed_block_by_hash(old_hash)? { - old_hash = block.block.header.parent_hash(); + old_hash = block.block.parent_hash(); old_chain.push(block); } else { // This shouldn't happen as we're walking back the canonical chain @@ -1082,7 +1082,7 @@ where // 2. ensure we can apply a new chain update for the head block if let Some(chain_update) = self.on_new_head(state.head_block_hash)? { - let tip = chain_update.tip().header.clone(); + let tip = chain_update.tip().sealed_header().clone(); self.on_canonical_chain_update(chain_update); // update the safe and finalized blocks and ensure their values are valid @@ -1617,8 +1617,11 @@ where hash: B256, ) -> ProviderResult>> { // check memory first - let block = - self.state.tree_state.block_by_hash(hash).map(|block| block.as_ref().clone().header); + let block = self + .state + .tree_state + .block_by_hash(hash) + .map(|block| block.as_ref().sealed_header().clone()); if block.is_some() { Ok(block) @@ -2031,7 +2034,7 @@ where // update the tracked canonical head self.state.tree_state.set_canonical_head(chain_update.tip().num_hash()); - let tip = chain_update.tip().header.clone(); + let tip = chain_update.tip().sealed_header().clone(); let notification = chain_update.to_chain_notification(); // reinsert any missing reorged blocks @@ -2543,7 +2546,7 @@ where }; // keep track of the invalid header - self.state.invalid_headers.insert(block.header.block_with_parent()); + self.state.invalid_headers.insert(block.block_with_parent()); Ok(PayloadStatus::new( PayloadStatusEnum::Invalid { validation_error: validation_err.to_string() }, latest_valid_hash, diff --git a/crates/exex/exex/src/manager.rs b/crates/exex/exex/src/manager.rs index 3edc9d8d3708..32944bd2805f 100644 --- a/crates/exex/exex/src/manager.rs +++ b/crates/exex/exex/src/manager.rs @@ -1327,7 +1327,7 @@ mod tests { }; let (finalized_headers_tx, rx) = watch::channel(None); - finalized_headers_tx.send(Some(genesis_block.header.clone()))?; + finalized_headers_tx.send(Some(genesis_block.sealed_header().clone()))?; let finalized_header_stream = ForkChoiceStream::new(rx); let mut exex_manager = std::pin::pin!(ExExManager::new( @@ -1361,7 +1361,7 @@ mod tests { [notification.clone()] ); - finalized_headers_tx.send(Some(block.header.clone()))?; + finalized_headers_tx.send(Some(block.sealed_header().clone()))?; assert!(exex_manager.as_mut().poll(&mut cx).is_pending()); // WAL isn't finalized because the ExEx didn't emit the `FinishedHeight` event assert_eq!( @@ -1374,7 +1374,7 @@ mod tests { .send(ExExEvent::FinishedHeight((rng.gen::(), rng.gen::()).into())) .unwrap(); - finalized_headers_tx.send(Some(block.header.clone()))?; + finalized_headers_tx.send(Some(block.sealed_header().clone()))?; assert!(exex_manager.as_mut().poll(&mut cx).is_pending()); // WAL isn't finalized because the ExEx emitted a `FinishedHeight` event with a // non-canonical block @@ -1386,7 +1386,7 @@ mod tests { // Send a `FinishedHeight` event with a canonical block events_tx.send(ExExEvent::FinishedHeight(block.num_hash())).unwrap(); - finalized_headers_tx.send(Some(block.header.clone()))?; + finalized_headers_tx.send(Some(block.sealed_header().clone()))?; assert!(exex_manager.as_mut().poll(&mut cx).is_pending()); // WAL is finalized assert_eq!(exex_manager.wal.iter_notifications()?.next().transpose()?, None); diff --git a/crates/net/downloaders/src/test_utils/mod.rs b/crates/net/downloaders/src/test_utils/mod.rs index 67c35e1a26ed..2e8d5365c0db 100644 --- a/crates/net/downloaders/src/test_utils/mod.rs +++ b/crates/net/downloaders/src/test_utils/mod.rs @@ -7,11 +7,7 @@ use alloy_primitives::B256; use futures::SinkExt; use reth_primitives::{BlockBody, SealedHeader}; use reth_testing_utils::generators::{self, random_block_range, BlockRangeParams}; -use std::{ - collections::HashMap, - io::SeekFrom, - ops::{Deref, RangeInclusive}, -}; +use std::{collections::HashMap, io::SeekFrom, ops::RangeInclusive}; use tokio::{fs::File, io::AsyncSeekExt}; use tokio_util::codec::FramedWrite; @@ -32,7 +28,7 @@ pub(crate) fn generate_bodies( BlockRangeParams { parent: Some(B256::ZERO), tx_count: 0..2, ..Default::default() }, ); - let headers = blocks.iter().map(|block| block.deref().clone()).collect(); + let headers = blocks.iter().map(|block| block.sealed_header().clone()).collect(); let bodies = blocks.into_iter().map(|block| (block.hash(), block.into_body())).collect(); (headers, bodies) diff --git a/crates/node/events/src/node.rs b/crates/node/events/src/node.rs index 129fe20ea785..4b38b4050bb4 100644 --- a/crates/node/events/src/node.rs +++ b/crates/node/events/src/node.rs @@ -255,12 +255,12 @@ impl NodeState { hash=?block.hash(), peers=self.num_connected_peers(), txs=block.body().transactions().len(), - gas=%format_gas(block.header.gas_used()), - gas_throughput=%format_gas_throughput(block.header.gas_used(), elapsed), - full=%format!("{:.1}%", block.header.gas_used() as f64 * 100.0 / block.header.gas_limit() as f64), - base_fee=%format!("{:.2}gwei", block.header.base_fee_per_gas().unwrap_or(0) as f64 / GWEI_TO_WEI as f64), - blobs=block.header.blob_gas_used().unwrap_or(0) / alloy_eips::eip4844::DATA_GAS_PER_BLOB, - excess_blobs=block.header.excess_blob_gas().unwrap_or(0) / alloy_eips::eip4844::DATA_GAS_PER_BLOB, + gas=%format_gas(block.gas_used()), + gas_throughput=%format_gas_throughput(block.gas_used(), elapsed), + full=%format!("{:.1}%", block.gas_used() as f64 * 100.0 / block.gas_limit() as f64), + base_fee=%format!("{:.2}gwei", block.base_fee_per_gas().unwrap_or(0) as f64 / GWEI_TO_WEI as f64), + blobs=block.blob_gas_used().unwrap_or(0) / alloy_eips::eip4844::DATA_GAS_PER_BLOB, + excess_blobs=block.excess_blob_gas().unwrap_or(0) / alloy_eips::eip4844::DATA_GAS_PER_BLOB, ?elapsed, "Block added to canonical chain" ); diff --git a/crates/rpc/rpc-eth-api/src/helpers/call.rs b/crates/rpc/rpc-eth-api/src/helpers/call.rs index 2aecb500dd5e..91bedbeb532e 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/call.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/call.rs @@ -126,10 +126,7 @@ pub trait EthCall: EstimateCall + Call + LoadPendingBlock + LoadBlock + FullEthA base_fee_params, ) } else { - base_block - .header - .next_block_base_fee(base_fee_params) - .unwrap_or_default() + base_block.next_block_base_fee(base_fee_params).unwrap_or_default() }; block_env.basefee = U256::from(base_fee); } else { diff --git a/crates/rpc/rpc-eth-api/src/helpers/trace.rs b/crates/rpc/rpc-eth-api/src/helpers/trace.rs index 66c6b5a27a92..bb4c9c5ebf58 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/trace.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/trace.rs @@ -496,12 +496,12 @@ pub trait Trace: db, cfg, block_env, - block.header.parent_beacon_block_root(), + block.parent_beacon_block_root(), ) .map_err(|_| EthApiError::EvmCustom("failed to apply 4788 system call".to_string()))?; system_caller - .pre_block_blockhashes_contract_call(db, cfg, block_env, block.header.parent_hash()) + .pre_block_blockhashes_contract_call(db, cfg, block_env, block.parent_hash()) .map_err(|_| { EthApiError::EvmCustom("failed to apply blockhashes system call".to_string()) })?; diff --git a/crates/rpc/rpc/src/trace.rs b/crates/rpc/rpc/src/trace.rs index 33e32aec0281..d906419021b9 100644 --- a/crates/rpc/rpc/src/trace.rs +++ b/crates/rpc/rpc/src/trace.rs @@ -314,7 +314,7 @@ where if let Some(base_block_reward) = self.calculate_base_block_reward(block.header())? { all_traces.extend( self.extract_reward_traces( - block.header.header(), + block.header(), block.body().ommers(), base_block_reward, ) @@ -391,11 +391,9 @@ where maybe_traces.map(|traces| traces.into_iter().flatten().collect::>()); if let (Some(block), Some(traces)) = (maybe_block, maybe_traces.as_mut()) { - if let Some(base_block_reward) = - self.calculate_base_block_reward(block.header.header())? - { + if let Some(base_block_reward) = self.calculate_base_block_reward(block.header())? { traces.extend(self.extract_reward_traces( - block.block.header(), + block.header(), block.body().ommers(), base_block_reward, )); @@ -490,7 +488,7 @@ where Ok(Some(BlockOpcodeGas { block_hash: block.hash(), - block_number: block.header.number(), + block_number: block.number(), transactions, })) } diff --git a/crates/rpc/rpc/src/validation.rs b/crates/rpc/rpc/src/validation.rs index cb3ab4f296cf..dcddd793836d 100644 --- a/crates/rpc/rpc/src/validation.rs +++ b/crates/rpc/rpc/src/validation.rs @@ -102,10 +102,10 @@ where message: BidTrace, registered_gas_limit: u64, ) -> Result<(), ValidationApiError> { - self.validate_message_against_header(&block.header, &message)?; + self.validate_message_against_header(block.sealed_header(), &message)?; - self.consensus.validate_header_with_total_difficulty(&block.header, U256::MAX)?; - self.consensus.validate_header(&block.header)?; + self.consensus.validate_header_with_total_difficulty(block.sealed_header(), U256::MAX)?; + self.consensus.validate_header(block.sealed_header())?; self.consensus.validate_block_pre_execution(&block)?; if !self.disallow.is_empty() { @@ -130,15 +130,14 @@ where let latest_header = self.provider.latest_header()?.ok_or_else(|| ValidationApiError::MissingLatestBlock)?; - if latest_header.hash() != block.header.parent_hash() { + if latest_header.hash() != block.parent_hash() { return Err(ConsensusError::ParentHashMismatch( - GotExpected { got: block.header.parent_hash(), expected: latest_header.hash() } - .into(), + GotExpected { got: block.parent_hash(), expected: latest_header.hash() }.into(), ) .into()) } - self.consensus.validate_header_against_parent(&block.header, &latest_header)?; - self.validate_gas_limit(registered_gas_limit, &latest_header, &block.header)?; + self.consensus.validate_header_against_parent(block.sealed_header(), &latest_header)?; + self.validate_gas_limit(registered_gas_limit, &latest_header, block.sealed_header())?; let latest_header_hash = latest_header.hash(); let state_provider = self.provider.state_by_block_hash(latest_header_hash)?; diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs index 6075e62158fd..78d55ddfc9e3 100644 --- a/crates/stages/stages/src/stages/hashing_storage.rs +++ b/crates/stages/stages/src/stages/hashing_storage.rs @@ -344,7 +344,7 @@ mod tests { BlockRangeParams { parent: Some(B256::ZERO), tx_count: 0..3, ..Default::default() }, ); - self.db.insert_headers(blocks.iter().map(|block| &block.header))?; + self.db.insert_headers(blocks.iter().map(|block| block.sealed_header()))?; let iter = blocks.iter(); let mut next_tx_num = 0; @@ -373,7 +373,7 @@ mod tests { tx, (block_number, *addr).into(), new_entry, - progress.header.number == stage_progress, + progress.number == stage_progress, )?; } @@ -392,7 +392,7 @@ mod tests { key: keccak256("mining"), value: U256::from(rng.gen::()), }, - progress.header.number == stage_progress, + progress.number == stage_progress, )?; } diff --git a/crates/static-file/static-file/src/static_file_producer.rs b/crates/static-file/static-file/src/static_file_producer.rs index 7653d0d5af3d..fcbbb9e3b0a3 100644 --- a/crates/static-file/static-file/src/static_file_producer.rs +++ b/crates/static-file/static-file/src/static_file_producer.rs @@ -292,7 +292,7 @@ mod tests { let tx = db.factory.db_ref().tx_mut().expect("init tx"); for block in &blocks { - TestStageDB::insert_header(None, &tx, &block.header, U256::ZERO) + TestStageDB::insert_header(None, &tx, block.sealed_header(), U256::ZERO) .expect("insert block header"); } tx.commit().expect("commit tx"); diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 6587047f1c39..00517d41bb6a 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -1357,7 +1357,7 @@ mod tests { let in_memory_block = in_memory_blocks.last().unwrap().clone(); // make sure that the finalized block is on db let finalized_block = database_blocks.get(database_blocks.len() - 3).unwrap(); - provider.set_finalized(finalized_block.deref().clone()); + provider.set_finalized(finalized_block.sealed_header().clone()); let blocks = [database_blocks, in_memory_blocks].concat(); @@ -1548,29 +1548,29 @@ mod tests { let block_number = database_block.number; assert_eq!( provider.header_by_number_or_tag(block_number.into()).unwrap(), - Some(database_block.deref().clone().unseal()) + Some(database_block.deref().header().clone()) ); assert_eq!( provider.sealed_header_by_number_or_tag(block_number.into())?, - Some(database_block.deref().clone()) + Some(database_block.sealed_header().clone()) ); assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.deref().clone().unseal()) + Some(canonical_block.header().clone()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Latest).unwrap(), - Some(canonical_block.deref().clone()) + Some(canonical_block.sealed_header().clone()) ); assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.deref().clone().unseal()) + Some(safe_block.header().clone()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Safe).unwrap(), - Some(safe_block.deref().clone()) + Some(safe_block.sealed_header().clone()) ); assert_eq!( @@ -1579,7 +1579,7 @@ mod tests { ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Finalized).unwrap(), - Some(finalized_block.deref().clone()) + Some(finalized_block.sealed_header().clone()) ); Ok(()) @@ -1603,20 +1603,20 @@ mod tests { assert_eq!( provider.header_by_id(block_number.into()).unwrap(), - Some(database_block.deref().clone().unseal()) + Some(database_block.header().clone()) ); assert_eq!( provider.sealed_header_by_id(block_number.into()).unwrap(), - Some(database_block.deref().clone()) + Some(database_block.sealed_header().clone()) ); assert_eq!( provider.header_by_id(block_hash.into()).unwrap(), - Some(database_block.deref().clone().unseal()) + Some(database_block.header().clone()) ); assert_eq!( provider.sealed_header_by_id(block_hash.into()).unwrap(), - Some(database_block.deref().clone()) + Some(database_block.sealed_header().clone()) ); let block_number = in_memory_block.number; @@ -1624,20 +1624,20 @@ mod tests { assert_eq!( provider.header_by_id(block_number.into()).unwrap(), - Some(in_memory_block.deref().clone().unseal()) + Some(in_memory_block.header().clone()) ); assert_eq!( provider.sealed_header_by_id(block_number.into()).unwrap(), - Some(in_memory_block.deref().clone()) + Some(in_memory_block.sealed_header().clone()) ); assert_eq!( provider.header_by_id(block_hash.into()).unwrap(), - Some(in_memory_block.deref().clone().unseal()) + Some(in_memory_block.header().clone()) ); assert_eq!( provider.sealed_header_by_id(block_hash.into()).unwrap(), - Some(in_memory_block.deref().clone()) + Some(in_memory_block.sealed_header().clone()) ); Ok(()) @@ -2023,7 +2023,7 @@ mod tests { ); // test state by block tag for safe block let safe_block = in_memory_blocks[in_memory_blocks.len() - 2].clone(); - in_memory_provider.canonical_in_memory_state.set_safe(safe_block.deref().clone()); + in_memory_provider.canonical_in_memory_state.set_safe(safe_block.sealed_header().clone()); assert_eq!( safe_block.hash(), in_memory_provider @@ -2106,11 +2106,11 @@ mod tests { // Set the safe block in memory let safe_block = in_memory_blocks[in_memory_blocks.len() - 2].clone(); - provider.canonical_in_memory_state.set_safe(safe_block.deref().clone()); + provider.canonical_in_memory_state.set_safe(safe_block.sealed_header().clone()); // Set the finalized block in memory let finalized_block = in_memory_blocks[in_memory_blocks.len() - 3].clone(); - provider.canonical_in_memory_state.set_finalized(finalized_block.deref().clone()); + provider.canonical_in_memory_state.set_finalized(finalized_block.sealed_header().clone()); // Verify the pending block number and hash assert_eq!( @@ -2324,8 +2324,8 @@ mod tests { // todo(joshie) add canonical_hashes_range below after changing its interface into range // instead start end test_by_block_range!([ - (headers_range, |block: &SealedBlock| block.header().clone().into()), - (sealed_headers_range, |block: &SealedBlock| block.deref().clone()), + (headers_range, |block: &SealedBlock| block.header().clone()), + (sealed_headers_range, |block: &SealedBlock| block.sealed_header().clone()), (block_range, |block: &SealedBlock| block.clone().unseal()), (block_with_senders_range, |block: &SealedBlock| block .clone() @@ -2458,7 +2458,7 @@ mod tests { header_by_number, |block: &SealedBlock, _: TxNumber, _: B256, _: &Vec>| ( block.number, - Some(block.sealed_header().clone()) + Some(block.header().clone()) ), u64::MAX ), From 1ec45e1a0d13a050f1321ae3dfb88a4302249e8c Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 7 Jan 2025 19:52:38 +0100 Subject: [PATCH 08/10] make it compile --- .../provider/src/providers/blockchain_provider.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/storage/provider/src/providers/blockchain_provider.rs b/crates/storage/provider/src/providers/blockchain_provider.rs index 00517d41bb6a..85f4b7e77279 100644 --- a/crates/storage/provider/src/providers/blockchain_provider.rs +++ b/crates/storage/provider/src/providers/blockchain_provider.rs @@ -1376,7 +1376,7 @@ mod tests { blocks .iter() .take_while(|header| header.number <= 8) - .map(|b| b.deref().clone()) + .map(|b| b.sealed_header().clone()) .collect::>() ); @@ -1548,7 +1548,7 @@ mod tests { let block_number = database_block.number; assert_eq!( provider.header_by_number_or_tag(block_number.into()).unwrap(), - Some(database_block.deref().header().clone()) + Some(database_block.header().clone()) ); assert_eq!( provider.sealed_header_by_number_or_tag(block_number.into())?, @@ -1575,7 +1575,7 @@ mod tests { assert_eq!( provider.header_by_number_or_tag(BlockNumberOrTag::Finalized).unwrap(), - Some(finalized_block.deref().clone().unseal()) + Some(finalized_block.header().clone()) ); assert_eq!( provider.sealed_header_by_number_or_tag(BlockNumberOrTag::Finalized).unwrap(), @@ -2033,7 +2033,9 @@ mod tests { ); // test state by block tag for finalized block let finalized_block = in_memory_blocks[in_memory_blocks.len() - 3].clone(); - in_memory_provider.canonical_in_memory_state.set_finalized(finalized_block.deref().clone()); + in_memory_provider + .canonical_in_memory_state + .set_finalized(finalized_block.sealed_header().clone()); assert_eq!( finalized_block.hash(), in_memory_provider From 990123bf1f187716aa49e1450328a26dcec1fdfd Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 7 Jan 2025 19:54:53 +0100 Subject: [PATCH 09/10] make it compile --- crates/storage/provider/src/providers/database/provider.rs | 7 ++----- crates/storage/provider/src/test_utils/blocks.rs | 7 ++----- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 3dc760620c83..2c0427d787bd 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -446,7 +446,7 @@ impl< } } - writer.append_header(block.deref().as_ref(), ttd, &block.hash())?; + writer.append_header(block.header(), ttd, &block.hash())?; self.insert_block(block, StorageLocation::Database) } @@ -2769,10 +2769,7 @@ impl BlockWrite durations_recorder.record_relative(metrics::Action::InsertCanonicalHeaders); // Put header with canonical hashes. - self.tx.put::>>( - block_number, - block.deref().as_ref().clone(), - )?; + self.tx.put::>>(block_number, block.header().clone())?; durations_recorder.record_relative(metrics::Action::InsertHeaders); self.tx.put::(block_number, ttd.into())?; diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 739c2b47d87f..2f46ef5c1e70 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -17,7 +17,7 @@ use reth_primitives::{ }; use reth_trie::root::{state_root_unhashed, storage_root_unhashed}; use revm::{db::BundleState, primitives::AccountInfo}; -use std::{ops::Deref, str::FromStr, sync::LazyLock}; +use std::{str::FromStr, sync::LazyLock}; /// Assert genesis block pub fn assert_genesis_block( @@ -29,10 +29,7 @@ pub fn assert_genesis_block( let tx = provider; // check if all tables are empty - assert_eq!( - tx.table::().unwrap(), - vec![(g.number, g.deref().clone().unseal())] - ); + assert_eq!(tx.table::().unwrap(), vec![(g.number, g.header().clone())]); assert_eq!(tx.table::().unwrap(), vec![(h, n)]); assert_eq!(tx.table::().unwrap(), vec![(n, h)]); From ca1e36f05c218dc9c543c616b00a875cbe4bf89b Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 7 Jan 2025 19:58:13 +0100 Subject: [PATCH 10/10] bench fixes --- crates/stages/stages/benches/setup/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/stages/stages/benches/setup/mod.rs b/crates/stages/stages/benches/setup/mod.rs index 2c1174d63292..d6bf4414450f 100644 --- a/crates/stages/stages/benches/setup/mod.rs +++ b/crates/stages/stages/benches/setup/mod.rs @@ -151,7 +151,7 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB { .unwrap(); let second_block = blocks.get_mut(1).unwrap(); let cloned_second = second_block.clone(); - let mut updated_header = cloned_second.header.clone().unseal(); + let mut updated_header = cloned_second.header().clone(); updated_header.state_root = root; *second_block = SealedBlock::new(SealedHeader::seal(updated_header), cloned_second.into_body()); @@ -185,7 +185,7 @@ pub(crate) fn txs_testdata(num_blocks: u64) -> TestStageDB { let last_block = blocks.last_mut().unwrap(); let cloned_last = last_block.clone(); - let mut updated_header = cloned_last.header.clone().unseal(); + let mut updated_header = cloned_last.header().clone(); updated_header.state_root = root; *last_block = SealedBlock::new(SealedHeader::seal(updated_header), cloned_last.into_body());