Skip to content

Commit b790d43

Browse files
authored
chore: refactor redundant interval creation (#694)
* chore: refactor redundant interval creation * fix: imports * fix: import * fix: unhide error * fix: handle err * fix: pass up the err * fix: inline requested * fix: cleanup * fix: inline * fix: format
1 parent 8b4854a commit b790d43

File tree

7 files changed

+56
-72
lines changed

7 files changed

+56
-72
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/ream/src/main.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,19 @@ pub async fn run_lean_node(config: LeanNodeConfig, executor: ReamExecutor) {
160160

161161
// Start the services concurrently.
162162
let chain_future = executor.spawn(async move {
163-
chain_service.start().await;
163+
if let Err(err) = chain_service.start().await {
164+
panic!("Chain service exited with error: {err}");
165+
}
164166
});
165167
let network_future = executor.spawn(async move {
166168
if let Err(err) = network_service.start().await {
167169
panic!("Network service exited with error: {err}");
168170
}
169171
});
170172
let validator_future = executor.spawn(async move {
171-
validator_service.start().await;
173+
if let Err(err) = validator_service.start().await {
174+
panic!("Validator service exited with error: {err}");
175+
}
172176
});
173177

174178
tokio::select! {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::time::{Duration, SystemTime, UNIX_EPOCH};
2+
3+
use anyhow::anyhow;
4+
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
5+
use ream_network_spec::networks::lean_network_spec;
6+
use tokio::time::{Instant, Interval, MissedTickBehavior, interval_at};
7+
8+
pub fn create_lean_clock_interval() -> anyhow::Result<Interval> {
9+
let genesis_instant = UNIX_EPOCH + Duration::from_secs(lean_network_spec().genesis_time);
10+
11+
let interval_start = Instant::now()
12+
+ genesis_instant
13+
.duration_since(SystemTime::now())
14+
.map_err(|err| anyhow!("Genesis time is in the past: {err}"))?;
15+
16+
let mut interval = interval_at(
17+
interval_start,
18+
Duration::from_secs(lean_network_spec().seconds_per_slot / INTERVALS_PER_SLOT),
19+
);
20+
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);
21+
22+
Ok(interval)
23+
}

crates/common/chain/lean/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod clock;
12
pub mod genesis;
23
pub mod lean_chain;
34
pub mod service;

crates/common/chain/lean/src/service.rs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
1-
use std::{
2-
collections::HashMap,
3-
sync::Arc,
4-
time::{Duration, SystemTime, UNIX_EPOCH},
5-
};
1+
use std::{collections::HashMap, sync::Arc};
62

73
use alloy_primitives::B256;
4+
use anyhow::anyhow;
85
use ream_consensus_lean::{QueueItem, VoteItem, block::Block, process_block};
9-
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
106
use ream_network_spec::networks::lean_network_spec;
11-
use tokio::{
12-
sync::{RwLock, mpsc},
13-
time::{Instant, MissedTickBehavior, interval_at},
14-
};
7+
use tokio::sync::{RwLock, mpsc};
158
use tracing::info;
169
use tree_hash::TreeHash;
1710

18-
use crate::{lean_chain::LeanChain, slot::get_current_slot};
11+
use crate::{clock::create_lean_clock_interval, lean_chain::LeanChain, slot::get_current_slot};
1912

2013
#[derive(Debug, Clone)]
2114
pub struct LeanChainServiceMessage {
@@ -50,32 +43,16 @@ impl LeanChainService {
5043
}
5144
}
5245

53-
pub async fn start(mut self) {
54-
// TODO: Duplicate clock logic from ValidatorService. May need to refactor later.
55-
56-
// Get the Lean network specification.
57-
let network_spec = lean_network_spec();
58-
let seconds_per_slot = network_spec.seconds_per_slot;
59-
let genesis_time = network_spec.genesis_time;
60-
61-
info!("LeanChainService started with genesis_time={genesis_time}");
62-
63-
// Calculate the genesis instant from the genesis time (in seconds).
64-
let genesis_instant = UNIX_EPOCH + Duration::from_secs(genesis_time);
65-
66-
// Assume genesis time is "always" in the future,
67-
// as we don't support syncing features yet.
68-
let interval_start = Instant::now()
69-
+ genesis_instant
70-
.duration_since(SystemTime::now())
71-
.expect("Genesis time is in the past");
46+
pub async fn start(mut self) -> anyhow::Result<()> {
47+
info!(
48+
"LeanChainService started with genesis_time: {}",
49+
lean_network_spec().genesis_time
50+
);
7251

7352
let mut tick_count = 0u64;
74-
let mut interval = interval_at(
75-
interval_start,
76-
Duration::from_secs(seconds_per_slot / INTERVALS_PER_SLOT),
77-
);
78-
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);
53+
54+
let mut interval = create_lean_clock_interval()
55+
.map_err(|err| anyhow!("Failed to create clock interval: {err}"))?;
7956

8057
loop {
8158
tokio::select! {

crates/common/validator/lean/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ rust-version.workspace = true
1010
version.workspace = true
1111

1212
[dependencies]
13+
anyhow.workspace = true
1314
tokio.workspace = true
1415
tracing = { workspace = true, features = ["log"] }
1516

crates/common/validator/lean/src/service.rs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
use std::{
2-
sync::Arc,
3-
time::{Duration, SystemTime, UNIX_EPOCH},
4-
};
1+
use std::sync::Arc;
52

3+
use anyhow::anyhow;
64
use ream_chain_lean::{
7-
lean_chain::LeanChain, service::LeanChainServiceMessage, slot::get_current_slot,
5+
clock::create_lean_clock_interval, lean_chain::LeanChain, service::LeanChainServiceMessage,
6+
slot::get_current_slot,
87
};
98
use ream_consensus_lean::{QueueItem, VoteItem};
10-
use ream_consensus_misc::constants::lean::INTERVALS_PER_SLOT;
119
use ream_network_spec::networks::lean_network_spec;
12-
use tokio::{
13-
sync::{RwLock, mpsc},
14-
time::{Instant, MissedTickBehavior, interval_at},
15-
};
10+
use tokio::sync::{RwLock, mpsc};
1611
use tracing::info;
1712

1813
// TODO: We need to replace this after PQC integration.
@@ -60,35 +55,17 @@ impl ValidatorService {
6055
}
6156
}
6257

63-
pub async fn start(self) {
64-
// TODO: Duplicate clock logic from LeanChainService. May need to refactor later.
65-
66-
// Get the Lean network specification.
67-
let network_spec = lean_network_spec();
68-
let seconds_per_slot = network_spec.seconds_per_slot;
69-
let genesis_time = network_spec.genesis_time;
70-
58+
pub async fn start(self) -> anyhow::Result<()> {
7159
info!(
72-
"ValidatorService started with {} validator(s), genesis_time={genesis_time}",
73-
self.keystores.len()
60+
"ValidatorService started with {} validator(s), genesis_time: {}",
61+
self.keystores.len(),
62+
lean_network_spec().genesis_time
7463
);
7564

76-
// Calculate the genesis instant from the genesis time (in seconds).
77-
let genesis_instant = UNIX_EPOCH + Duration::from_secs(genesis_time);
78-
79-
// Assume genesis time is "always" in the future,
80-
// as we don't support syncing features yet.
81-
let interval_start = Instant::now()
82-
+ genesis_instant
83-
.duration_since(SystemTime::now())
84-
.expect("Genesis time is in the past");
85-
8665
let mut tick_count = 0u64;
87-
let mut interval = interval_at(
88-
interval_start,
89-
Duration::from_secs(seconds_per_slot / INTERVALS_PER_SLOT),
90-
);
91-
interval.set_missed_tick_behavior(MissedTickBehavior::Burst);
66+
67+
let mut interval = create_lean_clock_interval()
68+
.map_err(|err| anyhow!("Failed to create clock interval: {err}"))?;
9269

9370
loop {
9471
tokio::select! {

0 commit comments

Comments
 (0)