diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -790,6 +790,29 @@ /** When our tip was last updated. */ std::atomic m_last_tip_update{0}; + /** + * Determine whether or not a peer can request a transaction, and return it + * (or nullptr if not found or not allowed). + */ + CTransactionRef FindTxForGetData(const CNode &peer, const TxId &txid, + const std::chrono::seconds mempool_req, + const std::chrono::seconds now); + + void ProcessGetData(const Config &config, CNode &pfrom, Peer &peer, + const std::atomic &interruptMsgProc) + EXCLUSIVE_LOCKS_REQUIRED(!cs_main, peer.m_getdata_requests_mutex); + + /** Relay map. */ + typedef std::map MapRelay; + MapRelay mapRelay GUARDED_BY(cs_main); + + /** + * Expiration-time ordered list of (expire time, relay map entry) pairs, + * protected by cs_main). + */ + std::deque> + vRelayExpiration GUARDED_BY(cs_main); + /** * Checks if address relay is permitted with peer. If needed, initializes * the m_addr_known bloom filter and sets m_addr_relay_enabled to true. @@ -825,16 +848,6 @@ /** Number of peers from which we're downloading blocks. */ int nPeersWithValidatedDownloads GUARDED_BY(cs_main) = 0; -/** Relay map. */ -typedef std::map MapRelay; -MapRelay mapRelay GUARDED_BY(cs_main); -/** - * Expiration-time ordered list of (expire time, relay map entry) pairs, - * protected by cs_main). - */ -std::deque> - g_relay_expiration GUARDED_BY(cs_main); - struct IteratorComparator { template bool operator()(const I &a, const I &b) const { return &(*a) < &(*b); @@ -2626,14 +2639,10 @@ } } -//! Determine whether or not a peer can request a transaction, and return it (or -//! nullptr if not found or not allowed). -static CTransactionRef FindTxForGetData(const CTxMemPool &mempool, - const CNode &peer, const TxId &txid, - const std::chrono::seconds mempool_req, - const std::chrono::seconds now) - LOCKS_EXCLUDED(cs_main) { - auto txinfo = mempool.info(txid); +CTransactionRef PeerManagerImpl::FindTxForGetData( + const CNode &peer, const TxId &txid, const std::chrono::seconds mempool_req, + const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main) { + auto txinfo = m_mempool.info(txid); if (txinfo.tx) { // If a TX could have been INVed in reply to a MEMPOOL request, // or is older than UNCONDITIONAL_RELAY_DELAY, permit the request @@ -2710,9 +2719,9 @@ return nullptr; } -static void ProcessGetData(const Config &config, CNode &pfrom, Peer &peer, - CConnman &connman, CTxMemPool &mempool, - const std::atomic &interruptMsgProc) +void PeerManagerImpl::ProcessGetData(const Config &config, CNode &pfrom, + Peer &peer, + const std::atomic &interruptMsgProc) EXCLUSIVE_LOCKS_REQUIRED(peer.m_getdata_requests_mutex) LOCKS_EXCLUDED(::cs_main) { AssertLockNotHeld(cs_main); @@ -2747,7 +2756,7 @@ const avalanche::ProofId proofid(inv.hash); auto proof = FindProofForGetData(pfrom, proofid, now); if (proof) { - connman.PushMessage( + m_connman.PushMessage( &pfrom, msgMaker.Make(NetMsgType::AVAPROOF, *proof)); g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { pm.removeUnbroadcastProof(proofid); @@ -2769,18 +2778,18 @@ const TxId txid(inv.hash); CTransactionRef tx = - FindTxForGetData(mempool, pfrom, txid, mempool_req, now); + FindTxForGetData(pfrom, txid, mempool_req, now); if (tx) { int nSendFlags = 0; - connman.PushMessage( + m_connman.PushMessage( &pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx)); - mempool.RemoveUnbroadcastTx(txid); + m_mempool.RemoveUnbroadcastTx(txid); // As we're going to send tx, make sure its unconfirmed parents // are made requestable. std::vector parent_ids_to_add; { - LOCK(mempool.cs); - auto txiter = mempool.GetIter(tx->GetId()); + LOCK(m_mempool.cs); + auto txiter = m_mempool.GetIter(tx->GetId()); if (txiter) { const CTxMemPoolEntry::Parents &parents = (*txiter)->GetMemPoolParentsConst(); @@ -2822,7 +2831,7 @@ if (it != peer.m_getdata_requests.end() && !pfrom.fPauseSend) { const CInv &inv = *it++; if (inv.IsGenBlkMsg()) { - ProcessGetBlockData(config, pfrom, peer, inv, connman); + ProcessGetBlockData(config, pfrom, peer, inv, m_connman); } // else: If the first item on the queue is an unknown type, we erase it // and continue processing the queue on the next call. @@ -2843,8 +2852,8 @@ // respond. In normal operation, we often send NOTFOUND messages for // parents of transactions that we relay; if a peer is missing a parent, // they may assume we have them and request the parents from us. - connman.PushMessage(&pfrom, - msgMaker.Make(NetMsgType::NOTFOUND, vNotFound)); + m_connman.PushMessage(&pfrom, + msgMaker.Make(NetMsgType::NOTFOUND, vNotFound)); } } @@ -4150,8 +4159,7 @@ LOCK(peer->m_getdata_requests_mutex); peer->m_getdata_requests.insert(peer->m_getdata_requests.end(), vInv.begin(), vInv.end()); - ProcessGetData(config, pfrom, *peer, m_connman, m_mempool, - interruptMsgProc); + ProcessGetData(config, pfrom, *peer, interruptMsgProc); } return; @@ -5729,8 +5737,7 @@ { LOCK(peer->m_getdata_requests_mutex); if (!peer->m_getdata_requests.empty()) { - ProcessGetData(config, *pfrom, *peer, m_connman, m_mempool, - interruptMsgProc); + ProcessGetData(config, *pfrom, *peer, interruptMsgProc); } } @@ -6720,17 +6727,16 @@ nRelayedTransactions++; { // Expire old relay messages - while (!g_relay_expiration.empty() && - g_relay_expiration.front().first < - current_time) { - mapRelay.erase(g_relay_expiration.front().second); - g_relay_expiration.pop_front(); + while (!vRelayExpiration.empty() && + vRelayExpiration.front().first < current_time) { + mapRelay.erase(vRelayExpiration.front().second); + vRelayExpiration.pop_front(); } auto ret = mapRelay.insert( std::make_pair(txid, std::move(txinfo.tx))); if (ret.second) { - g_relay_expiration.push_back(std::make_pair( + vRelayExpiration.push_back(std::make_pair( current_time + RELAY_TX_CACHE_TIME, ret.first)); } }