aboutsummaryrefslogtreecommitdiff
path: root/src/net_processing.h
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2020-09-07 18:12:19 +0100
committerJohn Newbery <john@johnnewbery.com>2020-12-07 11:55:28 +0000
commited7e469ceec6f7101a3fb7b15c21a6fb69697866 (patch)
treefe29c60491ef45e0a335e6bb52d39430916c709e /src/net_processing.h
parenta529fd3e3f2391e592ac937e291fec51e067ea2e (diff)
downloadbitcoin-ed7e469ceec6f7101a3fb7b15c21a6fb69697866.tar.xz
[net_processing] Move peer_map to PeerManager
Diffstat (limited to 'src/net_processing.h')
-rw-r--r--src/net_processing.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/net_processing.h b/src/net_processing.h
index 8f4ba4c0bb..0697cf82c3 100644
--- a/src/net_processing.h
+++ b/src/net_processing.h
@@ -39,6 +39,40 @@ struct CNodeStateStats {
std::vector<int> vHeightInFlight;
};
+/**
+ * Data structure for an individual peer. This struct is not protected by
+ * cs_main since it does not contain validation-critical data.
+ *
+ * Memory is owned by shared pointers and this object is destructed when
+ * the refcount drops to zero.
+ *
+ * TODO: move most members from CNodeState to this structure.
+ * TODO: move remaining application-layer data members from CNode to this structure.
+ */
+struct Peer {
+ /** Same id as the CNode object for this peer */
+ const NodeId m_id{0};
+
+ /** Protects misbehavior data members */
+ Mutex m_misbehavior_mutex;
+ /** Accumulated misbehavior score for this peer */
+ int m_misbehavior_score GUARDED_BY(m_misbehavior_mutex){0};
+ /** Whether this peer should be disconnected and marked as discouraged (unless it has the noban permission). */
+ bool m_should_discourage GUARDED_BY(m_misbehavior_mutex){false};
+
+ /** Set of txids to reconsider once their parent transactions have been accepted **/
+ std::set<uint256> m_orphan_work_set GUARDED_BY(g_cs_orphans);
+
+ /** Protects m_getdata_requests **/
+ Mutex m_getdata_requests_mutex;
+ /** Work queue of items requested by this peer **/
+ std::deque<CInv> m_getdata_requests GUARDED_BY(m_getdata_requests_mutex);
+
+ explicit Peer(NodeId id) : m_id(id) {}
+};
+
+using PeerRef = std::shared_ptr<Peer>;
+
class PeerManager final : public CValidationInterface, public NetEventsInterface {
public:
PeerManager(const CChainParams& chainparams, CConnman& connman, BanMan* banman,
@@ -105,6 +139,10 @@ public:
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats);
private:
+ /** Get a shared pointer to the Peer object.
+ * May return an empty shared_ptr if the Peer object can't be found. */
+ PeerRef GetPeerRef(NodeId id);
+
/**
* Potentially mark a node discouraged based on the contents of a BlockValidationState object
*
@@ -153,6 +191,16 @@ private:
TxRequestTracker m_txrequest GUARDED_BY(::cs_main);
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
+
+ /** Protects m_peer_map */
+ Mutex m_peer_mutex;
+ /**
+ * Map of all Peer objects, keyed by peer id. This map is protected
+ * by the m_peer_mutex. Once a shared pointer reference is
+ * taken, the lock may be released. Individual fields are protected by
+ * their own locks.
+ */
+ std::map<NodeId, PeerRef> m_peer_map GUARDED_BY(m_peer_mutex);
};
/** Relay transaction to every node */