upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorBitflash <btf01@proton.me>2026-07-30 18:22:51 -0300
committerGitHub <noreply@github.com>2026-07-30 18:22:51 -0300
commit86f080cc6deb0464edf3913daa2e1131b5eed7c7 (patch)
tree058f6aa96501191a76cd35ee1a5e7831ca3227ba /src/main.cpp
parent773440fcc7752b85d99ee5b8500b64dbb5869603 (diff)
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.
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp
index c77448d..15805ca 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1884,6 +1884,12 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
1884 if (pfrom->nVersion == 0) 1884 if (pfrom->nVersion == 0)
1885 return false; 1885 return false;
1886 1886
1887 // Optional: peers older than this field simply end the message here,
1888 // and nStartingHeight stays -1 for them. Never make this a requirement
1889 // -- every node released so far sends the short form.
1890 if (!vRecv.empty())
1891 vRecv >> pfrom->nStartingHeight;
1892
1887 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION)); 1893 pfrom->vSend.SetVersion(min(pfrom->nVersion, VERSION));
1888 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION)); 1894 pfrom->vRecv.SetVersion(min(pfrom->nVersion, VERSION));
1889 1895
@@ -1912,7 +1918,7 @@ bool ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
1912 if (!vPexOut.empty()) 1918 if (!vPexOut.empty())
1913 pfrom->PushMessage("btfpeers", vPexOut); 1919 pfrom->PushMessage("btfpeers", vPexOut);
1914 1920
1915 if (LogAcceptsCategory("net")) printf("version addrMe = %s\n", addrMe.ToString().c_str()); 1921 if (LogAcceptsCategory("net")) printf("version addrMe = %s, peer height = %d\n", addrMe.ToString().c_str(), pfrom->nStartingHeight);
1916 } 1922 }
1917 1923
1918 1924