Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: potential sync stuck #5760

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
SWvheerden marked this conversation as resolved.
Show resolved Hide resolved

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
Loading