[txorphanage] account for weight of orphans

This commit is contained in:
glozow 2025-01-27 06:56:43 -05:00
parent 82ba505134
commit 59cd0f0e09
2 changed files with 17 additions and 0 deletions

View File

@ -42,6 +42,7 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
for (const CTxIn& txin : tx->vin) {
m_outpoint_to_orphan_it[txin.prevout].insert(ret.first);
}
m_total_orphan_usage += sz;
LogDebug(BCLog::TXPACKAGES, "stored orphan tx %s (wtxid=%s), weight: %u (mapsz %u outsz %u)\n", hash.ToString(), wtxid.ToString(), sz,
m_orphans.size(), m_outpoint_to_orphan_it.size());
@ -77,6 +78,9 @@ int TxOrphanage::EraseTx(const Wtxid& wtxid)
m_outpoint_to_orphan_it.erase(itPrev);
}
const auto tx_size{it->second.GetUsage()};
m_total_orphan_usage -= tx_size;
size_t old_pos = it->second.list_pos;
assert(m_orphan_list[old_pos] == it);
if (old_pos + 1 != m_orphan_list.size()) {

View File

@ -5,6 +5,7 @@
#ifndef BITCOIN_TXORPHANAGE_H
#define BITCOIN_TXORPHANAGE_H
#include <consensus/validation.h>
#include <net.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
@ -83,15 +84,27 @@ public:
/** Peers added with AddTx or AddAnnouncer. */
std::set<NodeId> announcers;
NodeSeconds nTimeExpire;
/** Get the weight of this transaction, an approximation of its memory usage. */
unsigned int GetUsage() const {
return GetTransactionWeight(*tx);
}
};
std::vector<OrphanTxBase> GetOrphanTransactions() const;
/** Get the total usage (weight) of all orphans. If an orphan has multiple announcers, its usage is
* only counted once within this total. */
unsigned int TotalOrphanUsage() const { return m_total_orphan_usage; }
protected:
struct OrphanTx : public OrphanTxBase {
size_t list_pos;
};
/** Total usage (weight) of all entries in m_orphans. */
unsigned int m_total_orphan_usage{0};
/** Map from wtxid to orphan transaction record. Limited by
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
std::map<Wtxid, OrphanTx> m_orphans;