Skip to content

Commit 64cf7da

Browse files
committed
refactor: add timing info to pause logs
Signed-off-by: William Hankins <[email protected]>
1 parent bbc315b commit 64cf7da

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

modules/mithril_snapshot_fetcher/src/mithril_snapshot_fetcher.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,11 @@ impl MithrilSnapshotFetcher {
335335

336336
// Check pause constraint
337337
if pause_constraint.should_pause(&block_info) {
338-
let description = pause_constraint.get_description();
339-
let next_pause_constraint = pause_constraint.get_next();
340-
let next_description = next_pause_constraint.get_description();
341-
if prompt_pause(description, next_description).await {
338+
if prompt_pause(pause_constraint.get_description()).await {
342339
info!("Continuing without further pauses...");
343340
pause_constraint = PauseType::NoPause;
344341
} else {
345-
pause_constraint = next_pause_constraint;
342+
pause_constraint.next();
346343
}
347344
}
348345

@@ -433,9 +430,9 @@ impl MithrilSnapshotFetcher {
433430
}
434431

435432
/// Async helper to prompt user for pause behavior
436-
async fn prompt_pause(description: String, next_description: String) -> bool {
433+
async fn prompt_pause(description: String) -> bool {
437434
info!(
438-
"Paused at {description}. Press [Enter] to step to {next_description}, or [c + Enter] to continue without pauses."
435+
"Paused at {description}. Press [Enter] to step to to the next, or [c + Enter] to continue without pauses."
439436
);
440437
tokio::task::spawn_blocking(|| {
441438
use std::io::{self, BufRead};

modules/mithril_snapshot_fetcher/src/pause.rs

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1+
use std::time::Instant;
2+
13
use acropolis_common::BlockInfo;
24
use config::Config;
35
use tracing::{error, info};
46

57
#[derive(Debug, Clone, PartialEq)]
68
pub enum PauseType {
79
NoPause,
8-
Epoch(u64),
9-
Block(u64),
10+
Epoch {
11+
number: u64,
12+
start_time: std::time::Instant,
13+
},
14+
Block {
15+
number: u64,
16+
start_time: std::time::Instant,
17+
},
1018
}
1119

1220
impl PauseType {
@@ -26,15 +34,22 @@ impl PauseType {
2634

2735
let pause_type = parts[0].trim();
2836
let value = parts[1].trim().parse::<u64>().ok()?;
37+
let start_time = Instant::now();
2938

3039
match pause_type {
3140
"epoch" => {
3241
info!("Pausing enabled at epoch {value}");
33-
Some(PauseType::Epoch(value))
42+
Some(PauseType::Epoch {
43+
number: value,
44+
start_time,
45+
})
3446
}
3547
"block" => {
3648
info!("Pausing enabled at block {value}");
37-
Some(PauseType::Block(value))
49+
Some(PauseType::Block {
50+
number: value,
51+
start_time,
52+
})
3853
}
3954
_ => {
4055
error!(
@@ -48,29 +63,34 @@ impl PauseType {
4863

4964
pub fn should_pause(&self, block_info: &BlockInfo) -> bool {
5065
match self {
51-
PauseType::Epoch(target_epoch) => {
52-
if block_info.new_epoch {
53-
return block_info.epoch == *target_epoch;
54-
}
55-
false
56-
}
57-
PauseType::Block(target_block) => block_info.number == *target_block,
66+
PauseType::Epoch { number, .. } => block_info.new_epoch && block_info.epoch == *number,
67+
PauseType::Block { number, .. } => block_info.number == *number,
5868
PauseType::NoPause => false,
5969
}
6070
}
6171

62-
pub fn get_next(&self) -> Self {
72+
pub fn next(&mut self) {
6373
match self {
64-
PauseType::Epoch(target_epoch) => PauseType::Epoch(target_epoch + 1),
65-
PauseType::Block(target_block) => PauseType::Block(target_block + 1),
66-
PauseType::NoPause => PauseType::NoPause,
74+
PauseType::Epoch { number, start_time } => {
75+
*number += 1;
76+
*start_time = Instant::now();
77+
}
78+
PauseType::Block { number, start_time } => {
79+
*number += 1;
80+
*start_time = Instant::now();
81+
}
82+
PauseType::NoPause => {}
6783
}
6884
}
6985

7086
pub fn get_description(&self) -> String {
7187
match self {
72-
PauseType::Epoch(target_epoch) => format!("Epoch {target_epoch}"),
73-
PauseType::Block(target_block) => format!("Block {target_block}"),
88+
PauseType::Epoch { number, start_time } => {
89+
format!("Epoch {number} (started {:?} ago)", start_time.elapsed())
90+
}
91+
PauseType::Block { number, start_time } => {
92+
format!("Block {number} (started {:?} ago)", start_time.elapsed())
93+
}
7494
PauseType::NoPause => "No pause".to_string(),
7595
}
7696
}
@@ -85,14 +105,20 @@ mod tests {
85105
fn test_pause_type_from_config_epoch() {
86106
let config = Config::builder().set_override("pause", "epoch:100").unwrap().build().unwrap();
87107
let pause_type = PauseType::from_config(&config, DEFAULT_PAUSE);
88-
assert_eq!(pause_type, Some(PauseType::Epoch(100)));
108+
match pause_type {
109+
Some(PauseType::Epoch { number, .. }) => assert_eq!(number, 100),
110+
_ => panic!("Expected Some(PauseType::Epoch {{ number: 100, .. }})"),
111+
}
89112
}
90113

91114
#[test]
92115
fn test_pause_type_from_config_block() {
93116
let config = Config::builder().set_override("pause", "block:100").unwrap().build().unwrap();
94117
let pause_type = PauseType::from_config(&config, DEFAULT_PAUSE);
95-
assert_eq!(pause_type, Some(PauseType::Block(100)));
118+
match pause_type {
119+
Some(PauseType::Block { number, .. }) => assert_eq!(number, 100),
120+
_ => panic!("Expected Some(PauseType::Block {{ number: 100, .. }})"),
121+
}
96122
}
97123

98124
#[test]

0 commit comments

Comments
 (0)