Skip to content
Merged
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions bin/ream/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ use tokio::{
};
use tracing::{error, info};
use tracing_subscriber::EnvFilter;
use tokio::time;

pub const APP_NAME: &str = "ream";

Expand Down Expand Up @@ -316,6 +317,8 @@ 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 +706,34 @@ 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 = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("System time is before UNIX epoch")
.as_secs();
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this expect() comment misleading?, I think we should avoid expect here, handle the exception intentionally and safely

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Referenced from here.

.expect("System time is before UNIX epoch")

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I will be removing those in a PR I am working on

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  let now = SystemTime::now()
                      .duration_since(UNIX_EPOCH)
                      .unwrap_or(Duration::MAX)// remaining <= 0 will be true if error
                      .as_secs();
        let remaining = lean_network_spec().genesis_time - now;

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

Does this look ok? Or should safely log error for that specific case?

Copy link
Contributor

Choose a reason for hiding this comment

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

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

You should do a saturating sub then to avoid underflows

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let now = SystemTime::now()
                      .duration_since(UNIX_EPOCH)
                      .unwrap_or(Duration::MAX)
                      .as_secs();
let remaining = lean_network_spec().genesis_time.saturating_sub(now);

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

If this looks okay, then will update the PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the // remaining <= 0 will be true if error

also if now == Duration::MAX we should not print anything and just immediately break

So maybe do

 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;
}

^ so do something like this

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't suggest that looking at the full context, so you might need to make further modifications

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested.

let remaining = lean_network_spec().genesis_time - now;

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

// 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