From e4f699acd8951079d3513d0e84a581b30ecc78ab Mon Sep 17 00:00:00 2001 From: Brian Picciano <933154+mediocregopher@users.noreply.github.com> Date: Fri, 27 Mar 2026 11:51:38 +0100 Subject: [PATCH] feat(trie): Record trie cursor metrics Adds back trie cursor metrics in the proof workers. Metrics are recorded in memory (cursor_metrics_cache) and only actually recorded into the global metrics counters at the end of the block. --- crates/trie/parallel/src/proof_task.rs | 124 +++++++++++++++---------- 1 file changed, 73 insertions(+), 51 deletions(-) diff --git a/crates/trie/parallel/src/proof_task.rs b/crates/trie/parallel/src/proof_task.rs index 2dccd40b3f7..7cc66caad9d 100644 --- a/crates/trie/parallel/src/proof_task.rs +++ b/crates/trie/parallel/src/proof_task.rs @@ -34,7 +34,7 @@ use crate::{ }; use alloy_primitives::{ map::{B256Map, B256Set}, - B256, + B256, U256, }; use crossbeam_channel::{unbounded, Receiver as CrossbeamReceiver, Sender as CrossbeamSender}; use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind, StateProofError}; @@ -43,10 +43,10 @@ use reth_provider::{DatabaseProviderROFactory, ProviderError, ProviderResult}; use reth_storage_errors::db::DatabaseError; use reth_tasks::Runtime; use reth_trie::{ - hashed_cursor::HashedCursorFactory, + hashed_cursor::{HashedCursorFactory, HashedStorageCursor, InstrumentedHashedCursor}, proof::{ProofBlindedAccountProvider, ProofBlindedStorageProvider}, proof_v2, - trie_cursor::TrieCursorFactory, + trie_cursor::{InstrumentedTrieCursor, TrieCursorFactory, TrieStorageCursor}, DecodedMultiProofV2, HashedPostState, MultiProofTargetsV2, Nibbles, ProofTrieNodeV2, ProofV2Target, }; @@ -70,20 +70,20 @@ use crate::proof_task_metrics::{ type TrieNodeProviderResult = Result, SparseTrieError>; -/// Type alias for the V2 account proof calculator. +/// Type alias for the V2 account proof calculator with instrumented cursors. type V2AccountProofCalculator<'a, Provider> = proof_v2::ProofCalculator< - ::AccountTrieCursor<'a>, - ::AccountCursor<'a>, + InstrumentedTrieCursor<'a, ::AccountTrieCursor<'a>>, + InstrumentedHashedCursor<'a, ::AccountCursor<'a>>, AsyncAccountValueEncoder< - ::StorageTrieCursor<'a>, - ::StorageCursor<'a>, + InstrumentedTrieCursor<'a, ::StorageTrieCursor<'a>>, + InstrumentedHashedCursor<'a, ::StorageCursor<'a>>, >, >; -/// Type alias for the V2 storage proof calculator. +/// Type alias for the V2 storage proof calculator with instrumented cursors. type V2StorageProofCalculator<'a, Provider> = proof_v2::StorageProofCalculator< - ::StorageTrieCursor<'a>, - ::StorageCursor<'a>, + InstrumentedTrieCursor<'a, ::StorageTrieCursor<'a>>, + InstrumentedHashedCursor<'a, ::StorageCursor<'a>>, >; /// A handle that provides type-safe access to proof worker pools. @@ -428,14 +428,15 @@ impl ProofTaskTx where Provider: TrieCursorFactory + HashedCursorFactory, { - fn compute_v2_storage_proof( + fn compute_v2_storage_proof( &self, input: StorageProofInput, - calculator: &mut proof_v2::StorageProofCalculator< - ::StorageTrieCursor<'_>, - ::StorageCursor<'_>, - >, - ) -> Result { + calculator: &mut proof_v2::StorageProofCalculator, + ) -> Result + where + TC: TrieStorageCursor, + HC: HashedStorageCursor, + { let StorageProofInput { hashed_address, mut targets } = input; let span = debug_span!( @@ -706,8 +707,16 @@ where let mut cursor_metrics_cache = ProofTaskCursorMetricsCache::default(); let trie_cursor = proof_tx.provider.storage_trie_cursor(B256::ZERO)?; let hashed_cursor = proof_tx.provider.hashed_storage_cursor(B256::ZERO)?; - let mut v2_calculator = - proof_v2::StorageProofCalculator::new_storage(trie_cursor, hashed_cursor); + let instrumented_trie_cursor = + InstrumentedTrieCursor::new(trie_cursor, &mut cursor_metrics_cache.storage_trie_cursor); + let instrumented_hashed_cursor = InstrumentedHashedCursor::new( + hashed_cursor, + &mut cursor_metrics_cache.storage_hashed_cursor, + ); + let mut v2_calculator = proof_v2::StorageProofCalculator::new_storage( + instrumented_trie_cursor, + instrumented_hashed_cursor, + ); // Initially mark this worker as available. self.available_workers.fetch_add(1, Ordering::Relaxed); @@ -763,6 +772,9 @@ where idle_start = Instant::now(); } + // Drop calculator to release mutable borrows on cursor_metrics_cache. + drop(v2_calculator); + trace!( target: "trie::proof_task", worker_id = self.worker_id, @@ -783,18 +795,17 @@ where } /// Processes a storage proof request. - fn process_storage_proof( + fn process_storage_proof( &self, proof_tx: &ProofTaskTx, - v2_calculator: &mut proof_v2::StorageProofCalculator< - ::StorageTrieCursor<'_>, - ::StorageCursor<'_>, - >, + v2_calculator: &mut proof_v2::StorageProofCalculator, input: StorageProofInput, proof_result_sender: CrossbeamSender, storage_proofs_processed: &mut u64, ) where Provider: TrieCursorFactory + HashedCursorFactory, + TC: TrieStorageCursor, + HC: HashedStorageCursor, { let hashed_address = input.hashed_address; let proof_start = Instant::now(); @@ -980,18 +991,42 @@ where let storage_trie_cursor = provider.storage_trie_cursor(B256::ZERO)?; let storage_hashed_cursor = provider.hashed_storage_cursor(B256::ZERO)?; - let mut v2_account_calculator = proof_v2::ProofCalculator::< - _, - _, - AsyncAccountValueEncoder< - ::StorageTrieCursor<'_>, - ::StorageCursor<'_>, - >, - >::new(account_trie_cursor, account_hashed_cursor); + let instrumented_account_trie_cursor = InstrumentedTrieCursor::new( + account_trie_cursor, + &mut cursor_metrics_cache.account_trie_cursor, + ); + let instrumented_account_hashed_cursor = InstrumentedHashedCursor::new( + account_hashed_cursor, + &mut cursor_metrics_cache.account_hashed_cursor, + ); + let instrumented_storage_trie_cursor = InstrumentedTrieCursor::new( + storage_trie_cursor, + &mut cursor_metrics_cache.storage_trie_cursor, + ); + let instrumented_storage_hashed_cursor = InstrumentedHashedCursor::new( + storage_hashed_cursor, + &mut cursor_metrics_cache.storage_hashed_cursor, + ); + + let mut v2_account_calculator = + proof_v2::ProofCalculator::< + _, + _, + AsyncAccountValueEncoder< + InstrumentedTrieCursor< + '_, + ::StorageTrieCursor<'_>, + >, + InstrumentedHashedCursor< + '_, + ::StorageCursor<'_>, + >, + >, + >::new(instrumented_account_trie_cursor, instrumented_account_hashed_cursor); let v2_storage_calculator = Rc::new(RefCell::new(proof_v2::StorageProofCalculator::new_storage( - storage_trie_cursor, - storage_hashed_cursor, + instrumented_storage_trie_cursor, + instrumented_storage_hashed_cursor, ))); // Count this worker as available only after successful initialization. @@ -1027,7 +1062,6 @@ where v2_storage_calculator.clone(), *input, &mut account_proofs_processed, - &mut cursor_metrics_cache, ); total_idle_time += value_encoder_stats.storage_wait_time; value_encoder_stats_cache.extend(&value_encoder_stats); @@ -1050,6 +1084,10 @@ where idle_start = Instant::now(); } + // Drop calculators to release mutable borrows on cursor_metrics_cache. + drop(v2_account_calculator); + drop(v2_storage_calculator); + trace!( target: "trie::proof_task", worker_id=self.worker_id, @@ -1119,12 +1157,10 @@ where v2_storage_calculator: Rc>>, input: AccountMultiproofInput, account_proofs_processed: &mut u64, - cursor_metrics_cache: &mut ProofTaskCursorMetricsCache, ) -> ValueEncoderStats where Provider: TrieCursorFactory + HashedCursorFactory + 'a, { - let proof_cursor_metrics = ProofTaskCursorMetricsCache::default(); let proof_start = Instant::now(); let AccountMultiproofInput { targets, proof_result_sender } = input; @@ -1154,28 +1190,14 @@ where ); } - proof_cursor_metrics.record_spans(); - trace!( target: "trie::proof_task", proof_time_us = proof_elapsed.as_micros(), total_elapsed_us = total_elapsed.as_micros(), total_processed = account_proofs_processed, - account_trie_cursor_duration_us = proof_cursor_metrics.account_trie_cursor.total_duration.as_micros(), - account_hashed_cursor_duration_us = proof_cursor_metrics.account_hashed_cursor.total_duration.as_micros(), - storage_trie_cursor_duration_us = proof_cursor_metrics.storage_trie_cursor.total_duration.as_micros(), - storage_hashed_cursor_duration_us = proof_cursor_metrics.storage_hashed_cursor.total_duration.as_micros(), - account_trie_cursor_metrics = ?proof_cursor_metrics.account_trie_cursor, - account_hashed_cursor_metrics = ?proof_cursor_metrics.account_hashed_cursor, - storage_trie_cursor_metrics = ?proof_cursor_metrics.storage_trie_cursor, - storage_hashed_cursor_metrics = ?proof_cursor_metrics.storage_hashed_cursor, "Account multiproof completed" ); - #[cfg(feature = "metrics")] - // Accumulate per-proof metrics into the worker's cache - cursor_metrics_cache.extend(&proof_cursor_metrics); - value_encoder_stats }