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: prevent race condition between block propagation and sync #3536

Merged
Changes from all commits
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 @@ -36,6 +36,7 @@ use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Formatter},
ops::Deref,
time::{Duration, Instant},
};
use tari_common_types::chain_metadata::ChainMetadata;
use tari_crypto::tari_utilities::epoch_time::EpochTime;
Expand Down Expand Up @@ -101,6 +102,7 @@ impl Listening {
) -> StateEvent {
info!(target: LOG_TARGET, "Listening for chain metadata updates");
shared.set_state_info(StateInfo::Listening(ListeningInfo::new(self.is_synced)));
let mut time_since_better_block = None;
loop {
let metadata_event = shared.metadata_event_stream.recv().await;
match metadata_event.as_ref().map(|v| v.deref()) {
Expand Down Expand Up @@ -158,7 +160,7 @@ impl Listening {
None => {
debug!(
target: LOG_TARGET,
"No best metadata for {} peer(s)",
"No better metadata advertised for {} peer(s)",
peer_metadata_list.len()
);
continue;
Expand All @@ -172,6 +174,26 @@ impl Listening {
},
};

// If this node is just one block behind, wait for block propagation before
// rushing to sync mode
if self.is_synced &&
best_metadata.height_of_longest_chain() == local.height_of_longest_chain() + 1 &&
time_since_better_block
.map(|ts: Instant| ts.elapsed() < Duration::from_secs(30))
.unwrap_or(true)
{
if time_since_better_block.is_none() {
time_since_better_block = Some(Instant::now());
}
debug!(
target: LOG_TARGET,
"This node is one block behind. Best network metadata is at height {}.",
best_metadata.height_of_longest_chain()
);
continue;
}
time_since_better_block = None;

// If we have configured sync peers, they are already filtered at this point
let sync_peers = if configured_sync_peers.is_empty() {
select_sync_peers(&local, &best_metadata, &peer_metadata_list)
Expand Down