From d7263e09cf24616c1abbea7c0dd252f366f2101c Mon Sep 17 00:00:00 2001 From: Krystie Date: Mon, 3 Aug 2026 04:18:47 -0700 Subject: [PATCH] fix(sync): detect IBD when behind peers to prevent sync stall When a node restarts on a stalled chain (tip < 24h old from restart), IsInitialBlockDownload() returns false because the static nLastUpdate timestamp is recent. This prevents the stall recovery logic from triggering, leaving the node permanently stuck. Add a check: if our height is >5 blocks behind the peer median, we're in IBD regardless of the timestamp. This ensures nodes that are behind peers will enter IBD mode, send getheaders, and start downloading blocks. Fixes DNS2 sync stall at block 2,219,922 (4,841 blocks behind frozen tip). --- src/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 89d6f75..a625feb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1750,6 +1750,13 @@ bool IsInitialBlockDownload() // handles the specific staking-broker scenario. if (GetTime() - nLastUpdate > 24 * 60 * 60) return true; + // Also enter IBD if we're significantly behind peer heights, even if + // the tip was recently updated (e.g. after a daemon restart on a + // stalled chain). Without this, a node that restarts on a frozen + // chain thinks it's fully synced (tip < 24h old from restart) and + // never requests blocks from peers — permanently stuck. + if (nBestHeight > 0 && nBestHeight < GetNumBlocksOfPeers() - 5) + return true; return false; }