[refactor] create aliases for TxOrphanage Count and Usage

This commit is contained in:
glozow
2025-06-27 14:40:11 -04:00
parent b50bd72c42
commit 1a41e7962d
2 changed files with 16 additions and 13 deletions

View File

@@ -22,11 +22,11 @@ private:
};
/** Total usage (weight) of all entries in m_orphans. */
int64_t m_total_orphan_usage{0};
TxOrphanage::Usage m_total_orphan_usage{0};
/** Total number of <peer, tx> pairs. Can be larger than m_orphans.size() because multiple peers
* may have announced the same orphan. */
unsigned int m_total_announcements{0};
TxOrphanage::Count m_total_announcements{0};
/** Map from wtxid to orphan transaction record. Limited by
* DEFAULT_MAX_ORPHAN_TRANSACTIONS */
@@ -44,7 +44,7 @@ private:
* PeerOrphanInfo, so the total of all peers' m_total_usage may be larger than
* m_total_orphan_size. If a peer is removed as an announcer, even if the orphan still
* remains in the orphanage, this number will be decremented. */
int64_t m_total_usage{0};
TxOrphanage::Usage m_total_usage{0};
};
std::map<NodeId, PeerOrphanInfo> m_peer_orphanage_info;
@@ -85,8 +85,8 @@ public:
std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const override;
size_t Size() const override { return m_orphans.size(); }
std::vector<OrphanTxBase> GetOrphanTransactions() const override;
int64_t TotalOrphanUsage() const override { return m_total_orphan_usage; }
int64_t UsageByPeer(NodeId peer) const override;
TxOrphanage::Usage TotalOrphanUsage() const override { return m_total_orphan_usage; }
TxOrphanage::Usage UsageByPeer(NodeId peer) const override;
void SanityCheck() const override;
};
@@ -401,7 +401,7 @@ std::vector<TxOrphanage::OrphanTxBase> TxOrphanageImpl::GetOrphanTransactions()
return ret;
}
int64_t TxOrphanageImpl::UsageByPeer(NodeId peer) const
TxOrphanage::Usage TxOrphanageImpl::UsageByPeer(NodeId peer) const
{
auto peer_it = m_peer_orphanage_info.find(peer);
return peer_it == m_peer_orphanage_info.end() ? 0 : peer_it->second.m_total_usage;
@@ -410,12 +410,12 @@ int64_t TxOrphanageImpl::UsageByPeer(NodeId peer) const
void TxOrphanageImpl::SanityCheck() const
{
// Check that cached m_total_announcements is correct
unsigned int counted_total_announcements{0};
TxOrphanage::Count counted_total_announcements{0};
// Check that m_total_orphan_usage is correct
int64_t counted_total_usage{0};
TxOrphanage::Usage counted_total_usage{0};
// Check that cached PeerOrphanInfo::m_total_size is correct
std::map<NodeId, int64_t> counted_size_per_peer;
std::map<NodeId, TxOrphanage::Usage> counted_size_per_peer;
for (const auto& [wtxid, orphan] : m_orphans) {
counted_total_announcements += orphan.announcers.size();

View File

@@ -31,6 +31,9 @@ static const uint32_t DEFAULT_MAX_ORPHAN_TRANSACTIONS{100};
*/
class TxOrphanage {
public:
using Usage = int64_t;
using Count = unsigned int;
/** Allows providing orphan information externally */
struct OrphanTxBase {
CTransactionRef tx;
@@ -38,7 +41,7 @@ public:
std::set<NodeId> announcers;
/** Get the weight of this transaction, an approximation of its memory usage. */
unsigned int GetUsage() const {
TxOrphanage::Usage GetUsage() const {
return GetTransactionWeight(*tx);
}
};
@@ -99,12 +102,12 @@ public:
/** Get the total usage (weight) of all orphans. If an orphan has multiple announcers, its usage is
* only counted once within this total. */
virtual int64_t TotalOrphanUsage() const = 0;
virtual Usage TotalOrphanUsage() const = 0;
/** Total usage (weight) of orphans for which this peer is an announcer. If an orphan has multiple
* announcers, its weight will be accounted for in each PeerOrphanInfo, so the total of all
* peers' UsageByPeer() may be larger than TotalOrphanBytes(). */
virtual int64_t UsageByPeer(NodeId peer) const = 0;
* peers' UsageByPeer() may be larger than TotalOrphanUsage(). */
virtual Usage UsageByPeer(NodeId peer) const = 0;
/** Check consistency between PeerOrphanInfo and m_orphans. Recalculate counters and ensure they
* match what is cached. */