Skip to content

Commit

Permalink
feat: remove StateChanges and StateReverts (#9781)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Jul 24, 2024
1 parent 7531366 commit 527cc44
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 214 deletions.
5 changes: 1 addition & 4 deletions crates/storage/provider/src/bundle_state/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Bundle state module.
//! This module contains all the logic related to bundle state.

mod state_changes;
mod state_reverts;

pub use state_changes::StateChanges;
pub use state_reverts::{StateReverts, StorageRevertsIter};
pub use state_reverts::StorageRevertsIter;
88 changes: 0 additions & 88 deletions crates/storage/provider/src/bundle_state/state_changes.rs

This file was deleted.

100 changes: 2 additions & 98 deletions crates/storage/provider/src/bundle_state/state_reverts.rs
Original file line number Diff line number Diff line change
@@ -1,103 +1,7 @@
use crate::DatabaseProviderRW;
use rayon::slice::ParallelSliceMut;
use reth_db::{tables, Database};
use reth_db_api::{
cursor::{DbCursorRO, DbDupCursorRO, DbDupCursorRW},
models::{AccountBeforeTx, BlockNumberAddress},
transaction::DbTxMut,
};
use reth_primitives::{BlockNumber, StorageEntry, B256, U256};
use reth_storage_errors::db::DatabaseError;
use revm::db::states::{PlainStateReverts, PlainStorageRevert, RevertToSlot};
use reth_primitives::{B256, U256};
use revm::db::states::RevertToSlot;
use std::iter::Peekable;

/// Revert of the state.
#[derive(Debug, Default)]
pub struct StateReverts(pub PlainStateReverts);

impl From<PlainStateReverts> for StateReverts {
fn from(revm: PlainStateReverts) -> Self {
Self(revm)
}
}

impl StateReverts {
/// Write reverts to database.
///
/// `Note::` Reverts will delete all wiped storage from plain state.
pub fn write_to_db<DB>(
self,
provider: &DatabaseProviderRW<DB>,
first_block: BlockNumber,
) -> Result<(), DatabaseError>
where
DB: Database,
{
// Write storage changes
tracing::trace!(target: "provider::reverts", "Writing storage changes");
let mut storages_cursor =
provider.tx_ref().cursor_dup_write::<tables::PlainStorageState>()?;
let mut storage_changeset_cursor =
provider.tx_ref().cursor_dup_write::<tables::StorageChangeSets>()?;
for (block_index, mut storage_changes) in self.0.storage.into_iter().enumerate() {
let block_number = first_block + block_index as BlockNumber;

tracing::trace!(target: "provider::reverts", block_number, "Writing block change");
// sort changes by address.
storage_changes.par_sort_unstable_by_key(|a| a.address);
for PlainStorageRevert { address, wiped, storage_revert } in storage_changes {
let storage_id = BlockNumberAddress((block_number, address));

let mut storage = storage_revert
.into_iter()
.map(|(k, v)| (B256::new(k.to_be_bytes()), v))
.collect::<Vec<_>>();
// sort storage slots by key.
storage.par_sort_unstable_by_key(|a| a.0);

// If we are writing the primary storage wipe transition, the pre-existing plain
// storage state has to be taken from the database and written to storage history.
// See [StorageWipe::Primary] for more details.
let mut wiped_storage = Vec::new();
if wiped {
tracing::trace!(target: "provider::reverts", ?address, "Wiping storage");
if let Some((_, entry)) = storages_cursor.seek_exact(address)? {
wiped_storage.push((entry.key, entry.value));
while let Some(entry) = storages_cursor.next_dup_val()? {
wiped_storage.push((entry.key, entry.value))
}
}
}

tracing::trace!(target: "provider::reverts", ?address, ?storage, "Writing storage reverts");
for (key, value) in StorageRevertsIter::new(storage, wiped_storage) {
storage_changeset_cursor.append_dup(storage_id, StorageEntry { key, value })?;
}
}
}

// Write account changes
tracing::trace!(target: "provider::reverts", "Writing account changes");
let mut account_changeset_cursor =
provider.tx_ref().cursor_dup_write::<tables::AccountChangeSets>()?;

for (block_index, mut account_block_reverts) in self.0.accounts.into_iter().enumerate() {
let block_number = first_block + block_index as BlockNumber;
// Sort accounts by address.
account_block_reverts.par_sort_by_key(|a| a.0);

for (address, info) in account_block_reverts {
account_changeset_cursor.append_dup(
block_number,
AccountBeforeTx { address, info: info.map(Into::into) },
)?;
}
}

Ok(())
}
}

/// Iterator over storage reverts.
/// See [`StorageRevertsIter::next`] for more details.
#[allow(missing_debug_implementations)]
Expand Down
1 change: 0 additions & 1 deletion crates/storage/provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub use reth_storage_errors::provider::{ProviderError, ProviderResult};
pub use reth_execution_types::*;

pub mod bundle_state;
pub use bundle_state::{StateChanges, StateReverts};

/// Re-export `OriginalValuesKnown`
pub use revm::db::states::OriginalValuesKnown;
Expand Down
Loading

0 comments on commit 527cc44

Please sign in to comment.