From ef5d8df69c92b0ed7193a60fd71383ffbd5f0731 Mon Sep 17 00:00:00 2001 From: lesniak43 Date: Fri, 26 Jan 2024 14:53:11 +0100 Subject: [PATCH] A0-3751: Make our `SyncOracle` compatible with `SyncingService` (#1580) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Our `SyncOracle` now returns an `AtomicBool` denoting if we're in major sync. ## Type of change - New feature (non-breaking change which adds functionality) --------- Co-authored-by: Damian Leśniak --- bin/node/src/service.rs | 2 +- finality-aleph/src/party/mod.rs | 2 +- finality-aleph/src/sync/handler/mod.rs | 4 ++-- finality-aleph/src/sync_oracle.rs | 24 ++++++++++++++++++------ 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/bin/node/src/service.rs b/bin/node/src/service.rs index 7197b80f7b..e81c96b4ae 100644 --- a/bin/node/src/service.rs +++ b/bin/node/src/service.rs @@ -245,7 +245,7 @@ fn setup( block_relay: None, })?; - let sync_oracle = SyncOracle::new(); + let (sync_oracle, _) = SyncOracle::new(); let validator_address_cache = match collect_extra_debugging_data { true => Some(ValidatorAddressCache::new()), diff --git a/finality-aleph/src/party/mod.rs b/finality-aleph/src/party/mod.rs index 0d525c6f52..b6f8cbcdcc 100644 --- a/finality-aleph/src/party/mod.rs +++ b/finality-aleph/src/party/mod.rs @@ -498,7 +498,7 @@ mod tests { let readonly_session_authorities = shared_map.read_only(); let chain_state = Arc::new(MockChainState::new()); - let sync_oracle = SyncOracle::new(); + let (sync_oracle, _) = SyncOracle::new(); let session_manager = Arc::new(MockNodeSessionManager::new()); let session_info = SessionBoundaryInfo::new(session_period); diff --git a/finality-aleph/src/sync/handler/mod.rs b/finality-aleph/src/sync/handler/mod.rs index 4ab3b0a7da..96aab3f09b 100644 --- a/finality-aleph/src/sync/handler/mod.rs +++ b/finality-aleph/src/sync/handler/mod.rs @@ -924,7 +924,7 @@ mod tests { let handler = Handler::new( database_io, verifier, - SyncOracle::new(), + SyncOracle::new().0, SESSION_BOUNDARY_INFO, ) .expect("mock backend works"); @@ -1839,7 +1839,7 @@ mod tests { let mut handler = Handler::new( database_io, verifier, - SyncOracle::new(), + SyncOracle::new().0, SessionBoundaryInfo::new(SessionPeriod(20)), ) .expect("mock backend works"); diff --git a/finality-aleph/src/sync_oracle.rs b/finality-aleph/src/sync_oracle.rs index 155b9c4efb..12af47d696 100644 --- a/finality-aleph/src/sync_oracle.rs +++ b/finality-aleph/src/sync_oracle.rs @@ -1,5 +1,8 @@ use std::{ - sync::Arc, + sync::{ + atomic::{AtomicBool, Ordering}, + Arc, + }, time::{Duration, Instant}, }; @@ -17,14 +20,19 @@ const MAJOR_SYNC_THRESHOLD: Duration = Duration::from_secs(10); pub struct SyncOracle { last_far_behind: Arc>, last_update: Arc>, + // TODO: remove when SyncingService is no longer needed + is_major_syncing: Arc, } impl SyncOracle { - pub fn new() -> Self { - SyncOracle { + pub fn new() -> (Self, Arc) { + let is_major_syncing = Arc::new(AtomicBool::new(true)); + let oracle = SyncOracle { last_update: Arc::new(Mutex::new(Instant::now() - OFFLINE_THRESHOLD)), last_far_behind: Arc::new(Mutex::new(Instant::now())), - } + is_major_syncing: is_major_syncing.clone(), + }; + (oracle, is_major_syncing) } pub fn update_behind(&self, behind: u32) { @@ -33,16 +41,20 @@ impl SyncOracle { if behind > FAR_BEHIND_THRESHOLD { *self.last_far_behind.lock() = now; } + self.major_sync(); } pub fn major_sync(&self) -> bool { - self.last_far_behind.lock().elapsed() < MAJOR_SYNC_THRESHOLD + let is_major_syncing = self.last_far_behind.lock().elapsed() < MAJOR_SYNC_THRESHOLD; + self.is_major_syncing + .store(is_major_syncing, Ordering::Relaxed); + is_major_syncing } } impl Default for SyncOracle { fn default() -> Self { - SyncOracle::new() + SyncOracle::new().0 } }