Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ vergen = "9.0.4"
visibility = "0.1.1"
walkdir = "2.3.3"
vergen-git2 = "1.0.5"
mockall = "0.13.1"

# [patch.crates-io]
# alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "3049f232fbb44d1909883e154eb38ec5962f53a3" }
Expand Down
2 changes: 2 additions & 0 deletions crates/optimism/trie/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ reth-db-common.workspace = true
reth-ethereum-primitives.workspace = true
reth-evm-ethereum.workspace = true
reth-testing-utils.workspace = true
reth-storage-errors.workspace = true
secp256k1.workspace = true
mockall.workspace = true

# misc
serial_test.workspace = true
Expand Down
8 changes: 8 additions & 0 deletions crates/optimism/trie/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ pub struct BlockStateDiff {
pub post_state: HashedPostState,
}

impl BlockStateDiff {
/// Extend the [` BlockStateDiff`] from other latest [`BlockStateDiff`]
pub fn extend(&mut self, other: Self) {
self.trie_updates.extend(other.trie_updates);
self.post_state.extend(other.post_state);
}
}

/// Counts of trie updates written to storage.
#[derive(Debug, Clone, Default)]
pub struct WriteCounts {
Expand Down
17 changes: 8 additions & 9 deletions crates/optimism/trie/src/db/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ impl MdbxProofsStorage {
}
return Ok(keys);
}
// Hard delete removed entries (vv.value == None) at this exact subkey; append the rest.
// We need hard deletions at the time of pruning where we need to perform these steps:
// - Hard delete all the tombstones
// - Update new state to block zero (not append)
let (to_delete, to_append): (Vec<_>, Vec<_>) =
pairs.into_iter().partition(|(_, vv)| vv.value.0.is_none());

self.delete_dup_sorted::<T, _, V>(tx, block_number, to_delete.into_iter().map(|(k, _)| k))?;

for (k, vv) in to_append {
cur.append_dup(k, vv)?;
// For block 0, we need to update the existing entries.
cur.upsert(k, &vv)?;
}

Ok(keys)
Expand Down Expand Up @@ -654,7 +657,7 @@ impl OpProofsStore for MdbxProofsStorage {
return Ok(()); // Nothing to prune
}

let _ = self.env.update(|tx| {
self.env.update(|tx| {
// First, delete the old entries for the block range excluding block 0
self.delete_history_ranged(
tx,
Expand All @@ -670,12 +673,8 @@ impl OpProofsStore for MdbxProofsStorage {
tx,
new_earliest_block_number,
new_earliest_block_ref.block.hash,
)?;

Ok::<(), DatabaseError>(())
})?;

Ok(())
)
})?
}

async fn replace_updates(
Expand Down
3 changes: 3 additions & 0 deletions crates/optimism/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ pub use cursor_factory::{OpProofsHashedAccountCursorFactory, OpProofsTrieCursorF

pub mod error;
pub use error::{OpProofsStorageError, OpProofsStorageResult};

mod prune;
pub use prune::{OpProofStoragePruner, OpProofStoragePrunerResult, PrunerError, PrunerOutput};
56 changes: 56 additions & 0 deletions crates/optimism/trie/src/prune/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use crate::OpProofsStorageError;
use reth_provider::ProviderError;
use std::{
fmt,
fmt::{Display, Formatter},
time::Duration,
};
use strum::Display;
use thiserror::Error;

/// Result of [`OpProofStoragePruner::run`] execution.
pub type OpProofStoragePrunerResult = Result<PrunerOutput, PrunerError>;

/// Successful prune summary.
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct PrunerOutput {
/// Total elapsed wall time for this run (fetch + apply).
pub duration: Duration,
/// Earliest block at the start of the run.
pub start_block: u64,
/// New earliest block at the end of the run.
pub end_block: u64,
/// Total number of entries removed across tables.
pub total_entries_pruned: u64,
}

impl Display for PrunerOutput {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let blocks = self.end_block.saturating_sub(self.start_block);
write!(
f,
"Pruned {}→{} ({} blocks), entries={}, elapsed={:.3}s",
self.start_block,
self.end_block,
blocks,
self.total_entries_pruned,
self.duration.as_secs_f64(),
)
}
}

/// Error returned by the pruner.
#[derive(Debug, Error, Display)]
pub enum PrunerError {
/// Wrapped error from the underlying `OpProofStorage` layer.
Storage(#[from] OpProofsStorageError),

/// Wrapped error from the reth db provider.
Provider(#[from] ProviderError),

/// Block not found in the underlying reth storage provider.
BlockNotFound(u64),

/// The pruner timed out before finishing the prune
TimedOut(Duration),
}
5 changes: 5 additions & 0 deletions crates/optimism/trie/src/prune/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod error;
pub use error::{OpProofStoragePrunerResult, PrunerError, PrunerOutput};

mod pruner;
pub use pruner::OpProofStoragePruner;
Loading
Loading