Skip to content

Commit e5ea7da

Browse files
committed
[txorphanage] add per-peer weight accounting
1 parent 672c69c commit e5ea7da

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/txorphanage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
4343
m_outpoint_to_orphan_it[txin.prevout].insert(ret.first);
4444
}
4545
m_total_orphan_usage += sz;
46+
auto& peer_info = m_peer_orphanage_info.try_emplace(peer).first->second;
47+
peer_info.m_total_usage += sz;
4648

4749
LogDebug(BCLog::TXPACKAGES, "stored orphan tx %s (wtxid=%s), weight: %u (mapsz %u outsz %u)\n", hash.ToString(), wtxid.ToString(), sz,
4850
m_orphans.size(), m_outpoint_to_orphan_it.size());
@@ -56,6 +58,8 @@ bool TxOrphanage::AddAnnouncer(const Wtxid& wtxid, NodeId peer)
5658
Assume(!it->second.announcers.empty());
5759
const auto ret = it->second.announcers.insert(peer);
5860
if (ret.second) {
61+
auto& peer_info = m_peer_orphanage_info.try_emplace(peer).first->second;
62+
peer_info.m_total_usage += it->second.GetUsage();
5963
LogDebug(BCLog::TXPACKAGES, "added peer=%d as announcer of orphan tx %s\n", peer, wtxid.ToString());
6064
return true;
6165
}
@@ -80,6 +84,13 @@ int TxOrphanage::EraseTx(const Wtxid& wtxid)
8084

8185
const auto tx_size{it->second.GetUsage()};
8286
m_total_orphan_usage -= tx_size;
87+
// Decrement each announcer's m_total_usage
88+
for (const auto& peer : it->second.announcers) {
89+
auto peer_it = m_peer_orphanage_info.find(peer);
90+
if (Assume(peer_it != m_peer_orphanage_info.end())) {
91+
peer_it->second.m_total_usage -= tx_size;
92+
}
93+
}
8394

8495
size_t old_pos = it->second.list_pos;
8596
assert(m_orphan_list[old_pos] == it);
@@ -103,6 +114,7 @@ int TxOrphanage::EraseTx(const Wtxid& wtxid)
103114

104115
void TxOrphanage::EraseForPeer(NodeId peer)
105116
{
117+
// Zeroes out this peer's m_total_usage.
106118
m_peer_orphanage_info.erase(peer);
107119

108120
int nErased = 0;

src/txorphanage.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ class TxOrphanage {
9797
* only counted once within this total. */
9898
unsigned int TotalOrphanUsage() const { return m_total_orphan_usage; }
9999

100+
/** Total usage (weight) of orphans for which this peer is an announcer. If an orphan has multiple
101+
* announcers, its weight will be accounted for in each PeerOrphanInfo, so the total of all
102+
* peers' UsageByPeer() may be larger than TotalOrphanBytes(). */
103+
unsigned int UsageByPeer(NodeId peer) const {
104+
auto peer_it = m_peer_orphanage_info.find(peer);
105+
return peer_it == m_peer_orphanage_info.end() ? 0 : peer_it->second.m_total_usage;
106+
}
107+
100108
protected:
101109
struct OrphanTx : public OrphanTxBase {
102110
size_t list_pos;
@@ -115,6 +123,13 @@ class TxOrphanage {
115123
* transactions that are no longer present in orphanage; these are lazily removed in
116124
* GetTxToReconsider. */
117125
std::set<Wtxid> m_work_set;
126+
127+
/** Total weight of orphans for which this peer is an announcer.
128+
* If orphans are provided by different peers, its weight will be accounted for in each
129+
* PeerOrphanInfo, so the total of all peers' m_total_usage may be larger than
130+
* m_total_orphan_size. If a peer is removed as an announcer, even if the orphan still
131+
* remains in the orphanage, this number will be decremented. */
132+
unsigned int m_total_usage{0};
118133
};
119134
std::map<NodeId, PeerOrphanInfo> m_peer_orphanage_info;
120135

0 commit comments

Comments
 (0)