From d7ebbeb2aa0ff79e57d86dfe5dbf709f62d2bd0b Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 19 May 2019 15:58:11 -0700 Subject: [PATCH] Modify PartitionCheck to be aware of pre & post Blossom target spacing. --- src/init.cpp | 7 +++---- src/main.cpp | 27 ++++++++++++++++++++++++++- src/main.h | 2 +- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 2743f4731..6d6fd1f4a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1861,11 +1861,10 @@ 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; + // Monitor the chain every minute, and alert if we get blocks much quicker or slower than expected. CScheduler::Function f = boost::bind(&PartitionCheck, &IsInitialBlockDownload, - boost::ref(cs_main), boost::cref(pindexBestHeader), nPowTargetSpacing); - scheduler.scheduleEvery(f, nPowTargetSpacing); + boost::ref(cs_main), boost::cref(pindexBestHeader), 0); + scheduler.scheduleEvery(f, 60); #ifdef ENABLE_MINING // Generate coins in the background diff --git a/src/main.cpp b/src/main.cpp index f2d65c30f..fa28964e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2436,6 +2436,9 @@ void ThreadScriptCheck() { // we're being fed a bad chain (blocks being generated much // too slowly or too quickly). // +// When parameter nPowTargetSpacing is not set, the default value of 0 +// means use the block height of the best header to determine target spacing. +// Setting the parameter value is useful for testing. void PartitionCheck(bool (*initialDownloadCheck)(const CChainParams&), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing) @@ -2448,14 +2451,36 @@ void PartitionCheck(bool (*initialDownloadCheck)(const CChainParams&), const int SPAN_HOURS=4; const int SPAN_SECONDS=SPAN_HOURS*60*60; + + LOCK(cs); + + int bestHeaderHeight = bestHeader->nHeight; + int blossomHeight = Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT; + if (nPowTargetSpacing == 0) { + nPowTargetSpacing = Params().GetConsensus().PoWTargetSpacing(bestHeaderHeight); + blossomHeight = Params().GetConsensus().vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight; + } + int BLOCKS_EXPECTED = SPAN_SECONDS / nPowTargetSpacing; + // If the span period includes Blossom activation, adjust the number of expected blocks. + if (blossomHeight != Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT && + bestHeaderHeight > blossomHeight) + { + int t = SPAN_SECONDS; + int nBlossomBlocks = bestHeaderHeight - blossomHeight; + t -= nBlossomBlocks * Consensus::POST_BLOSSOM_POW_TARGET_SPACING; + if (t > 0) { + int nPreBlossomBlocks = t / Consensus::PRE_BLOSSOM_POW_TARGET_SPACING; + BLOCKS_EXPECTED = nPreBlossomBlocks + nBlossomBlocks; + } + } + boost::math::poisson_distribution poisson(BLOCKS_EXPECTED); std::string strWarning; int64_t startTime = GetAdjustedTime()-SPAN_SECONDS; - LOCK(cs); const CBlockIndex* i = bestHeader; int nBlocks = 0; while (i->GetBlockTime() >= startTime) { diff --git a/src/main.h b/src/main.h index cd2e39a09..deda1276c 100644 --- a/src/main.h +++ b/src/main.h @@ -234,7 +234,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle); /** Run an instance of the script checking thread */ void ThreadScriptCheck(); /** Try to detect Partition (network isolation) attacks against us */ -void PartitionCheck(bool (*initialDownloadCheck)(const CChainParams&), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing); +void PartitionCheck(bool (*initialDownloadCheck)(const CChainParams&), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing = 0); /** Check whether we are doing an initial block download (synchronizing from disk or network) */ bool IsInitialBlockDownload(const CChainParams& chainParams); /** Format a string that describes several potential problems detected by the core */