From dfe6e167afc32276607eba3712ea0b82445e97e1 Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Wed, 11 Dec 2024 11:39:36 +0000 Subject: [PATCH] feat(engine): integrate blinded provider factory into state root task --- crates/engine/tree/src/tree/root.rs | 52 +++++++++++++++------------ crates/trie/sparse/src/blinded.rs | 4 +-- crates/trie/trie/src/proof/blinded.rs | 12 +++---- crates/trie/trie/src/witness.rs | 4 +-- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/crates/engine/tree/src/tree/root.rs b/crates/engine/tree/src/tree/root.rs index 72b18d49f52c..2995434cfd06 100644 --- a/crates/engine/tree/src/tree/root.rs +++ b/crates/engine/tree/src/tree/root.rs @@ -14,7 +14,8 @@ use reth_trie::{ use reth_trie_db::DatabaseProof; use reth_trie_parallel::root::ParallelStateRootError; use reth_trie_sparse::{ - errors::{SparseStateTrieResult, SparseTrieErrorKind}, + blinded::{BlindedProvider, BlindedProviderFactory}, + errors::{SparseStateTrieResult, SparseTrieError, SparseTrieErrorKind}, SparseStateTrie, }; use revm_primitives::{keccak256, EvmState, B256}; @@ -61,14 +62,14 @@ impl StateRootHandle { pub struct StateRootConfig { /// View over the state in the database. pub consistent_view: ConsistentDbView, - /// Latest trie input. + /// Latest trie input pub input: Arc, } /// Messages used internally by the state root task #[derive(Debug)] #[allow(dead_code)] -pub enum StateRootMessage { +pub enum StateRootMessage { /// New state update from transaction execution StateUpdate(EvmState), /// Proof calculation completed for a specific state update @@ -83,7 +84,7 @@ pub enum StateRootMessage { /// State root calculation completed RootCalculated { /// The updated sparse trie - trie: Box, + trie: Box>, /// Time taken to calculate the root elapsed: Duration, }, @@ -159,24 +160,24 @@ impl ProofSequencer { /// A wrapper for the sender that signals completion when dropped #[allow(dead_code)] -pub(crate) struct StateHookSender(Sender); +pub(crate) struct StateHookSender(Sender>); #[allow(dead_code)] -impl StateHookSender { - pub(crate) const fn new(inner: Sender) -> Self { +impl StateHookSender { + pub(crate) const fn new(inner: Sender>) -> Self { Self(inner) } } -impl Deref for StateHookSender { - type Target = Sender; +impl Deref for StateHookSender { + type Target = Sender>; fn deref(&self) -> &Self::Target { &self.0 } } -impl Drop for StateHookSender { +impl Drop for StateHookSender { fn drop(&mut self) { // Send completion signal when the sender is dropped let _ = self.0.send(StateRootMessage::FinishedStateUpdates); @@ -224,24 +225,24 @@ fn evm_state_to_hashed_post_state(update: EvmState) -> HashedPostState { /// to the tree. /// Then it updates relevant leaves according to the result of the transaction. #[derive(Debug)] -pub struct StateRootTask { +pub struct StateRootTask { /// Task configuration. config: StateRootConfig, /// Receiver for state root related messages. - rx: Receiver, + rx: Receiver>, /// Sender for state root related messages. - tx: Sender, + tx: Sender>, /// Proof targets that have been already fetched. fetched_proof_targets: MultiProofTargets, /// Proof sequencing handler. proof_sequencer: ProofSequencer, /// The sparse trie used for the state root calculation. If [`None`], then update is in /// progress. - sparse_trie: Option>, + sparse_trie: Option>>, } #[allow(dead_code)] -impl StateRootTask +impl StateRootTask where Factory: DatabaseProviderFactory + StateCommitmentProvider @@ -249,9 +250,12 @@ where + Send + Sync + 'static, + ABP: BlindedProvider, + SBP: BlindedProvider, + BPF: BlindedProviderFactory, { /// Creates a new state root task with the unified message channel - pub fn new(config: StateRootConfig) -> Self { + pub fn new(config: StateRootConfig, blinded_provider: BPF) -> Self { let (tx, rx) = channel(); Self { @@ -260,7 +264,7 @@ where tx, fetched_proof_targets: Default::default(), proof_sequencer: ProofSequencer::new(), - sparse_trie: Some(Box::new(SparseStateTrie::default().with_updates(true))), + sparse_trie: Some(Box::new(SparseStateTrie::new(blinded_provider).with_updates(true))), } } @@ -299,7 +303,7 @@ where update: EvmState, fetched_proof_targets: &mut MultiProofTargets, proof_sequence_number: u64, - state_root_message_sender: Sender, + state_root_message_sender: Sender>, ) { let hashed_state_update = evm_state_to_hashed_post_state(update); @@ -555,12 +559,16 @@ fn get_proof_targets( /// Updates the sparse trie with the given proofs and state, and returns the updated trie and the /// time it took. -fn update_sparse_trie( - mut trie: Box, +fn update_sparse_trie< + ABP: BlindedProvider, + SBP: BlindedProvider, + BPF: BlindedProviderFactory, +>( + mut trie: Box>, multiproof: MultiProof, targets: MultiProofTargets, state: HashedPostState, -) -> SparseStateTrieResult<(Box, Duration)> { +) -> SparseStateTrieResult<(Box>, Duration)> { trace!(target: "engine::root::sparse", "Updating sparse trie"); let started_at = Instant::now(); @@ -582,12 +590,10 @@ fn update_sparse_trie( trace!(target: "engine::root::sparse", ?address, "Wiping storage"); storage_trie.wipe()?; } - for (slot, value) in storage.storage { let slot_nibbles = Nibbles::unpack(slot); if value.is_zero() { trace!(target: "engine::root::sparse", ?address, ?slot, "Removing storage slot"); - storage_trie.remove_leaf(&slot_nibbles)?; } else { trace!(target: "engine::root::sparse", ?address, ?slot, "Updating storage slot"); diff --git a/crates/trie/sparse/src/blinded.rs b/crates/trie/sparse/src/blinded.rs index 22471cf99ffd..54f93b35be8f 100644 --- a/crates/trie/sparse/src/blinded.rs +++ b/crates/trie/sparse/src/blinded.rs @@ -5,7 +5,7 @@ use reth_execution_errors::SparseTrieError; use reth_trie_common::Nibbles; /// Factory for instantiating blinded node providers. -pub trait BlindedProviderFactory { +pub trait BlindedProviderFactory: Send + Sync { /// Type capable of fetching blinded account nodes. type AccountNodeProvider: BlindedProvider; /// Type capable of fetching blinded storage nodes. @@ -19,7 +19,7 @@ pub trait BlindedProviderFactory { } /// Trie node provider for retrieving blinded nodes. -pub trait BlindedProvider { +pub trait BlindedProvider: Send + Sync { /// The error type for the provider. type Error: Into; diff --git a/crates/trie/trie/src/proof/blinded.rs b/crates/trie/trie/src/proof/blinded.rs index 1383453f344d..55f8bdfbc48c 100644 --- a/crates/trie/trie/src/proof/blinded.rs +++ b/crates/trie/trie/src/proof/blinded.rs @@ -33,8 +33,8 @@ impl ProofBlindedProviderFactory { impl BlindedProviderFactory for ProofBlindedProviderFactory where - T: TrieCursorFactory + Clone, - H: HashedCursorFactory + Clone, + T: TrieCursorFactory + Clone + Send + Sync, + H: HashedCursorFactory + Clone + Send + Sync, { type AccountNodeProvider = ProofBlindedAccountProvider; type StorageNodeProvider = ProofBlindedStorageProvider; @@ -81,8 +81,8 @@ impl ProofBlindedAccountProvider { impl BlindedProvider for ProofBlindedAccountProvider where - T: TrieCursorFactory + Clone, - H: HashedCursorFactory + Clone, + T: TrieCursorFactory + Clone + Send + Sync, + H: HashedCursorFactory + Clone + Send + Sync, { type Error = SparseTrieError; @@ -125,8 +125,8 @@ impl ProofBlindedStorageProvider { impl BlindedProvider for ProofBlindedStorageProvider where - T: TrieCursorFactory + Clone, - H: HashedCursorFactory + Clone, + T: TrieCursorFactory + Clone + Send + Sync, + H: HashedCursorFactory + Clone + Send + Sync, { type Error = SparseTrieError; diff --git a/crates/trie/trie/src/witness.rs b/crates/trie/trie/src/witness.rs index 5e56cbf21c71..f5e6d46d6ce3 100644 --- a/crates/trie/trie/src/witness.rs +++ b/crates/trie/trie/src/witness.rs @@ -75,8 +75,8 @@ impl TrieWitness { impl TrieWitness where - T: TrieCursorFactory + Clone, - H: HashedCursorFactory + Clone, + T: TrieCursorFactory + Clone + Send + Sync, + H: HashedCursorFactory + Clone + Send + Sync, { /// Compute the state transition witness for the trie. Gather all required nodes /// to apply `state` on top of the current trie state.