Skip to content

Commit 4df3d13

Browse files
sdaftuarjnewbery
authored andcommitted
Add a wtxid-index to the mempool
1 parent 80aa83a commit 4df3d13

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

src/txmempool.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -724,12 +724,12 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
724724
assert(innerUsage == cachedInnerUsage);
725725
}
726726

727-
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb)
727+
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid)
728728
{
729729
LOCK(cs);
730-
indexed_transaction_set::const_iterator i = mapTx.find(hasha);
730+
indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha);
731731
if (i == mapTx.end()) return false;
732-
indexed_transaction_set::const_iterator j = mapTx.find(hashb);
732+
indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb);
733733
if (j == mapTx.end()) return true;
734734
uint64_t counta = i->GetCountWithAncestors();
735735
uint64_t countb = j->GetCountWithAncestors();
@@ -809,10 +809,10 @@ CTransactionRef CTxMemPool::get(const uint256& hash) const
809809
return i->GetSharedTx();
810810
}
811811

812-
TxMempoolInfo CTxMemPool::info(const uint256& hash) const
812+
TxMempoolInfo CTxMemPool::info(const uint256& hash, bool wtxid) const
813813
{
814814
LOCK(cs);
815-
indexed_transaction_set::const_iterator i = mapTx.find(hash);
815+
indexed_transaction_set::const_iterator i = (wtxid ? get_iter_from_wtxid(hash) : mapTx.find(hash));
816816
if (i == mapTx.end())
817817
return TxMempoolInfo();
818818
return GetInfo(i);
@@ -915,8 +915,8 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
915915

916916
size_t CTxMemPool::DynamicMemoryUsage() const {
917917
LOCK(cs);
918-
// Estimate the overhead of mapTx to be 12 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
919-
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 12 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
918+
// Estimate the overhead of mapTx to be 15 pointers + an allocation, as no exact formula for boost::multi_index_contained is implemented.
919+
return memusage::MallocUsage(sizeof(CTxMemPoolEntry) + 15 * sizeof(void*)) * mapTx.size() + memusage::DynamicUsage(mapNextTx) + memusage::DynamicUsage(mapDeltas) + memusage::DynamicUsage(mapLinks) + memusage::DynamicUsage(vTxHashes) + cachedInnerUsage;
920920
}
921921

922922
void CTxMemPool::RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) {

src/txmempool.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ struct mempoolentry_txid
198198
}
199199
};
200200

201+
// extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
202+
struct mempoolentry_wtxid
203+
{
204+
typedef uint256 result_type;
205+
result_type operator() (const CTxMemPoolEntry &entry) const
206+
{
207+
return entry.GetTx().GetWitnessHash();
208+
}
209+
210+
result_type operator() (const CTransactionRef& tx) const
211+
{
212+
return tx->GetWitnessHash();
213+
}
214+
};
215+
216+
201217
/** \class CompareTxMemPoolEntryByDescendantScore
202218
*
203219
* Sort an entry by max(score/size of entry's tx, score/size with all descendants).
@@ -318,6 +334,7 @@ class CompareTxMemPoolEntryByAncestorFee
318334
struct descendant_score {};
319335
struct entry_time {};
320336
struct ancestor_score {};
337+
struct index_by_wtxid {};
321338

322339
class CBlockPolicyEstimator;
323340

@@ -383,8 +400,9 @@ class SaltedTxidHasher
383400
*
384401
* CTxMemPool::mapTx, and CTxMemPoolEntry bookkeeping:
385402
*
386-
* mapTx is a boost::multi_index that sorts the mempool on 4 criteria:
387-
* - transaction hash
403+
* mapTx is a boost::multi_index that sorts the mempool on 5 criteria:
404+
* - transaction hash (txid)
405+
* - witness-transaction hash (wtxid)
388406
* - descendant feerate [we use max(feerate of tx, feerate of tx with all descendants)]
389407
* - time in mempool
390408
* - ancestor feerate [we use min(feerate of tx, feerate of tx with all unconfirmed ancestors)]
@@ -469,6 +487,12 @@ class CTxMemPool
469487
boost::multi_index::indexed_by<
470488
// sorted by txid
471489
boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
490+
// sorted by wtxid
491+
boost::multi_index::hashed_unique<
492+
boost::multi_index::tag<index_by_wtxid>,
493+
mempoolentry_wtxid,
494+
SaltedTxidHasher
495+
>,
472496
// sorted by fee rate
473497
boost::multi_index::ordered_non_unique<
474498
boost::multi_index::tag<descendant_score>,
@@ -583,7 +607,7 @@ class CTxMemPool
583607

584608
void clear();
585609
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
586-
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
610+
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
587611
void queryHashes(std::vector<uint256>& vtxid) const;
588612
bool isSpent(const COutPoint& outpoint) const;
589613
unsigned int GetTransactionsUpdated() const;
@@ -686,14 +710,22 @@ class CTxMemPool
686710
return totalTxSize;
687711
}
688712

689-
bool exists(const uint256& hash) const
713+
bool exists(const uint256& hash, bool wtxid=false) const
690714
{
691715
LOCK(cs);
716+
if (wtxid) {
717+
return (mapTx.get<index_by_wtxid>().count(hash) != 0);
718+
}
692719
return (mapTx.count(hash) != 0);
693720
}
694721

695722
CTransactionRef get(const uint256& hash) const;
696-
TxMempoolInfo info(const uint256& hash) const;
723+
txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
724+
{
725+
AssertLockHeld(cs);
726+
return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
727+
}
728+
TxMempoolInfo info(const uint256& hash, bool wtxid=false) const;
697729
std::vector<TxMempoolInfo> infoAll() const;
698730

699731
size_t DynamicMemoryUsage() const;

0 commit comments

Comments
 (0)