Skip to content

Commit 8119026

Browse files
committed
Provide a flat list of txid/terators to txn in CTxMemPool
1 parent 678ee97 commit 8119026

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/txmempool.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ bool CTxMemPool::addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry,
438438
totalTxSize += entry.GetTxSize();
439439
minerPolicyEstimator->processTransaction(entry, fCurrentEstimate);
440440

441+
vTxHashes.emplace_back(hash, newit);
442+
newit->vTxHashesIdx = vTxHashes.size() - 1;
443+
441444
return true;
442445
}
443446

@@ -447,6 +450,15 @@ void CTxMemPool::removeUnchecked(txiter it)
447450
BOOST_FOREACH(const CTxIn& txin, it->GetTx().vin)
448451
mapNextTx.erase(txin.prevout);
449452

453+
if (vTxHashes.size() > 1) {
454+
vTxHashes[it->vTxHashesIdx] = std::move(vTxHashes.back());
455+
vTxHashes[it->vTxHashesIdx].second->vTxHashesIdx = it->vTxHashesIdx;
456+
vTxHashes.pop_back();
457+
if (vTxHashes.size() * 2 < vTxHashes.capacity())
458+
vTxHashes.shrink_to_fit();
459+
} else
460+
vTxHashes.clear();
461+
450462
totalTxSize -= it->GetTxSize();
451463
cachedInnerUsage -= it->DynamicMemoryUsage();
452464
cachedInnerUsage -= memusage::DynamicUsage(mapLinks[it].parents) + memusage::DynamicUsage(mapLinks[it].children);
@@ -965,7 +977,7 @@ bool CCoinsViewMemPool::HaveCoins(const uint256 &txid) const {
965977
size_t CTxMemPool::DynamicMemoryUsage() const {
966978
LOCK(cs);
967979
// Estimate the overhead of mapTx to be 15 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
968-
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + cachedInnerUsage;
980+
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
969981
}
970982

971983
void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants) {

src/txmempool.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class CTxMemPoolEntry
150150
uint64_t GetSizeWithAncestors() const { return nSizeWithAncestors; }
151151
CAmount GetModFeesWithAncestors() const { return nModFeesWithAncestors; }
152152
unsigned int GetSigOpCountWithAncestors() const { return nSigOpCountWithAncestors; }
153+
154+
mutable size_t vTxHashesIdx; //!< Index in mempool's vTxHashes
153155
};
154156

155157
// Helpers for modifying CTxMemPool::mapTx, which is a boost multi_index.
@@ -457,7 +459,10 @@ class CTxMemPool
457459

458460
mutable CCriticalSection cs;
459461
indexed_transaction_set mapTx;
462+
460463
typedef indexed_transaction_set::nth_index<0>::type::iterator txiter;
464+
std::vector<std::pair<uint256, txiter> > vTxHashes; //!< All tx hashes/entries in mapTx, in random order
465+
461466
struct CompareIteratorByHash {
462467
bool operator()(const txiter &a, const txiter &b) const {
463468
return a->GetTx().GetHash() < b->GetTx().GetHash();

0 commit comments

Comments
 (0)