Skip to content

Commit

Permalink
Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip
Browse files Browse the repository at this point in the history
Ensures that callbacks are invoked in the order in which the chain is updated
Resolves bitcoin#12978

Github-Pull: #2290
Rebased-From: ef24337
  • Loading branch information
skeees authored and furszy committed Apr 8, 2021
1 parent c202bc1 commit 7ab7112
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2264,16 +2264,17 @@ bool ActivateBestChain(CValidationState& state, std::shared_ptr<const CBlock> pb
assert(trace.pblock && trace.pindex);
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
}
}

// Notify external listeners about the new tip.
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
// Notify external listeners about the new tip.
// Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);

// Always notify the UI if a new block tip was connected
if (pindexFork != pindexNewTip) {
// Notify the UI
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
// Always notify the UI if a new block tip was connected
if (pindexFork != pindexNewTip) {
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
}
}
// When we reach this point, we switched to a new tip (stored in pindexNewTip).

} while (pindexMostWork != chainActive.Tip());

Expand Down
4 changes: 4 additions & 0 deletions src/validationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason
}

void CMainSignals::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) {
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
// the chain actually updates. One way to ensure this is for the caller to invoke this signal
// in the same critical section where the chain is updated

m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] {
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
});
Expand Down

0 comments on commit 7ab7112

Please sign in to comment.