From af1f45190ff01bf6cc4da4e1b1e2dce6c8fd2928 Mon Sep 17 00:00:00 2001 From: stringhandler Date: Fri, 2 Aug 2024 09:19:07 +0200 Subject: [PATCH] fix: lagging code (#6441) Fixes a bug where the node would not reorg to a chain with a lower height, even if it had a higher difficulty --- .../state_machine_service/states/listening.rs | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/base_layer/core/src/base_node/state_machine_service/states/listening.rs b/base_layer/core/src/base_node/state_machine_service/states/listening.rs index 9e097d8d9b..4575434bb6 100644 --- a/base_layer/core/src/base_node/state_machine_service/states/listening.rs +++ b/base_layer/core/src/base_node/state_machine_service/states/listening.rs @@ -380,19 +380,26 @@ fn determine_sync_mode( } // This is to test the block propagation by delaying lagging. - if local_tip_height.saturating_add(blocks_behind_before_considered_lagging) > network_tip_height { - info!( - target: LOG_TARGET, - "While we are behind, we are still within {} blocks of them, so we are staying as listening and \ - waiting for the propagated blocks", - blocks_behind_before_considered_lagging - ); - return SyncStatus::BehindButNotYetLagging { - local: local.clone(), - network: network.claimed_chain_metadata().clone(), - sync_peers: vec![network.clone().into()], + // If the config is 0, ignore this set. + if blocks_behind_before_considered_lagging > 0 { + // Otherwise, only wait when the tip is above us, otherwise + // chains with a lower height will never be reorged to. + if network_tip_height > local_tip_height && + local_tip_height.saturating_add(blocks_behind_before_considered_lagging) > network_tip_height + { + info!( + target: LOG_TARGET, + "While we are behind, we are still within {} blocks of them, so we are staying as listening and \ + waiting for the propagated blocks", + blocks_behind_before_considered_lagging + ); + return SyncStatus::BehindButNotYetLagging { + local: local.clone(), + network: network.claimed_chain_metadata().clone(), + sync_peers: vec![network.clone().into()], + }; }; - }; + } debug!( target: LOG_TARGET,