@@ -224,6 +224,9 @@ struct Peer {
224
224
/* * Services this peer offered to us. */
225
225
std::atomic<ServiceFlags> m_their_services{NODE_NONE};
226
226
227
+ // ! Whether this peer is an inbound connection
228
+ const bool m_is_inbound;
229
+
227
230
/* * Protects misbehavior data members */
228
231
Mutex m_misbehavior_mutex;
229
232
/* * Whether this peer should be disconnected and marked as discouraged (unless it has NetPermissionFlags::NoBan permission). */
@@ -394,9 +397,10 @@ struct Peer {
394
397
* timestamp the peer sent in the version message. */
395
398
std::atomic<std::chrono::seconds> m_time_offset{0s};
396
399
397
- explicit Peer (NodeId id, ServiceFlags our_services)
400
+ explicit Peer (NodeId id, ServiceFlags our_services, bool is_inbound )
398
401
: m_id{id}
399
402
, m_our_services{our_services}
403
+ , m_is_inbound{is_inbound}
400
404
{}
401
405
402
406
private:
@@ -476,11 +480,6 @@ struct CNodeState {
476
480
477
481
// ! Time of last new block announcement
478
482
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) {}
484
483
};
485
484
486
485
class PeerManagerImpl final : public PeerManager
@@ -1015,7 +1014,7 @@ class PeerManagerImpl final : public PeerManager
1015
1014
bool IsBlockRequested (const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1016
1015
1017
1016
/* * 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 );
1019
1018
1020
1019
/* * Remove this block from our tracked requested blocks. Called if:
1021
1020
* - the block has been received from a peer
@@ -1099,7 +1098,7 @@ class PeerManagerImpl final : public PeerManager
1099
1098
* lNodesAnnouncingHeaderAndIDs, and keeping that list under a certain size by
1100
1099
* removing the first element if necessary.
1101
1100
*/
1102
- void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
1101
+ void MaybeSetPeerAsAnnouncingHeaderAndIDs (NodeId nodeid) EXCLUSIVE_LOCKS_REQUIRED(cs_main, !m_peer_mutex );
1103
1102
1104
1103
/* * Stack of nodes which we have set to announce using compact blocks */
1105
1104
std::list<NodeId> lNodesAnnouncingHeaderAndIDs GUARDED_BY (cs_main);
@@ -1302,8 +1301,8 @@ bool PeerManagerImpl::IsBlockRequestedFromOutbound(const uint256& hash)
1302
1301
{
1303
1302
for (auto range = mapBlocksInFlight.equal_range (hash); range.first != range.second ; range.first ++) {
1304
1303
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 ;
1307
1306
}
1308
1307
1309
1308
return false ;
@@ -1392,6 +1391,7 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1392
1391
if (m_opts.ignore_incoming_txs ) return ;
1393
1392
1394
1393
CNodeState* nodestate = State (nodeid);
1394
+ PeerRef peer{GetPeerRef (nodeid)};
1395
1395
if (!nodestate || !nodestate->m_provides_cmpctblocks ) {
1396
1396
// Don't request compact blocks if the peer has not signalled support
1397
1397
return ;
@@ -1404,15 +1404,15 @@ void PeerManagerImpl::MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid)
1404
1404
lNodesAnnouncingHeaderAndIDs.push_back (nodeid);
1405
1405
return ;
1406
1406
}
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;
1409
1409
}
1410
- if (nodestate ->m_is_inbound ) {
1410
+ if (peer && peer ->m_is_inbound ) {
1411
1411
// If we're adding an inbound HB peer, make sure we're not removing
1412
1412
// our last outbound HB peer in the process.
1413
1413
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 ) {
1416
1416
// Put the HB outbound peer in the second slot, so that it
1417
1417
// doesn't get removed.
1418
1418
std::swap (lNodesAnnouncingHeaderAndIDs.front (), *std::next (lNodesAnnouncingHeaderAndIDs.begin ()));
@@ -1720,7 +1720,7 @@ void PeerManagerImpl::InitializeNode(const CNode& node, ServiceFlags our_service
1720
1720
NodeId nodeid = node.GetId ();
1721
1721
{
1722
1722
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);
1724
1724
}
1725
1725
{
1726
1726
LOCK (m_tx_download_mutex);
@@ -1731,7 +1731,7 @@ void PeerManagerImpl::InitializeNode(const CNode& node, ServiceFlags our_service
1731
1731
our_services = static_cast <ServiceFlags>(our_services | NODE_BLOOM);
1732
1732
}
1733
1733
1734
- PeerRef peer = std::make_shared<Peer>(nodeid, our_services);
1734
+ PeerRef peer = std::make_shared<Peer>(nodeid, our_services, node. IsInboundConn () );
1735
1735
{
1736
1736
LOCK (m_peer_mutex);
1737
1737
m_peer_map.emplace_hint (m_peer_map.end (), nodeid, peer);
@@ -1968,15 +1968,9 @@ void PeerManagerImpl::MaybePunishNodeForBlock(NodeId nodeid, const BlockValidati
1968
1968
break ;
1969
1969
case BlockValidationResult::BLOCK_CACHED_INVALID:
1970
1970
{
1971
- LOCK (cs_main);
1972
- CNodeState *node_state = State (nodeid);
1973
- if (node_state == nullptr ) {
1974
- break ;
1975
- }
1976
-
1977
1971
// Discourage outbound (but not inbound) peers if on an invalid chain.
1978
1972
// 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 ) {
1980
1974
if (peer) Misbehaving (*peer, message);
1981
1975
return ;
1982
1976
}
0 commit comments