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

[block relayer]is_nearly_synced. #2394

Merged
merged 2 commits into from
Apr 8, 2021
Merged
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: 3 additions & 3 deletions block-relayer/src/block_relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ impl BlockRelayer {
}
}

pub fn is_synced(&self) -> bool {
pub fn is_nearly_synced(&self) -> bool {
match self.sync_status.as_ref() {
Some(sync_status) => sync_status.is_synced(),
Some(sync_status) => sync_status.is_nearly_synced(),
None => false,
}
}
Expand All @@ -67,7 +67,7 @@ impl BlockRelayer {
network: NetworkServiceRef,
executed_block: Arc<ExecutedBlock>,
) {
if !self.is_synced() {
if !self.is_nearly_synced() {
debug!("[block-relay] Ignore NewHeadBlock event because the node has not been synchronized yet.");
return;
}
Expand Down
23 changes: 23 additions & 0 deletions types/src/sync_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct SyncStatus {
state: SyncState,
}

pub const NEARLY_SYNCED_BLOCKS: u64 = 24;

impl SyncStatus {
pub fn new(chain_status: ChainStatus) -> Self {
Self {
Expand Down Expand Up @@ -78,6 +80,27 @@ impl SyncStatus {
self.state.is_syncing()
}

pub fn is_nearly_synced(&self) -> bool {
match &self.state {
SyncState::Prepare => false,
SyncState::Synchronized => true,
SyncState::Synchronizing {
target,
total_difficulty,
} => {
if target.number() < self.chain_status.head().number() {
false
} else {
target
.number
.saturating_sub(self.chain_status.head().number())
<= NEARLY_SYNCED_BLOCKS
|| self.chain_status.total_difficulty() >= *total_difficulty
}
}
}
}

pub fn is_synced(&self) -> bool {
self.state.is_synced()
}
Expand Down