From 2feb5d213c0fcccf0257927f419112549a0400d3 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 27 Apr 2022 05:51:22 +0000 Subject: [PATCH] better logs and only allow being 1 block behind --- src/block_watcher.rs | 6 ++---- src/main.rs | 12 +++++++++--- src/provider.rs | 11 ++++++++--- src/provider_tiers.rs | 2 +- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/block_watcher.rs b/src/block_watcher.rs index 6509ac95..cd1b2300 100644 --- a/src/block_watcher.rs +++ b/src/block_watcher.rs @@ -121,12 +121,10 @@ impl BlockWatcher { // TODO: include time since last update? info!( - "{:?} = {} Ts: {:?}, block number: {}, age: {}s {}", + "{:?} = {}, {}, {} sec, {}", new_block.hash.unwrap(), - rpc, - // TODO: human readable time? - new_block.timestamp, new_block.number.unwrap(), + rpc, now - new_block.timestamp.as_u64() as i64, label_slow_heads ); diff --git a/src/main.rs b/src/main.rs index ef99037d..b760935e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -345,10 +345,16 @@ async fn main() { vec![("ws://10.11.12.16:8545", 0), ("ws://10.11.12.16:8946", 0)], // paid nodes // TODO: add paid nodes (with rate limits) - // free nodes - // TODO: add rate limits vec![ - ("https://main-rpc.linkpool.io", 0), + // chainstack.com archive + ( + "wss://ws-nd-373-761-850.p2pify.com/106d73af4cebc487df5ba92f1ad8dee7", + 0, + ), + ], + // free nodes + vec![ + // ("https://main-rpc.linkpool.io", 0), // linkpool is slow ("https://rpc.ankr.com/eth", 0), ], ], diff --git a/src/provider.rs b/src/provider.rs index c4fe493b..999fa418 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -1,6 +1,6 @@ ///! Communicate with a web3 providers use derive_more::From; -use ethers::prelude::Middleware; +use ethers::prelude::{BlockNumber, Middleware}; use futures::StreamExt; use std::time::Duration; use std::{cmp::Ordering, sync::Arc}; @@ -40,16 +40,21 @@ impl Web3Provider { // TODO: automatically reconnect match &self { - Web3Provider::Http(_provider) => { + Web3Provider::Http(provider) => { // TODO: there is a "watch_blocks" function, but a lot of public nodes do not support the necessary rpc endpoints // TODO: how often? + // TODO: maybe it would be better to have one interval for all of these, but this works for now let mut interval = interval(Duration::from_secs(2)); loop { // wait for 2 seconds interval.tick().await; - unimplemented!("todo: send block number") + match provider.get_block(BlockNumber::Latest).await { + Ok(Some(block)) => block_watcher_sender.send((url.clone(), block)).unwrap(), + Ok(None) => warn!("no black at {}", url), + Err(e) => warn!("getBlock at {} failed: {}", url, e), + } } } Web3Provider::Ws(provider) => { diff --git a/src/provider_tiers.rs b/src/provider_tiers.rs index eaaa1bec..8a60ce73 100644 --- a/src/provider_tiers.rs +++ b/src/provider_tiers.rs @@ -155,7 +155,7 @@ impl Web3ProviderTier { for selected_rpc in self.rpcs.read().await.iter() { // check that the server is synced if !block_watcher - .is_synced(selected_rpc.clone(), 3) + .is_synced(selected_rpc.clone(), 1) .await .expect("checking is_synced failed") {