From 36cba8f1182e8284ec0017e57c5ffb519cd9798f Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Thu, 26 Mar 2015 11:20:59 -0400 Subject: Alert if it is very likely we are getting a bad chain Create a monitoring task that counts how many blocks have been found in the last four hours. If very few or too many have been found, an alert is triggered. "Very few" and "too many" are set based on a false positive rate of once every fifty years of constant running with constant hashing power, which works out to getting 5 or fewer or 48 or more blocks in four hours (instead of the average of 24). Only one alert per day is triggered, so if you get disconnected from the network (or are being Sybil'ed) -alertnotify will be triggered after 3.5 hours but you won't get another -alertnotify for 24 hours. Tested with a new unit test and by running on the main network with -debug=partitioncheck Run test/test_bitcoin --log_level=message to see the alert messages: WARNING: check your network connection, 3 blocks received in the last 4 hours (24 expected) WARNING: abnormally high number of blocks generated, 60 blocks received in the last 4 hours (24 expected) The -debug=partitioncheck debug.log messages look like: ThreadPartitionCheck : Found 22 blocks in the last 4 hours ThreadPartitionCheck : likelihood: 0.0777702 --- src/init.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/init.cpp') diff --git a/src/init.cpp b/src/init.cpp index bacc938375..3f189dd68b 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -39,7 +39,9 @@ #include #include +#include #include +#include #include #include #include @@ -1380,6 +1382,12 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) StartNode(threadGroup, scheduler); + // Monitor the chain, and alert if we get blocks much quicker or slower than expected + int64_t nPowTargetSpacing = Params().GetConsensus().nPowTargetSpacing; + CScheduler::Function f = boost::bind(&PartitionCheck, &IsInitialBlockDownload, + boost::ref(cs_main), boost::cref(chainActive), nPowTargetSpacing); + scheduler.scheduleEvery(f, nPowTargetSpacing); + #ifdef ENABLE_WALLET // Generate coins in the background if (pwalletMain) -- cgit v1.2.3