Skip to content

Commit 0761b0b

Browse files
MuhtasimTanmoylaborant
andauthored
feat: add countdown log till genesis (#1104)
* add countdown log till genesis * Handle exception intentionally and safely * genesis countdown logic made cleaner * cargo fmt fix --------- Co-authored-by: laborant <[email protected]>
1 parent d131153 commit 0761b0b

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

bin/ream/src/main.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ use ream_validator_lean::{
9292
use ssz_types::VariableList;
9393
use tokio::{
9494
sync::{broadcast, mpsc},
95+
time,
9596
time::Instant,
9697
};
9798
use tracing::{error, info};
@@ -316,6 +317,10 @@ pub async fn run_lean_node(config: LeanNodeConfig, executor: ReamExecutor, ream_
316317
ream_rpc_lean::server::start(server_config, lean_chain_reader, network_state).await
317318
});
318319

320+
executor.spawn(async move {
321+
countdown_for_genesis().await;
322+
});
323+
319324
tokio::select! {
320325
result = chain_future => {
321326
error!("Chain service has stopped unexpectedly: {result:?}");
@@ -703,6 +708,40 @@ pub async fn run_generate_private_key(config: GeneratePrivateKeyConfig) {
703708
process::exit(0);
704709
}
705710

711+
// Countdown logs until the genesis timestamp reaches
712+
pub async fn countdown_for_genesis() {
713+
loop {
714+
let now = SystemTime::now()
715+
.duration_since(UNIX_EPOCH)
716+
.unwrap_or(Duration::MAX)
717+
.as_secs();
718+
let genesis = lean_network_spec().genesis_time;
719+
720+
if now >= genesis {
721+
// Only log the "Genesis reached" message if we are starting within
722+
// a small 2-second window of the actual event.
723+
if now <= genesis + 2 {
724+
info!("Genesis reached! Starting services...");
725+
}
726+
break;
727+
}
728+
729+
let remaining = lean_network_spec().genesis_time.saturating_sub(now);
730+
731+
// Format the remaining time for a cleaner log
732+
let minutes = (remaining % 3600) / 60;
733+
let seconds = remaining % 60;
734+
735+
info!(
736+
"Waiting for genesis in {:02}:{:02} seconds",
737+
minutes, seconds
738+
);
739+
740+
// Sleep for 1 second before ticking again
741+
time::sleep(Duration::from_secs(1)).await;
742+
}
743+
}
744+
706745
#[cfg(test)]
707746
mod tests {
708747
use std::time::Duration;

0 commit comments

Comments
 (0)