Skip to content

Commit

Permalink
fix potential sync stuck
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Sep 11, 2023
1 parent 45c20a3 commit 427d296
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base_layer/core/src/base_node/sync/block_sync/synchronizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ use crate::{

const LOG_TARGET: &str = "c::bn::block_sync";

const MAX_LATENCY_INCREASES: usize = 10;

pub struct BlockSynchronizer<'a, B> {
config: BlockchainSyncConfig,
db: AsyncBlockchainDb<B>,
Expand Down Expand Up @@ -99,6 +101,7 @@ impl<'a, B: BlockchainBackend + 'static> BlockSynchronizer<'a, B> {
pub async fn synchronize(&mut self) -> Result<(), BlockSyncError> {
let mut max_latency = self.config.initial_max_sync_latency;
let mut sync_round = 0;
let mut latency_increases_counter = 0;
loop {
match self.attempt_block_sync(max_latency).await {
Ok(_) => return Ok(()),
Expand All @@ -111,6 +114,10 @@ impl<'a, B: BlockchainBackend + 'static> BlockSynchronizer<'a, B> {
max_latency,
self.sync_peers.len()
);
latency_increases_counter += 1;
if latency_increases_counter > MAX_LATENCY_INCREASES {
return Err(err);
}
// Prohibit using a few slow sync peers only, rather get new sync peers assigned
if self.sync_peers.len() < 2 {
return Err(err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const LOG_TARGET: &str = "c::bn::header_sync";

const NUM_INITIAL_HEADERS_TO_REQUEST: usize = 1000;

const MAX_LATENCY_INCREASES: usize = 10;

pub struct HeaderSynchronizer<'a, B> {
config: BlockchainSyncConfig,
db: AsyncBlockchainDb<B>,
Expand Down Expand Up @@ -113,6 +115,7 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
self.sync_peers.len()
);
let mut max_latency = self.config.initial_max_sync_latency;
let mut latency_increases_counter = 0;
loop {
match self.try_sync_from_all_peers(max_latency).await {
Ok(sync_peer) => break Ok(sync_peer),
Expand All @@ -122,6 +125,10 @@ impl<'a, B: BlockchainBackend + 'static> HeaderSynchronizer<'a, B> {
return Err(err);
}
max_latency += self.config.max_latency_increase;
latency_increases_counter += 1;
if latency_increases_counter > MAX_LATENCY_INCREASES {
return Err(err);
}
},
Err(err) => break Err(err),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ use crate::{

const LOG_TARGET: &str = "c::bn::state_machine_service::states::horizon_state_sync";

const MAX_LATENCY_INCREASES: usize = 10;

pub struct HorizonStateSynchronization<'a, B> {
config: BlockchainSyncConfig,
db: AsyncBlockchainDb<B>,
Expand Down Expand Up @@ -153,6 +155,7 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
}
})?;

let mut latency_increases_counter = 0;
loop {
match self.sync(&header).await {
Ok(()) => return Ok(()),
Expand All @@ -172,6 +175,10 @@ impl<'a, B: BlockchainBackend + 'static> HorizonStateSynchronization<'a, B> {
return Err(err);
}
self.max_latency += self.config.max_latency_increase;
latency_increases_counter += 1;
if latency_increases_counter > MAX_LATENCY_INCREASES {
return Err(err);
}
},
Err(err) => return Err(err),
}
Expand Down

0 comments on commit 427d296

Please sign in to comment.