[go: up one dir, main page]

Skip to content

Commit

Permalink
chore: use DatabaseProviderRW on StateWriter::write_to_storage in…
Browse files Browse the repository at this point in the history
…stead of `TX` (paradigmxyz#9449)
  • Loading branch information
joshieDo authored Jul 11, 2024
1 parent b040b86 commit 22df39a
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 58 deletions.
2 changes: 1 addition & 1 deletion bin/reth/src/commands/debug_cmd/in_memory_merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl Command {
.try_seal_with_senders()
.map_err(|_| BlockValidationError::SenderRecoveryError)?,
)?;
execution_outcome.write_to_storage(provider_rw.tx_ref(), None, OriginalValuesKnown::No)?;
execution_outcome.write_to_storage(&provider_rw, None, OriginalValuesKnown::No)?;
let storage_lists = provider_rw.changed_storages_with_range(block.number..=block.number)?;
let storages = provider_rw.plain_state_storages(storage_lists)?;
provider_rw.insert_storage_for_hashing(storages)?;
Expand Down
6 changes: 1 addition & 5 deletions bin/reth/src/commands/debug_cmd/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,7 @@ impl Command {
),
));
executor.execute_and_verify_one((&sealed_block.clone().unseal(), td).into())?;
executor.finalize().write_to_storage(
provider_rw.tx_ref(),
None,
OriginalValuesKnown::Yes,
)?;
executor.finalize().write_to_storage(&provider_rw, None, OriginalValuesKnown::Yes)?;

let checkpoint = Some(StageCheckpoint::new(
block_number.checked_sub(1).ok_or(eyre::eyre!("GenesisBlockHasNoParent"))?,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/commands/src/stage/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::common::{AccessRights, Environment, EnvironmentArgs};
use clap::Parser;
use itertools::Itertools;
use reth_db::{static_file::iter_static_files, tables, DatabaseEnv};
use reth_db::{static_file::iter_static_files, tables};
use reth_db_api::transaction::DbTxMut;
use reth_db_common::{
init::{insert_genesis_header, insert_genesis_history, insert_genesis_state},
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Command {
Default::default(),
)?;
let alloc = &self.env.chain.genesis().alloc;
insert_genesis_state::<DatabaseEnv>(tx, alloc.len(), alloc.iter())?;
insert_genesis_state(&provider_rw, alloc.len(), alloc.iter())?;
}
StageEnum::AccountHashing => {
tx.clear::<tables::HashedAccounts>()?;
Expand Down
6 changes: 1 addition & 5 deletions crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ impl<DB: Database> Persistence<DB> {
// Write state and changesets to the database.
// Must be written after blocks because of the receipt lookup.
let execution_outcome = block.execution_outcome().clone();
execution_outcome.write_to_storage(
provider_rw.tx_ref(),
None,
OriginalValuesKnown::No,
)?;
execution_outcome.write_to_storage(&provider_rw, None, OriginalValuesKnown::No)?;

// insert hashes and intermediate merkle nodes
{
Expand Down
10 changes: 4 additions & 6 deletions crates/optimism/cli/src/commands/import_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use clap::Parser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_db::tables;
use reth_db_api::{database::Database, transaction::DbTx};
use reth_db_api::database::Database;
use reth_downloaders::{
file_client::{ChunkedFileReader, DEFAULT_BYTE_LEN_CHUNK_CHAIN_FILE},
file_codec_ovm_receipt::HackReceiptFileCodec,
Expand Down Expand Up @@ -109,8 +109,6 @@ where
);
}

// prepare the tx for `write_to_storage`
let tx = provider.into_tx();
let mut total_decoded_receipts = 0;
let mut total_filtered_out_dup_txns = 0;

Expand Down Expand Up @@ -149,14 +147,14 @@ where
static_file_provider.get_writer(first_block, StaticFileSegment::Receipts)?;

// finally, write the receipts
execution_outcome.write_to_storage::<DB::TXMut>(
&tx,
execution_outcome.write_to_storage(
&provider,
Some(static_file_producer),
OriginalValuesKnown::Yes,
)?;
}

tx.commit()?;
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()?;
Expand Down
6 changes: 1 addition & 5 deletions crates/stages/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,7 @@ where

let time = Instant::now();
// write output
state.write_to_storage(
provider.tx_ref(),
static_file_producer,
OriginalValuesKnown::Yes,
)?;
state.write_to_storage(provider, static_file_producer, OriginalValuesKnown::Yes)?;
let db_write_duration = time.elapsed();
debug!(
target: "sync::stages::execution",
Expand Down
21 changes: 9 additions & 12 deletions crates/storage/db-common/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
io::BufRead,
ops::DerefMut,
sync::Arc,
};
use tracing::{debug, error, info, trace};
Expand Down Expand Up @@ -113,11 +112,10 @@ pub fn init_genesis<DB: Database>(factory: ProviderFactory<DB>) -> Result<B256,
insert_genesis_history(&provider_rw, alloc.iter())?;

// Insert header
let tx = provider_rw.tx_ref();
let static_file_provider = factory.static_file_provider();
insert_genesis_header(&provider_rw, &static_file_provider, chain.clone())?;

insert_genesis_state::<DB>(tx, alloc.len(), alloc.iter())?;
insert_genesis_state(&provider_rw, alloc.len(), alloc.iter())?;

// insert sync stage
for stage in StageId::ALL {
Expand All @@ -132,16 +130,16 @@ pub fn init_genesis<DB: Database>(factory: ProviderFactory<DB>) -> Result<B256,

/// Inserts the genesis state into the database.
pub fn insert_genesis_state<'a, 'b, DB: Database>(
tx: &<DB as Database>::TXMut,
provider: &DatabaseProviderRW<DB>,
capacity: usize,
alloc: impl Iterator<Item = (&'a Address, &'b GenesisAccount)>,
) -> ProviderResult<()> {
insert_state::<DB>(tx, capacity, alloc, 0)
insert_state::<DB>(provider, capacity, alloc, 0)
}

/// Inserts state at given block into database.
pub fn insert_state<'a, 'b, DB: Database>(
tx: &<DB as Database>::TXMut,
provider: &DatabaseProviderRW<DB>,
capacity: usize,
alloc: impl Iterator<Item = (&'a Address, &'b GenesisAccount)>,
block: u64,
Expand Down Expand Up @@ -203,7 +201,7 @@ pub fn insert_state<'a, 'b, DB: Database>(
Vec::new(),
);

execution_outcome.write_to_storage(tx, None, OriginalValuesKnown::Yes)?;
execution_outcome.write_to_storage(provider, None, OriginalValuesKnown::Yes)?;

trace!(target: "reth::cli", "Inserted state");

Expand Down Expand Up @@ -323,8 +321,8 @@ pub fn init_from_state_dump<DB: Database>(
let collector = parse_accounts(&mut reader, etl_config)?;

// write state to db
let mut provider_rw = factory.provider_rw()?;
dump_state(collector, &mut provider_rw, block)?;
let provider_rw = factory.provider_rw()?;
dump_state(collector, &provider_rw, block)?;

// compute and compare state root. this advances the stage checkpoints.
let computed_state_root = compute_state_root(&provider_rw)?;
Expand Down Expand Up @@ -398,7 +396,7 @@ fn parse_accounts(
/// Takes a [`Collector`] and processes all accounts.
fn dump_state<DB: Database>(
mut collector: Collector<Address, GenesisAccount>,
provider_rw: &mut DatabaseProviderRW<DB>,
provider_rw: &DatabaseProviderRW<DB>,
block: u64,
) -> Result<(), eyre::Error> {
let accounts_len = collector.len();
Expand Down Expand Up @@ -435,9 +433,8 @@ fn dump_state<DB: Database>(
)?;

// block is already written to static files
let tx = provider_rw.deref_mut().tx_mut();
insert_state::<DB>(
tx,
provider_rw,
accounts.len(),
accounts.iter().map(|(address, account)| (address, account)),
block,
Expand Down
26 changes: 15 additions & 11 deletions crates/storage/provider/src/bundle_state/execution_outcome.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::{providers::StaticFileProviderRWRefMut, StateChanges, StateReverts, StateWriter};
use reth_db::tables;
use crate::{
providers::StaticFileProviderRWRefMut, DatabaseProviderRW, StateChanges, StateReverts,
StateWriter,
};
use reth_db::{tables, Database};
use reth_db_api::{
cursor::{DbCursorRO, DbCursorRW},
transaction::{DbTx, DbTxMut},
Expand All @@ -10,15 +13,16 @@ use reth_storage_errors::provider::{ProviderError, ProviderResult};
pub use revm::db::states::OriginalValuesKnown;

impl StateWriter for ExecutionOutcome {
fn write_to_storage<TX>(
fn write_to_storage<DB>(
self,
tx: &TX,
provider_rw: &DatabaseProviderRW<DB>,
mut static_file_producer: Option<StaticFileProviderRWRefMut<'_>>,
is_value_known: OriginalValuesKnown,
) -> ProviderResult<()>
where
TX: DbTxMut + DbTx,
DB: Database,
{
let tx = provider_rw.tx_ref();
let (plain_state, reverts) = self.bundle.into_plain_state_and_reverts(is_value_known);

StateReverts(reverts).write_to_db(tx, self.first_block)?;
Expand Down Expand Up @@ -290,7 +294,7 @@ mod tests {
state.merge_transitions(BundleRetention::Reverts);

ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 1, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write bundle state to DB");

// Check plain storage state
Expand Down Expand Up @@ -388,7 +392,7 @@ mod tests {

state.merge_transitions(BundleRetention::Reverts);
ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 2, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write bundle state to DB");

assert_eq!(
Expand Down Expand Up @@ -452,7 +456,7 @@ mod tests {
)]));
init_state.merge_transitions(BundleRetention::Reverts);
ExecutionOutcome::new(init_state.take_bundle(), Receipts::default(), 0, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write init bundle state to DB");

let mut state = State::builder().with_bundle_update().build();
Expand Down Expand Up @@ -598,7 +602,7 @@ mod tests {
let bundle = state.take_bundle();

ExecutionOutcome::new(bundle, Receipts::default(), 1, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write bundle state to DB");

let mut storage_changeset_cursor = provider
Expand Down Expand Up @@ -761,7 +765,7 @@ mod tests {
)]));
init_state.merge_transitions(BundleRetention::Reverts);
ExecutionOutcome::new(init_state.take_bundle(), Receipts::default(), 0, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write init bundle state to DB");

let mut state = State::builder().with_bundle_update().build();
Expand Down Expand Up @@ -806,7 +810,7 @@ mod tests {
// Commit block #1 changes to the database.
state.merge_transitions(BundleRetention::Reverts);
ExecutionOutcome::new(state.take_bundle(), Receipts::default(), 1, Vec::new())
.write_to_storage(provider.tx_ref(), None, OriginalValuesKnown::Yes)
.write_to_storage(&provider, None, OriginalValuesKnown::Yes)
.expect("Could not write bundle state to DB");

let mut storage_changeset_cursor = provider
Expand Down
8 changes: 4 additions & 4 deletions crates/storage/provider/src/providers/database/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<TX: DbTx + 'static> DatabaseProvider<TX> {
}
}

impl<TX: DbTxMut + DbTx> DatabaseProvider<TX> {
impl<DB: Database> DatabaseProviderRW<DB> {
// TODO: uncomment below, once `reth debug_cmd` has been feature gated with dev.
// #[cfg(any(test, feature = "test-utils"))]
/// Inserts an historical block. **Used for setting up test environments**
Expand Down Expand Up @@ -2990,7 +2990,7 @@ impl<TX: DbTxMut + DbTx> HistoryWriter for DatabaseProvider<TX> {
}
}

impl<TX: DbTxMut + DbTx> BlockExecutionWriter for DatabaseProvider<TX> {
impl<DB: Database> BlockExecutionWriter for DatabaseProviderRW<DB> {
fn get_block_and_execution_range(
&self,
range: RangeInclusive<BlockNumber>,
Expand Down Expand Up @@ -3093,7 +3093,7 @@ impl<TX: DbTxMut + DbTx> BlockExecutionWriter for DatabaseProvider<TX> {
}
}

impl<TX: DbTxMut + DbTx> BlockWriter for DatabaseProvider<TX> {
impl<DB: Database> BlockWriter for DatabaseProviderRW<DB> {
fn insert_block(
&self,
block: SealedBlockWithSenders,
Expand Down Expand Up @@ -3264,7 +3264,7 @@ impl<TX: DbTxMut + DbTx> BlockWriter for DatabaseProvider<TX> {

// Write state and changesets to the database.
// Must be written after blocks because of the receipt lookup.
execution_outcome.write_to_storage(self.tx_ref(), None, OriginalValuesKnown::No)?;
execution_outcome.write_to_storage(self, None, OriginalValuesKnown::No)?;
durations_recorder.record_relative(metrics::Action::InsertState);

// insert hashes and intermediate merkle nodes
Expand Down
3 changes: 1 addition & 2 deletions crates/storage/provider/src/traits/block.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use reth_db_api::models::StoredBlockBodyIndices;
use reth_execution_types::{Chain, ExecutionOutcome};
use reth_primitives::{BlockNumber, SealedBlockWithSenders};
use reth_storage_api::BlockReader;
use reth_storage_errors::provider::ProviderResult;
use reth_trie::{updates::TrieUpdates, HashedPostState};
use std::ops::RangeInclusive;

/// BlockExecution Writer
#[auto_impl::auto_impl(&, Arc, Box)]
pub trait BlockExecutionWriter: BlockWriter + BlockReader + Send + Sync {
pub trait BlockExecutionWriter: BlockWriter + Send + Sync {
/// Get range of blocks and its execution result
fn get_block_and_execution_range(
&self,
Expand Down
10 changes: 5 additions & 5 deletions crates/storage/provider/src/traits/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::providers::StaticFileProviderRWRefMut;
use reth_db_api::transaction::{DbTx, DbTxMut};
use crate::{providers::StaticFileProviderRWRefMut, DatabaseProviderRW};
use reth_db::Database;
use reth_storage_errors::provider::ProviderResult;
use revm::db::OriginalValuesKnown;

Expand All @@ -8,12 +8,12 @@ use revm::db::OriginalValuesKnown;
pub trait StateWriter {
/// Write the data and receipts to the database or static files if `static_file_producer` is
/// `Some`. It should be `None` if there is any kind of pruning/filtering over the receipts.
fn write_to_storage<TX>(
fn write_to_storage<DB>(
self,
tx: &TX,
provider_rw: &DatabaseProviderRW<DB>,
static_file_producer: Option<StaticFileProviderRWRefMut<'_>>,
is_value_known: OriginalValuesKnown,
) -> ProviderResult<()>
where
TX: DbTxMut + DbTx;
DB: Database;
}

0 comments on commit 22df39a

Please sign in to comment.