From e8e9e212dfb8227a1d96336df85cc6b3e4c42d03 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Thu, 9 Nov 2023 07:16:15 -0500 Subject: [PATCH] Move additional functions which retry until success into Network trait --- processor/src/main.rs | 11 +++--- processor/src/networks/mod.rs | 65 ++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/processor/src/main.rs b/processor/src/main.rs index e1aa7b54..52916924 100644 --- a/processor/src/main.rs +++ b/processor/src/main.rs @@ -23,7 +23,7 @@ mod plan; pub use plan::*; mod networks; -use networks::{Block, Network, get_latest_block_number, get_block}; +use networks::{Block, Network}; #[cfg(feature = "bitcoin")] use networks::Bitcoin; #[cfg(feature = "monero")] @@ -252,9 +252,9 @@ async fn handle_coordinator_msg( // 10 + 1 - 10 = 1 let mut block_i; while { - block_i = - (get_latest_block_number(network).await + 1).saturating_sub(N::CONFIRMATIONS); - get_block(network, block_i).await.time(network).await < context.serai_time + block_i = (network.get_latest_block_number_with_retries().await + 1) + .saturating_sub(N::CONFIRMATIONS); + network.get_block_with_retries(block_i).await.time(network).await < context.serai_time } { info!( "serai confirmed the first key pair for a set. {} {}", @@ -270,7 +270,8 @@ async fn handle_coordinator_msg( // which... should be impossible // Yet a prevented panic is a prevented panic while (earliest > 0) && - (get_block(network, earliest - 1).await.time(network).await >= context.serai_time) + (network.get_block_with_retries(earliest - 1).await.time(network).await >= + context.serai_time) { earliest -= 1; } diff --git a/processor/src/networks/mod.rs b/processor/src/networks/mod.rs index c0ad9e8a..162203b3 100644 --- a/processor/src/networks/mod.rs +++ b/processor/src/networks/mod.rs @@ -298,6 +298,39 @@ pub trait Network: 'static + Send + Sync + Clone + PartialEq + Eq + Debug { async fn get_latest_block_number(&self) -> Result; /// Get a block by its number. async fn get_block(&self, number: usize) -> Result; + + /// Get the latest block's number, retrying until success. + async fn get_latest_block_number_with_retries(&self) -> usize { + loop { + match self.get_latest_block_number().await { + Ok(number) => { + return number; + } + Err(e) => { + error!( + "couldn't get the latest block number in the with retry get_latest_block_number: {e:?}", + ); + sleep(Duration::from_secs(10)).await; + } + } + } + } + + /// Get a block, retrying until success. + async fn get_block_with_retries(&self, block_number: usize) -> Self::Block { + loop { + match self.get_block(block_number).await { + Ok(block) => { + return block; + } + Err(e) => { + error!("couldn't get block {block_number} in the with retry get_block: {:?}", e); + sleep(Duration::from_secs(10)).await; + } + } + } + } + /// Get the outputs within a block for a specific key. async fn get_outputs( &self, @@ -539,35 +572,3 @@ pub trait Network: 'static + Send + Sync + Clone + PartialEq + Eq + Debug { #[cfg(test)] async fn test_send(&self, key: Self::Address) -> Self::Block; } - -// TODO: Move into above trait -pub async fn get_latest_block_number(network: &N) -> usize { - loop { - match network.get_latest_block_number().await { - Ok(number) => { - return number; - } - Err(e) => { - error!( - "couldn't get the latest block number in main's error-free get_block. {} {}", - "this should only happen if the node is offline. error: ", e - ); - sleep(Duration::from_secs(10)).await; - } - } - } -} - -pub async fn get_block(network: &N, block_number: usize) -> N::Block { - loop { - match network.get_block(block_number).await { - Ok(block) => { - return block; - } - Err(e) => { - error!("couldn't get block {block_number} in main's error-free get_block. error: {}", e); - sleep(Duration::from_secs(10)).await; - } - } - } -}