node: check get_info sync progress

This commit is contained in:
hinto.janai 2023-11-24 09:12:03 -05:00
parent 846b6265ea
commit ff4b0181ad
No known key found for this signature in database
GPG key ID: D47CE05FA175A499

View file

@ -239,8 +239,8 @@ impl NodeData {
// A struct repr of the JSON-RPC we're // A struct repr of the JSON-RPC we're
// expecting back from the pinged nodes. // expecting back from the pinged nodes.
// //
// This structs leave out most fields on purpose, // This struct leaves out most fields on purpose,
// we only need a few to verify it works. // we only need a few to verify the node is ok.
#[derive(Debug, serde::Deserialize)] #[derive(Debug, serde::Deserialize)]
struct GetInfo<'a> { struct GetInfo<'a> {
id: &'a str, id: &'a str,
@ -379,22 +379,37 @@ impl Ping {
Ok(fastest_info) Ok(fastest_info)
} }
async fn response(client: Client<HttpConnector>, request: Request<Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) { async fn response(
client: Client<HttpConnector>,
request: Request<Body>,
ip: &'static str,
ping: Arc<Mutex<Self>>,
percent: f32,
node_vec: Arc<Mutex<Vec<NodeData>>>
) {
const DEAD_NODE_PING: u128 = 5000;
let ms; let ms;
let now = Instant::now(); let now = Instant::now();
const DEAD_NODE_PING: u128 = 5000;
match tokio::time::timeout(Duration::from_secs(5), client.request(request)).await { match tokio::time::timeout(Duration::from_secs(5), client.request(request)).await {
Ok(Ok(json_rpc)) => { Ok(Ok(json_rpc)) => {
// Attempt to convert to JSON-RPC. // Attempt to convert to JSON-RPC.
match hyper::body::to_bytes(json_rpc.into_body()).await { match hyper::body::to_bytes(json_rpc.into_body()).await {
Ok(b) => { Ok(b) => {
if serde_json::from_slice::<GetInfo<'_>>(&b).is_ok() { match serde_json::from_slice::<GetInfo<'_>>(&b) {
ms = now.elapsed().as_millis(); Ok(rpc) => {
} else { if rpc.result.mainnet && rpc.result.synchronized {
ms = DEAD_NODE_PING; ms = now.elapsed().as_millis();
warn!("Ping | {ip} responded but with invalid get_info, remove this node!"); } else {
ms = DEAD_NODE_PING;
warn!("Ping | {ip} responded with valid get_info but is not in sync, remove this node!");
}
}
_ => {
ms = DEAD_NODE_PING;
warn!("Ping | {ip} responded but with invalid get_info, remove this node!");
}
} }
}, },
_ => ms = DEAD_NODE_PING, _ => ms = DEAD_NODE_PING,
@ -406,21 +421,21 @@ impl Ping {
let info = format!("{ms}ms ... {ip}"); let info = format!("{ms}ms ... {ip}");
info!("Ping | {ms}ms ... {ip}"); info!("Ping | {ms}ms ... {ip}");
let color; let color = if ms < 300 {
if ms < 300 { GREEN
color = GREEN;
} else if ms < 500 { } else if ms < 500 {
color = YELLOW; YELLOW
} else if ms < DEAD_NODE_PING { } else if ms < DEAD_NODE_PING {
color = RED; RED
} else { } else {
color = BLACK; BLACK
} };
let mut ping = lock!(ping); let mut ping = lock!(ping);
ping.msg = info; ping.msg = info;
ping.prog += percent; ping.prog += percent;
drop(ping); drop(ping);
lock!(node_vec).push(NodeData { ip, ms, color, }); lock!(node_vec).push(NodeData { ip, ms, color });
} }
} }