diff --git a/block-relayer/src/block_relayer.rs b/block-relayer/src/block_relayer.rs index f1dd86af5a..28a391bec0 100644 --- a/block-relayer/src/block_relayer.rs +++ b/block-relayer/src/block_relayer.rs @@ -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, } } @@ -67,7 +67,7 @@ impl BlockRelayer { network: NetworkServiceRef, executed_block: Arc, ) { - if !self.is_synced() { + if !self.is_nearly_synced() { debug!("[block-relay] Ignore NewHeadBlock event because the node has not been synchronized yet."); return; } diff --git a/types/src/sync_status.rs b/types/src/sync_status.rs index 41d08c5bf3..26e128b333 100644 --- a/types/src/sync_status.rs +++ b/types/src/sync_status.rs @@ -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 { @@ -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() }