Skip to content

Commit 59cd0f0

Browse files
committed
[txorphanage] account for weight of orphans
1 parent 82ba505 commit 59cd0f0

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/txorphanage.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
4242
for (const CTxIn& txin : tx->vin) {
4343
m_outpoint_to_orphan_it[txin.prevout].insert(ret.first);
4444
}
45+
m_total_orphan_usage += sz;
4546

4647
LogDebug(BCLog::TXPACKAGES, "stored orphan tx %s (wtxid=%s), weight: %u (mapsz %u outsz %u)\n", hash.ToString(), wtxid.ToString(), sz,
4748
m_orphans.size(), m_outpoint_to_orphan_it.size());
@@ -77,6 +78,9 @@ int TxOrphanage::EraseTx(const Wtxid& wtxid)
7778
m_outpoint_to_orphan_it.erase(itPrev);
7879
}
7980

81+
const auto tx_size{it->second.GetUsage()};
82+
m_total_orphan_usage -= tx_size;
83+
8084
size_t old_pos = it->second.list_pos;
8185
assert(m_orphan_list[old_pos] == it);
8286
if (old_pos + 1 != m_orphan_list.size()) {

src/txorphanage.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef BITCOIN_TXORPHANAGE_H
66
#define BITCOIN_TXORPHANAGE_H
77

8+
#include <consensus/validation.h>
89
#include <net.h>
910
#include <primitives/block.h>
1011
#include <primitives/transaction.h>
@@ -83,15 +84,27 @@ class TxOrphanage {
8384
/** Peers added with AddTx or AddAnnouncer. */
8485
std::set<NodeId> announcers;
8586
NodeSeconds nTimeExpire;
87+
88+
/** Get the weight of this transaction, an approximation of its memory usage. */
89+
unsigned int GetUsage() const {
90+
return GetTransactionWeight(*tx);
91+
}
8692
};
8793

8894
std::vector<OrphanTxBase> GetOrphanTransactions() const;
8995

96+
/** Get the total usage (weight) of all orphans. If an orphan has multiple announcers, its usage is
97+
* only counted once within this total. */
98+
unsigned int TotalOrphanUsage() const { return m_total_orphan_usage; }
99+
90100
protected:
91101
struct OrphanTx : public OrphanTxBase {
92102
size_t list_pos;
93103
};
94104

105+
/** Total usage (weight) of all entries in m_orphans. */
106+
unsigned int m_total_orphan_usage{0};
107+
95108
/** Map from wtxid to orphan transaction record. Limited by
96109
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
97110
std::map<Wtxid, OrphanTx> m_orphans;

0 commit comments

Comments
 (0)