Skip to content

Commit

Permalink
feat: wait for node start
Browse files Browse the repository at this point in the history
  • Loading branch information
alextes committed Dec 1, 2023
1 parent f0533ed commit 1cbcd2e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ pub async fn peer_count() -> anyhow::Result<u64> {
let peer_count = u64::from_str_radix(&raw_peer_count, 16)?;
Ok(peer_count)
}

pub async fn ping_ok() -> anyhow::Result<bool> {
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())
}
6 changes: 6 additions & 0 deletions src/lighthouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ pub async fn peer_counts(beacon_client: &Client) -> anyhow::Result<PeerCounts> {
Ok(body)
}

pub async fn ping_ok(beacon_client: &Client) -> anyhow::Result<bool> {
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;
Expand Down
28 changes: 27 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod server;

use std::{
sync::{atomic::AtomicBool, Arc},
time::Duration,
time::{Duration, SystemTime},
};

use node_health::{geth, log};
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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(())
}

0 comments on commit 1cbcd2e

Please sign in to comment.