[mempool] sanity check that all unbroadcast txns are in mempool

- before reattempting broadcast for unbroadcast txns, check they are in mempool and remove if not
- this protects from memory leaks and network spam just in case unbroadcast set (incorrectly) has extra txns
- check that tx is in mempool before adding to unbroadcast set to try to prevent this from happening
This commit is contained in:
gzhao408 2020-05-01 15:48:23 -07:00
parent a7ebe48b94
commit 9d3f7eb986
2 changed files with 10 additions and 2 deletions

View file

@ -819,7 +819,12 @@ void PeerLogicValidation::ReattemptInitialBroadcast(CScheduler& scheduler) const
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs(); std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
for (const uint256& txid : unbroadcast_txids) { for (const uint256& txid : unbroadcast_txids) {
// Sanity check: all unbroadcast txns should exist in the mempool
if (m_mempool.exists(txid)) {
RelayTransaction(txid, *connman); RelayTransaction(txid, *connman);
} else {
m_mempool.RemoveUnbroadcastTx(txid, true);
}
} }
// schedule next run for 10-15 minutes in the future // schedule next run for 10-15 minutes in the future

View file

@ -704,8 +704,11 @@ public:
/** Adds a transaction to the unbroadcast set */ /** Adds a transaction to the unbroadcast set */
void AddUnbroadcastTx(const uint256& txid) { void AddUnbroadcastTx(const uint256& txid) {
LOCK(cs); LOCK(cs);
/** Sanity Check: the transaction should also be in the mempool */
if (exists(txid)) {
m_unbroadcast_txids.insert(txid); m_unbroadcast_txids.insert(txid);
} }
}
/** Removes a transaction from the unbroadcast set */ /** Removes a transaction from the unbroadcast set */
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false); void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);