Skip to content
Closed
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
3 changes: 3 additions & 0 deletions rust/Cargo.lock

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

3 changes: 3 additions & 0 deletions rust/op-reth/crates/exex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ reth-primitives-traits.workspace = true
reth-ethereum-primitives.workspace = true
reth-evm-ethereum.workspace = true
reth-exex-test-utils.workspace = true
reth-chain-state.workspace = true
reth-chainspec.workspace = true
rstest.workspace = true
tempfile.workspace = true

[features]
Expand Down
332 changes: 287 additions & 45 deletions rust/op-reth/crates/exex/src/lib.rs

Large diffs are not rendered by default.

74 changes: 70 additions & 4 deletions rust/op-reth/crates/trie/src/engine/buffer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use alloy_primitives::{B256, map::HashMap};
use parking_lot::RwLock;
use std::{collections::BTreeMap, sync::Arc};

/// Persisted block hashes the buffer keeps; an unwind onto an older block has only the window's
/// ends to check against.
const RETAINED_PERSISTED_HASHES: usize = 1024;

/// Buffer for holding blocks waiting to be persisted.
///
/// This acts as the in-memory "tip" of the chain for the trie calculator.
Expand All @@ -18,6 +22,9 @@ struct TrieBuffer {
blocks: RwLock<HashMap<B256, Arc<(BlockWithParent, BlockStateDiff)>>>,
/// Mapping of block numbers to block hashes.
numbers: RwLock<BTreeMap<u64, B256>>,
/// Hashes of the newest blocks that left the buffer through a save, so an unwind can check
/// its target against a block that is no longer buffered.
persisted_hashes: RwLock<BTreeMap<u64, B256>>,
#[cfg(feature = "metrics")]
metrics: BufferMetrics,
}
Expand All @@ -28,6 +35,7 @@ impl TrieBuffer {
Self {
blocks: RwLock::new(HashMap::default()),
numbers: RwLock::new(BTreeMap::new()),
persisted_hashes: RwLock::new(BTreeMap::new()),
#[cfg(feature = "metrics")]
metrics: BufferMetrics::new_with_labels(&[] as &[(&str, &str)]),
}
Expand Down Expand Up @@ -55,18 +63,24 @@ impl TrieBuffer {
self.blocks.read().len()
}

/// Prunes blocks from the buffer that are strictly before the given block number.
/// Prunes blocks from the buffer that are strictly before the given block number, keeping
/// their hashes.
fn prune(&self, number: u64) {
let mut blocks = self.blocks.write();
let mut numbers = self.numbers.write();
let mut persisted_hashes = self.persisted_hashes.write();

// Split off the entries we want to keep (number >= threshold).
// The original map now contains only entries to prune (num < number).
let to_keep = numbers.split_off(&number);
for hash in numbers.values() {
for (&pruned_number, hash) in numbers.iter() {
blocks.remove(hash);
persisted_hashes.insert(pruned_number, *hash);
}
*numbers = to_keep;
while persisted_hashes.len() > RETAINED_PERSISTED_HASHES {
persisted_hashes.pop_first();
}

#[cfg(feature = "metrics")]
self.metrics.buffer_size.set(blocks.len() as f64);
Expand All @@ -85,10 +99,18 @@ impl TrieBuffer {
for hash in to_remove.values() {
blocks.remove(hash);
}
self.persisted_hashes.write().split_off(&from);

#[cfg(feature = "metrics")]
self.metrics.buffer_size.set(blocks.len() as f64);
}

/// Hash of the block at `number` if it is buffered or left the buffer through a save.
fn hash_at(&self, number: u64) -> Option<B256> {
let numbers = self.numbers.read();
let persisted_hashes = self.persisted_hashes.read();
numbers.get(&number).or_else(|| persisted_hashes.get(&number)).copied()
}
}

/// Manager for the in-memory state of the live trie.
Expand Down Expand Up @@ -129,11 +151,17 @@ impl TrieBufferState {
self.inner.prune(number);
}

/// Removes blocks starting from `from` (inclusive) through the tip.
/// Removes blocks starting from `from` (inclusive) through the tip, and forgets the hashes of
/// persisted blocks from `from` up.
pub(in crate::engine) fn unwind(&self, from: u64) {
self.inner.unwind(from);
}

/// Hash of the block at `number` if it is buffered or was pruned from the buffer by a save.
pub(in crate::engine) fn hash_at(&self, number: u64) -> Option<B256> {
self.inner.hash_at(number)
}

/// Returns the highest buffered block as a [`NumHash`], or `None` if the buffer is empty.
pub(in crate::engine) fn tip(&self) -> Option<NumHash> {
let numbers = self.inner.numbers.read();
Expand Down Expand Up @@ -189,7 +217,7 @@ mod tests {
use super::*;
use crate::BlockStateDiff;
use alloy_eips::NumHash;
use alloy_primitives::B256;
use alloy_primitives::{B256, U256};

/// Build a `(BlockWithParent, BlockStateDiff)` pair with predictable hashes.
///
Expand Down Expand Up @@ -318,6 +346,44 @@ mod tests {
assert_eq!(state.len(), 3);
}

#[test]
fn hash_at_follows_blocks_through_prune_and_unwind() {
let state = TrieBufferState::new();
for i in 1..=5u64 {
insert(&state, i, i as u8, (i - 1) as u8);
}
assert_eq!(state.hash_at(3), Some(B256::repeat_byte(0x03)));
assert_eq!(state.hash_at(6), None);

state.prune(4); // 1, 2, 3 persisted; 4, 5 buffered
assert_eq!(state.hash_at(2), Some(B256::repeat_byte(0x02)));
assert_eq!(state.hash_at(5), Some(B256::repeat_byte(0x05)));

state.unwind(3); // removes 3, 4, 5 whether persisted or buffered
assert_eq!(state.hash_at(2), Some(B256::repeat_byte(0x02)));
assert_eq!(state.hash_at(3), None);
assert_eq!(state.hash_at(5), None);
}

#[test]
fn retained_persisted_hashes_drop_the_oldest_beyond_the_bound() {
let state = TrieBufferState::new();
let extra = 5u64;
let highest = RETAINED_PERSISTED_HASHES as u64 + extra;
for i in 1..=highest {
state.insert(
BlockWithParent::new(B256::ZERO, NumHash::new(i, B256::from(U256::from(i)))),
BlockStateDiff::default(),
);
}

state.prune(highest + 1);

assert_eq!(state.hash_at(extra), None);
assert_eq!(state.hash_at(extra + 1), Some(B256::from(U256::from(extra + 1))));
assert_eq!(state.hash_at(highest), Some(B256::from(U256::from(highest))));
}

#[test]
fn blocks_ordered_returns_oldest_to_newest() {
let state = TrieBufferState::new();
Expand Down
5 changes: 3 additions & 2 deletions rust/op-reth/crates/trie/src/engine/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ pub enum EngineError {
/// The collector engine thread terminated unexpectedly.
#[error("Collector engine terminated unexpectedly")]
EngineDied,
/// Block is at the correct number but its parent hash does not match the current tip.
/// A block's parent hash does not match the stored block below it: the tip for a new block,
/// the block below the target for an unwind or a reorg.
#[error(
"Parent hash mismatch at block {block_number}: expected {expected_parent_hash}, got {actual_parent_hash}"
)]
ParentHashMismatch {
/// The block number where the mismatch occurred.
block_number: u64,
/// The expected parent hash (current tip hash).
/// The hash of the stored block the block should build on.
expected_parent_hash: B256,
/// The actual parent hash from the block header.
actual_parent_hash: B256,
Expand Down
17 changes: 10 additions & 7 deletions rust/op-reth/crates/trie/src/engine/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{
error::EngineError,
runner::Engine,
service_guard::ServiceGuard,
tasks::{ExecuteBlockTask, IndexBlockTask, ReorgTask, SyncToTask, UnwindTask},
tasks::{ExecuteBlockTask, IndexBlockTask, ReorgBlock, ReorgTask, SyncToTask, UnwindTask},
};
use crate::{OpProofStoragePruner, OpProofsStore};
use alloy_eips::eip1898::BlockWithParent;
Expand Down Expand Up @@ -135,12 +135,15 @@ impl<Block: reth_primitives_traits::Block + Send + 'static> EngineHandle<Block>
})
}

/// Handle a chain reorg: unwind to the common ancestor then buffer new fork blocks.
pub fn reorg(
&self,
block_updates: Vec<(BlockWithParent, Arc<TrieUpdatesSorted>, Arc<HashedPostStateSorted>)>,
) -> Result<(), EngineError> {
self.send_and_recv(|reply| EngineAction::Reorg(ReorgTask { block_updates, reply }))
/// Handle a chain reorg: unwind to the common ancestor then buffer the new chain, indexing or
/// executing each block as its [`ReorgBlock`] says.
///
/// Fails without unwinding when the unwind would reach the window's earliest block, or when
/// the first block's parent differs from the stored block below it and the engine knows that
/// block's hash (it has buffered or persisted it, or it is a window end). A block that fails
/// after the unwind leaves the store unwound with the blocks before it buffered.
pub fn reorg(&self, blocks: Vec<ReorgBlock<Block>>) -> Result<(), EngineError> {
self.send_and_recv(|reply| EngineAction::Reorg(ReorgTask { blocks, reply }))
}

/// Unwind indexed data back to `to` (first block number removed, inclusive).
Expand Down
5 changes: 3 additions & 2 deletions rust/op-reth/crates/trie/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
mod buffer;
pub mod persistence;
mod tasks;
pub use tasks::ReorgBlock;

mod canonical_state;
pub use canonical_state::CanonicalInMemoryStateProvider;
Expand Down Expand Up @@ -50,8 +51,8 @@ enum EngineAction<Block: reth_primitives_traits::Block> {
ExecuteBlock(tasks::ExecuteBlockTask<Block>),
/// Index pre-computed trie updates for a block (no EVM execution).
IndexBlock(tasks::IndexBlockTask),
/// Handle a reorg: unwind to the common ancestor then index the new chain.
Reorg(tasks::ReorgTask),
/// Handle a reorg: unwind to the common ancestor then index or execute the new chain.
Reorg(tasks::ReorgTask<Block>),
/// Unwind indexed data back to a given block.
Unwind(tasks::UnwindTask),
/// Block the caller until any in-flight persistence completes.
Expand Down
Loading
Loading