mirror of
https://github.com/serai-dex/serai.git
synced 2024-11-16 17:07:35 +00:00
Move additional functions which retry until success into Network trait
This commit is contained in:
parent
19187d2c30
commit
e8e9e212df
2 changed files with 39 additions and 37 deletions
|
@ -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<D: Db, N: Network, Co: Coordinator>(
|
|||
// 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<D: Db, N: Network, Co: Coordinator>(
|
|||
// 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;
|
||||
}
|
||||
|
|
|
@ -298,6 +298,39 @@ pub trait Network: 'static + Send + Sync + Clone + PartialEq + Eq + Debug {
|
|||
async fn get_latest_block_number(&self) -> Result<usize, NetworkError>;
|
||||
/// Get a block by its number.
|
||||
async fn get_block(&self, number: usize) -> Result<Self::Block, NetworkError>;
|
||||
|
||||
/// 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<N: Network>(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<N: Network>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue