Calculate descendant information for mempool RPC output on-the-fly

This is in preparation for removing the cached descendant state from the
mempool.
This commit is contained in:
Suhas Daftuar
2023-10-03 11:57:16 -04:00
parent bdcefb8a8b
commit c0bd04d18f
3 changed files with 20 additions and 3 deletions

View File

@@ -291,13 +291,14 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
AssertLockHeld(pool.cs);
auto [ancestor_count, ancestor_size, ancestor_fees] = pool.CalculateAncestorData(e);
auto [descendant_count, descendant_size, descendant_fees] = pool.CalculateDescendantData(e);
info.pushKV("vsize", (int)e.GetTxSize());
info.pushKV("weight", (int)e.GetTxWeight());
info.pushKV("time", count_seconds(e.GetTime()));
info.pushKV("height", (int)e.GetHeight());
info.pushKV("descendantcount", e.GetCountWithDescendants());
info.pushKV("descendantsize", e.GetSizeWithDescendants());
info.pushKV("descendantcount", descendant_count);
info.pushKV("descendantsize", descendant_size);
info.pushKV("ancestorcount", ancestor_count);
info.pushKV("ancestorsize", ancestor_size);
info.pushKV("wtxid", e.GetTx().GetWitnessHash().ToString());
@@ -306,7 +307,7 @@ static void entryToJSON(const CTxMemPool& pool, UniValue& info, const CTxMemPool
fees.pushKV("base", ValueFromAmount(e.GetFee()));
fees.pushKV("modified", ValueFromAmount(e.GetModifiedFee()));
fees.pushKV("ancestor", ValueFromAmount(ancestor_fees));
fees.pushKV("descendant", ValueFromAmount(e.GetModFeesWithDescendants()));
fees.pushKV("descendant", ValueFromAmount(descendant_fees));
info.pushKV("fees", std::move(fees));
const CTransaction& tx = e.GetTx();

View File

@@ -1217,6 +1217,21 @@ std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateAncestorData(const CTxM
return {ancestor_count, ancestor_size, ancestor_fees};
}
std::tuple<size_t, size_t, CAmount> CTxMemPool::CalculateDescendantData(const CTxMemPoolEntry& entry) const
{
auto descendants = m_txgraph->GetDescendants(entry, TxGraph::Level::MAIN);
size_t descendant_count = descendants.size();
size_t descendant_size = 0;
CAmount descendant_fees = 0;
for (auto tx: descendants) {
const CTxMemPoolEntry &desc = static_cast<const CTxMemPoolEntry&>(*tx);
descendant_size += desc.GetTxSize();
descendant_fees += desc.GetModifiedFee();
}
return {descendant_count, descendant_size, descendant_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);

View File

@@ -287,6 +287,7 @@ public:
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);
std::tuple<size_t, size_t, CAmount> CalculateDescendantData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
private:
typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;