Skip to content

Commit a03aef9

Browse files
glozowTheCharlatan
authored andcommitted
[refactor] rewrite vTxHashes as a vector of CTransactionRef
vTxHashes exposes a complex mapTx iterator type that its external users don't need. Directly populate it with CTransactionRef instead.
1 parent 938643c commit a03aef9

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

src/blockencodings.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& c
107107
std::vector<bool> have_txn(txn_available.size());
108108
{
109109
LOCK(pool->cs);
110-
for (size_t i = 0; i < pool->vTxHashes.size(); i++) {
111-
uint64_t shortid = cmpctblock.GetShortID(pool->vTxHashes[i].first);
110+
for (const auto& tx : pool->vTxHashes) {
111+
uint64_t shortid = cmpctblock.GetShortID(tx->GetWitnessHash());
112112
std::unordered_map<uint64_t, uint16_t>::iterator idit = shorttxids.find(shortid);
113113
if (idit != shorttxids.end()) {
114114
if (!have_txn[idit->second]) {
115-
txn_available[idit->second] = pool->vTxHashes[i].second->GetSharedTx();
115+
txn_available[idit->second] = tx;
116116
have_txn[idit->second] = true;
117117
mempool_count++;
118118
} else {

src/test/blockencodings_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ static CBlock BuildBlockTestCase() {
5151
}
5252

5353
// Number of shared use_counts we expect for a tx we haven't touched
54-
// (block + mempool + our copy from the GetSharedTx call)
55-
constexpr long SHARED_TX_OFFSET{3};
54+
// (block + mempool entry + mempool vTxHashes + our copy from the GetSharedTx call)
55+
constexpr long SHARED_TX_OFFSET{4};
5656

5757
BOOST_AUTO_TEST_CASE(SimpleRoundTripTest)
5858
{

src/txmempool.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ void CTxMemPool::addUnchecked(const CTxMemPoolEntry &entry, setEntries &setAnces
480480
minerPolicyEstimator->processTransaction(entry, validFeeEstimate);
481481
}
482482

483-
vTxHashes.emplace_back(tx.GetWitnessHash(), newit);
483+
vTxHashes.emplace_back(newit->GetSharedTx());
484484
newit->vTxHashesIdx = vTxHashes.size() - 1;
485485

486486
TRACE3(mempool, added,
@@ -518,8 +518,10 @@ void CTxMemPool::removeUnchecked(txiter it, MemPoolRemovalReason reason)
518518
RemoveUnbroadcastTx(hash, true /* add logging because unchecked */ );
519519

520520
if (vTxHashes.size() > 1) {
521+
// Update vTxHashesIdx of the to-be-moved entry.
522+
Assert(GetEntry(vTxHashes.back()->GetHash()))->vTxHashesIdx = it->vTxHashesIdx;
523+
// Remove entry from vTxHashes by replacing it with the back and deleting the back.
521524
vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
522-
vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
523525
vTxHashes.pop_back();
524526
if (vTxHashes.size() * 2 < vTxHashes.capacity())
525527
vTxHashes.shrink_to_fit();

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class CTxMemPool
392392
indexed_transaction_set mapTx GUARDED_BY(cs);
393393

394394
using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
395-
std::vector<std::pair<uint256, txiter>> vTxHashes GUARDED_BY(cs); //!< All tx witness hashes/entries in mapTx, in random order
395+
std::vector<CTransactionRef> vTxHashes GUARDED_BY(cs); //!< All transactions in mapTx, in random order
396396

397397
typedef std::set<txiter, CompareIteratorByHash> setEntries;
398398

0 commit comments

Comments
 (0)