p2p: Add PeerManager method to count the amount of inbound/outbounds fanout peers

This commit is contained in:
Sergi Delgado Segura 2024-06-07 13:54:52 -04:00
parent 0e29099687
commit a461a12041
2 changed files with 24 additions and 0 deletions

View file

@ -503,6 +503,7 @@ public:
std::vector<TxOrphanage::OrphanTxBase> GetOrphanTransactions() override EXCLUSIVE_LOCKS_REQUIRED(!m_tx_download_mutex);
PeerManagerInfo GetInfo() const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
std::pair<size_t, size_t> GetFanoutPeersCount() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void SetBestBlock(int height, std::chrono::seconds time) override
{
@ -2114,6 +2115,26 @@ void PeerManagerImpl::SendPings()
for(auto& it : m_peer_map) it.second->m_ping_queued = true;
}
std::pair<size_t, size_t> PeerManagerImpl::GetFanoutPeersCount()
{
size_t inbounds_fanout_tx_relay = 0, outbounds_fanout_tx_relay = 0;
if (m_txreconciliation) {
LOCK(m_peer_mutex);
for(const auto& [peer_id, peer] : m_peer_map) {
if (const auto tx_relay = peer->GetTxRelay()) {
const bool peer_relays_txs = WITH_LOCK(tx_relay->m_bloom_filter_mutex, return tx_relay->m_relay_txs);
if (peer_relays_txs && !m_txreconciliation->IsPeerRegistered(peer_id)) {
peer->m_is_inbound ? ++inbounds_fanout_tx_relay : ++outbounds_fanout_tx_relay;
}
}
}
}
return std::pair(inbounds_fanout_tx_relay, outbounds_fanout_tx_relay);
}
void PeerManagerImpl::RelayTransaction(const uint256& txid, const uint256& wtxid)
{
LOCK(m_peer_mutex);

View file

@ -108,6 +108,9 @@ public:
/** Relay transaction to all peers. */
virtual void RelayTransaction(const uint256& txid, const uint256& wtxid) = 0;
/** Get the amount of inbounds (first) and outbounds fanout peers (second). */
virtual std::pair<size_t, size_t> GetFanoutPeersCount() = 0;
/** Send ping message to all peers */
virtual void SendPings() = 0;