Skip to content

Commit bb862d7

Browse files
committed
Merge bitcoin#14384: Fire TransactionRemovedFromMempool callbacks from mempool
e20c72f Fire TransactionRemovedFromMempool from mempool (251) Pull request description: This pull request fires TransactionRemovedFromMempool callbacks from the mempool and cleans up a bunch of code. It also resolves the `txmempool -> validation -> validationinterface -> txmempool` circular dependency. Ideally, `validationinterface` is a dumb component that doesn't have any knowledge of the sub-systems it sends its notifications to. The commit that aims to resolve this circular dependency by moving `txmempool` specific code out of `validationinterface` to `txmempool` where it belongs. ACKs for top commit: jnewbery: ACK e20c72f Tree-SHA512: 354c3ff1113b21a0b511d80d604edfe3846dddae3355e43d1387f68906e54bf5dc01e7c029edc0b8e635b500b2ab97ee50362e2486eb4319f7347ee9a9e6cef3
2 parents 03f6f40 + e20c72f commit bb862d7

5 files changed

+15
-36
lines changed

src/init.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,6 @@ void Shutdown(NodeContext& node)
281281
node.chain_clients.clear();
282282
UnregisterAllValidationInterfaces();
283283
GetMainSignals().UnregisterBackgroundSignalScheduler();
284-
GetMainSignals().UnregisterWithMempoolSignals(mempool);
285284
globalVerifyHandle.reset();
286285
ECC_Stop();
287286
if (node.mempool) node.mempool = nullptr;
@@ -1263,7 +1262,6 @@ bool AppInitMain(NodeContext& node)
12631262
}, 60000);
12641263

12651264
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
1266-
GetMainSignals().RegisterWithMempoolSignals(mempool);
12671265

12681266
// Create client interfaces for wallets that are supposed to be loaded
12691267
// according to -wallet and -disablewallet options. This only constructs

src/txmempool.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <util/system.h>
1818
#include <util/moneystr.h>
1919
#include <util/time.h>
20+
#include <validationinterface.h>
2021

2122
CTxMemPoolEntry::CTxMemPoolEntry(const CTransactionRef& _tx, const CAmount& _nFee,
2223
int64_t _nTime, unsigned int _entryHeight,
@@ -403,7 +404,12 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
403404

404405
void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
405406
{
406-
NotifyEntryRemoved(it->GetSharedTx(), reason);
407+
CTransactionRef ptx = it->GetSharedTx();
408+
NotifyEntryRemoved(ptx, reason);
409+
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
410+
GetMainSignals().TransactionRemovedFromMempool(ptx);
411+
}
412+
407413
const uint256 hash = it->GetTx().GetHash();
408414
for (const CTxIn& txin : it->GetTx().vin)
409415
mapNextTx.erase(txin.prevout);

src/validationinterface.cpp

+7-24
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
#include <primitives/block.h>
99
#include <scheduler.h>
10-
#include <txmempool.h>
1110

1211
#include <future>
12+
#include <unordered_map>
1313
#include <utility>
1414

1515
#include <boost/signals2/signal.hpp>
@@ -46,11 +46,6 @@ struct MainSignalsInstance {
4646

4747
static CMainSignals g_signals;
4848

49-
// This map has to a separate global instead of a member of MainSignalsInstance,
50-
// because RegisterWithMempoolSignals is currently called before RegisterBackgroundSignalScheduler,
51-
// so MainSignalsInstance hasn't been created yet.
52-
static std::unordered_map<CTxMemPool*, boost::signals2::scoped_connection> g_connNotifyEntryRemoved;
53-
5449
void CMainSignals::RegisterBackgroundSignalScheduler(CScheduler& scheduler) {
5550
assert(!m_internals);
5651
m_internals.reset(new MainSignalsInstance(&scheduler));
@@ -71,17 +66,6 @@ size_t CMainSignals::CallbacksPending() {
7166
return m_internals->m_schedulerClient.CallbacksPending();
7267
}
7368

74-
void CMainSignals::RegisterWithMempoolSignals(CTxMemPool& pool) {
75-
g_connNotifyEntryRemoved.emplace(std::piecewise_construct,
76-
std::forward_as_tuple(&pool),
77-
std::forward_as_tuple(pool.NotifyEntryRemoved.connect(std::bind(&CMainSignals::MempoolEntryRemoved, this, std::placeholders::_1, std::placeholders::_2)))
78-
);
79-
}
80-
81-
void CMainSignals::UnregisterWithMempoolSignals(CTxMemPool& pool) {
82-
g_connNotifyEntryRemoved.erase(&pool);
83-
}
84-
8569
CMainSignals& GetMainSignals()
8670
{
8771
return g_signals;
@@ -126,13 +110,6 @@ void SyncWithValidationInterfaceQueue() {
126110
promise.get_future().wait();
127111
}
128112

129-
void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) {
130-
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
131-
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
132-
m_internals->TransactionRemovedFromMempool(ptx);
133-
});
134-
}
135-
}
136113

137114
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
138115
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which
@@ -150,6 +127,12 @@ void CMainSignals::TransactionAddedToMempool(const CTransactionRef &ptx) {
150127
});
151128
}
152129

130+
void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef &ptx) {
131+
m_internals->m_schedulerClient.AddToProcessQueue([ptx, this] {
132+
m_internals->TransactionRemovedFromMempool(ptx);
133+
});
134+
}
135+
153136
void CMainSignals::BlockConnected(const std::shared_ptr<const CBlock> &pblock, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>>& pvtxConflicted) {
154137
m_internals->m_schedulerClient.AddToProcessQueue([pblock, pindex, pvtxConflicted, this] {
155138
m_internals->BlockConnected(pblock, pindex, *pvtxConflicted);

src/validationinterface.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ class CConnman;
2121
class CValidationInterface;
2222
class uint256;
2323
class CScheduler;
24-
class CTxMemPool;
25-
enum class MemPoolRemovalReason;
2624

2725
// These functions dispatch to one or all registered wallets
2826

@@ -158,8 +156,6 @@ class CMainSignals {
158156
friend void ::UnregisterAllValidationInterfaces();
159157
friend void ::CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
160158

161-
void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason);
162-
163159
public:
164160
/** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
165161
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
@@ -170,13 +166,10 @@ class CMainSignals {
170166

171167
size_t CallbacksPending();
172168

173-
/** Register with mempool to call TransactionRemovedFromMempool callbacks */
174-
void RegisterWithMempoolSignals(CTxMemPool& pool);
175-
/** Unregister with mempool */
176-
void UnregisterWithMempoolSignals(CTxMemPool& pool);
177169

178170
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
179171
void TransactionAddedToMempool(const CTransactionRef &);
172+
void TransactionRemovedFromMempool(const CTransactionRef &);
180173
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::shared_ptr<const std::vector<CTransactionRef>> &);
181174
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
182175
void ChainStateFlushed(const CBlockLocator &);

test/lint/lint-circular-dependencies.sh

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ EXPECTED_CIRCULAR_DEPENDENCIES=(
2626
"wallet/fees -> wallet/wallet -> wallet/fees"
2727
"wallet/wallet -> wallet/walletdb -> wallet/wallet"
2828
"policy/fees -> txmempool -> validation -> policy/fees"
29-
"txmempool -> validation -> validationinterface -> txmempool"
3029
"wallet/scriptpubkeyman -> wallet/wallet -> wallet/scriptpubkeyman"
3130
)
3231

0 commit comments

Comments
 (0)