Skip to content

Commit 0b2f42d

Browse files
committed
Add CallFunctionInQueue to wait on validation interface queue drain
1 parent 2b4b345 commit 0b2f42d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/validationinterface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ void UnregisterAllValidationInterfaces() {
104104
g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
105105
}
106106

107+
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func) {
108+
g_signals.m_internals->m_schedulerClient.AddToProcessQueue(std::move(func));
109+
}
110+
107111
void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) {
108112
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
109113
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {

src/validationinterface.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
#ifndef BITCOIN_VALIDATIONINTERFACE_H
77
#define BITCOIN_VALIDATIONINTERFACE_H
88

9-
#include <memory>
10-
119
#include "primitives/transaction.h" // CTransaction(Ref)
1210

11+
#include <functional>
12+
#include <memory>
13+
1314
class CBlock;
1415
class CBlockIndex;
1516
struct CBlockLocator;
@@ -31,6 +32,16 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn);
3132
void UnregisterValidationInterface(CValidationInterface* pwalletIn);
3233
/** Unregister all wallets from core */
3334
void UnregisterAllValidationInterfaces();
35+
/**
36+
* Pushes a function to callback onto the notification queue, guaranteeing any
37+
* callbacks generated prior to now are finished when the function is called.
38+
*
39+
* Be very careful blocking on func to be called if any locks are held -
40+
* validation interface clients may not be able to make progress as they often
41+
* wait for things like cs_main, so blocking until func is called with cs_main
42+
* will result in a deadlock (that DEBUG_LOCKORDER will miss).
43+
*/
44+
void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
3445

3546
class CValidationInterface {
3647
protected:
@@ -86,6 +97,7 @@ class CMainSignals {
8697
friend void ::RegisterValidationInterface(CValidationInterface*);
8798
friend void ::UnregisterValidationInterface(CValidationInterface*);
8899
friend void ::UnregisterAllValidationInterfaces();
100+
friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
89101

90102
void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason);
91103

0 commit comments

Comments
 (0)