Skip to content

Commit d86edd3

Browse files
committed
Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip
Ensures that callbacks are invoked in the order in which the chain is updated Resolves #12978
1 parent 5f2a399 commit d86edd3

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

src/validation.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool
353353

354354
CBlockIndex* tip = chainActive.Tip();
355355
assert(tip != nullptr);
356-
356+
357357
CBlockIndex index;
358358
index.pprev = tip;
359359
// CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
@@ -2677,18 +2677,17 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
26772677
assert(trace.pblock && trace.pindex);
26782678
GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs);
26792679
}
2680-
}
2681-
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
26822680

2683-
// Notifications/callbacks that can run without cs_main
2681+
// Notify external listeners about the new tip.
2682+
// Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected
2683+
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
26842684

2685-
// Notify external listeners about the new tip.
2686-
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
2687-
2688-
// Always notify the UI if a new block tip was connected
2689-
if (pindexFork != pindexNewTip) {
2690-
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
2685+
// Always notify the UI if a new block tip was connected
2686+
if (pindexFork != pindexNewTip) {
2687+
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
2688+
}
26912689
}
2690+
// When we reach this point, we switched to a new tip (stored in pindexNewTip).
26922691

26932692
if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown();
26942693

src/validationinterface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason
139139
}
140140

141141
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
142+
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
143+
// the chain actually updates. One way to ensure this is for the caller to invoke this signal
144+
// in the same critical section where the chain is updated
145+
142146
m_internals->m_schedulerClient.AddToProcessQueue([pindexNew, pindexFork, fInitialDownload, this] {
143147
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
144148
});

0 commit comments

Comments
 (0)