Skip to content

Commit 2b4b90a

Browse files
committed
Add a wtxid-index to the mempool
1 parent 090d877 commit 2b4b90a

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
@@ -726,12 +726,12 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins) const
726726
assert(innerUsage == cachedInnerUsage);
727727
}
728728

729-
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb)
729+
bool CTxMemPool::CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid)
730730
{
731731
LOCK(cs);
732-
indexed_transaction_set::const_iterator i = mapTx.find(hasha);
732+
indexed_transaction_set::const_iterator i = wtxid ? get_iter_from_wtxid(hasha) : mapTx.find(hasha);
733733
if (i == mapTx.end()) return false;
734-
indexed_transaction_set::const_iterator j = mapTx.find(hashb);
734+
indexed_transaction_set::const_iterator j = wtxid ? get_iter_from_wtxid(hashb) : mapTx.find(hashb);
735735
if (j == mapTx.end()) return true;
736736
uint64_t counta = i->GetCountWithAncestors();
737737
uint64_t countb = j->GetCountWithAncestors();
@@ -811,10 +811,10 @@ CTransactionRef CTxMemPool::get(const uint256& hash) const
811811
return i->GetSharedTx();
812812
}
813813

814-
TxMempoolInfo CTxMemPool::info(const uint256& hash) const
814+
TxMempoolInfo CTxMemPool::info(const uint256& hash, bool wtxid) const
815815
{
816816
LOCK(cs);
817-
indexed_transaction_set::const_iterator i = mapTx.find(hash);
817+
indexed_transaction_set::const_iterator i = (wtxid ? get_iter_from_wtxid(hash) : mapTx.find(hash));
818818
if (i == mapTx.end())
819819
return TxMempoolInfo();
820820
return GetInfo(i);
@@ -917,8 +917,8 @@ bool CCoinsViewMemPool::GetCoin(const COutPoint &outpoint, Coin &coin) const {
917917

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

924924
void CTxMemPool::RemoveUnbroadcastTx(const uint256& txid, const bool unchecked) {

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>,
@@ -586,7 +610,7 @@ class CTxMemPool
586610

587611
void clear();
588612
void _clear() EXCLUSIVE_LOCKS_REQUIRED(cs); //lock free
589-
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb);
613+
bool CompareDepthAndScore(const uint256& hasha, const uint256& hashb, bool wtxid=false);
590614
void queryHashes(std::vector<uint256>& vtxid) const;
591615
bool isSpent(const COutPoint& outpoint) const;
592616
unsigned int GetTransactionsUpdated() const;
@@ -689,14 +713,22 @@ class CTxMemPool
689713
return totalTxSize;
690714
}
691715

692-
bool exists(const uint256& hash) const
716+
bool exists(const uint256& hash, bool wtxid=false) const
693717
{
694718
LOCK(cs);
719+
if (wtxid) {
720+
return (mapTx.get<index_by_wtxid>().count(hash) != 0);
721+
}
695722
return (mapTx.count(hash) != 0);
696723
}
697724

698725
CTransactionRef get(const uint256& hash) const;
699-
TxMempoolInfo info(const uint256& hash) const;
726+
txiter get_iter_from_wtxid(const uint256& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
727+
{
728+
AssertLockHeld(cs);
729+
return mapTx.project<0>(mapTx.get<index_by_wtxid>().find(wtxid));
730+
}
731+
TxMempoolInfo info(const uint256& hash, bool wtxid=false) const;
700732
std::vector<TxMempoolInfo> infoAll() const;
701733

702734
size_t DynamicMemoryUsage() const;

0 commit comments

Comments
 (0)