Skip to content
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

perf(root): prefetch account proofs for the block #13431

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,7 @@ where
//
// let state_root_task = StateRootTask::new(state_root_config,
// blinded_provider_factory); let state_hook = state_root_task.state_hook();
// state_root_task.prefetch_account_proofs(&block);
// (Some(state_root_task.spawn(scope)), Box::new(state_hook) as Box<dyn OnStateHook>)
// } else {
// (None, Box::new(|_state: &EvmState| {}) as Box<dyn OnStateHook>)
Expand Down
40 changes: 36 additions & 4 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//! State root task related functionality.

use alloy_primitives::{map::HashSet, Address};
use alloy_primitives::map::HashSet;
use derive_more::derive::Deref;
use rayon::iter::{ParallelBridge, ParallelIterator};
use reth_errors::{ProviderError, ProviderResult};
use reth_evm::system_calls::OnStateHook;
use reth_primitives::BlockWithSenders;
use reth_primitives_traits::{BlockBody, SignedTransaction};
use reth_provider::{
providers::ConsistentDbView, BlockReader, DBProvider, DatabaseProviderFactory,
StateCommitmentProvider,
Expand All @@ -25,7 +27,7 @@ use reth_trie_sparse::{
errors::{SparseStateTrieError, SparseStateTrieResult, SparseTrieError, SparseTrieErrorKind},
SparseStateTrie,
};
use revm_primitives::{keccak256, EvmState, B256};
use revm_primitives::{keccak256, map::AddressHashSet, EvmState, B256};
use std::{
collections::BTreeMap,
sync::{
Expand Down Expand Up @@ -109,7 +111,7 @@ impl<Factory> StateRootConfig<Factory> {
#[allow(dead_code)]
pub enum StateRootMessage<BPF: BlindedProviderFactory> {
/// Prefetch proof targets
PrefetchProofs(HashSet<Address>),
PrefetchProofs(AddressHashSet),
/// New state update from transaction execution
StateUpdate(EvmState),
/// Proof calculation completed for a specific state update
Expand Down Expand Up @@ -342,11 +344,41 @@ where
}
}

/// Prefetch proofs for the accounts in the provided block.
///
/// Accounts that will be prefetched are:
/// - Transaction senders
/// - Transaction recipients
/// - Withdrawal recipients
///
/// This method does not prefetch the proofs on its own, but only sends the message to the
/// [`StateRootTask`] that will be processed by the main loop.
pub fn prefetch_account_proofs<
T: SignedTransaction + alloy_consensus::Transaction,
B: BlockBody<Transaction = T>,
>(
&self,
body: &BlockWithSenders<B>,
) {
let mut accounts = AddressHashSet::with_capacity_and_hasher(
body.transactions().len() * 2 +
body.withdrawals().map_or(0, |withdrawals| withdrawals.len()),
Default::default(),
);
accounts.extend(body.senders.iter().copied());
accounts.extend(body.transactions().iter().filter_map(|tx| tx.kind().to().copied()));
if let Some(withdrawals) = body.withdrawals() {
accounts.extend(withdrawals.iter().map(|withdrawal| withdrawal.address));
}

let _ = self.tx.send(StateRootMessage::PrefetchProofs(accounts));
}

/// Handles request for proof prefetch.
fn on_prefetch_proof(
scope: &rayon::Scope<'env>,
config: StateRootConfig<Factory>,
targets: HashSet<Address>,
targets: AddressHashSet,
fetched_proof_targets: &mut MultiProofTargets,
proof_sequence_number: u64,
state_root_message_sender: Sender<StateRootMessage<BPF>>,
Expand Down
Loading