From 1cbcd2e70e3ca0631e179a1587f38e4d6339aba2 Mon Sep 17 00:00:00 2001 From: Alexander Tesfamichael Date: Fri, 1 Dec 2023 16:58:34 +0100 Subject: [PATCH] feat: wait for node start --- src/geth.rs | 13 +++++++++++++ src/lighthouse.rs | 6 ++++++ src/main.rs | 28 +++++++++++++++++++++++++++- tests/integration.rs | 14 ++++++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/geth.rs b/src/geth.rs index de9caae..601407f 100644 --- a/src/geth.rs +++ b/src/geth.rs @@ -39,3 +39,16 @@ pub async fn peer_count() -> anyhow::Result { let peer_count = u64::from_str_radix(&raw_peer_count, 16)?; Ok(peer_count) } + +pub async fn ping_ok() -> anyhow::Result { + let client = reqwest::Client::new(); + let body: String = + json!({ "jsonrpc":"2.0","method":"net_version","params":[],"id":1 }).to_string(); + let res = client + .post(&ENV_CONFIG.geth_url) + .header("content-type", "application/json") + .body(body) + .send() + .await?; + Ok(res.status().is_success()) +} diff --git a/src/lighthouse.rs b/src/lighthouse.rs index af0372f..5fd4b54 100644 --- a/src/lighthouse.rs +++ b/src/lighthouse.rs @@ -103,6 +103,12 @@ pub async fn peer_counts(beacon_client: &Client) -> anyhow::Result { Ok(body) } +pub async fn ping_ok(beacon_client: &Client) -> anyhow::Result { + let url = format!("{}/eth/v1/node/version", &ENV_CONFIG.beacon_url); + let res = beacon_client.get(url).send().await?; + Ok(res.status().is_success()) +} + #[cfg(test)] mod tests { use serde_json::json; diff --git a/src/main.rs b/src/main.rs index 8dee6da..bb46bbb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ mod server; use std::{ sync::{atomic::AtomicBool, Arc}, - time::Duration, + time::{Duration, SystemTime}, }; use node_health::{geth, log}; @@ -26,6 +26,32 @@ async fn main() -> anyhow::Result<()> { let beacon_client = reqwest::Client::new(); + // It can take a long long time for the geth and lighthouse nodes to start responding to + // requests, so we wait until they are ready before we start the server. + const MAX_STARTUP_TIME: Duration = Duration::from_secs(60 * 2); + let start_time = SystemTime::now(); + loop { + let geth_ping_ok = geth::ping_ok().await?; + let lighthouse_ping_ok = node_health::lighthouse::ping_ok(&beacon_client).await?; + + if geth_ping_ok && lighthouse_ping_ok { + info!("geth and lighthouse are up"); + break; + } else { + debug!( + "geth_ping_ok: {}, lighthouse_ping_ok: {}", + geth_ping_ok, lighthouse_ping_ok + ); + } + + if start_time.elapsed()? > MAX_STARTUP_TIME { + anyhow::bail!("geth and lighthouse did not start responding in time"); + } + + debug!("sleeping 4s until next check"); + sleep(Duration::from_secs(4)).await; + } + loop { let geth_syncing = geth::syncing().await?; if geth_syncing { diff --git a/tests/integration.rs b/tests/integration.rs index e695b3f..f632ae2 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -12,6 +12,12 @@ async fn test_geth_sync_status() -> anyhow::Result<()> { Ok(()) } +#[tokio::test] +async fn test_geth_ping_ok() -> anyhow::Result<()> { + node_health::geth::ping_ok().await?; + Ok(()) +} + #[tokio::test] async fn test_lighthouse_peer_counts() -> anyhow::Result<()> { let beacon_client = Client::new(); @@ -35,3 +41,11 @@ async fn test_lighthouse_eth1_syncing() -> anyhow::Result<()> { dbg!(syncing); Ok(()) } + +#[tokio::test] +async fn test_lighthouse_ping_ok() -> anyhow::Result<()> { + let beacon_client = Client::new(); + let ping_ok = node_health::lighthouse::ping_ok(&beacon_client).await?; + dbg!(ping_ok); + Ok(()) +}