Skip to content

Commit

Permalink
CValidationInterface: add new callback `MempoolTransactionsRemovedFor…
Browse files Browse the repository at this point in the history
…ConnectedBlock`

`MempoolTransactionsRemovedForConnectedBlock` notifies listeners
of the txs that are removed from the mempool because a new block is connected.

It also returns the block height the txs were confirmed.

Co-authored-by: Matt Corallo <git@bluematt.me>
  • Loading branch information
ismaelsadeeq and TheBlueMatt committed Oct 5, 2023
1 parent 5b44784 commit 4986edb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/txmempool.cpp
Expand Up @@ -649,17 +649,21 @@ void CTxMemPool::removeForBlock(const std::vector<CTransactionRef>& vtx, unsigne
}
// Before the txs in the new block have been removed from the mempool, update policy estimates
if (minerPolicyEstimator) {minerPolicyEstimator->processBlock(nBlockHeight, entries);}
std::vector<CTransactionRef> txs_removed_for_block;
txs_removed_for_block.reserve(vtx.size());
for (const auto& tx : vtx)
{
txiter it = mapTx.find(tx->GetHash());
if (it != mapTx.end()) {
setEntries stage;
stage.insert(it);
txs_removed_for_block.push_back(tx);
RemoveStaged(stage, true, MemPoolRemovalReason::BLOCK);
}
removeConflicts(*tx);
ClearPrioritisation(tx->GetHash());
}
GetMainSignals().MempoolTransactionsRemovedForConnectedBlock(txs_removed_for_block, nBlockHeight);
lastRollingFeeUpdate = GetTime();
blockSinceLastRollingFeeBump = true;
}
Expand Down
10 changes: 10 additions & 0 deletions src/validationinterface.cpp
Expand Up @@ -214,6 +214,16 @@ void CMainSignals::TransactionAddedToMempool(const CTransactionRef& tx, uint64_t
tx->GetWitnessHash().ToString());
}

void CMainSignals::MempoolTransactionsRemovedForConnectedBlock(const std::vector<CTransactionRef>& txs_removed_for_block, unsigned int nBlockHeight)
{
auto event = [txs_removed_for_block, nBlockHeight, this] {
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.MempoolTransactionsRemovedForConnectedBlock(txs_removed_for_block, nBlockHeight); });
};
ENQUEUE_AND_LOG_EVENT(event, "%s: block height=%s txs=%s", __func__,
nBlockHeight,
txs_removed_for_block.size());
}

void CMainSignals::TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {
auto event = [tx, reason, mempool_sequence, this] {
m_internals->Iterate([&](CValidationInterface& callbacks) { callbacks.TransactionRemovedFromMempool(tx, reason, mempool_sequence); });
Expand Down
18 changes: 14 additions & 4 deletions src/validationinterface.h
Expand Up @@ -60,10 +60,10 @@ void CallFunctionInValidationInterfaceQueue(std::function<void ()> func);
void SyncWithValidationInterfaceQueue() LOCKS_EXCLUDED(cs_main);

/**
* Implement this to subscribe to events generated in validation
* Implement this to subscribe to events generated in validation and mempool
*
* Each CValidationInterface() subscriber will receive event callbacks
* in the order in which the events were generated by validation.
* in the order in which the events were generated by validation and mempool.
* Furthermore, each ValidationInterface() subscriber may assume that
* callbacks effectively run in a single thread with single-threaded
* memory consistency. That is, for a given ValidationInterface()
Expand Down Expand Up @@ -113,7 +113,7 @@ class CValidationInterface {
* This does not fire for transactions that are removed from the mempool
* because they have been included in a block. Any client that is interested
* in transactions removed from the mempool for inclusion in a block can learn
* about those transactions from the BlockConnected notification.
* about those transactions from the MempoolTransactionsRemovedForConnectedBlock notification.
*
* Transactions that are removed from the mempool because they conflict
* with a transaction in the new block will have
Expand All @@ -131,9 +131,18 @@ class CValidationInterface {
* Called on a background thread.
*/
virtual void TransactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) {}
/* Notifies listeners of transactions removed from the mempool as
* as a result of new block being connected.
* MempoolTransactionsRemovedForConnectedBlock will be fired before BlockConnected.
*
* Called on a background thread.
*/
virtual void MempoolTransactionsRemovedForConnectedBlock(const std::vector<CTransactionRef>& txs_removed_for_block, unsigned int nBlockHeight) {}
/**
* Notifies listeners of a block being connected.
* Provides a vector of transactions evicted from the mempool as a result.
* Provides the block that was connected.
* The block contains a vector of transactions from the new block,
* some of the transactions in the vector may not be in our mempool.
*
* Called on a background thread.
*/
Expand Down Expand Up @@ -202,6 +211,7 @@ class CMainSignals {
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload);
void TransactionAddedToMempool(const CTransactionRef&, uint64_t mempool_sequence);
void TransactionRemovedFromMempool(const CTransactionRef&, MemPoolRemovalReason, uint64_t mempool_sequence);
void MempoolTransactionsRemovedForConnectedBlock(const std::vector<CTransactionRef>&, unsigned int nBlockHeight);
void BlockConnected(ChainstateRole, const std::shared_ptr<const CBlock> &, const CBlockIndex *pindex);
void BlockDisconnected(const std::shared_ptr<const CBlock> &, const CBlockIndex* pindex);
void ChainStateFlushed(ChainstateRole, const CBlockLocator &);
Expand Down

0 comments on commit 4986edb

Please sign in to comment.