Skip to content

Commit a7d3936

Browse files
committed
Add a CValidationInterface::TransactionRemovedFromMempool
This is currently unused, but will by used by wallet to cache when transactions are in the mempool, obviating the need for calls to mempool from CWalletTx::InMempool()
1 parent 326a565 commit a7d3936

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

src/init.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ void Shutdown()
265265
#endif
266266
UnregisterAllValidationInterfaces();
267267
GetMainSignals().UnregisterBackgroundSignalScheduler();
268+
GetMainSignals().UnregisterWithMempoolSignals(mempool);
268269
#ifdef ENABLE_WALLET
269270
CloseWallets();
270271
#endif
@@ -1240,6 +1241,7 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
12401241
threadGroup.create_thread(boost::bind(&TraceThread<CScheduler::Function>, "scheduler", serviceLoop));
12411242

12421243
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
1244+
GetMainSignals().RegisterWithMempoolSignals(mempool);
12431245

12441246
/* Start the RPC server already. It will be started in "warmup" mode
12451247
* and not really process calls already (but it will signify connections

src/txmempool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ class CTxMemPool
513513
// to track size/count of descendant transactions. First version of
514514
// addUnchecked can be used to have it call CalculateMemPoolAncestors(), and
515515
// then invoke the second version.
516+
// Note that addUnchecked is ONLY called from ATMP outside of tests
517+
// and any other callers may break wallet's in-mempool tracking (due to
518+
// lack of CValidationInterface::TransactionAddedToMempool callbacks).
516519
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, bool validFeeEstimate = true);
517520
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool validFeeEstimate = true);
518521

src/validationinterface.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "primitives/block.h"
1010
#include "scheduler.h"
1111
#include "sync.h"
12+
#include "txmempool.h"
1213
#include "util.h"
1314

1415
#include <list>
@@ -21,6 +22,7 @@ struct MainSignalsInstance {
2122
boost::signals2::signal<void (const CTransactionRef &)> TransactionAddedToMempool;
2223
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef>&)> BlockConnected;
2324
boost::signals2::signal<void (const std::shared_ptr<const CBlock> &)> BlockDisconnected;
25+
boost::signals2::signal<void (const CTransactionRef &)> TransactionRemovedFromMempool;
2426
boost::signals2::signal<void (const CBlockLocator &)> SetBestChain;
2527
boost::signals2::signal<void (const uint256 &)> Inventory;
2628
boost::signals2::signal<void (int64_t nBestBlockTime, CConnman* connman)> Broadcast;
@@ -50,6 +52,14 @@ void CMainSignals::FlushBackgroundCallbacks() {
5052
m_internals->m_schedulerClient.EmptyQueue();
5153
}
5254

55+
void CMainSignals::RegisterWithMempoolSignals(CTxMemPool& pool) {
56+
pool.NotifyEntryRemoved.connect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
57+
}
58+
59+
void CMainSignals::UnregisterWithMempoolSignals(CTxMemPool& pool) {
60+
pool.NotifyEntryRemoved.disconnect(boost::bind(&CMainSignals::MempoolEntryRemoved, this, _1, _2));
61+
}
62+
5363
CMainSignals& GetMainSignals()
5464
{
5565
return g_signals;
@@ -60,6 +70,7 @@ void RegisterValidationInterface(CValidationInterface* pwalletIn) {
6070
g_signals.m_internals->TransactionAddedToMempool.connect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
6171
g_signals.m_internals->BlockConnected.connect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
6272
g_signals.m_internals->BlockDisconnected.connect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
73+
g_signals.m_internals->TransactionRemovedFromMempool.connect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
6374
g_signals.m_internals->SetBestChain.connect(boost::bind(&CValidationInterface::SetBestChain, pwalletIn, _1));
6475
g_signals.m_internals->Inventory.connect(boost::bind(&CValidationInterface::Inventory, pwalletIn, _1));
6576
g_signals.m_internals->Broadcast.connect(boost::bind(&CValidationInterface::ResendWalletTransactions, pwalletIn, _1, _2));
@@ -75,6 +86,7 @@ void UnregisterValidationInterface(CValidationInterface* pwalletIn) {
7586
g_signals.m_internals->TransactionAddedToMempool.disconnect(boost::bind(&CValidationInterface::TransactionAddedToMempool, pwalletIn, _1));
7687
g_signals.m_internals->BlockConnected.disconnect(boost::bind(&CValidationInterface::BlockConnected, pwalletIn, _1, _2, _3));
7788
g_signals.m_internals->BlockDisconnected.disconnect(boost::bind(&CValidationInterface::BlockDisconnected, pwalletIn, _1));
89+
g_signals.m_internals->TransactionRemovedFromMempool.disconnect(boost::bind(&CValidationInterface::TransactionRemovedFromMempool, pwalletIn, _1));
7890
g_signals.m_internals->UpdatedBlockTip.disconnect(boost::bind(&CValidationInterface::UpdatedBlockTip, pwalletIn, _1, _2, _3));
7991
g_signals.m_internals->NewPoWValidBlock.disconnect(boost::bind(&CValidationInterface::NewPoWValidBlock, pwalletIn, _1, _2));
8092
}
@@ -87,10 +99,17 @@ void UnregisterAllValidationInterfaces() {
8799
g_signals.m_internals->TransactionAddedToMempool.disconnect_all_slots();
88100
g_signals.m_internals->BlockConnected.disconnect_all_slots();
89101
g_signals.m_internals->BlockDisconnected.disconnect_all_slots();
102+
g_signals.m_internals->TransactionRemovedFromMempool.disconnect_all_slots();
90103
g_signals.m_internals->UpdatedBlockTip.disconnect_all_slots();
91104
g_signals.m_internals->NewPoWValidBlock.disconnect_all_slots();
92105
}
93106

107+
void CMainSignals::MempoolEntryRemoved(CTransactionRef ptx, MemPoolRemovalReason reason) {
108+
if (reason != MemPoolRemovalReason::BLOCK && reason != MemPoolRemovalReason::CONFLICT) {
109+
m_internals->TransactionRemovedFromMempool(ptx);
110+
}
111+
}
112+
94113
void CMainSignals::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
95114
m_internals->UpdatedBlockTip(pindexNew, pindexFork, fInitialDownload);
96115
}

src/validationinterface.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class CValidationInterface;
2020
class CValidationState;
2121
class uint256;
2222
class CScheduler;
23+
class CTxMemPool;
24+
enum class MemPoolRemovalReason;
2325

2426
// These functions dispatch to one or all registered wallets
2527

@@ -36,6 +38,15 @@ class CValidationInterface {
3638
virtual void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {}
3739
/** Notifies listeners of a transaction having been added to mempool. */
3840
virtual void TransactionAddedToMempool(const CTransactionRef &ptxn) {}
41+
/**
42+
* Notifies listeners of a transaction leaving mempool.
43+
*
44+
* This only fires for transactions which leave mempool because of expiry,
45+
* size limiting, reorg (changes in lock times/coinbase maturity), or
46+
* replacement. This does not include any transactions which are included
47+
* in BlockConnectedDisconnected either in block->vtx or in txnConflicted.
48+
*/
49+
virtual void TransactionRemovedFromMempool(const CTransactionRef &ptx) {}
3950
/**
4051
* Notifies listeners of a block being connected.
4152
* Provides a vector of transactions evicted from the mempool as a result.
@@ -74,6 +85,8 @@ class CMainSignals {
7485
friend void ::UnregisterValidationInterface(CValidationInterface*);
7586
friend void ::UnregisterAllValidationInterfaces();
7687

88+
void MempoolEntryRemoved(CTransactionRef tx, MemPoolRemovalReason reason);
89+
7790
public:
7891
/** Register a CScheduler to give callbacks which should run in the background (may only be called once) */
7992
void RegisterBackgroundSignalScheduler(CScheduler& scheduler);
@@ -82,6 +95,11 @@ class CMainSignals {
8295
/** Call any remaining callbacks on the calling thread */
8396
void FlushBackgroundCallbacks();
8497

98+
/** Register with mempool to call TransactionRemovedFromMempool callbacks */
99+
void RegisterWithMempoolSignals(CTxMemPool& pool);
100+
/** Unregister with mempool */
101+
void UnregisterWithMempoolSignals(CTxMemPool& pool);
102+
85103
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
86104
void TransactionAddedToMempool(const CTransactionRef &);
87105
void BlockConnected(const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex, const std::vector<CTransactionRef> &);

0 commit comments

Comments
 (0)