Modify PartitionCheck to be aware of pre & post Blossom target spacing.

This commit is contained in:
Simon 2019-05-19 15:58:11 -07:00 committed by Eirik Ogilvie-Wigley
parent 3a8fd5ea33
commit d7ebbeb2aa
3 changed files with 30 additions and 6 deletions

View File

@ -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

View File

@ -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<double> 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) {

View File

@ -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 */