diff --git a/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs b/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs index 3d5e20c8f47c4..b0cae6f90b616 100644 --- a/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs +++ b/bin/reth/src/commands/debug_cmd/in_memory_merkle.rs @@ -16,9 +16,9 @@ use reth_network::{BlockDownloaderProvider, NetworkHandle}; use reth_network_api::NetworkInfo; use reth_primitives::BlockHashOrNumber; use reth_provider::{ - writer::StorageWriter, AccountExtReader, ChainSpecProvider, HashingWriter, HeaderProvider, - LatestStateProviderRef, OriginalValuesKnown, ProviderFactory, StageCheckpointReader, - StateWriter, StaticFileProviderFactory, StorageReader, + writer::UnifiedStorageWriter, AccountExtReader, ChainSpecProvider, HashingWriter, + HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, ProviderFactory, + StageCheckpointReader, StateWriter, StaticFileProviderFactory, StorageReader, }; use reth_revm::database::StateProviderDatabase; use reth_stages::StageId; @@ -171,7 +171,7 @@ impl Command { .try_seal_with_senders() .map_err(|_| BlockValidationError::SenderRecoveryError)?, )?; - let mut storage_writer = StorageWriter::new(Some(&provider_rw), None); + let mut storage_writer = UnifiedStorageWriter::from_database(&provider_rw); storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::No)?; let storage_lists = provider_rw.changed_storages_with_range(block.number..=block.number)?; let storages = provider_rw.plain_state_storages(storage_lists)?; diff --git a/bin/reth/src/commands/debug_cmd/merkle.rs b/bin/reth/src/commands/debug_cmd/merkle.rs index 9bfaf641245d7..08236e30ec4a5 100644 --- a/bin/reth/src/commands/debug_cmd/merkle.rs +++ b/bin/reth/src/commands/debug_cmd/merkle.rs @@ -18,7 +18,7 @@ use reth_network_api::NetworkInfo; use reth_network_p2p::full_block::FullBlockClient; use reth_primitives::BlockHashOrNumber; use reth_provider::{ - writer::StorageWriter, BlockNumReader, BlockWriter, ChainSpecProvider, HeaderProvider, + writer::UnifiedStorageWriter, BlockNumReader, BlockWriter, ChainSpecProvider, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, ProviderError, ProviderFactory, StateWriter, }; use reth_revm::database::StateProviderDatabase; @@ -155,7 +155,7 @@ impl Command { executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?; let execution_outcome = executor.finalize(); - let mut storage_writer = StorageWriter::new(Some(&provider_rw), None); + let mut storage_writer = UnifiedStorageWriter::from_database(&provider_rw); storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::Yes)?; let checkpoint = Some(StageCheckpoint::new( diff --git a/crates/cli/commands/src/stage/drop.rs b/crates/cli/commands/src/stage/drop.rs index ec7a91b6dda0f..d0fa6efb51587 100644 --- a/crates/cli/commands/src/stage/drop.rs +++ b/crates/cli/commands/src/stage/drop.rs @@ -9,7 +9,7 @@ use reth_db_common::{ DbTool, }; use reth_node_core::args::StageEnum; -use reth_provider::{providers::StaticFileWriter, StaticFileProviderFactory}; +use reth_provider::{writer::UnifiedStorageWriter, StaticFileProviderFactory}; use reth_stages::StageId; use reth_static_file_types::{find_fixed_range, StaticFileSegment}; @@ -174,8 +174,7 @@ impl Command { tx.put::(StageId::Finish.to_string(), Default::default())?; - static_file_provider.commit()?; - provider_rw.commit()?; + UnifiedStorageWriter::commit_unwind(provider_rw, static_file_provider)?; Ok(()) } diff --git a/crates/cli/commands/src/stage/run.rs b/crates/cli/commands/src/stage/run.rs index ddcbdd13ca76d..a7349f6e95fb4 100644 --- a/crates/cli/commands/src/stage/run.rs +++ b/crates/cli/commands/src/stage/run.rs @@ -27,8 +27,8 @@ use reth_node_metrics::{ version::VersionInfo, }; use reth_provider::{ - ChainSpecProvider, StageCheckpointReader, StageCheckpointWriter, StaticFileProviderFactory, - StaticFileWriter, + writer::UnifiedStorageWriter, ChainSpecProvider, StageCheckpointReader, StageCheckpointWriter, + StaticFileProviderFactory, }; use reth_stages::{ stages::{ @@ -272,12 +272,10 @@ impl Command { } if self.commit { - // For unwinding it makes more sense to commit the database first, since if - // this function is interrupted before the static files commit, we can just - // truncate the static files according to the - // checkpoints on the next start-up. - provider_rw.commit()?; - provider_factory.static_file_provider().commit()?; + UnifiedStorageWriter::commit_unwind( + provider_rw, + provider_factory.static_file_provider(), + )?; provider_rw = provider_factory.provider_rw()?; } } @@ -300,8 +298,7 @@ impl Command { provider_rw.save_stage_checkpoint(exec_stage.id(), checkpoint)?; } if self.commit { - provider_factory.static_file_provider().commit()?; - provider_rw.commit()?; + UnifiedStorageWriter::commit(provider_rw, provider_factory.static_file_provider())?; provider_rw = provider_factory.provider_rw()?; } diff --git a/crates/engine/tree/src/persistence.rs b/crates/engine/tree/src/persistence.rs index 7d396c3dfee1f..0c576d4ee2fd5 100644 --- a/crates/engine/tree/src/persistence.rs +++ b/crates/engine/tree/src/persistence.rs @@ -3,7 +3,7 @@ use reth_chain_state::ExecutedBlock; use reth_db::Database; use reth_primitives::{SealedBlock, B256}; -use reth_provider::{writer::StorageWriter, ProviderFactory, StaticFileProviderFactory}; +use reth_provider::{writer::UnifiedStorageWriter, ProviderFactory, StaticFileProviderFactory}; use reth_prune::{Pruner, PrunerOutput}; use std::sync::{ mpsc::{Receiver, SendError, Sender}, @@ -62,10 +62,10 @@ where let provider_rw = self.provider.provider_rw().expect("todo: handle errors"); let sf_provider = self.provider.static_file_provider(); - StorageWriter::from(&provider_rw, &sf_provider) + UnifiedStorageWriter::from(&provider_rw, &sf_provider) .remove_blocks_above(new_tip_num) .expect("todo: handle errors"); - StorageWriter::commit_unwind(provider_rw, sf_provider) + UnifiedStorageWriter::commit_unwind(provider_rw, sf_provider) .expect("todo: handle errors"); // we ignore the error because the caller may or may not care about the result @@ -80,10 +80,10 @@ where let provider_rw = self.provider.provider_rw().expect("todo: handle errors"); let static_file_provider = self.provider.static_file_provider(); - StorageWriter::from(&provider_rw, &static_file_provider) + UnifiedStorageWriter::from(&provider_rw, &static_file_provider) .save_blocks(&blocks) .expect("todo: handle errors"); - StorageWriter::commit(provider_rw, static_file_provider) + UnifiedStorageWriter::commit(provider_rw, static_file_provider) .expect("todo: handle errors"); // we ignore the error because the caller may or may not care about the result diff --git a/crates/optimism/cli/src/commands/import_receipts.rs b/crates/optimism/cli/src/commands/import_receipts.rs index 30d2f82c9c614..cf3f6cf0de27c 100644 --- a/crates/optimism/cli/src/commands/import_receipts.rs +++ b/crates/optimism/cli/src/commands/import_receipts.rs @@ -16,7 +16,7 @@ use reth_node_core::version::SHORT_VERSION; use reth_optimism_primitives::bedrock_import::is_dup_tx; use reth_primitives::Receipts; use reth_provider::{ - writer::StorageWriter, DatabaseProviderFactory, OriginalValuesKnown, ProviderFactory, + writer::UnifiedStorageWriter, DatabaseProviderFactory, OriginalValuesKnown, ProviderFactory, StageCheckpointReader, StateWriter, StaticFileProviderFactory, StaticFileWriter, StatsReader, }; use reth_stages::StageId; @@ -222,7 +222,7 @@ where } // We're reusing receipt writing code internal to - // `StorageWriter::append_receipts_from_blocks`, so we just use a default empty + // `UnifiedStorageWriter::append_receipts_from_blocks`, so we just use a default empty // `BundleState`. let execution_outcome = ExecutionOutcome::new(Default::default(), receipts, first_block, Default::default()); @@ -231,14 +231,13 @@ where static_file_provider.get_writer(first_block, StaticFileSegment::Receipts)?; // finally, write the receipts - let mut storage_writer = StorageWriter::new(Some(&provider), Some(static_file_producer)); + let mut storage_writer = UnifiedStorageWriter::from(&provider, static_file_producer); storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::Yes)?; } - provider.commit()?; // as static files works in file ranges, internally it will be committing when creating the // next file range already, so we only need to call explicitly at the end. - static_file_provider.commit()?; + UnifiedStorageWriter::commit(provider, static_file_provider)?; Ok(ImportReceiptsResult { total_decoded_receipts, total_filtered_out_dup_txns }) } diff --git a/crates/stages/api/src/pipeline/mod.rs b/crates/stages/api/src/pipeline/mod.rs index ce6b1a2863a69..79578f942d701 100644 --- a/crates/stages/api/src/pipeline/mod.rs +++ b/crates/stages/api/src/pipeline/mod.rs @@ -8,7 +8,7 @@ use futures_util::Future; use reth_db_api::database::Database; use reth_primitives_traits::constants::BEACON_CONSENSUS_REORG_UNWIND_DEPTH; use reth_provider::{ - providers::StaticFileWriter, FinalizedBlockReader, FinalizedBlockWriter, ProviderFactory, + writer::UnifiedStorageWriter, FinalizedBlockReader, FinalizedBlockWriter, ProviderFactory, StageCheckpointReader, StageCheckpointWriter, StaticFileProviderFactory, }; use reth_prune::PrunerBuilder; @@ -342,12 +342,10 @@ where ))?; } - // For unwinding it makes more sense to commit the database first, since if - // this function is interrupted before the static files commit, we can just - // truncate the static files according to the - // checkpoints on the next start-up. - provider_rw.commit()?; - self.provider_factory.static_file_provider().commit()?; + UnifiedStorageWriter::commit_unwind( + provider_rw, + self.provider_factory.static_file_provider(), + )?; stage.post_unwind_commit()?; @@ -455,14 +453,10 @@ where result: out.clone(), }); - // For execution it makes more sense to commit the static files first, since if - // this function is interrupted before the database commit, we can just truncate - // the static files according to the checkpoints on the next - // start-up. - self.provider_factory.static_file_provider().commit()?; - provider_rw.commit()?; - - stage.post_execute_commit()?; + UnifiedStorageWriter::commit( + provider_rw, + self.provider_factory.static_file_provider(), + )?; if done { let block_number = checkpoint.block_number; @@ -520,8 +514,8 @@ fn on_stage_error( StageId::MerkleExecute, prev_checkpoint.unwrap_or_default(), )?; - factory.static_file_provider().commit()?; - provider_rw.commit()?; + + UnifiedStorageWriter::commit(provider_rw, factory.static_file_provider())?; // We unwind because of a validation error. If the unwind itself // fails, we bail entirely, diff --git a/crates/stages/stages/src/stages/execution.rs b/crates/stages/stages/src/stages/execution.rs index e7acbd5afddeb..b3d2122661d23 100644 --- a/crates/stages/stages/src/stages/execution.rs +++ b/crates/stages/stages/src/stages/execution.rs @@ -10,7 +10,7 @@ use reth_primitives::{BlockNumber, Header, StaticFileSegment}; use reth_primitives_traits::format_gas_throughput; use reth_provider::{ providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter}, - writer::StorageWriter, + writer::UnifiedStorageWriter, BlockReader, DatabaseProviderRW, HeaderProvider, LatestStateProviderRef, OriginalValuesKnown, ProviderError, StateWriter, StatsReader, TransactionVariant, }; @@ -361,7 +361,7 @@ where let time = Instant::now(); // write output - let mut writer = StorageWriter::new(Some(provider), static_file_producer); + let mut writer = UnifiedStorageWriter::new(provider, static_file_producer); writer.write_to_storage(state, OriginalValuesKnown::Yes)?; let db_write_duration = time.elapsed(); diff --git a/crates/storage/db-common/src/init.rs b/crates/storage/db-common/src/init.rs index 19c48637e55a3..95324cb6820d6 100644 --- a/crates/storage/db-common/src/init.rs +++ b/crates/storage/db-common/src/init.rs @@ -13,7 +13,7 @@ use reth_primitives::{ use reth_provider::{ errors::provider::ProviderResult, providers::{StaticFileProvider, StaticFileWriter}, - writer::StorageWriter, + writer::UnifiedStorageWriter, BlockHashReader, BlockNumReader, BundleStateInit, ChainSpecProvider, DatabaseProviderRW, ExecutionOutcome, HashingWriter, HistoryWriter, OriginalValuesKnown, ProviderError, ProviderFactory, RevertsInit, StageCheckpointWriter, StateWriter, StaticFileProviderFactory, @@ -131,8 +131,9 @@ pub fn init_genesis(factory: ProviderFactory) -> Result( Vec::new(), ); - let mut storage_writer = StorageWriter::new(Some(provider), None); + let mut storage_writer = UnifiedStorageWriter::from_database(provider); storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::Yes)?; trace!(target: "reth::cli", "Inserted state"); diff --git a/crates/storage/errors/src/provider.rs b/crates/storage/errors/src/provider.rs index c3616d1c7f1dd..1fd22a9f0a840 100644 --- a/crates/storage/errors/src/provider.rs +++ b/crates/storage/errors/src/provider.rs @@ -2,7 +2,7 @@ use reth_primitives::{ Address, BlockHash, BlockHashOrNumber, BlockNumber, GotExpected, StaticFileSegment, TxHashOrNumber, TxNumber, B256, U256, }; -use crate::{db::DatabaseError, lockfile::StorageLockError, writer::StorageWriterError}; +use crate::{db::DatabaseError, lockfile::StorageLockError, writer::UnifiedStorageWriterError}; #[cfg(feature = "std")] use std::path::PathBuf; @@ -143,7 +143,7 @@ pub enum ProviderError { /// Storage lock error. StorageLockError(StorageLockError), /// Storage writer error. - StorageWriterError(StorageWriterError), + UnifiedStorageWriterError(UnifiedStorageWriterError), } #[cfg(feature = "std")] impl std::error::Error for ProviderError { @@ -158,7 +158,7 @@ impl std::error::Error for ProviderError { Self::StorageLockError(source) => { std::error::Error::source(source) }, - Self::StorageWriterError(source) => { + Self::UnifiedStorageWriterError(source) => { std::error::Error::source(source) }, _ => Option::None @@ -184,9 +184,9 @@ impl From for ProviderError { } } -impl From for ProviderError { - fn from(value: StorageWriterError) -> Self { - Self::StorageWriterError(value) +impl From for ProviderError { + fn from(value: UnifiedStorageWriterError) -> Self { + Self::UnifiedStorageWriterError(value) } } diff --git a/crates/storage/errors/src/writer.rs b/crates/storage/errors/src/writer.rs index bf5cd3d250dee..204b9f35be976 100644 --- a/crates/storage/errors/src/writer.rs +++ b/crates/storage/errors/src/writer.rs @@ -1,9 +1,9 @@ use crate::db::DatabaseError; use reth_primitives::StaticFileSegment; -/// `StorageWriter` related errors +/// `UnifiedStorageWriter` related errors #[derive(Clone, Debug, derive_more::Display, PartialEq, Eq)] -pub enum StorageWriterError { +pub enum UnifiedStorageWriterError { /// Database writer is missing #[display(fmt = "Database writer is missing")] MissingDatabaseWriter, @@ -18,7 +18,7 @@ pub enum StorageWriterError { } #[cfg(feature = "std")] -impl std::error::Error for StorageWriterError { +impl std::error::Error for UnifiedStorageWriterError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { Self::Database(source) => { @@ -29,7 +29,7 @@ impl std::error::Error for StorageWriterError { } } -impl From for StorageWriterError { +impl From for UnifiedStorageWriterError { fn from(value: DatabaseError) -> Self { Self::Database(value) } diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 7b8bf97704192..4c105e6f35a34 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -5,7 +5,7 @@ use crate::{ traits::{ AccountExtReader, BlockSource, ChangeSetReader, ReceiptProvider, StageCheckpointWriter, }, - writer::StorageWriter, + writer::UnifiedStorageWriter, AccountReader, BlockExecutionReader, BlockExecutionWriter, BlockHashReader, BlockNumReader, BlockReader, BlockWriter, BundleStateInit, EvmEnvProvider, FinalizedBlockReader, FinalizedBlockWriter, HashingWriter, HeaderProvider, HeaderSyncGap, HeaderSyncGapProvider, @@ -3570,7 +3570,7 @@ impl BlockWriter for DatabaseProvider { Ok(block_indices) } - /// TODO(joshie): this fn should be moved to `StorageWriter` eventually + /// TODO(joshie): this fn should be moved to `UnifiedStorageWriter` eventually fn append_blocks_with_state( &self, blocks: Vec, @@ -3600,7 +3600,7 @@ impl BlockWriter for DatabaseProvider { // Must be written after blocks because of the receipt lookup. // TODO: should _these_ be moved to storagewriter? seems like storagewriter should be // _above_ db provider - let mut storage_writer = StorageWriter::new(Some(self), None); + let mut storage_writer = UnifiedStorageWriter::from_database(self); storage_writer.write_to_storage(execution_outcome, OriginalValuesKnown::No)?; durations_recorder.record_relative(metrics::Action::InsertState); diff --git a/crates/storage/provider/src/writer/mod.rs b/crates/storage/provider/src/writer/mod.rs index 00776a86d924c..39dc3cf4ebc60 100644 --- a/crates/storage/provider/src/writer/mod.rs +++ b/crates/storage/provider/src/writer/mod.rs @@ -21,7 +21,7 @@ use reth_stages_types::{StageCheckpoint, StageId}; use reth_storage_api::{ BlockNumReader, HeaderProvider, ReceiptWriter, StageCheckpointWriter, TransactionsProviderExt, }; -use reth_storage_errors::writer::StorageWriterError; +use reth_storage_errors::writer::UnifiedStorageWriterError; use revm::db::OriginalValuesKnown; use std::{borrow::Borrow, sync::Arc}; use tracing::{debug, instrument}; @@ -35,46 +35,41 @@ enum StorageType { StaticFile(S), } -/// [`StorageWriter`] is responsible for managing the writing to either database, static file or -/// both. +/// [`UnifiedStorageWriter`] is responsible for managing the writing to storage with both database +/// and static file providers. #[derive(Debug)] -pub struct StorageWriter<'a, TX, SF> { - database: Option<&'a DatabaseProvider>, +pub struct UnifiedStorageWriter<'a, TX, SF> { + database: &'a DatabaseProvider, static_file: Option, } -impl<'a, TX, SF> StorageWriter<'a, TX, SF> { - /// Creates a new instance of [`StorageWriter`]. +impl<'a, TX, SF> UnifiedStorageWriter<'a, TX, SF> { + /// Creates a new instance of [`UnifiedStorageWriter`]. /// /// # Parameters /// - `database`: An optional reference to a database provider. /// - `static_file`: An optional mutable reference to a static file instance. - pub const fn new(database: Option<&'a DatabaseProvider>, static_file: Option) -> Self { + pub const fn new(database: &'a DatabaseProvider, static_file: Option) -> Self { Self { database, static_file } } - /// Creates a new instance of [`StorageWriter`] from a database provider and a static file - /// instance. + /// Creates a new instance of [`UnifiedStorageWriter`] from a database provider and a static + /// file instance. pub const fn from(database: &'a DatabaseProvider, static_file: SF) -> Self { - Self::new(Some(database), Some(static_file)) + Self::new(database, Some(static_file)) } - /// Creates a new instance of [`StorageWriter`] from a static file instance. - pub const fn from_static_file(static_file: SF) -> Self { - Self::new(None, Some(static_file)) - } - - /// Creates a new instance of [`StorageWriter`] from a database provider. + /// Creates a new instance of [`UnifiedStorageWriter`] from a database provider. pub const fn from_database(database: &'a DatabaseProvider) -> Self { - Self::new(Some(database), None) + Self::new(database, None) } /// Returns a reference to the database writer. /// /// # Panics /// If the database provider is not set. - fn database(&self) -> &DatabaseProvider { - self.database.as_ref().expect("should exist") + const fn database(&self) -> &DatabaseProvider { + self.database } /// Returns a reference to the static file instance. @@ -93,35 +88,28 @@ impl<'a, TX, SF> StorageWriter<'a, TX, SF> { self.static_file.as_mut().expect("should exist") } - /// Ensures that the database provider is set. - /// - /// # Returns - /// - `Ok(())` if the database provider is set. - /// - `Err(StorageWriterError::MissingDatabaseWriter)` if the database provider is not set. - const fn ensure_database(&self) -> Result<(), StorageWriterError> { - if self.database.is_none() { - return Err(StorageWriterError::MissingDatabaseWriter) - } - Ok(()) - } - /// Ensures that the static file instance is set. /// /// # Returns /// - `Ok(())` if the static file instance is set. /// - `Err(StorageWriterError::MissingStaticFileWriter)` if the static file instance is not set. #[allow(unused)] - const fn ensure_static_file(&self) -> Result<(), StorageWriterError> { + const fn ensure_static_file(&self) -> Result<(), UnifiedStorageWriterError> { if self.static_file.is_none() { - return Err(StorageWriterError::MissingStaticFileWriter) + return Err(UnifiedStorageWriterError::MissingStaticFileWriter) } Ok(()) } } -impl StorageWriter<'_, (), ()> { +impl UnifiedStorageWriter<'_, (), ()> { /// Commits both storage types in the right order. /// + /// For non-unwinding operations it makes more sense to commit the static files first, since if + /// it is interrupted before the database commit, we can just truncate + /// the static files according to the checkpoints on the next + /// start-up. + /// /// NOTE: If unwinding data from storage, use `commit_unwind` instead! pub fn commit( database: DatabaseProviderRW, @@ -134,6 +122,11 @@ impl StorageWriter<'_, (), ()> { /// Commits both storage types in the right order for an unwind operation. /// + /// For unwinding it makes more sense to commit the database first, since if + /// it is interrupted before the static files commit, we can just + /// truncate the static files according to the + /// checkpoints on the next start-up. + /// /// NOTE: Should only be used after unwinding data from storage! pub fn commit_unwind( database: DatabaseProviderRW, @@ -145,7 +138,7 @@ impl StorageWriter<'_, (), ()> { } } -impl<'a, 'b, TX> StorageWriter<'a, TX, &'b StaticFileProvider> +impl<'a, 'b, TX> UnifiedStorageWriter<'a, TX, &'b StaticFileProvider> where TX: DbTxMut + DbTx, { @@ -164,14 +157,11 @@ where // Only write receipts to static files if there is no receipt pruning configured. let mut state_writer = if self.database().prune_modes_ref().has_receipts_pruning() { - StorageWriter::from_database(self.database()) + UnifiedStorageWriter::from_database(self.database()) } else { - StorageWriter::new( - Some(self.database()), - Some( - self.static_file() - .get_writer(first_block.number, StaticFileSegment::Receipts)?, - ), + UnifiedStorageWriter::from( + self.database(), + self.static_file().get_writer(first_block.number, StaticFileSegment::Receipts)?, ) }; @@ -226,7 +216,7 @@ where { let header_writer = self.static_file().get_writer(block.number, StaticFileSegment::Headers)?; - let mut storage_writer = StorageWriter::new(Some(self.database()), Some(header_writer)); + let mut storage_writer = UnifiedStorageWriter::from(self.database(), header_writer); let td = storage_writer.append_headers_from_blocks( block.header().number, std::iter::once(&(block.header(), block.hash())), @@ -244,7 +234,7 @@ where let transactions_writer = self.static_file().get_writer(block.number, StaticFileSegment::Transactions)?; let mut storage_writer = - StorageWriter::new(Some(self.database()), Some(transactions_writer)); + UnifiedStorageWriter::from(self.database(), transactions_writer); let no_hash_transactions = block.body.clone().into_iter().map(TransactionSignedNoHash::from).collect(); storage_writer.append_transactions_from_blocks( @@ -299,7 +289,7 @@ where } } -impl<'a, 'b, TX> StorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> +impl<'a, 'b, TX> UnifiedStorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> where TX: DbTx, { @@ -311,11 +301,11 @@ where fn ensure_static_file_segment( &self, segment: StaticFileSegment, - ) -> Result<(), StorageWriterError> { + ) -> Result<(), UnifiedStorageWriterError> { match &self.static_file { Some(writer) => { if writer.user_header().segment() != segment { - Err(StorageWriterError::IncorrectStaticFileWriter( + Err(UnifiedStorageWriterError::IncorrectStaticFileWriter( writer.user_header().segment(), segment, )) @@ -323,7 +313,7 @@ where Ok(()) } } - None => Err(StorageWriterError::MissingStaticFileWriter), + None => Err(UnifiedStorageWriterError::MissingStaticFileWriter), } } @@ -331,8 +321,8 @@ where /// [`HeaderTerminalDifficulties`](tables::HeaderTerminalDifficulties) table to determine the /// total difficulty of the parent block during header insertion. /// - /// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for - /// the Headers segment. + /// NOTE: The static file writer used to construct this [`UnifiedStorageWriter`] MUST be a + /// writer for the Headers segment. pub fn append_headers_from_blocks( &mut self, initial_block_number: BlockNumber, @@ -342,7 +332,6 @@ where I: Borrow<(H, B256)>, H: Borrow
, { - self.ensure_database()?; self.ensure_static_file_segment(StaticFileSegment::Headers)?; let mut td = self @@ -364,8 +353,8 @@ where /// [`BlockBodyIndices`](tables::BlockBodyIndices) table to determine the transaction number /// when appending to static files. /// - /// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for - /// the Transactions segment. + /// NOTE: The static file writer used to construct this [`UnifiedStorageWriter`] MUST be a + /// writer for the Transactions segment. pub fn append_transactions_from_blocks( &mut self, initial_block_number: BlockNumber, @@ -374,7 +363,6 @@ where where T: Borrow>, { - self.ensure_database()?; self.ensure_static_file_segment(StaticFileSegment::Transactions)?; let mut bodies_cursor = @@ -412,17 +400,18 @@ where } } -impl<'a, 'b, TX> StorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> +impl<'a, 'b, TX> UnifiedStorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> where TX: DbTxMut + DbTx, { /// Appends receipts block by block. /// - /// ATTENTION: If called from [`StorageWriter`] without a static file producer, it will always - /// write them to database. Otherwise, it will look into the pruning configuration to decide. + /// ATTENTION: If called from [`UnifiedStorageWriter`] without a static file producer, it will + /// always write them to database. Otherwise, it will look into the pruning configuration to + /// decide. /// - /// NOTE: The static file writer used to construct this [`StorageWriter`] MUST be a writer for - /// the Receipts segment. + /// NOTE: The static file writer used to construct this [`UnifiedStorageWriter`] MUST be a + /// writer for the Receipts segment. /// /// # Parameters /// - `initial_block_number`: The starting block number. @@ -433,13 +422,12 @@ where initial_block_number: BlockNumber, blocks: impl Iterator>>, ) -> ProviderResult<()> { - self.ensure_database()?; let mut bodies_cursor = self.database().tx_ref().cursor_read::()?; // We write receipts to database in two situations: - // * If we are in live sync. In this case, `StorageWriter` is built without a static file - // writer. + // * If we are in live sync. In this case, `UnifiedStorageWriter` is built without a static + // file writer. // * If there is any kind of receipt pruning let mut storage_type = if self.static_file.is_none() || self.database().prune_modes_ref().has_receipts_pruning() @@ -493,7 +481,7 @@ where } } -impl<'a, 'b, TX> StateWriter for StorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> +impl<'a, 'b, TX> StateWriter for UnifiedStorageWriter<'a, TX, StaticFileProviderRWRefMut<'b>> where TX: DbTxMut + DbTx, { @@ -504,7 +492,6 @@ where execution_outcome: ExecutionOutcome, is_value_known: OriginalValuesKnown, ) -> ProviderResult<()> { - self.ensure_database()?; let (plain_state, reverts) = execution_outcome.bundle.into_plain_state_and_reverts(is_value_known); @@ -791,7 +778,7 @@ mod tests { let outcome = ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 1, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -892,7 +879,7 @@ mod tests { state.merge_transitions(BundleRetention::Reverts); let outcome = ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 2, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -960,7 +947,7 @@ mod tests { let outcome = ExecutionOutcome::new(init_state.take_bundle(), Receipts::default(), 0, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -1108,7 +1095,7 @@ mod tests { let bundle = state.take_bundle(); let outcome = ExecutionOutcome::new(bundle, Receipts::default(), 1, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -1274,7 +1261,7 @@ mod tests { init_state.merge_transitions(BundleRetention::Reverts); let outcome = ExecutionOutcome::new(init_state.take_bundle(), Receipts::default(), 0, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB"); @@ -1322,7 +1309,7 @@ mod tests { state.merge_transitions(BundleRetention::Reverts); let outcome = ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 1, Vec::new()); - let mut writer = StorageWriter::new(Some(&provider), None); + let mut writer = UnifiedStorageWriter::from_database(&provider); writer .write_to_storage(outcome, OriginalValuesKnown::Yes) .expect("Could not write bundle state to DB");