Skip to content

Commit ed7e469

Browse files
committed
[net_processing] Move peer_map to PeerManager
1 parent a529fd3 commit ed7e469

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

src/net_processing.cpp

Lines changed: 11 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -422,58 +422,6 @@ static CNodeState *State(NodeId pnode) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
422422
return &it->second;
423423
}
424424

425-
/**
426-
* Data structure for an individual peer. This struct is not protected by
427-
* cs_main since it does not contain validation-critical data.
428-
*
429-
* Memory is owned by shared pointers and this object is destructed when
430-
* the refcount drops to zero.
431-
*
432-
* TODO: move most members from CNodeState to this structure.
433-
* TODO: move remaining application-layer data members from CNode to this structure.
434-
*/
435-
struct Peer {
436-
/** Same id as the CNode object for this peer */
437-
const NodeId m_id{0};
438-
439-
/** Protects misbehavior data members */
440-
Mutex m_misbehavior_mutex;
441-
/** Accumulated misbehavior score for this peer */
442-
int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
443-
/** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
444-
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
445-
446-
/** Set of txids to reconsider once their parent transactions have been accepted **/
447-
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
448-
449-
/** Protects m_getdata_requests **/
450-
Mutex m_getdata_requests_mutex;
451-
/** Work queue of items requested by this peer **/
452-
std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
453-
454-
explicit Peer(NodeId id) : m_id(id) {}
455-
};
456-
457-
using PeerRef = std::shared_ptr<Peer>;
458-
459-
/**
460-
* Map of all Peer objects, keyed by peer id. This map is protected
461-
* by the global g_peer_mutex. Once a shared pointer reference is
462-
* taken, the lock may be released. Individual fields are protected by
463-
* their own locks.
464-
*/
465-
Mutex g_peer_mutex;
466-
static std::map<NodeId, PeerRef> g_peer_map GUARDED_BY(g_peer_mutex);
467-
468-
/** Get a shared pointer to the Peer object.
469-
* May return nullptr if the Peer object can't be found. */
470-
static PeerRef GetPeerRef(NodeId id)
471-
{
472-
LOCK(g_peer_mutex);
473-
auto it = g_peer_map.find(id);
474-
return it != g_peer_map.end() ? it->second : nullptr;
475-
}
476-
477425
static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
478426
{
479427
nPreferredDownload -= state->fPreferredDownload;
@@ -807,8 +755,8 @@ void PeerManager::InitializeNode(CNode *pnode) {
807755
}
808756
{
809757
PeerRef peer = std::make_shared<Peer>(nodeid);
810-
LOCK(g_peer_mutex);
811-
g_peer_map.emplace_hint(g_peer_map.end(), nodeid, std::move(peer));
758+
LOCK(m_peer_mutex);
759+
m_peer_map.emplace_hint(m_peer_map.end(), nodeid, std::move(peer));
812760
}
813761
if (!pnode->IsInboundConn()) {
814762
PushNodeVersion(*pnode, m_connman, GetTime());
@@ -845,8 +793,8 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
845793
PeerRef peer = GetPeerRef(nodeid);
846794
assert(peer != nullptr);
847795
misbehavior = WITH_LOCK(peer->m_misbehavior_mutex, return peer->m_misbehavior_score);
848-
LOCK(g_peer_mutex);
849-
g_peer_map.erase(nodeid);
796+
LOCK(m_peer_mutex);
797+
m_peer_map.erase(nodeid);
850798
}
851799
CNodeState *state = State(nodeid);
852800
assert(state != nullptr);
@@ -887,6 +835,13 @@ void PeerManager::FinalizeNode(const CNode& node, bool& fUpdateConnectionTime) {
887835
LogPrint(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
888836
}
889837

838+
PeerRef PeerManager::GetPeerRef(NodeId id)
839+
{
840+
LOCK(m_peer_mutex);
841+
auto it = m_peer_map.find(id);
842+
return it != m_peer_map.end() ? it->second : nullptr;
843+
}
844+
890845
bool PeerManager::GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
891846
{
892847
LOCK(cs_main);

src/net_processing.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,40 @@ struct CNodeStateStats {
3939
std::vector<int> vHeightInFlight;
4040
};
4141

42+
/**
43+
* Data structure for an individual peer. This struct is not protected by
44+
* cs_main since it does not contain validation-critical data.
45+
*
46+
* Memory is owned by shared pointers and this object is destructed when
47+
* the refcount drops to zero.
48+
*
49+
* TODO: move most members from CNodeState to this structure.
50+
* TODO: move remaining application-layer data members from CNode to this structure.
51+
*/
52+
struct Peer {
53+
/** Same id as the CNode object for this peer */
54+
const NodeId m_id{0};
55+
56+
/** Protects misbehavior data members */
57+
Mutex m_misbehavior_mutex;
58+
/** Accumulated misbehavior score for this peer */
59+
int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
60+
/** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
61+
bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
62+
63+
/** Set of txids to reconsider once their parent transactions have been accepted **/
64+
std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
65+
66+
/** Protects m_getdata_requests **/
67+
Mutex m_getdata_requests_mutex;
68+
/** Work queue of items requested by this peer **/
69+
std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
70+
71+
explicit Peer(NodeId id) : m_id(id) {}
72+
};
73+
74+
using PeerRef = std::shared_ptr<Peer>;
75+
4276
class PeerManager final : public CValidationInterface, public NetEventsInterface {
4377
public:
4478
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
@@ -105,6 +139,10 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
105139
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
106140

107141
private:
142+
/** Get a shared pointer to the Peer object.
143+
* May return an empty shared_ptr if the Peer object can't be found. */
144+
PeerRef GetPeerRef(NodeId id);
145+
108146
/**
109147
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
110148
*
@@ -153,6 +191,16 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
153191
TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
154192

155193
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
194+
195+
/** Protects m_peer_map */
196+
Mutex m_peer_mutex;
197+
/**
198+
* Map of all Peer objects, keyed by peer id. This map is protected
199+
* by the m_peer_mutex. Once a shared pointer reference is
200+
* taken, the lock may be released. Individual fields are protected by
201+
* their own locks.
202+
*/
203+
std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
156204
};
157205

158206
/** Relay transaction to every node */

0 commit comments

Comments
 (0)