@@ -224,6 +224,9 @@ struct Peer {
224224 /* * Services this peer offered to us. */
225225 std::atomic<ServiceFlags> m_their_services{NODE_NONE};
226226
227+ // ! Whether this peer is an inbound connection
228+ const bool m_is_inbound;
229+
227230 /* * Protects misbehavior data members */
228231 Mutex m_misbehavior_mutex;
229232 /* * Whether this peer should be disconnected and marked as discouraged (unless it has NetPermissionFlags::NoBan permission). */
@@ -394,9 +397,10 @@ struct Peer {
394397 * timestamp the peer sent in the version message. */
395398 std::atomic<std::chrono::seconds> m_time_offset{0s};
396399
397- explicit Peer (NodeId id, ServiceFlags our_services)
400+ explicit Peer (NodeId id, ServiceFlags our_services, bool is_inbound )
398401 : m_id{id}
399402 , m_our_services{our_services}
403+ , m_is_inbound{is_inbound}
400404 {}
401405
402406private:
@@ -476,11 +480,6 @@ struct CNodeState {
476480
477481 // ! Time of last new block announcement
478482 int64_t m_last_block_announcement{0 };
479-
480- // ! Whether this peer is an inbound connection
481- const bool m_is_inbound;
482-
483- CNodeState (bool is_inbound) : m_is_inbound(is_inbound) {}
484483};
485484
486485class PeerManagerImpl final : public PeerManager
@@ -1015,7 +1014,7 @@ class PeerManagerImpl final : public PeerManager
10151014 bool IsBlockRequested (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
10161015
10171016 /* * Have we requested this block from an outbound peer */
1018- bool IsBlockRequestedFromOutbound (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1017+ bool IsBlockRequestedFromOutbound (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex );
10191018
10201019 /* * Remove this block from our tracked requested blocks. Called if:
10211020 * - the block has been received from a peer
@@ -1099,7 +1098,7 @@ class PeerManagerImpl final : public PeerManager
10991098 * lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
11001099 * removing the first element if necessary.
11011100 */
1102- void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1101+ void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex );
11031102
11041103 /* * Stack of nodes which we have set to announce using compact blocks */
11051104 std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY (cs_main);
@@ -1302,8 +1301,8 @@ bool PeerManagerImpl::IsBlockRequestedFromOutbound(const uint256& hash)
13021301{
13031302 for (auto range = mapBlocksInFlight.equal_range (hash); range.first != range.second ; range.first ++) {
13041303 auto [nodeid, block_it] = range.first ->second ;
1305- CNodeState& nodestate = * Assert ( State ( nodeid)) ;
1306- if (!nodestate. m_is_inbound ) return true ;
1304+ PeerRef peer{ GetPeerRef ( nodeid)} ;
1305+ if (peer && !peer-> m_is_inbound ) return true ;
13071306 }
13081307
13091308 return false ;
@@ -1392,6 +1391,7 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
13921391 if (m_opts.ignore_incoming_txs ) return ;
13931392
13941393 CNodeState* nodestate = State (nodeid);
1394+ PeerRef peer{GetPeerRef (nodeid)};
13951395 if (!nodestate || !nodestate->m_provides_cmpctblocks ) {
13961396 // Don't request compact blocks if the peer has not signalled support
13971397 return ;
@@ -1404,15 +1404,15 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
14041404 lNodesAnnouncingHeaderAndIDs.push_back (nodeid);
14051405 return ;
14061406 }
1407- CNodeState *state = State (*it);
1408- if (state != nullptr && !state ->m_is_inbound ) ++num_outbound_hb_peers;
1407+ PeerRef peer_ref{ GetPeerRef (*it)} ;
1408+ if (peer_ref && !peer_ref ->m_is_inbound ) ++num_outbound_hb_peers;
14091409 }
1410- if (nodestate ->m_is_inbound ) {
1410+ if (peer && peer ->m_is_inbound ) {
14111411 // If we're adding an inbound HB peer, make sure we're not removing
14121412 // our last outbound HB peer in the process.
14131413 if (lNodesAnnouncingHeaderAndIDs.size () >= 3 && num_outbound_hb_peers == 1 ) {
1414- CNodeState *remove_node = State (lNodesAnnouncingHeaderAndIDs.front ());
1415- if (remove_node != nullptr && !remove_node ->m_is_inbound ) {
1414+ PeerRef remove_peer{ GetPeerRef (lNodesAnnouncingHeaderAndIDs.front ())} ;
1415+ if (remove_peer && !remove_peer ->m_is_inbound ) {
14161416 // Put the HB outbound peer in the second slot, so that it
14171417 // doesn't get removed.
14181418 std::swap (lNodesAnnouncingHeaderAndIDs.front (), *std::next (lNodesAnnouncingHeaderAndIDs.begin ()));
@@ -1720,7 +1720,7 @@ void PeerManagerImpl::InitializeNode(const CNode& node, ServiceFlags our_service
17201720 NodeId nodeid = node.GetId ();
17211721 {
17221722 LOCK (cs_main); // For m_node_states
1723- m_node_states.emplace_hint (m_node_states.end (), std::piecewise_construct, std::forward_as_tuple ( nodeid), std::forward_as_tuple (node. IsInboundConn ()) );
1723+ m_node_states.try_emplace (m_node_states.end (), nodeid);
17241724 }
17251725 {
17261726 LOCK (m_tx_download_mutex);
@@ -1731,7 +1731,7 @@ void PeerManagerImpl::InitializeNode(const CNode& node, ServiceFlags our_service
17311731 our_services = static_cast <ServiceFlags>(our_services | NODE_BLOOM);
17321732 }
17331733
1734- PeerRef peer = std::make_shared<Peer>(nodeid, our_services);
1734+ PeerRef peer = std::make_shared<Peer>(nodeid, our_services, node. IsInboundConn () );
17351735 {
17361736 LOCK (m_peer_mutex);
17371737 m_peer_map.emplace_hint (m_peer_map.end (), nodeid, peer);
@@ -1968,15 +1968,9 @@ void PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidati
19681968 break ;
19691969 case BlockValidationResult::BLOCK_CACHED_INVALID:
19701970 {
1971- LOCK (cs_main);
1972- CNodeState *node_state = State (nodeid);
1973- if (node_state == nullptr ) {
1974- break ;
1975- }
1976-
19771971 // Discourage outbound (but not inbound) peers if on an invalid chain.
19781972 // Exempt HB compact block peers. Manual connections are always protected from discouragement.
1979- if (!via_compact_block && !node_state ->m_is_inbound ) {
1973+ if (peer && !via_compact_block && !peer ->m_is_inbound ) {
19801974 if (peer) Misbehaving (*peer, message);
19811975 return ;
19821976 }
0 commit comments