Skip to content

Commit cb79b9d

Browse files
committed
[mempool] Revert unbroadcast set to tracking just txid
When I originally implemented the unbroadcast set in 18038, it just tracked txids. After 18038 was merged, I offered a patch to 18044 to make the unbroadcast changes compatible with wtxid relay. In this patch, I updated `unbroadcast_txids` to a map of txid -> wtxid. Post merge review comments shed light on the fact that this update was unnecessary, and distracting. So, this commit updates the unbroadcast ids back to a set.
1 parent 23d3ae7 commit cb79b9d

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/net_processing.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,15 +889,16 @@ void PeerLogicValidation::InitializeNode(CNode *pnode) {
889889

890890
void PeerLogicValidation::ReattemptInitialBroadcast(CScheduler& scheduler) const
891891
{
892-
std::map<uint256, uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
892+
std::set<uint256> unbroadcast_txids = m_mempool.GetUnbroadcastTxs();
893893

894-
for (const auto& elem : unbroadcast_txids) {
895-
// Sanity check: all unbroadcast txns should exist in the mempool
896-
if (m_mempool.exists(elem.first)) {
894+
for (const auto& txid : unbroadcast_txids) {
895+
CTransactionRef tx = m_mempool.get(txid);
896+
897+
if (tx != nullptr) {
897898
LOCK(cs_main);
898-
RelayTransaction(elem.first, elem.second, m_connman);
899+
RelayTransaction(txid, tx->GetWitnessHash(), m_connman);
899900
} else {
900-
m_mempool.RemoveUnbroadcastTx(elem.first, true);
901+
m_mempool.RemoveUnbroadcastTx(txid, true);
901902
}
902903
}
903904

src/node/transaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
8080
if (relay) {
8181
// the mempool tracks locally submitted transactions to make a
8282
// best-effort of initial broadcast
83-
node.mempool->AddUnbroadcastTx(hashTx, tx->GetWitnessHash());
83+
node.mempool->AddUnbroadcastTx(hashTx);
8484

8585
LOCK(cs_main);
8686
RelayTransaction(hashTx, tx->GetWitnessHash(), *node.connman);

src/txmempool.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,9 @@ class CTxMemPool
574574
std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
575575

576576
/**
577-
* track locally submitted transactions to periodically retry initial broadcast
578-
* map of txid -> wtxid
577+
* Track locally submitted transactions to periodically retry initial broadcast.
579578
*/
580-
std::map<uint256, uint256> m_unbroadcast_txids GUARDED_BY(cs);
579+
std::set<uint256> m_unbroadcast_txids GUARDED_BY(cs);
581580

582581
public:
583582
indirectmap<COutPoint, const CTransaction*> mapNextTx GUARDED_BY(cs);
@@ -739,19 +738,20 @@ class CTxMemPool
739738
size_t DynamicMemoryUsage() const;
740739

741740
/** Adds a transaction to the unbroadcast set */
742-
void AddUnbroadcastTx(const uint256& txid, const uint256& wtxid) {
741+
void AddUnbroadcastTx(const uint256& txid)
742+
{
743743
LOCK(cs);
744-
// Sanity Check: the transaction should also be in the mempool
745-
if (exists(txid)) {
746-
m_unbroadcast_txids[txid] = wtxid;
747-
}
748-
}
744+
// Sanity check the transaction is in the mempool & insert into
745+
// unbroadcast set.
746+
if (exists(txid)) m_unbroadcast_txids.insert(txid);
747+
};
749748

750749
/** Removes a transaction from the unbroadcast set */
751750
void RemoveUnbroadcastTx(const uint256& txid, const bool unchecked = false);
752751

753752
/** Returns transactions in unbroadcast set */
754-
std::map<uint256, uint256> GetUnbroadcastTxs() const {
753+
std::set<uint256> GetUnbroadcastTxs() const
754+
{
755755
LOCK(cs);
756756
return m_unbroadcast_txids;
757757
}

src/validation.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5116,21 +5116,18 @@ bool LoadMempool(CTxMemPool& pool)
51165116
}
51175117

51185118
// TODO: remove this try except in v0.22
5119-
std::map<uint256, uint256> unbroadcast_txids;
5119+
std::set<uint256> unbroadcast_txids;
51205120
try {
51215121
file >> unbroadcast_txids;
51225122
unbroadcast = unbroadcast_txids.size();
51235123
} catch (const std::exception&) {
51245124
// mempool.dat files created prior to v0.21 will not have an
51255125
// unbroadcast set. No need to log a failure if parsing fails here.
51265126
}
5127-
for (const auto& elem : unbroadcast_txids) {
5128-
// Don't add unbroadcast transactions that didn't get back into the
5129-
// mempool.
5130-
const CTransactionRef& added_tx = pool.get(elem.first);
5131-
if (added_tx != nullptr) {
5132-
pool.AddUnbroadcastTx(elem.first, added_tx->GetWitnessHash());
5133-
}
5127+
for (const auto& txid : unbroadcast_txids) {
5128+
// Ensure transactions were accepted to mempool then add to
5129+
// unbroadcast set.
5130+
if (pool.get(txid) != nullptr) pool.AddUnbroadcastTx(txid);
51345131
}
51355132
} catch (const std::exception& e) {
51365133
LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
@@ -5147,7 +5144,7 @@ bool DumpMempool(const CTxMemPool& pool)
51475144

51485145
std::map<uint256, CAmount> mapDeltas;
51495146
std::vector<TxMempoolInfo> vinfo;
5150-
std::map<uint256, uint256> unbroadcast_txids;
5147+
std::set<uint256> unbroadcast_txids;
51515148

51525149
static Mutex dump_mutex;
51535150
LOCK(dump_mutex);

0 commit comments

Comments
 (0)