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

Smooth out sync-to-tip #3170

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions node/bft/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use crate::{
use snarkos_account::Account;
use snarkos_node_bft_events::PrimaryPing;
use snarkos_node_bft_ledger_service::LedgerService;
use snarkos_node_sync::MAX_BLOCKS_BEHIND;
use snarkvm::{
console::{
account::Signature,
Expand Down Expand Up @@ -1005,8 +1006,9 @@ impl<N: Network> Primary<N> {
let self_ = self.clone();
self.spawn(async move {
while let Some((peer_ip, batch_certificate)) = rx_batch_certified.recv().await {
// If the primary is not synced, then do not store the certificate.
if !self_.sync.is_synced() {
// If the primary is not synced and lagging by more than `MAX_BLOCKS_BEHIND + 1`, then do not store the certificate.
// This allows us to start processing the certificate as soon as we are within `MAX_BLOCKS_BEHIND + 1` blocks.
if !self_.sync.is_synced() && self_.sync.num_blocks_behind() > MAX_BLOCKS_BEHIND.saturating_add(1) {
trace!("Skipping a certified batch from '{peer_ip}' {}", "(node is syncing)".dimmed());
continue;
}
Expand Down
5 changes: 5 additions & 0 deletions node/bft/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ impl<N: Network> Sync<N> {
self.block_sync.is_block_synced()
}

/// Returns the number of blocks the node is behind the greatest peer height.
pub fn num_blocks_behind(&self) -> u32 {
self.block_sync.num_blocks_behind()
}

/// Returns `true` if the node is in gateway mode.
pub const fn is_gateway_mode(&self) -> bool {
self.block_sync.mode().is_gateway()
Expand Down
13 changes: 12 additions & 1 deletion node/sync/src/block_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::{
collections::BTreeMap,
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::{
atomic::{AtomicBool, Ordering},
atomic::{AtomicBool, AtomicU32, Ordering},
Arc,
},
time::Instant,
Expand Down Expand Up @@ -109,6 +109,8 @@ pub struct BlockSync<N: Network> {
request_timeouts: Arc<RwLock<IndexMap<SocketAddr, Vec<Instant>>>>,
/// The boolean indicator of whether the node is synced up to the latest block (within the given tolerance).
is_block_synced: Arc<AtomicBool>,
/// The number of blocks the peer is behind the greatest peer height.
num_blocks_behind: Arc<AtomicU32>,
/// The lock to guarantee advance_with_sync_blocks() is called only once at a time.
advance_with_sync_blocks_lock: Arc<Mutex<()>>,
}
Expand All @@ -126,6 +128,7 @@ impl<N: Network> BlockSync<N> {
request_timestamps: Default::default(),
request_timeouts: Default::default(),
is_block_synced: Default::default(),
num_blocks_behind: Default::default(),
advance_with_sync_blocks_lock: Default::default(),
}
}
Expand All @@ -141,6 +144,12 @@ impl<N: Network> BlockSync<N> {
pub fn is_block_synced(&self) -> bool {
self.is_block_synced.load(Ordering::SeqCst)
}

/// Returns the number of blocks the node is behind the greatest peer height.
#[inline]
pub fn num_blocks_behind(&self) -> u32 {
self.num_blocks_behind.load(Ordering::SeqCst)
}
}

#[allow(dead_code)]
Expand Down Expand Up @@ -439,6 +448,8 @@ impl<N: Network> BlockSync<N> {
let num_blocks_behind = greatest_peer_height.saturating_sub(canon_height);
// Determine if the primary is synced.
let is_synced = num_blocks_behind <= max_blocks_behind;
// Update the num blocks behind.
self.num_blocks_behind.store(num_blocks_behind, Ordering::SeqCst);
// Update the sync status.
self.is_block_synced.store(is_synced, Ordering::SeqCst);
}
Expand Down