Reimplement GetTransactionAncestry() to not rely on cached data

In preparation for removing ancestor data from CTxMemPoolEntry, recalculate the
ancestor statistics on demand wherever needed.
This commit is contained in:
Suhas Daftuar committed 2025-11-18 08:57:51 -05:00
1 parent feceaa42e8
commit 7961496dda
2 files changed
+22 -3

No files matched your search

+19 -3
View File
@@ -1217,15 +1217,31 @@ uint64_t CTxMemPool::CalculateDescendantMaximum(txiter entry) const {
return maximum;
}
std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateAncestorData(const CTxMemPoolEntry& entry) const
{
auto ancestors = m_txgraph->GetAncestors(entry, TxGraph::Level::MAIN);
size_t ancestor_count = ancestors.size();
size_t ancestor_size = 0;
CAmount ancestor_fees = 0;
for (auto tx: ancestors) {
const CTxMemPoolEntry& anc = static_cast<const CTxMemPoolEntry&>(*tx);
ancestor_size += anc.GetTxSize();
ancestor_fees += anc.GetModifiedFee();
}
return {ancestor_count, ancestor_size, ancestor_fees};
}
void CTxMemPool::GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& descendants, size_t* const ancestorsize, CAmount* const ancestorfees) const {
LOCK(cs);
auto it = mapTx.find(txid);
ancestors = descendants = 0;
if (it != mapTx.end()) {
ancestors = it->GetCountWithAncestors();
if (ancestorsize) *ancestorsize = it->GetSizeWithAncestors();
if (ancestorfees) *ancestorfees = it->GetModFeesWithAncestors();
auto [ancestor_count, ancestor_size, ancestor_fees] = CalculateAncestorData(*it);
descendants = CalculateDescendantMaximum(it);
ancestors = ancestor_count;
if (ancestorsize) *ancestorsize = ancestor_size;
if (ancestorfees) *ancestorfees = ancestor_fees;
}
}
+3
View File
@@ -285,6 +285,9 @@ public:
using Limits = kernel::MemPoolLimits;
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
std::tuple<size_t, size_t, CAmount> CalculateAncestorData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
private:
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;