diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp index f0984fa09e1..cf6da55aa9a 100644 --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -114,12 +114,12 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c std::vector have_txn(txn_available.size()); { LOCK(pool->cs); - for (const auto& tx : pool->txns_randomized) { - uint64_t shortid = cmpctblock.GetShortID(tx->GetWitnessHash()); + for (const auto& [wtxid, txit] : pool->txns_randomized) { + uint64_t shortid = cmpctblock.GetShortID(wtxid); std::unordered_map::iterator idit = shorttxids.find(shortid); if (idit != shorttxids.end()) { if (!have_txn[idit->second]) { - txn_available[idit->second] = tx; + txn_available[idit->second] = txit->GetSharedTx(); have_txn[idit->second] = true; mempool_count++; } else { diff --git a/src/test/blockencodings_tests.cpp b/src/test/blockencodings_tests.cpp index 82293fd5207..641617b724e 100644 --- a/src/test/blockencodings_tests.cpp +++ b/src/test/blockencodings_tests.cpp @@ -56,8 +56,8 @@ static CBlock BuildBlockTestCase(FastRandomContext& ctx) { } // Number of shared use_counts we expect for a tx we haven't touched -// (block + mempool entry + mempool txns_randomized + our copy from the GetSharedTx call) -constexpr long SHARED_TX_OFFSET{4}; +// (block + mempool entry + our copy from the GetSharedTx call) +constexpr long SHARED_TX_OFFSET{3}; BOOST_AUTO_TEST_CASE(SimpleRoundTripTest) { diff --git a/src/txmempool.cpp b/src/txmempool.cpp index 6bb910e30e6..cae16a675bd 100644 --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -507,7 +507,7 @@ void CTxMemPool::addNewTransaction(CTxMemPool::txiter newit, CTxMemPool::setEntr totalTxSize += entry.GetTxSize(); m_total_fee += entry.GetFee(); - txns_randomized.emplace_back(newit->GetSharedTx()); + txns_randomized.emplace_back(tx.GetWitnessHash(), newit); newit->idx_randomized = txns_randomized.size() - 1; TRACEPOINT(mempool, added, @@ -544,15 +544,16 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason) RemoveUnbroadcastTx(it->GetTx().GetHash(), true /* add logging because unchecked */); if (txns_randomized.size() > 1) { - // Update idx_randomized of the to-be-moved entry. - Assert(GetEntry(txns_randomized.back()->GetHash()))->idx_randomized = it->idx_randomized; // Remove entry from txns_randomized by replacing it with the back and deleting the back. txns_randomized[it->idx_randomized] = std::move(txns_randomized.back()); + txns_randomized[it->idx_randomized].second->idx_randomized = it->idx_randomized; txns_randomized.pop_back(); - if (txns_randomized.size() * 2 < txns_randomized.capacity()) + if (txns_randomized.size() * 2 < txns_randomized.capacity()) { txns_randomized.shrink_to_fit(); - } else + } + } else { txns_randomized.clear(); + } totalTxSize -= it->GetTxSize(); m_total_fee -= it->GetFee(); diff --git a/src/txmempool.h b/src/txmempool.h index f471726cac1..a36754810f7 100644 --- a/src/txmempool.h +++ b/src/txmempool.h @@ -368,7 +368,7 @@ public: indexed_transaction_set mapTx GUARDED_BY(cs); using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator; - std::vector txns_randomized GUARDED_BY(cs); //!< All transactions in mapTx, in random order + std::vector> txns_randomized GUARDED_BY(cs); //!< All transactions in mapTx with their wtxids, in arbitrary order typedef std::set setEntries;