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

fix(trie): do not reveal same node twice in sparse trie #14370

Merged
merged 6 commits into from
Feb 10, 2025
Merged
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
48 changes: 21 additions & 27 deletions crates/engine/tree/src/tree/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ pub struct StateRootComputeOutcome {
pub struct SparseTrieUpdate {
/// The state update that was used to calculate the proof
state: HashedPostState,
/// The proof targets
targets: MultiProofTargets,
/// The calculated multiproof
multiproof: MultiProof,
}
Expand All @@ -84,7 +82,6 @@ impl SparseTrieUpdate {
/// Extend update with contents of the other.
pub fn extend(&mut self, other: Self) {
self.state.extend(other.state);
extend_multi_proof_targets(&mut self.targets, other.targets);
self.multiproof.extend(other.multiproof);
}
}
Expand Down Expand Up @@ -179,6 +176,10 @@ pub struct ProofCalculated {
sequence_number: u64,
/// Sparse trie update
update: SparseTrieUpdate,
/// Total number of account targets
account_targets: usize,
/// Total number of storage slot targets
storage_targets: usize,
/// The time taken to calculate the proof.
elapsed: Duration,
}
Expand Down Expand Up @@ -397,19 +398,26 @@ where
let thread_pool = self.thread_pool.clone();

self.thread_pool.spawn(move || {
let account_targets = proof_targets.len();
let storage_targets = proof_targets.values().map(|slots| slots.len()).sum();

trace!(
target: "engine::root",
proof_sequence_number,
?proof_targets,
?account_targets,
?storage_targets,
"Starting multiproof calculation",
);
let start = Instant::now();
let result = calculate_multiproof(thread_pool, config, proof_targets.clone());
let result = calculate_multiproof(thread_pool, config, proof_targets);
let elapsed = start.elapsed();
trace!(
target: "engine::root",
proof_sequence_number,
?elapsed,
?account_targets,
?storage_targets,
"Multiproof calculated",
);

Expand All @@ -420,9 +428,10 @@ where
sequence_number: proof_sequence_number,
update: SparseTrieUpdate {
state: hashed_state_update,
targets: proof_targets,
multiproof: proof,
},
account_targets,
storage_targets,
elapsed,
}),
));
Expand Down Expand Up @@ -787,11 +796,7 @@ where

if let Some(combined_update) = self.on_proof(
sequence_number,
SparseTrieUpdate {
state,
targets: MultiProofTargets::default(),
multiproof: MultiProof::default(),
},
SparseTrieUpdate { state, multiproof: MultiProof::default() },
) {
let _ = sparse_trie_tx
.as_ref()
Expand Down Expand Up @@ -825,15 +830,10 @@ where
.record(proof_calculated.elapsed);
self.metrics
.proof_calculation_account_targets_histogram
.record(proof_calculated.update.targets.len() as f64);
self.metrics.proof_calculation_storage_targets_histogram.record(
proof_calculated
.update
.targets
.values()
.map(|targets| targets.len() as f64)
.sum::<f64>(),
);
.record(proof_calculated.account_targets as f64);
self.metrics
.proof_calculation_storage_targets_histogram
.record(proof_calculated.storage_targets as f64);

debug!(
target: "engine::root",
Expand Down Expand Up @@ -1077,7 +1077,7 @@ where
/// Updates the sparse trie with the given proofs and state, and returns the elapsed time.
fn update_sparse_trie<BPF>(
trie: &mut SparseStateTrie<BPF>,
SparseTrieUpdate { state, targets, multiproof }: SparseTrieUpdate,
SparseTrieUpdate { state, multiproof }: SparseTrieUpdate,
) -> SparseStateTrieResult<Duration>
where
BPF: BlindedProviderFactory + Send + Sync,
Expand All @@ -1088,7 +1088,7 @@ where
let started_at = Instant::now();

// Reveal new accounts and storage slots.
trie.reveal_multiproof(targets, multiproof)?;
trie.reveal_multiproof(multiproof)?;

// Update storage slots with new values and calculate storage roots.
let (tx, rx) = mpsc::channel();
Expand Down Expand Up @@ -1142,12 +1142,6 @@ where
Ok(elapsed)
}

fn extend_multi_proof_targets(targets: &mut MultiProofTargets, other: MultiProofTargets) {
for (address, slots) in other {
targets.entry(address).or_default().extend(slots);
}
}

fn extend_multi_proof_targets_ref(targets: &mut MultiProofTargets, other: &MultiProofTargets) {
for (address, slots) in other {
targets.entry(*address).or_default().extend(slots);
Expand Down
Loading
Loading