Skip to content

fix: Make DatabaseTrieWitness stateful #15833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/state/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use reth_trie::{
};
use reth_trie_db::{
DatabaseHashedPostState, DatabaseHashedStorage, DatabaseProof, DatabaseStateRoot,
DatabaseStorageProof, DatabaseStorageRoot, DatabaseTrieWitness, StateCommitment,
DatabaseStorageProof, DatabaseStorageRoot, DatabaseTrieWitness, StateCommitment,DatabaseTrieCursorFactory,DatabaseHashedCursorFactory
};
use std::fmt::Debug;

Expand Down Expand Up @@ -385,7 +385,7 @@ impl<Provider: DBProvider + BlockNumReader + StateCommitmentProvider> StateProof

fn witness(&self, mut input: TrieInput, target: HashedPostState) -> ProviderResult<Vec<Bytes>> {
input.prepend(self.revert_state()?);
TrieWitness::overlay_witness(self.tx(), input, target)
TrieWitness::overlay_witness(&TrieWitness::new(DatabaseTrieCursorFactory::new(self.tx()),DatabaseHashedCursorFactory::new(self.tx())), input, target)
.map_err(ProviderError::from)
.map(|hm| hm.into_values().collect())
}
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/state/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use reth_trie::{
};
use reth_trie_db::{
DatabaseProof, DatabaseStateRoot, DatabaseStorageProof, DatabaseStorageRoot,
DatabaseTrieWitness, StateCommitment,
DatabaseTrieWitness, StateCommitment,DatabaseTrieCursorFactory,DatabaseHashedCursorFactory
};

/// State provider over latest state that takes tx reference.
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<Provider: DBProvider + StateCommitmentProvider> StateProofProvider
}

fn witness(&self, input: TrieInput, target: HashedPostState) -> ProviderResult<Vec<Bytes>> {
TrieWitness::overlay_witness(self.tx(), input, target)
TrieWitness::overlay_witness(&TrieWitness::new(DatabaseTrieCursorFactory::new(self.tx()),DatabaseHashedCursorFactory::new(self.tx())),input, target)
.map_err(ProviderError::from)
.map(|hm| hm.into_values().collect())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trie/db/src/commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub trait StateCommitment: std::fmt::Debug + Clone + Send + Sync + Unpin + 'stat
/// The state proof type.
type StateProof<'a, TX: DbTx + 'a>: DatabaseProof<'a, TX>;
/// The state witness type.
type StateWitness<'a, TX: DbTx + 'a>: DatabaseTrieWitness<'a, TX>;
type StateWitness<'a, TX: DbTx + 'a>: DatabaseTrieWitness;
/// The key hasher type.
type KeyHasher: KeyHasher;
}
Expand Down
20 changes: 10 additions & 10 deletions crates/trie/db/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@ use reth_trie::{
};

/// Extends [`TrieWitness`] with operations specific for working with a database transaction.
pub trait DatabaseTrieWitness<'a, TX> {
pub trait DatabaseTrieWitness {
/// Create a new [`TrieWitness`] from database transaction.
fn from_tx(tx: &'a TX) -> Self;
fn from_tx(&self) -> Self;
Comment on lines 12 to +13
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't want this method on the trait any more


/// Generates trie witness for target state based on [`TrieInput`].
fn overlay_witness(
tx: &'a TX,
&self,
input: TrieInput,
target: HashedPostState,
) -> Result<B256Map<Bytes>, TrieWitnessError>;
}

impl<'a, TX: DbTx> DatabaseTrieWitness<'a, TX>
impl<'a, TX: DbTx> DatabaseTrieWitness
for TrieWitness<DatabaseTrieCursorFactory<'a, TX>, DatabaseHashedCursorFactory<'a, TX>>
{
fn from_tx(tx: &'a TX) -> Self {
Self::new(DatabaseTrieCursorFactory::new(tx), DatabaseHashedCursorFactory::new(tx))
fn from_tx(&self) -> Self {
Self::new(self.trie_cursor_factory.clone(), self.hashed_cursor_factory.clone())
}
Comment on lines +26 to 28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should move this to be a method directly on TrieWitness, and we still want the argument to be &'a TX


fn overlay_witness(
tx: &'a TX,
&self,
input: TrieInput,
target: HashedPostState,
) -> Result<B256Map<Bytes>, TrieWitnessError> {
let nodes_sorted = input.nodes.into_sorted();
let state_sorted = input.state.into_sorted();
Self::from_tx(tx)
Self::from_tx(&self)
.with_trie_cursor_factory(InMemoryTrieCursorFactory::new(
DatabaseTrieCursorFactory::new(tx),
self.trie_cursor_factory.clone(),
&nodes_sorted,
))
.with_hashed_cursor_factory(HashedPostStateCursorFactory::new(
DatabaseHashedCursorFactory::new(tx),
self.hashed_cursor_factory.clone(),
Comment on lines 30 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this actually motivates changing the &self, to be self in the trait, that way we could do something like:

self.
// with_trie_cursor_factory call
// with_hashed_cursor_factory call
// ...
.compute(target)

&state_sorted,
))
.with_prefix_sets_mut(input.prefix_sets)
Expand Down
4 changes: 2 additions & 2 deletions crates/trie/trie/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use std::sync::{mpsc, Arc};
#[derive(Debug)]
pub struct TrieWitness<T, H> {
/// The cursor factory for traversing trie nodes.
trie_cursor_factory: T,
pub trie_cursor_factory: T,
/// The factory for hashed cursors.
hashed_cursor_factory: H,
pub hashed_cursor_factory: H,
Comment on lines +30 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these need to be pub?

/// A set of prefix sets that have changes.
prefix_sets: TriePrefixSetsMut,
/// Recorded witness.
Expand Down
Loading