Skip to content

Commit 89eeb4a

Browse files
committed
[mempool] Track "unbroadcast" transactions
- Mempool tracks locally submitted transactions (wallet or rpc) - Transactions are removed from set when the node receives a GETDATA request from a peer, or if the transaction is removed from the mempool.
1 parent 23991ee commit 89eeb4a

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/net_processing.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1556,7 +1556,7 @@ void static ProcessGetBlockData(CNode* pfrom, const CChainParams& chainparams, c
15561556
}
15571557
}
15581558

1559-
void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnman* connman, const CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) LOCKS_EXCLUDED(cs_main)
1559+
void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnman* connman, CTxMemPool& mempool, const std::atomic<bool>& interruptMsgProc) LOCKS_EXCLUDED(cs_main)
15601560
{
15611561
AssertLockNotHeld(cs_main);
15621562

@@ -1605,7 +1605,13 @@ void static ProcessGetData(CNode* pfrom, const CChainParams& chainparams, CConnm
16051605
push = true;
16061606
}
16071607
}
1608-
if (!push) {
1608+
1609+
if (push) {
1610+
// We interpret fulfilling a GETDATA for a transaction as a
1611+
// successful initial broadcast and remove it from our
1612+
// unbroadcast set.
1613+
mempool.RemoveUnbroadcastTx(inv.hash);
1614+
} else {
16091615
vNotFound.push_back(inv);
16101616
}
16111617
}

src/node/transaction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
7878
}
7979

8080
if (relay) {
81+
// the mempool tracks locally submitted transactions to make a
82+
// best-effort of initial broadcast
83+
node.mempool->AddUnbroadcastTx(hashTx);
84+
8185
RelayTransaction(hashTx, *node.connman);
8286
}
8387

src/txmempool.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,8 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
417417
for (const CTxIn& txin : it->GetTx().vin)
418418
mapNextTx.erase(txin.prevout);
419419

420+
RemoveUnbroadcastTx(hash, true /* add logging because unchecked */ );
421+
420422
if (vTxHashes.size() > 1) {
421423
vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
422424
vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
@@ -919,6 +921,15 @@ size_t CTxMemPool::DynamicMemoryUsage() const {
919921
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 12 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
920922
}
921923

924+
void CTxMemPool::RemoveUnbroadcastTx(const uint256& txid, const bool unchecked) {
925+
LOCK(cs);
926+
927+
if (m_unbroadcast_txids.erase(txid))
928+
{
929+
LogPrint(BCLog::MEMPOOL, "Removed %i from set of unbroadcast txns%s\n", txid.GetHex(), (unchecked ? " before confirmation that txn was sent out" : ""));
930+
}
931+
}
932+
922933
void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) {
923934
AssertLockHeld(cs);
924935
UpdateForRemoveFromMempool(stage, updateDescendants);

src/txmempool.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,9 @@ class CTxMemPool
549549

550550
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
551551

552+
/** track locally submitted transactions to periodically retry initial broadcast */
553+
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
554+
552555
public:
553556
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
554557
std::map<uint256, CAmount> mapDeltas;
@@ -698,6 +701,21 @@ class CTxMemPool
698701

699702
size_t DynamicMemoryUsage() const;
700703

704+
/** Adds a transaction to the unbroadcast set */
705+
void AddUnbroadcastTx(const uint256& txid) {
706+
LOCK(cs);
707+
m_unbroadcast_txids.insert(txid);
708+
}
709+
710+
/** Removes a transaction from the unbroadcast set */
711+
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
712+
713+
/** Returns transactions in unbroadcast set */
714+
const std::set<uint256> GetUnbroadcastTxs() const {
715+
LOCK(cs);
716+
return m_unbroadcast_txids;
717+
}
718+
701719
private:
702720
/** UpdateForDescendants is used by UpdateTransactionsFromBlock to update
703721
* the descendants for a single transaction that has been added to the

0 commit comments

Comments
 (0)