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.h | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) (limited to 'src/net.h') diff --git a/src/net.h b/src/net.h index 449cff5..831a880 100644 --- a/src/net.h +++ b/src/net.h @@ -69,6 +69,21 @@ static const int BTF_HANDSHAKE_GRACE_SECS = 60; // stay well under BTF_RECV_TIMEOUT_SECS. static const int BTF_PING_INTERVAL_SECS = 10 * 60; +// How long a node must stay behind the network before it says so, and how +// often it repeats itself. Five minutes is two and a half block intervals -- +// long enough that ordinary relay lag never trips it. +static const int BTF_BEHIND_GRACE_SECS = 5 * 60; +static const int BTF_BEHIND_WARN_INTERVAL_SECS = 5 * 60; + +// Defined in main.cpp. Declared here because CNode announces it in the version +// message, and net.h is included before main.h. +extern int nBestHeight; + +// Median height announced by the peers we are connected to, or -1 while nobody +// has told us anything. Median rather than maximum so one peer claiming an +// absurd height cannot move it. +int GetPeerMedianHeight(); + // Descriptors to hand a peer: ours first, then peers that answered us. void BtfPexCollect(std::vector& vDescOut); // Verify descriptors a peer sent and remember the good ones. Returns how many. @@ -528,6 +543,11 @@ public: int64 nLastRecv; int64 nLastSendEmpty; + // Height this peer announced in its version message, or -1 if it sent a + // version message that predates the field. Knowing how far along everyone + // else is, is the only way a node can tell "in sync" from "not hearing". + int nStartingHeight; + CNode(SOCKET hSocketIn, CAddress addrIn, bool fInboundIn=false) { @@ -549,12 +569,16 @@ public: nLastSend = 0; nLastRecv = 0; nLastSendEmpty = GetTime(); + nStartingHeight = -1; vfSubscribe.assign(256, false); // Push a version message /// when NTP implemented, change to just nTime = GetAdjustedTime() int64 nTime = (fInbound ? GetAdjustedTime() : GetTime()); - PushMessage("version", VERSION, nLocalServices, nTime, addr); + // nBestHeight goes last so a node that predates it stops reading after + // addr and treats the rest as trailing bytes, which cost it a log line + // and nothing else. + PushMessage("version", VERSION, nLocalServices, nTime, addr, nBestHeight); } ~CNode() @@ -771,6 +795,22 @@ public: } } + template + void PushMessage(const char* pszCommand, const T1& a1, const T2& a2, const T3& a3, const T4& a4, const T5& a5) + { + try + { + BeginMessage(pszCommand); + vSend << a1 << a2 << a3 << a4 << a5; + EndMessage(); + } + catch (...) + { + AbortMessage(); + throw; + } + } + void PushRequest(const char* pszCommand, void (*fn)(void*, CDataStream&), void* param1) -- cgit v1.2.3