add TestSetIBD(bool) for testing

This commit is contained in:
Larry Ruane 2021-09-30 08:53:17 -06:00
parent 3ab6e1b3d9
commit af6b18b3ba
2 changed files with 13 additions and 4 deletions

View File

@ -2239,16 +2239,23 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
}
}
static std::atomic<bool> IBDLatchToFalse{false};
// testing-only, allow initial block down state to be set or reset
bool TestSetIBD(bool ibd) {
bool ret = !IBDLatchToFalse;
IBDLatchToFalse.store(!ibd, std::memory_order_relaxed);
return ret;
}
bool IsInitialBlockDownload(const Consensus::Params& params)
{
// Once this function has returned false, it must remain false.
static std::atomic<bool> latchToFalse{false};
// Optimization: pre-test latch before taking the lock.
if (latchToFalse.load(std::memory_order_relaxed))
if (IBDLatchToFalse.load(std::memory_order_relaxed))
return false;
LOCK(cs_main);
if (latchToFalse.load(std::memory_order_relaxed))
if (IBDLatchToFalse.load(std::memory_order_relaxed))
return false;
if (fImporting || fReindex)
return true;
@ -2310,7 +2317,7 @@ bool IsInitialBlockDownload(const Consensus::Params& params)
if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
return true;
LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
latchToFalse.store(true, std::memory_order_relaxed);
IBDLatchToFalse.store(true, std::memory_order_relaxed);
return false;
}

View File

@ -273,6 +273,8 @@ bool SendMessages(const Consensus::Params& params, CNode* pto);
void ThreadScriptCheck();
/** Check whether we are doing an initial block download (synchronizing from disk or network) */
bool IsInitialBlockDownload(const Consensus::Params& params);
/** testing-only, set or reset initial block down (IBD) state, return previous */
bool TestSetIBD(bool);
/** Format a string that describes several potential problems detected by the core */
std::pair<std::string, int64_t> GetWarnings(const std::string& strFor);
/** Retrieve a transaction (from memory pool, or from disk, if possible) */