Skip to content

Commit

Permalink
Merge #1728: IsInitialBlockDownload: usually avoid locking
Browse files Browse the repository at this point in the history
9e378e4 IsInitialBlockDownload: usually avoid locking (furszy)

Pull request description:

  Straightforward, coming from [btc@8007](bitcoin#8007)

ACKs for top commit:
  random-zebra:
    Nice! utACK 9e378e4
  Fuzzbawls:
    utACK 9e378e4

Tree-SHA512: 3f5ebfbafa64ca7ff5397405c87f0030d81a4342486e420c7f3bf03daef08947afb22aed9d20bff02b4376a64e02902b9882c637e7a94ec235c4a5d4e1280391
  • Loading branch information
furszy committed Jul 7, 2020
2 parents f3b54df + 9e378e4 commit 1c472eb
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1554,17 +1554,22 @@ int64_t GetMasternodePayment()

bool IsInitialBlockDownload()
{
// 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))
return false;

LOCK(cs_main);
if (latchToFalse.load(std::memory_order_relaxed))
return false;
const int chainHeight = chainActive.Height();
if (fImporting || fReindex || fVerifyingBlocks || chainHeight < Checkpoints::GetTotalBlocksEstimate())
return true;
static bool lockIBDState = false;
if (lockIBDState)
return false;
bool state = (chainHeight < pindexBestHeader->nHeight - 24 * 6 ||
pindexBestHeader->GetBlockTime() < GetTime() - nMaxTipAge);
if (!state)
lockIBDState = true;
latchToFalse.store(true, std::memory_order_relaxed);
return state;
}

Expand Down

0 comments on commit 1c472eb

Please sign in to comment.