Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions bin/ream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ use ream_validator_lean::{
registry::load_validator_registry, service::ValidatorService as LeanValidatorService,
};
use ssz_types::VariableList;
use tokio::time;
use tokio::{
sync::{broadcast, mpsc},
time::Instant,
Expand Down Expand Up @@ -316,6 +317,10 @@ pub async fn run_lean_node(config: LeanNodeConfig, executor: ReamExecutor, ream_
ream_rpc_lean::server::start(server_config, lean_chain_reader, network_state).await
});

executor.spawn(async move {
countdown_for_genesis().await;
});

tokio::select! {
result = chain_future => {
error!("Chain service has stopped unexpectedly: {result:?}");
Expand Down Expand Up @@ -703,6 +708,36 @@ pub async fn run_generate_private_key(config: GeneratePrivateKeyConfig) {
process::exit(0);
}

// Countdown logs until the genesis timestamp reaches
pub async fn countdown_for_genesis() {
loop {
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(now) => now.as_secs(),
// The chain is already past genesis, we don't need a countdown
Err(_) => break,
};

let remaining = lean_network_spec().genesis_time.saturating_sub(now);

if remaining == 0 {
info!("Genesis reached! Starting services...");
break;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let now = match SystemTime::now().duration_since(UNIX_EPOCH) {
Ok(now) => now.as_secs(),
// The chain is already past genesis, we don't need a countdown
Err(_) => break,
};
let remaining = lean_network_spec().genesis_time.saturating_sub(now);
if remaining == 0 {
info!("Genesis reached! Starting services...");
break;
}
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or(Duration::MAX)
.as_secs();
let genesis = lean_network_spec().genesis_time;
if now >= genesis {
if now <= genesis + 2 {
info!("Genesis reached! Starting services...");
}
break;
}

After reading the code a bit more, could you do this ^


// Format the remaining time for a cleaner log
let minutes = (remaining % 3600) / 60;
let seconds = remaining % 60;

info!(
"Waiting for genesis in {:02}:{:02} seconds",
minutes, seconds
);

// Sleep for 1 second before ticking again
time::sleep(Duration::from_secs(1)).await;
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;
Expand Down