Skip to content

Commit

Permalink
chore(trie): simplify blinded provider (#13753)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored Jan 9, 2025
1 parent 017217f commit 66f934b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 58 deletions.
26 changes: 12 additions & 14 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use reth_trie::{
use reth_trie_parallel::{proof::ParallelProof, root::ParallelStateRootError};
use reth_trie_sparse::{
blinded::{BlindedProvider, BlindedProviderFactory},
errors::{SparseStateTrieError, SparseStateTrieResult, SparseTrieError, SparseTrieErrorKind},
errors::{SparseStateTrieError, SparseStateTrieResult, SparseTrieErrorKind},
SparseStateTrie,
};
use revm_primitives::{keccak256, EvmState, B256};
Expand Down Expand Up @@ -278,20 +278,17 @@ pub struct StateRootTask<'env, Factory, BPF: BlindedProviderFactory> {
thread_pool: &'env rayon::ThreadPool,
}

impl<'env, Factory, ABP, SBP, BPF> StateRootTask<'env, Factory, BPF>
impl<'env, Factory, BPF> StateRootTask<'env, Factory, BPF>
where
Factory: DatabaseProviderFactory<Provider: BlockReader>
+ StateCommitmentProvider
+ Clone
+ Send
+ Sync
+ 'static,
ABP: BlindedProvider<Error = SparseTrieError> + Send + Sync + 'env,
SBP: BlindedProvider<Error = SparseTrieError> + Send + Sync + 'env,
BPF: BlindedProviderFactory<AccountNodeProvider = ABP, StorageNodeProvider = SBP>
+ Send
+ Sync
+ 'env,
BPF: BlindedProviderFactory + Send + Sync + 'env,
BPF::AccountNodeProvider: BlindedProvider + Send + Sync + 'env,
BPF::StorageNodeProvider: BlindedProvider + Send + Sync + 'env,
{
/// Creates a new state root task with the unified message channel
pub fn new(
Expand Down Expand Up @@ -759,16 +756,17 @@ where

/// Updates the sparse trie with the given proofs and state, and returns the updated trie and the
/// time it took.
fn update_sparse_trie<
ABP: BlindedProvider<Error = SparseTrieError> + Send + Sync,
SBP: BlindedProvider<Error = SparseTrieError> + Send + Sync,
BPF: BlindedProviderFactory<AccountNodeProvider = ABP, StorageNodeProvider = SBP> + Send + Sync,
>(
fn update_sparse_trie<BPF>(
mut trie: Box<SparseStateTrie<BPF>>,
multiproof: MultiProof,
targets: MultiProofTargets,
state: HashedPostState,
) -> SparseStateTrieResult<(Box<SparseStateTrie<BPF>>, Duration)> {
) -> SparseStateTrieResult<(Box<SparseStateTrie<BPF>>, Duration)>
where
BPF: BlindedProviderFactory + Send + Sync,
BPF::AccountNodeProvider: BlindedProvider + Send + Sync,
BPF::StorageNodeProvider: BlindedProvider + Send + Sync,
{
trace!(target: "engine::root::sparse", "Updating sparse trie");
let started_at = Instant::now();

Expand Down
9 changes: 2 additions & 7 deletions crates/trie/sparse/src/blinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ pub trait BlindedProviderFactory {

/// Trie node provider for retrieving blinded nodes.
pub trait BlindedProvider {
/// The error type for the provider.
type Error: Into<SparseTrieError>;

/// Retrieve blinded node by path.
fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error>;
fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, SparseTrieError>;
}

/// Default blinded node provider factory that creates [`DefaultBlindedProvider`].
Expand All @@ -49,9 +46,7 @@ impl BlindedProviderFactory for DefaultBlindedProviderFactory {
pub struct DefaultBlindedProvider;

impl BlindedProvider for DefaultBlindedProvider {
type Error = SparseTrieError;

fn blinded_node(&mut self, _path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
fn blinded_node(&mut self, _path: &Nibbles) -> Result<Option<Bytes>, SparseTrieError> {
Ok(None)
}
}
Expand Down
13 changes: 3 additions & 10 deletions crates/trie/sparse/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
blinded::{BlindedProvider, BlindedProviderFactory, DefaultBlindedProviderFactory},
blinded::{BlindedProviderFactory, DefaultBlindedProviderFactory},
RevealedSparseTrie, SparseTrie,
};
use alloy_primitives::{
Expand All @@ -8,9 +8,7 @@ use alloy_primitives::{
Bytes, B256,
};
use alloy_rlp::{Decodable, Encodable};
use reth_execution_errors::{
SparseStateTrieErrorKind, SparseStateTrieResult, SparseTrieError, SparseTrieErrorKind,
};
use reth_execution_errors::{SparseStateTrieErrorKind, SparseStateTrieResult, SparseTrieErrorKind};
use reth_primitives_traits::Account;
use reth_tracing::tracing::trace;
use reth_trie_common::{
Expand Down Expand Up @@ -452,12 +450,7 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
})
}
}
impl<F> SparseStateTrie<F>
where
F: BlindedProviderFactory,
SparseTrieError: From<<F::AccountNodeProvider as BlindedProvider>::Error>
+ From<<F::StorageNodeProvider as BlindedProvider>::Error>,
{
impl<F: BlindedProviderFactory> SparseStateTrie<F> {
/// Update the account leaf node.
pub fn update_account_leaf(
&mut self,
Expand Down
16 changes: 4 additions & 12 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloy_primitives::{
B256,
};
use alloy_rlp::Decodable;
use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind, SparseTrieResult};
use reth_execution_errors::{SparseTrieErrorKind, SparseTrieResult};
use reth_tracing::tracing::trace;
use reth_trie_common::{
prefix_set::{PrefixSet, PrefixSetMut},
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<P> SparseTrie<P> {
}

/// Returns reference to revealed sparse trie if the trie is not blind.
pub fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie<P>> {
pub const fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie<P>> {
if let Self::Revealed(revealed) = self {
Some(revealed)
} else {
Expand Down Expand Up @@ -131,11 +131,7 @@ impl<P> SparseTrie<P> {
}
}

impl<P> SparseTrie<P>
where
P: BlindedProvider,
SparseTrieError: From<P::Error>,
{
impl<P: BlindedProvider> SparseTrie<P> {
/// Update the leaf node.
pub fn update_leaf(&mut self, path: Nibbles, value: Vec<u8>) -> SparseTrieResult<()> {
let revealed = self.as_revealed_mut().ok_or(SparseTrieErrorKind::Blind)?;
Expand Down Expand Up @@ -825,11 +821,7 @@ impl<P> RevealedSparseTrie<P> {
}
}

impl<P> RevealedSparseTrie<P>
where
P: BlindedProvider,
SparseTrieError: From<P::Error>,
{
impl<P: BlindedProvider> RevealedSparseTrie<P> {
/// Update the leaf node with provided value.
pub fn update_leaf(&mut self, path: Nibbles, value: Vec<u8>) -> SparseTrieResult<()> {
self.prefix_set.insert(path.clone());
Expand Down
8 changes: 2 additions & 6 deletions crates/trie/trie/src/proof/blinded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ where
T: TrieCursorFactory + Clone + Send + Sync,
H: HashedCursorFactory + Clone + Send + Sync,
{
type Error = SparseTrieError;

fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, SparseTrieError> {
let targets = HashMap::from_iter([(pad_path_to_key(path), HashSet::default())]);
let proof =
Proof::new(self.trie_cursor_factory.clone(), self.hashed_cursor_factory.clone())
Expand Down Expand Up @@ -128,9 +126,7 @@ where
T: TrieCursorFactory + Clone + Send + Sync,
H: HashedCursorFactory + Clone + Send + Sync,
{
type Error = SparseTrieError;

fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, SparseTrieError> {
let targets = HashSet::from_iter([pad_path_to_key(path)]);
let storage_prefix_set =
self.prefix_sets.storage_prefix_sets.get(&self.account).cloned().unwrap_or_default();
Expand Down
13 changes: 4 additions & 9 deletions crates/trie/trie/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ impl<F> WitnessBlindedProviderFactory<F> {
impl<F> BlindedProviderFactory for WitnessBlindedProviderFactory<F>
where
F: BlindedProviderFactory,
F::AccountNodeProvider: BlindedProvider<Error = SparseTrieError>,
F::StorageNodeProvider: BlindedProvider<Error = SparseTrieError>,
F::AccountNodeProvider: BlindedProvider,
F::StorageNodeProvider: BlindedProvider,
{
type AccountNodeProvider = WitnessBlindedProvider<F::AccountNodeProvider>;
type StorageNodeProvider = WitnessBlindedProvider<F::StorageNodeProvider>;
Expand Down Expand Up @@ -243,13 +243,8 @@ impl<P> WitnessBlindedProvider<P> {
}
}

impl<P> BlindedProvider for WitnessBlindedProvider<P>
where
P: BlindedProvider<Error = SparseTrieError>,
{
type Error = P::Error;

fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, Self::Error> {
impl<P: BlindedProvider> BlindedProvider for WitnessBlindedProvider<P> {
fn blinded_node(&mut self, path: &Nibbles) -> Result<Option<Bytes>, SparseTrieError> {
let maybe_node = self.provider.blinded_node(path)?;
if let Some(node) = &maybe_node {
self.tx
Expand Down

0 comments on commit 66f934b

Please sign in to comment.