Skip to content

Commit a6152fa

Browse files
committed
refactor: use guard pattern
1 parent 0f53a6e commit a6152fa

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

crates/blockchain/state_transition/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub enum Error {
2929
///
3030
/// Similar to the spec's `State.state_transition`: https://github.com/leanEthereum/leanSpec/blob/bf0f606a75095cf1853529bc770516b1464d9716/src/lean_spec/subspecs/containers/state/state.py#L569
3131
pub fn state_transition(state: &mut State, block: &Block) -> Result<(), Error> {
32-
let start = std::time::Instant::now();
32+
let _timing = metrics::time_state_transition();
3333

3434
process_slots(state, block.slot)?;
3535
process_block(state, block)?;
@@ -53,13 +53,12 @@ pub fn state_transition(state: &mut State, block: &Block) -> Result<(), Error> {
5353
computed: computed_state_root,
5454
});
5555
}
56-
metrics::observe_state_transition_time(start.elapsed().as_secs_f64());
5756
Ok(())
5857
}
5958

6059
/// Advance the state through empty slots up to, but not including, target_slot.
6160
pub fn process_slots(state: &mut State, target_slot: u64) -> Result<(), Error> {
62-
let start = std::time::Instant::now();
61+
let _timing = metrics::time_slots_processing();
6362

6463
if state.slot >= target_slot {
6564
return Err(Error::StateSlotIsNewer {
@@ -74,18 +73,16 @@ pub fn process_slots(state: &mut State, target_slot: u64) -> Result<(), Error> {
7473
let slots_processed = target_slot - state.slot;
7574
state.slot = target_slot;
7675
metrics::inc_slots_processed(slots_processed);
77-
metrics::observe_slots_processing_time(start.elapsed().as_secs_f64());
7876
Ok(())
7977
}
8078

8179
/// Apply full block processing including header and body.
8280
pub fn process_block(state: &mut State, block: &Block) -> Result<(), Error> {
83-
let start = std::time::Instant::now();
81+
let _timing = metrics::time_block_processing();
8482

8583
process_block_header(state, block)?;
8684
process_attestations(state, &block.body.attestations)?;
8785

88-
metrics::observe_block_processing_time(start.elapsed().as_secs_f64());
8986
Ok(())
9087
}
9188

@@ -194,7 +191,7 @@ fn process_attestations(
194191
state: &mut State,
195192
attestations: &AggregatedAttestations,
196193
) -> Result<(), Error> {
197-
let start = std::time::Instant::now();
194+
let _timing = metrics::time_attestations_processing();
198195
let validator_count = state.validators.len();
199196
let mut attestations_processed: u64 = 0;
200197
let mut justifications: HashMap<H256, Vec<bool>> = state
@@ -349,7 +346,6 @@ fn process_attestations(
349346
.expect("justifications_roots limit exceeded");
350347
state.justifications_validators = justifications_validators;
351348
metrics::inc_attestations_processed(attestations_processed);
352-
metrics::observe_attestations_processing_time(start.elapsed().as_secs_f64());
353349
Ok(())
354350
}
355351

crates/blockchain/state_transition/src/metrics.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Prometheus metrics for state transition.
22
33
use std::sync::LazyLock;
4+
use std::time::Instant;
45

5-
use prometheus::{IntCounter, IntCounterVec, register_int_counter, register_int_counter_vec};
6+
use prometheus::{
7+
Histogram, IntCounter, IntCounterVec, register_int_counter, register_int_counter_vec,
8+
};
69

710
static LEAN_STATE_TRANSITION_SLOTS_PROCESSED_TOTAL: LazyLock<IntCounter> = LazyLock::new(|| {
811
register_int_counter!(
@@ -45,7 +48,7 @@ pub fn inc_finalizations(result: &str) {
4548
LEAN_FINALIZATIONS_TOTAL.with_label_values(&[result]).inc();
4649
}
4750

48-
static LEAN_STATE_TRANSITION_TIME_SECONDS: LazyLock<prometheus::Histogram> = LazyLock::new(|| {
51+
static LEAN_STATE_TRANSITION_TIME_SECONDS: LazyLock<Histogram> = LazyLock::new(|| {
4952
prometheus::register_histogram!(
5053
"lean_state_transition_time_seconds",
5154
"Duration of the entire state transition",
@@ -54,7 +57,7 @@ static LEAN_STATE_TRANSITION_TIME_SECONDS: LazyLock<prometheus::Histogram> = Laz
5457
.unwrap()
5558
});
5659

57-
static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS: LazyLock<prometheus::Histogram> =
60+
static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS: LazyLock<Histogram> =
5861
LazyLock::new(|| {
5962
prometheus::register_histogram!(
6063
"lean_state_transition_slots_processing_time_seconds",
@@ -64,7 +67,7 @@ static LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS: LazyLock<prometheus:
6467
.unwrap()
6568
});
6669

67-
static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS: LazyLock<prometheus::Histogram> =
70+
static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS: LazyLock<Histogram> =
6871
LazyLock::new(|| {
6972
prometheus::register_histogram!(
7073
"lean_state_transition_block_processing_time_seconds",
@@ -74,7 +77,7 @@ static LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS: LazyLock<prometheus:
7477
.unwrap()
7578
});
7679

77-
static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS: LazyLock<prometheus::Histogram> =
80+
static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS: LazyLock<Histogram> =
7881
LazyLock::new(|| {
7982
prometheus::register_histogram!(
8083
"lean_state_transition_attestations_processing_time_seconds",
@@ -84,22 +87,43 @@ static LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS: LazyLock<prom
8487
.unwrap()
8588
});
8689

87-
/// Record state transition time in seconds.
88-
pub fn observe_state_transition_time(duration_secs: f64) {
89-
LEAN_STATE_TRANSITION_TIME_SECONDS.observe(duration_secs);
90+
/// A guard that records elapsed time to a histogram when dropped.
91+
pub struct TimingGuard {
92+
histogram: &'static Histogram,
93+
start: Instant,
9094
}
9195

92-
/// Record slots processing time in seconds.
93-
pub fn observe_slots_processing_time(duration_secs: f64) {
94-
LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS.observe(duration_secs);
96+
impl TimingGuard {
97+
fn new(histogram: &'static Histogram) -> Self {
98+
Self {
99+
histogram,
100+
start: Instant::now(),
101+
}
102+
}
95103
}
96104

97-
/// Record block processing time in seconds.
98-
pub fn observe_block_processing_time(duration_secs: f64) {
99-
LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS.observe(duration_secs);
105+
impl Drop for TimingGuard {
106+
fn drop(&mut self) {
107+
self.histogram.observe(self.start.elapsed().as_secs_f64());
108+
}
100109
}
101110

102-
/// Record attestations processing time in seconds.
103-
pub fn observe_attestations_processing_time(duration_secs: f64) {
104-
LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS.observe(duration_secs);
111+
/// Start timing state transition. Records duration when the guard is dropped.
112+
pub fn time_state_transition() -> TimingGuard {
113+
TimingGuard::new(&LEAN_STATE_TRANSITION_TIME_SECONDS)
114+
}
115+
116+
/// Start timing slots processing. Records duration when the guard is dropped.
117+
pub fn time_slots_processing() -> TimingGuard {
118+
TimingGuard::new(&LEAN_STATE_TRANSITION_SLOTS_PROCESSING_TIME_SECONDS)
119+
}
120+
121+
/// Start timing block processing. Records duration when the guard is dropped.
122+
pub fn time_block_processing() -> TimingGuard {
123+
TimingGuard::new(&LEAN_STATE_TRANSITION_BLOCK_PROCESSING_TIME_SECONDS)
124+
}
125+
126+
/// Start timing attestations processing. Records duration when the guard is dropped.
127+
pub fn time_attestations_processing() -> TimingGuard {
128+
TimingGuard::new(&LEAN_STATE_TRANSITION_ATTESTATIONS_PROCESSING_TIME_SECONDS)
105129
}

0 commit comments

Comments
 (0)