From 66f934b8d0ee80920b03c6754dcc0c2da0587e7b Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Thu, 9 Jan 2025 14:23:31 +0100 Subject: [PATCH] chore(trie): simplify blinded provider (#13753) --- crates/engine/tree/src/tree/root.rs | 26 ++++++++++++-------------- crates/trie/sparse/src/blinded.rs | 9 ++------- crates/trie/sparse/src/state.rs | 13 +++---------- crates/trie/sparse/src/trie.rs | 16 ++++------------ crates/trie/trie/src/proof/blinded.rs | 8 ++------ crates/trie/trie/src/witness.rs | 13 ++++--------- 6 files changed, 27 insertions(+), 58 deletions(-) diff --git a/crates/engine/tree/src/tree/root.rs b/crates/engine/tree/src/tree/root.rs index 660c304f75e5..b41de299a125 100644 --- a/crates/engine/tree/src/tree/root.rs +++ b/crates/engine/tree/src/tree/root.rs @@ -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}; @@ -278,7 +278,7 @@ 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 + StateCommitmentProvider @@ -286,12 +286,9 @@ where + Send + Sync + 'static, - ABP: BlindedProvider + Send + Sync + 'env, - SBP: BlindedProvider + Send + Sync + 'env, - BPF: BlindedProviderFactory - + 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( @@ -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 + Send + Sync, - SBP: BlindedProvider + Send + Sync, - BPF: BlindedProviderFactory + Send + Sync, ->( +fn update_sparse_trie( mut trie: Box>, multiproof: MultiProof, targets: MultiProofTargets, state: HashedPostState, -) -> SparseStateTrieResult<(Box>, Duration)> { +) -> SparseStateTrieResult<(Box>, 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(); diff --git a/crates/trie/sparse/src/blinded.rs b/crates/trie/sparse/src/blinded.rs index a9f0e89c29c1..28a41ba11fa6 100644 --- a/crates/trie/sparse/src/blinded.rs +++ b/crates/trie/sparse/src/blinded.rs @@ -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; - /// Retrieve blinded node by path. - fn blinded_node(&mut self, path: &Nibbles) -> Result, Self::Error>; + fn blinded_node(&mut self, path: &Nibbles) -> Result, SparseTrieError>; } /// Default blinded node provider factory that creates [`DefaultBlindedProvider`]. @@ -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, Self::Error> { + fn blinded_node(&mut self, _path: &Nibbles) -> Result, SparseTrieError> { Ok(None) } } diff --git a/crates/trie/sparse/src/state.rs b/crates/trie/sparse/src/state.rs index 953198a36794..13718f87ac02 100644 --- a/crates/trie/sparse/src/state.rs +++ b/crates/trie/sparse/src/state.rs @@ -1,5 +1,5 @@ use crate::{ - blinded::{BlindedProvider, BlindedProviderFactory, DefaultBlindedProviderFactory}, + blinded::{BlindedProviderFactory, DefaultBlindedProviderFactory}, RevealedSparseTrie, SparseTrie, }; use alloy_primitives::{ @@ -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::{ @@ -452,12 +450,7 @@ impl SparseStateTrie { }) } } -impl SparseStateTrie -where - F: BlindedProviderFactory, - SparseTrieError: From<::Error> - + From<::Error>, -{ +impl SparseStateTrie { /// Update the account leaf node. pub fn update_account_leaf( &mut self, diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index c41fe5ee420d..7ff0e40e1a21 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -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}, @@ -73,7 +73,7 @@ impl

SparseTrie

{ } /// Returns reference to revealed sparse trie if the trie is not blind. - pub fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie

> { + pub const fn as_revealed_ref(&self) -> Option<&RevealedSparseTrie

> { if let Self::Revealed(revealed) = self { Some(revealed) } else { @@ -131,11 +131,7 @@ impl

SparseTrie

{ } } -impl

SparseTrie

-where - P: BlindedProvider, - SparseTrieError: From, -{ +impl SparseTrie

{ /// Update the leaf node. pub fn update_leaf(&mut self, path: Nibbles, value: Vec) -> SparseTrieResult<()> { let revealed = self.as_revealed_mut().ok_or(SparseTrieErrorKind::Blind)?; @@ -825,11 +821,7 @@ impl

RevealedSparseTrie

{ } } -impl

RevealedSparseTrie

-where - P: BlindedProvider, - SparseTrieError: From, -{ +impl RevealedSparseTrie

{ /// Update the leaf node with provided value. pub fn update_leaf(&mut self, path: Nibbles, value: Vec) -> SparseTrieResult<()> { self.prefix_set.insert(path.clone()); diff --git a/crates/trie/trie/src/proof/blinded.rs b/crates/trie/trie/src/proof/blinded.rs index 33a1a43b579b..c8d0f3bb5a22 100644 --- a/crates/trie/trie/src/proof/blinded.rs +++ b/crates/trie/trie/src/proof/blinded.rs @@ -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, Self::Error> { + fn blinded_node(&mut self, path: &Nibbles) -> Result, 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()) @@ -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, Self::Error> { + fn blinded_node(&mut self, path: &Nibbles) -> Result, 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(); diff --git a/crates/trie/trie/src/witness.rs b/crates/trie/trie/src/witness.rs index e29792146856..5d43c0ea145f 100644 --- a/crates/trie/trie/src/witness.rs +++ b/crates/trie/trie/src/witness.rs @@ -212,8 +212,8 @@ impl WitnessBlindedProviderFactory { impl BlindedProviderFactory for WitnessBlindedProviderFactory where F: BlindedProviderFactory, - F::AccountNodeProvider: BlindedProvider, - F::StorageNodeProvider: BlindedProvider, + F::AccountNodeProvider: BlindedProvider, + F::StorageNodeProvider: BlindedProvider, { type AccountNodeProvider = WitnessBlindedProvider; type StorageNodeProvider = WitnessBlindedProvider; @@ -243,13 +243,8 @@ impl

WitnessBlindedProvider

{ } } -impl

BlindedProvider for WitnessBlindedProvider

-where - P: BlindedProvider, -{ - type Error = P::Error; - - fn blinded_node(&mut self, path: &Nibbles) -> Result, Self::Error> { +impl BlindedProvider for WitnessBlindedProvider

{ + fn blinded_node(&mut self, path: &Nibbles) -> Result, SparseTrieError> { let maybe_node = self.provider.blinded_node(path)?; if let Some(node) = &maybe_node { self.tx