Skip to main content

reth_trie/proof_v2/
metrics.rs

1//! Cursor metrics of multiproof calculators.
2
3use crate::{
4    hashed_cursor::InstrumentedHashedCursorFactory, trie_cursor::InstrumentedTrieCursorFactory,
5};
6
7#[cfg(feature = "metrics")]
8use crate::{hashed_cursor::HashedCursorMetrics, trie_cursor::TrieCursorMetrics, TrieType};
9
10/// Cursor metric handles shared by multiproof calculators.
11#[cfg(feature = "metrics")]
12#[derive(Clone, Debug)]
13pub struct ProofCursorMetrics {
14    account_trie_cursor: TrieCursorMetrics,
15    storage_trie_cursor: TrieCursorMetrics,
16    account_hashed_cursor: HashedCursorMetrics,
17    storage_hashed_cursor: HashedCursorMetrics,
18}
19
20#[cfg(feature = "metrics")]
21impl Default for ProofCursorMetrics {
22    /// Registers cursor metric handles with the installed metrics recorder.
23    fn default() -> Self {
24        Self {
25            account_trie_cursor: TrieCursorMetrics::new(TrieType::State),
26            storage_trie_cursor: TrieCursorMetrics::new(TrieType::Storage),
27            account_hashed_cursor: HashedCursorMetrics::new(TrieType::State),
28            storage_hashed_cursor: HashedCursorMetrics::new(TrieType::Storage),
29        }
30    }
31}
32
33#[cfg(feature = "metrics")]
34impl ProofCursorMetrics {
35    /// Instruments all account and storage cursors opened by `factory`.
36    pub fn instrument_trie_factory<F>(&self, factory: F) -> InstrumentedTrieCursorFactory<F> {
37        InstrumentedTrieCursorFactory::new(
38            factory,
39            self.account_trie_cursor.clone(),
40            self.storage_trie_cursor.clone(),
41        )
42    }
43
44    /// Instruments all hashed account and storage cursors opened by `factory`.
45    pub fn instrument_hashed_factory<F>(&self, factory: F) -> InstrumentedHashedCursorFactory<F> {
46        InstrumentedHashedCursorFactory::new(
47            factory,
48            self.account_hashed_cursor.clone(),
49            self.storage_hashed_cursor.clone(),
50        )
51    }
52}
53
54/// No-op cursor metric handles used when the `metrics` feature is disabled.
55#[cfg(not(feature = "metrics"))]
56#[derive(Clone, Debug, Default)]
57pub struct ProofCursorMetrics;
58
59#[cfg(not(feature = "metrics"))]
60impl ProofCursorMetrics {
61    /// Returns the factory unchanged when cursor metrics are disabled.
62    pub const fn instrument_trie_factory<F>(&self, factory: F) -> InstrumentedTrieCursorFactory<F> {
63        factory
64    }
65
66    /// Returns the factory unchanged when cursor metrics are disabled.
67    pub const fn instrument_hashed_factory<F>(
68        &self,
69        factory: F,
70    ) -> InstrumentedHashedCursorFactory<F> {
71        factory
72    }
73}