Skip to main content

reth_snap_sync/
metrics.rs

1//! Metrics for a snap sync run.
2
3use alloy_primitives::{B256, U256};
4use reth_metrics::{
5    metrics::{Counter, Gauge},
6    Metrics,
7};
8
9// Progress of a snap sync run, updated when a step commits or a stored attempt resumes.
10#[derive(Clone, Metrics)]
11#[metrics(scope = "sync.snap")]
12pub(crate) struct SnapSyncMetrics {
13    /// Block the downloaded state is anchored to.
14    pub(crate) pivot_block: Gauge,
15    /// Last block whose access list the downloaded state includes.
16    pub(crate) applied_block: Gauge,
17    /// Share of the account keyspace downloaded, from 0 to 1.
18    pub(crate) account_progress: Gauge,
19    /// Attempts abandoned and started over.
20    pub(crate) restarts: Counter,
21}
22
23impl SnapSyncMetrics {
24    // Drops the abandoned attempt's progress until the next one reports its own.
25    pub(crate) fn clear_attempt(&self) {
26        self.pivot_block.set(0.0);
27        self.applied_block.set(0.0);
28        self.account_progress.set(0.0);
29    }
30
31    // Accounts download in key order, so the cursor's most significant limb gives the share done.
32    pub(crate) fn set_account_progress(&self, next: Option<B256>) {
33        let progress = next
34            .map_or(1.0, |next| U256::from_be_bytes(next.0).as_limbs()[3] as f64 / u64::MAX as f64);
35        self.account_progress.set(progress);
36    }
37}