Skip to content

Commit e0f2e6d

Browse files
committed
net_processing: move PeerManagerImpl into cpp file
1 parent a568b82 commit e0f2e6d

File tree

2 files changed

+126
-129
lines changed

2 files changed

+126
-129
lines changed

src/net_processing.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <streams.h>
2727
#include <tinyformat.h>
2828
#include <txmempool.h>
29+
#include <txrequest.h>
2930
#include <util/check.h> // For NDEBUG compile time check
3031
#include <util/strencodings.h>
3132
#include <util/system.h>
@@ -167,6 +168,131 @@ std::map<uint256, std::map<uint256, COrphanTx>::iterator> g_orphans_by_wtxid GUA
167168
void EraseOrphansFor(NodeId peer);
168169

169170
// Internal stuff
171+
namespace {
172+
class PeerManagerImpl final : public PeerManager
173+
{
174+
public:
175+
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
176+
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
177+
bool ignore_incoming_txs);
178+
179+
/** Overridden from CValidationInterface. */
180+
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
181+
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
182+
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
183+
void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
184+
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
185+
186+
/** Implement NetEventsInterface */
187+
void InitializeNode(CNode* pnode) override;
188+
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
189+
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
190+
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
191+
192+
/** Implement PeerManager */
193+
void CheckForStaleTipAndEvictPeers() override;
194+
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
195+
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
196+
void SetBestHeight(int height) override { m_best_height = height; };
197+
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
198+
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
199+
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
200+
201+
private:
202+
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
203+
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
204+
205+
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
206+
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
207+
208+
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
209+
void ReattemptInitialBroadcast(CScheduler& scheduler) const;
210+
211+
/** Get a shared pointer to the Peer object.
212+
* May return an empty shared_ptr if the Peer object can't be found. */
213+
PeerRef GetPeerRef(NodeId id) const;
214+
215+
/** Get a shared pointer to the Peer object and remove it from m_peer_map.
216+
* May return an empty shared_ptr if the Peer object can't be found. */
217+
PeerRef RemovePeer(NodeId id);
218+
219+
/**
220+
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
221+
*
222+
* @param[in] via_compact_block this bool is passed in because net_processing should
223+
* punish peers differently depending on whether the data was provided in a compact
224+
* block message or not. If the compact block had a valid header, but contained invalid
225+
* txs, the peer should not be punished. See BIP 152.
226+
*
227+
* @return Returns true if the peer was punished (probably disconnected)
228+
*/
229+
bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
230+
bool via_compact_block, const std::string& message = "");
231+
232+
/**
233+
* Potentially disconnect and discourage a node based on the contents of a TxValidationState object
234+
*
235+
* @return Returns true if the peer was punished (probably disconnected)
236+
*/
237+
bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "");
238+
239+
/** Maybe disconnect a peer and discourage future connections from its address.
240+
*
241+
* @param[in] pnode The node to check.
242+
* @return True if the peer was marked for disconnection in this function
243+
*/
244+
bool MaybeDiscourageAndDisconnect(CNode& pnode);
245+
246+
void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
247+
/** Process a single headers message from a peer. */
248+
void ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
249+
const std::vector<CBlockHeader>& headers,
250+
bool via_compact_block);
251+
252+
void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req);
253+
254+
/** Register with TxRequestTracker that an INV has been received from a
255+
* peer. The announcement parameters are decided in PeerManager and then
256+
* passed to TxRequestTracker. */
257+
void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
258+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
259+
260+
/** Send a version message to a peer */
261+
void PushNodeVersion(CNode& pnode, int64_t nTime);
262+
263+
const CChainParams& m_chainparams;
264+
CConnman& m_connman;
265+
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
266+
BanMan* const m_banman;
267+
ChainstateManager& m_chainman;
268+
CTxMemPool& m_mempool;
269+
TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
270+
271+
/** The height of the best chain */
272+
std::atomic<int> m_best_height{-1};
273+
274+
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
275+
276+
/** Whether this node is running in blocks only mode */
277+
const bool m_ignore_incoming_txs;
278+
279+
/** Whether we've completed initial sync yet, for determining when to turn
280+
* on extra block-relay-only peers. */
281+
bool m_initial_sync_finished{false};
282+
283+
/** Protects m_peer_map. This mutex must not be locked while holding a lock
284+
* on any of the mutexes inside a Peer object. */
285+
mutable Mutex m_peer_mutex;
286+
/**
287+
* Map of all Peer objects, keyed by peer id. This map is protected
288+
* by the m_peer_mutex. Once a shared pointer reference is
289+
* taken, the lock may be released. Individual fields are protected by
290+
* their own locks.
291+
*/
292+
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
293+
};
294+
} // namespace
295+
170296
namespace {
171297
/** Number of nodes with fSyncStarted. */
172298
int nSyncStarted GUARDED_BY(cs_main) = 0;

src/net_processing.h

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@
66
#ifndef BITCOIN_NET_PROCESSING_H
77
#define BITCOIN_NET_PROCESSING_H
88

9-
#include <consensus/params.h>
109
#include <net.h>
1110
#include <sync.h>
12-
#include <txrequest.h>
1311
#include <validationinterface.h>
1412

15-
class BlockTransactionsRequest;
16-
class BlockValidationState;
17-
class CBlockHeader;
1813
class CChainParams;
1914
class CTxMemPool;
2015
class ChainstateManager;
21-
class TxValidationState;
2216

2317
extern RecursiveMutex cs_main;
2418
extern RecursiveMutex g_cs_orphans;
@@ -129,129 +123,6 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
129123
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) = 0;
130124
};
131125

132-
class PeerManagerImpl final : public PeerManager
133-
{
134-
public:
135-
PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
136-
CScheduler& scheduler, ChainstateManager& chainman, CTxMemPool& pool,
137-
bool ignore_incoming_txs);
138-
139-
/** Overridden from CValidationInterface. */
140-
void BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexConnected) override;
141-
void BlockDisconnected(const std::shared_ptr<const CBlock> &block, const CBlockIndex* pindex) override;
142-
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override;
143-
void BlockChecked(const CBlock& block, const BlockValidationState& state) override;
144-
void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr<const CBlock>& pblock) override;
145-
146-
/** Implement NetEventsInterface */
147-
void InitializeNode(CNode* pnode) override;
148-
void FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) override;
149-
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override;
150-
bool SendMessages(CNode* pto) override EXCLUSIVE_LOCKS_REQUIRED(pto->cs_sendProcessing);
151-
152-
/** Implement PeerManager */
153-
void CheckForStaleTipAndEvictPeers() override;
154-
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) override;
155-
bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
156-
void SetBestHeight(int height) override { m_best_height = height; };
157-
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message) override;
158-
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
159-
const std::chrono::microseconds time_received, const std::atomic<bool>& interruptMsgProc) override;
160-
161-
private:
162-
/** Consider evicting an outbound peer based on the amount of time they've been behind our tip */
163-
void ConsiderEviction(CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
164-
165-
/** If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
166-
void EvictExtraOutboundPeers(int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
167-
168-
/** Retrieve unbroadcast transactions from the mempool and reattempt sending to peers */
169-
void ReattemptInitialBroadcast(CScheduler& scheduler) const;
170-
171-
/** Get a shared pointer to the Peer object.
172-
* May return an empty shared_ptr if the Peer object can't be found. */
173-
PeerRef GetPeerRef(NodeId id) const;
174-
175-
/** Get a shared pointer to the Peer object and remove it from m_peer_map.
176-
* May return an empty shared_ptr if the Peer object can't be found. */
177-
PeerRef RemovePeer(NodeId id);
178-
179-
/**
180-
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
181-
*
182-
* @param[in] via_compact_block this bool is passed in because net_processing should
183-
* punish peers differently depending on whether the data was provided in a compact
184-
* block message or not. If the compact block had a valid header, but contained invalid
185-
* txs, the peer should not be punished. See BIP 152.
186-
*
187-
* @return Returns true if the peer was punished (probably disconnected)
188-
*/
189-
bool MaybePunishNodeForBlock(NodeId nodeid, const BlockValidationState& state,
190-
bool via_compact_block, const std::string& message = "");
191-
192-
/**
193-
* Potentially disconnect and discourage a node based on the contents of a TxValidationState object
194-
*
195-
* @return Returns true if the peer was punished (probably disconnected)
196-
*/
197-
bool MaybePunishNodeForTx(NodeId nodeid, const TxValidationState& state, const std::string& message = "");
198-
199-
/** Maybe disconnect a peer and discourage future connections from its address.
200-
*
201-
* @param[in] pnode The node to check.
202-
* @return True if the peer was marked for disconnection in this function
203-
*/
204-
bool MaybeDiscourageAndDisconnect(CNode& pnode);
205-
206-
void ProcessOrphanTx(std::set<uint256>& orphan_work_set) EXCLUSIVE_LOCKS_REQUIRED(cs_main, g_cs_orphans);
207-
/** Process a single headers message from a peer. */
208-
void ProcessHeadersMessage(CNode& pfrom, const Peer& peer,
209-
const std::vector<CBlockHeader>& headers,
210-
bool via_compact_block);
211-
212-
void SendBlockTransactions(CNode& pfrom, const CBlock& block, const BlockTransactionsRequest& req);
213-
214-
/** Register with TxRequestTracker that an INV has been received from a
215-
* peer. The announcement parameters are decided in PeerManager and then
216-
* passed to TxRequestTracker. */
217-
void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
218-
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
219-
220-
/** Send a version message to a peer */
221-
void PushNodeVersion(CNode& pnode, int64_t nTime);
222-
223-
const CChainParams& m_chainparams;
224-
CConnman& m_connman;
225-
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
226-
BanMan* const m_banman;
227-
ChainstateManager& m_chainman;
228-
CTxMemPool& m_mempool;
229-
TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
230-
231-
/** The height of the best chain */
232-
std::atomic<int> m_best_height{-1};
233-
234-
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
235-
236-
/** Whether this node is running in blocks only mode */
237-
const bool m_ignore_incoming_txs;
238-
239-
/** Whether we've completed initial sync yet, for determining when to turn
240-
* on extra block-relay-only peers. */
241-
bool m_initial_sync_finished{false};
242-
243-
/** Protects m_peer_map. This mutex must not be locked while holding a lock
244-
* on any of the mutexes inside a Peer object. */
245-
mutable Mutex m_peer_mutex;
246-
/**
247-
* Map of all Peer objects, keyed by peer id. This map is protected
248-
* by the m_peer_mutex. Once a shared pointer reference is
249-
* taken, the lock may be released. Individual fields are protected by
250-
* their own locks.
251-
*/
252-
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
253-
};
254-
255126
/** Relay transaction to every node */
256127
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
257128

0 commit comments

Comments
 (0)