From 86f080cc6deb0464edf3913daa2e1131b5eed7c7 Mon Sep 17 00:00:00 2001 From: Bitflash Date: Thu, 30 Jul 2026 18:22:51 -0300 Subject: net: announce our height in the handshake, and say when we are behind (#56) A node running this code cannot tell "in sync with everyone" from "not hearing anyone". The version message carries no height -- 0.1.0 never had the field and this fork inherited the gap -- so the only evidence of a problem is a number on screen that stopped moving, which looks the same as a network with nothing to report. It is the structural reason a broken node here has always been indistinguishable from an idle one. Append nBestHeight to the version message and keep what a peer announced in CNode::nStartingHeight. The field goes last, so a node that predates it stops reading after addr and treats the rest as trailing bytes, which cost it one log line. Reading it is guarded by vRecv.empty() and never required: peers that do not send it keep nStartingHeight at -1 and are left out of the median. Every release so far sends the short form. GetPeerMedianHeight() takes the median rather than the maximum so a single peer claiming an absurd height cannot move it. Then use it. After five minutes more than one block behind the peer median, the node says so in the log -- not behind a debug category, because an operator who already enabled -debug is not the one who needs telling -- and the GUI carries the same sentence in its status area. "8 blocks behind for 27 minutes" is something a user can act on; a frozen counter is not. Verified against two nodes on the live network: the one starting from an empty datadir read its peer at 3797, reported 2636 then 1274 then 433 blocks behind as it caught up, and went quiet on its own once level. The other logged "peer height = -1" for the older nodes it met on the real network, which is the compatibility path doing its job. Known limit: nStartingHeight is a snapshot from handshake time and goes stale on a long-lived connection, so this catches a node that is receiving something but not blocks, not one that has gone completely deaf. The inactivity timeout from #55 covers that case and refreshes these heights when it forces the reconnect. --- src/net.cpp | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'src/net.cpp') diff --git a/src/net.cpp b/src/net.cpp index 81932d7..ac8df82 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1225,6 +1225,65 @@ void ThreadMessageHandler(void* parg) } } +int GetPeerMedianHeight() +{ + vector vHeights; + CRITICAL_BLOCK(cs_vNodes) + foreach(CNode* pnode, vNodes) + if (pnode->nStartingHeight >= 0) + vHeights.push_back(pnode->nStartingHeight); + + if (vHeights.empty()) + return -1; + + sort(vHeights.begin(), vHeights.end()); + return vHeights[vHeights.size() / 2]; +} + + +// Say out loud when we are behind the network. +// +// This is the whole reason the height is in the handshake. Every serious bug +// this node has had looked identical from the outside: running, threads alive, +// nothing in the log, and no blocks arriving. A node that knows where everyone +// else is can say so, and "8 blocks behind for 27 minutes" is a sentence a +// user can act on. Deliberately not behind a debug category -- an operator who +// already suspects trouble is not the one who needs telling. +static void WarnIfBehind() +{ + static int64 nLastWarned; + static int64 nBehindSince; + + int nPeers = GetPeerMedianHeight(); + if (nPeers < 0 || nBestHeight < 0) + return; + + // One block of slack: somebody is always mid-relay. + if (nBestHeight >= nPeers - 1) + { + nBehindSince = 0; + return; + } + + int64 nNow = GetTime(); + if (nBehindSince == 0) + { + nBehindSince = nNow; + return; + } + + // Falling briefly behind is ordinary. Staying behind is not. + if (nNow - nBehindSince < BTF_BEHIND_GRACE_SECS) + return; + if (nNow - nLastWarned < BTF_BEHIND_WARN_INTERVAL_SECS) + return; + + nLastWarned = nNow; + printf("WARNING: %d blocks behind the network (height %d, peers report %d) for %d minutes\n", + nPeers - nBestHeight, nBestHeight, nPeers, (int)((nNow - nBehindSince) / 60)); +} + + void ThreadMessageHandler2(void* parg) { printf("ThreadMessageHandler started\n"); @@ -1250,6 +1309,8 @@ void ThreadMessageHandler2(void* parg) pnode->Release(); } + WarnIfBehind(); + // Wait and allow messages to bunch up vfThreadRunning[2] = false; Sleep(100); -- cgit v1.2.3