Skip to content

Commit

Permalink
A0-3751: Make our SyncOracle compatible with SyncingService (#1580)
Browse files Browse the repository at this point in the history
# 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 <[email protected]>
  • Loading branch information
lesniak43 and Damian Leśniak authored Jan 26, 2024
1 parent c832a8c commit ef5d8df
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bin/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion finality-aleph/src/party/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions finality-aleph/src/sync/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
24 changes: 18 additions & 6 deletions finality-aleph/src/sync_oracle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::{
sync::Arc,
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::{Duration, Instant},
};

Expand All @@ -17,14 +20,19 @@ const MAJOR_SYNC_THRESHOLD: Duration = Duration::from_secs(10);
pub struct SyncOracle {
last_far_behind: Arc<Mutex<Instant>>,
last_update: Arc<Mutex<Instant>>,
// TODO: remove when SyncingService is no longer needed
is_major_syncing: Arc<AtomicBool>,
}

impl SyncOracle {
pub fn new() -> Self {
SyncOracle {
pub fn new() -> (Self, Arc<AtomicBool>) {
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) {
Expand All @@ -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
}
}

Expand Down

0 comments on commit ef5d8df

Please sign in to comment.