From 2c1366ebb270f196e69e5c603a388a7eae51e804 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Sat, 16 Dec 2023 20:31:11 +0200 Subject: [PATCH] Fix node sync state by handling potentially disappearing target when node is almost synced --- src/frontend/running.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/frontend/running.rs b/src/frontend/running.rs index 20d9c6ca..844ee461 100644 --- a/src/frontend/running.rs +++ b/src/frontend/running.rs @@ -6,6 +6,10 @@ use relm4::prelude::*; use subspace_core_primitives::BlockNumber; use tracing::warn; +/// Maximum blocks to store in the import queue. +// HACK: This constant comes from Substrate's sync, but it is not public in there +const MAX_IMPORTING_BLOCKS: BlockNumber = 2048; + #[derive(Debug)] pub enum RunningInput { Initialize { @@ -256,11 +260,37 @@ impl RunningView { }; } RunningInput::NodeNotification(node_notification) => match node_notification { - NodeNotification::SyncStateUpdate(sync_state) => { + NodeNotification::SyncStateUpdate(mut sync_state) => { + if let SyncState::Syncing { + target: new_target, .. + } = &mut sync_state + { + *new_target = (*new_target).max(self.node_state.best_block_number); + + // Ensure target is never below current block + if let SyncState::Syncing { + target: old_target, .. + } = &self.node_state.sync_state + { + // If old target was within `MAX_IMPORTING_BLOCKS` from new target, keep old target + if old_target + .checked_sub(*new_target) + .map(|diff| diff <= MAX_IMPORTING_BLOCKS) + .unwrap_or_default() + { + *new_target = *old_target; + } + } + } self.node_state.sync_state = sync_state; } NodeNotification::BlockImported { number } => { self.node_state.best_block_number = number; + + // Ensure target is never below current block + if let SyncState::Syncing { target, .. } = &mut self.node_state.sync_state { + *target = (*target).max(self.node_state.best_block_number); + } } }, RunningInput::FarmerNotification(farmer_notification) => match farmer_notification {