upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp61
1 files changed, 61 insertions, 0 deletions
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)
1225 } 1225 }
1226} 1226}
1227 1227
1228int GetPeerMedianHeight()
1229{
1230 vector<int> vHeights;
1231 CRITICAL_BLOCK(cs_vNodes)
1232 foreach(CNode* pnode, vNodes)
1233 if (pnode->nStartingHeight >= 0)
1234 vHeights.push_back(pnode->nStartingHeight);
1235
1236 if (vHeights.empty())
1237 return -1;
1238
1239 sort(vHeights.begin(), vHeights.end());
1240 return vHeights[vHeights.size() / 2];
1241}
1242
1243
1244// Say out loud when we are behind the network.
1245//
1246// This is the whole reason the height is in the handshake. Every serious bug
1247// this node has had looked identical from the outside: running, threads alive,
1248// nothing in the log, and no blocks arriving. A node that knows where everyone
1249// else is can say so, and "8 blocks behind for 27 minutes" is a sentence a
1250// user can act on. Deliberately not behind a debug category -- an operator who
1251// already suspects trouble is not the one who needs telling.
1252static void WarnIfBehind()
1253{
1254 static int64 nLastWarned;
1255 static int64 nBehindSince;
1256
1257 int nPeers = GetPeerMedianHeight();
1258 if (nPeers < 0 || nBestHeight < 0)
1259 return;
1260
1261 // One block of slack: somebody is always mid-relay.
1262 if (nBestHeight >= nPeers - 1)
1263 {
1264 nBehindSince = 0;
1265 return;
1266 }
1267
1268 int64 nNow = GetTime();
1269 if (nBehindSince == 0)
1270 {
1271 nBehindSince = nNow;
1272 return;
1273 }
1274
1275 // Falling briefly behind is ordinary. Staying behind is not.
1276 if (nNow - nBehindSince < BTF_BEHIND_GRACE_SECS)
1277 return;
1278 if (nNow - nLastWarned < BTF_BEHIND_WARN_INTERVAL_SECS)
1279 return;
1280
1281 nLastWarned = nNow;
1282 printf("WARNING: %d blocks behind the network (height %d, peers report %d) for %d minutes\n",
1283 nPeers - nBestHeight, nBestHeight, nPeers, (int)((nNow - nBehindSince) / 60));
1284}
1285
1286
1228void ThreadMessageHandler2(void* parg) 1287void ThreadMessageHandler2(void* parg)
1229{ 1288{
1230 printf("ThreadMessageHandler started\n"); 1289 printf("ThreadMessageHandler started\n");
@@ -1250,6 +1309,8 @@ void ThreadMessageHandler2(void* parg)
1250 pnode->Release(); 1309 pnode->Release();
1251 } 1310 }
1252 1311
1312 WarnIfBehind();
1313
1253 // Wait and allow messages to bunch up 1314 // Wait and allow messages to bunch up
1254 vfThreadRunning[2] = false; 1315 vfThreadRunning[2] = false;
1255 Sleep(100); 1316 Sleep(100);