Skip to content

Commit

Permalink
Merge bitcoin#12287: Optimise lock behaviour for GuessVerificationPro…
Browse files Browse the repository at this point in the history
…gress()

90ba2df Fix missing cs_main lock for GuessVerificationProgress() (Jonas Schnelli)

Pull request description:

  `GuessVerificationProgress()` needs `cs_main` due to accessing the `pindex->nChainTx`.
  This adds a `AssertLockHeld` in `GuessVerificationProgress()` and adds the missing locks in...
  * `LoadChainTip()`
  * `ScanForWalletTransactions()` (got missed in bitcoin#11281)
  * GUI, `ClientModel::getVerificationProgress()` <--- **this may have GUI performance impacts**, but could be relaxed later with a cache or something more efficient.

Tree-SHA512: 13302946571422375f32af8e396b9d2c1180f2693ea363aeba9e98c8266ddec64fe7862bfdcbb5a93a4b12165a61eec1e51e4e7d7a8515fa50879095dc163412
  • Loading branch information
jonasschnelli authored and PastaPastaPasta committed Jun 10, 2020
1 parent 26ca61e commit fe6b44d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/qt/clientmodel.cpp
Expand Up @@ -174,9 +174,9 @@ size_t ClientModel::getInstantSentLockCount() const
double ClientModel::getVerificationProgress(const CBlockIndex *tipIn) const
{
CBlockIndex *tip = const_cast<CBlockIndex *>(tipIn);
LOCK(cs_main);
if (!tip)
{
LOCK(cs_main);
tip = chainActive.Tip();
}
return GuessVerificationProgress(Params().TxData(), tip);
Expand Down
1 change: 1 addition & 0 deletions src/validation.cpp
Expand Up @@ -4989,6 +4989,7 @@ bool DumpMempool(void)
}

//! Guess how far we are in the verification process at the given block index
//! require cs_main if pindex has not been validated yet (because nChainTx might be unset)
double GuessVerificationProgress(const ChainTxData& data, const CBlockIndex *pindex) {
if (pindex == nullptr)
return 0.0;
Expand Down
12 changes: 4 additions & 8 deletions src/wallet/wallet.cpp
Expand Up @@ -2065,20 +2065,15 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlock
dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
dProgressTip = GuessVerificationProgress(chainParams.TxData(), tip);
}
double gvp = dProgressStart;
while (pindex && !fAbortRescan)
{
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) {
double gvp = 0;
{
LOCK(cs_main);
gvp = GuessVerificationProgress(chainParams.TxData(), pindex);
}
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((gvp - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
}
if (GetTime() >= nNow + 60) {
nNow = GetTime();
LOCK(cs_main);
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, gvp);
}

CBlock block;
Expand All @@ -2102,6 +2097,7 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlock
{
LOCK(cs_main);
pindex = chainActive.Next(pindex);
gvp = GuessVerificationProgress(chainParams.TxData(), pindex);
if (tip != chainActive.Tip()) {
tip = chainActive.Tip();
// in case the tip has changed, update progress max
Expand All @@ -2110,7 +2106,7 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, CBlock
}
}
if (pindex && fAbortRescan) {
LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
LogPrintf("Rescan aborted at block %d. Progress=%f\n", pindex->nHeight, gvp);
}
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
}
Expand Down

0 comments on commit fe6b44d

Please sign in to comment.