From af6b18b3ba7efe69b69da6e989ecabad4de6d3ab Mon Sep 17 00:00:00 2001 From: Larry Ruane Date: Thu, 30 Sep 2021 08:53:17 -0600 Subject: [PATCH] add TestSetIBD(bool) for testing --- src/main.cpp | 15 +++++++++++---- src/main.h | 2 ++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index f1b27f8ef..14d3fb4d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2239,16 +2239,23 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams) } } +static std::atomic 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 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; } diff --git a/src/main.h b/src/main.h index 85ca9af29..ab2cb54b8 100644 --- a/src/main.h +++ b/src/main.h @@ -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 GetWarnings(const std::string& strFor); /** Retrieve a transaction (from memory pool, or from disk, if possible) */