From 4c39b98b621c53524c6533a9c7b52fc42c25abd6 Mon Sep 17 00:00:00 2001 From: joshieDo <93316087+joshieDo@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:56:35 +0000 Subject: [PATCH] chore: add `StorageLocation` to `BlockBodyWriter` trait (#13266) --- crates/optimism/node/src/node.rs | 8 +++-- .../src/providers/database/provider.rs | 20 ++++++------ crates/storage/provider/src/traits/block.rs | 31 +++---------------- crates/storage/storage-api/src/chain.rs | 6 +++- crates/storage/storage-api/src/storage.rs | 23 ++++++++++++++ 5 files changed, 47 insertions(+), 41 deletions(-) diff --git a/crates/optimism/node/src/node.rs b/crates/optimism/node/src/node.rs index e9e7e23bc9cd..e77b50f1e8f0 100644 --- a/crates/optimism/node/src/node.rs +++ b/crates/optimism/node/src/node.rs @@ -36,7 +36,7 @@ use reth_payload_builder::{PayloadBuilderHandle, PayloadBuilderService}; use reth_primitives::{BlockBody, PooledTransactionsElement, TransactionSigned}; use reth_provider::{ providers::ChainStorage, BlockBodyReader, BlockBodyWriter, CanonStateSubscriptions, - ChainSpecProvider, DBProvider, EthStorage, ProviderResult, ReadBodyInput, + ChainSpecProvider, DBProvider, EthStorage, ProviderResult, ReadBodyInput, StorageLocation, }; use reth_rpc_server_types::RethRpcModule; use reth_tracing::tracing::{debug, info}; @@ -56,16 +56,18 @@ impl> BlockBodyWriter for &self, provider: &Provider, bodies: Vec<(u64, Option)>, + write_to: StorageLocation, ) -> ProviderResult<()> { - self.0.write_block_bodies(provider, bodies) + self.0.write_block_bodies(provider, bodies, write_to) } fn remove_block_bodies_above( &self, provider: &Provider, block: alloy_primitives::BlockNumber, + remove_from: StorageLocation, ) -> ProviderResult<()> { - self.0.remove_block_bodies_above(provider, block) + self.0.remove_block_bodies_above(provider, block, remove_from) } } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 05e4ed4c0c00..aa6a167d7153 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -2893,12 +2893,12 @@ impl BlockWrite fn append_block_bodies( &self, bodies: Vec<(BlockNumber, Option>)>, - write_transactions_to: StorageLocation, + write_to: StorageLocation, ) -> ProviderResult<()> { let Some(from_block) = bodies.first().map(|(block, _)| *block) else { return Ok(()) }; // Initialize writer if we will be writing transactions to staticfiles - let mut tx_static_writer = write_transactions_to + let mut tx_static_writer = write_to .static_files() .then(|| { self.static_file_provider.get_writer(from_block, StaticFileSegment::Transactions) @@ -2909,7 +2909,7 @@ impl BlockWrite let mut tx_block_cursor = self.tx.cursor_write::()?; // Initialize cursor if we will be writing transactions to database - let mut tx_cursor = write_transactions_to + let mut tx_cursor = write_to .database() .then(|| self.tx.cursor_write::>>()) .transpose()?; @@ -2962,7 +2962,7 @@ impl BlockWrite ); } - self.storage.writer().write_block_bodies(self, bodies)?; + self.storage.writer().write_block_bodies(self, bodies, write_to)?; Ok(()) } @@ -2970,7 +2970,7 @@ impl BlockWrite fn remove_blocks_above( &self, block: BlockNumber, - remove_transactions_from: StorageLocation, + remove_from: StorageLocation, ) -> ProviderResult<()> { let mut canonical_headers_cursor = self.tx.cursor_write::()?; let mut rev_headers = canonical_headers_cursor.walk_back(None)?; @@ -3010,7 +3010,7 @@ impl BlockWrite self.remove::(unwind_tx_from..)?; - self.remove_bodies_above(block, remove_transactions_from)?; + self.remove_bodies_above(block, remove_from)?; Ok(()) } @@ -3018,9 +3018,9 @@ impl BlockWrite fn remove_bodies_above( &self, block: BlockNumber, - remove_transactions_from: StorageLocation, + remove_from: StorageLocation, ) -> ProviderResult<()> { - self.storage.writer().remove_block_bodies_above(self, block)?; + self.storage.writer().remove_block_bodies_above(self, block, remove_from)?; // First transaction to be removed let unwind_tx_from = self @@ -3032,11 +3032,11 @@ impl BlockWrite self.remove::(block + 1..)?; self.remove::(unwind_tx_from..)?; - if remove_transactions_from.database() { + if remove_from.database() { self.remove::>>(unwind_tx_from..)?; } - if remove_transactions_from.static_files() { + if remove_from.static_files() { let static_file_tx_num = self .static_file_provider .get_highest_static_file_tx(StaticFileSegment::Transactions); diff --git a/crates/storage/provider/src/traits/block.rs b/crates/storage/provider/src/traits/block.rs index d12f240e6164..9c5821057fc8 100644 --- a/crates/storage/provider/src/traits/block.rs +++ b/crates/storage/provider/src/traits/block.rs @@ -3,33 +3,10 @@ use reth_db_api::models::StoredBlockBodyIndices; use reth_execution_types::{Chain, ExecutionOutcome}; use reth_node_types::NodePrimitives; use reth_primitives::SealedBlockWithSenders; -use reth_storage_api::NodePrimitivesProvider; +use reth_storage_api::{NodePrimitivesProvider, StorageLocation}; use reth_storage_errors::provider::ProviderResult; use reth_trie::{updates::TrieUpdates, HashedPostStateSorted}; -/// An enum that represents the storage location for a piece of data. -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum StorageLocation { - /// Write only to static files. - StaticFiles, - /// Write only to the database. - Database, - /// Write to both the database and static files. - Both, -} - -impl StorageLocation { - /// Returns true if the storage location includes static files. - pub const fn static_files(&self) -> bool { - matches!(self, Self::StaticFiles | Self::Both) - } - - /// Returns true if the storage location includes the database. - pub const fn database(&self) -> bool { - matches!(self, Self::Database | Self::Both) - } -} - /// `BlockExecution` Writer pub trait BlockExecutionWriter: NodePrimitivesProvider> + BlockWriter + Send + Sync @@ -120,7 +97,7 @@ pub trait BlockWriter: Send + Sync { fn append_block_bodies( &self, bodies: Vec<(BlockNumber, Option<::Body>)>, - write_transactions_to: StorageLocation, + write_to: StorageLocation, ) -> ProviderResult<()>; /// Removes all blocks above the given block number from the database. @@ -129,14 +106,14 @@ pub trait BlockWriter: Send + Sync { fn remove_blocks_above( &self, block: BlockNumber, - remove_transactions_from: StorageLocation, + remove_from: StorageLocation, ) -> ProviderResult<()>; /// Removes all block bodies above the given block number from the database. fn remove_bodies_above( &self, block: BlockNumber, - remove_transactions_from: StorageLocation, + remove_from: StorageLocation, ) -> ProviderResult<()>; /// Appends a batch of sealed blocks to the blockchain, including sender information, and diff --git a/crates/storage/storage-api/src/chain.rs b/crates/storage/storage-api/src/chain.rs index 9b9c24c68633..978c4f51b5f4 100644 --- a/crates/storage/storage-api/src/chain.rs +++ b/crates/storage/storage-api/src/chain.rs @@ -1,4 +1,4 @@ -use crate::DBProvider; +use crate::{DBProvider, StorageLocation}; use alloy_primitives::BlockNumber; use reth_chainspec::{ChainSpecProvider, EthereumHardforks}; use reth_db::{ @@ -22,6 +22,7 @@ pub trait BlockBodyWriter { &self, provider: &Provider, bodies: Vec<(BlockNumber, Option)>, + write_to: StorageLocation, ) -> ProviderResult<()>; /// Removes all block bodies above the given block number from the database. @@ -29,6 +30,7 @@ pub trait BlockBodyWriter { &self, provider: &Provider, block: BlockNumber, + remove_from: StorageLocation, ) -> ProviderResult<()>; } @@ -87,6 +89,7 @@ where &self, provider: &Provider, bodies: Vec<(u64, Option)>, + _write_to: StorageLocation, ) -> ProviderResult<()> { let mut ommers_cursor = provider.tx_ref().cursor_write::()?; let mut withdrawals_cursor = @@ -116,6 +119,7 @@ where &self, provider: &Provider, block: BlockNumber, + _remove_from: StorageLocation, ) -> ProviderResult<()> { provider.tx_ref().unwind_table_by_num::(block)?; provider.tx_ref().unwind_table_by_num::(block)?; diff --git a/crates/storage/storage-api/src/storage.rs b/crates/storage/storage-api/src/storage.rs index e1443347e4bb..0544f9a74c1c 100644 --- a/crates/storage/storage-api/src/storage.rs +++ b/crates/storage/storage-api/src/storage.rs @@ -41,3 +41,26 @@ pub trait StorageChangeSetReader: Send + Sync { block_number: BlockNumber, ) -> ProviderResult>; } + +/// An enum that represents the storage location for a piece of data. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum StorageLocation { + /// Write only to static files. + StaticFiles, + /// Write only to the database. + Database, + /// Write to both the database and static files. + Both, +} + +impl StorageLocation { + /// Returns true if the storage location includes static files. + pub const fn static_files(&self) -> bool { + matches!(self, Self::StaticFiles | Self::Both) + } + + /// Returns true if the storage location includes the database. + pub const fn database(&self) -> bool { + matches!(self, Self::Database | Self::Both) + } +}