Skip to content

Commit 1f52c47

Browse files
jnewberydergoegge
authored andcommitted
[net processing] Add m_our_services and m_their_services to Peer
Track services offered by us and the peer in the Peer object.
1 parent 31c6309 commit 1f52c47

File tree

6 files changed

+42
-33
lines changed

6 files changed

+42
-33
lines changed

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
10261026
pnode->AddRef();
10271027
pnode->m_permissionFlags = permissionFlags;
10281028
pnode->m_prefer_evict = discouraged;
1029-
m_msgproc->InitializeNode(pnode);
1029+
m_msgproc->InitializeNode(*pnode, nodeServices);
10301030

10311031
LogPrint(BCLog::NET, "connection from %s accepted\n", addr.ToString());
10321032

@@ -1964,7 +1964,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19641964
if (grantOutbound)
19651965
grantOutbound->MoveTo(pnode->grantOutbound);
19661966

1967-
m_msgproc->InitializeNode(pnode);
1967+
m_msgproc->InitializeNode(*pnode, nLocalServices);
19681968
{
19691969
LOCK(m_nodes_mutex);
19701970
m_nodes.push_back(pnode);

src/net.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -592,20 +592,6 @@ class CNode
592592
std::atomic<int> m_greatest_common_version{INIT_PROTO_VERSION};
593593

594594
//! Services offered to this peer.
595-
//!
596-
//! This is supplied by the parent CConnman during peer connection
597-
//! (CConnman::ConnectNode()) from its attribute of the same name.
598-
//!
599-
//! This is const because there is no protocol defined for renegotiating
600-
//! services initially offered to a peer. The set of local services we
601-
//! offer should not change after initialization.
602-
//!
603-
//! An interesting example of this is NODE_NETWORK and initial block
604-
//! download: a node which starts up from scratch doesn't have any blocks
605-
//! to serve, but still advertises NODE_NETWORK because it will eventually
606-
//! fulfill this role after IBD completes. P2P code is written in such a
607-
//! way that it can gracefully handle peers who don't make good on their
608-
//! service advertisements.
609595
const ServiceFlags nLocalServices;
610596

611597
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
@@ -625,7 +611,7 @@ class NetEventsInterface
625611
{
626612
public:
627613
/** Initialize a peer (setup state, queue any initial messages) */
628-
virtual void InitializeNode(CNode* pnode) = 0;
614+
virtual void InitializeNode(CNode& node, ServiceFlags our_services) = 0;
629615

630616
/** Handle removal of a peer (clear state) */
631617
virtual void FinalizeNode(const CNode& node) = 0;

src/net_processing.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ struct Peer {
207207
/** Same id as the CNode object for this peer */
208208
const NodeId m_id{0};
209209

210+
/** Services we offered to this peer.
211+
*
212+
* This is supplied by CConnman during peer initialization. It's const
213+
* because there is no protocol defined for renegotiating services
214+
* initially offered to a peer. The set of local services we offer should
215+
* not change after initialization.
216+
*
217+
* An interesting example of this is NODE_NETWORK and initial block
218+
* download: a node which starts up from scratch doesn't have any blocks
219+
* to serve, but still advertises NODE_NETWORK because it will eventually
220+
* fulfill this role after IBD completes. P2P code is written in such a
221+
* way that it can gracefully handle peers who don't make good on their
222+
* service advertisements.
223+
*
224+
* TODO: remove redundant CNode::nLocalServices*/
225+
const ServiceFlags m_our_services;
226+
/** Services this peer offered to us.
227+
*
228+
* TODO: remove redundant CNode::nServices */
229+
std::atomic<ServiceFlags> m_their_services{NODE_NONE};
230+
210231
/** Protects misbehavior data members */
211232
Mutex m_misbehavior_mutex;
212233
/** Accumulated misbehavior score for this peer */
@@ -360,8 +381,9 @@ struct Peer {
360381
/** Time of the last getheaders message to this peer */
361382
std::atomic<NodeClock::time_point> m_last_getheaders_timestamp{NodeSeconds{}};
362383

363-
Peer(NodeId id)
384+
explicit Peer(NodeId id, ServiceFlags our_services)
364385
: m_id{id}
386+
, m_our_services{our_services}
365387
{}
366388

367389
private:
@@ -482,7 +504,7 @@ class PeerManagerImpl final : public PeerManager
482504
EXCLUSIVE_LOCKS_REQUIRED(!m_most_recent_block_mutex);
483505

484506
/** Implement NetEventsInterface */
485-
void InitializeNode(CNode* pnode) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
507+
void InitializeNode(CNode& node, ServiceFlags our_services) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
486508
void FinalizeNode(const CNode& node) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
487509
bool ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt) override
488510
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, !m_recent_confirmed_transactions_mutex, !m_most_recent_block_mutex);
@@ -1299,21 +1321,21 @@ void PeerManagerImpl::UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_s
12991321
if (state) state->m_last_block_announcement = time_in_seconds;
13001322
}
13011323

1302-
void PeerManagerImpl::InitializeNode(CNode *pnode)
1324+
void PeerManagerImpl::InitializeNode(CNode& node, ServiceFlags our_services)
13031325
{
1304-
NodeId nodeid = pnode->GetId();
1326+
NodeId nodeid = node.GetId();
13051327
{
13061328
LOCK(cs_main);
1307-
m_node_states.emplace_hint(m_node_states.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(pnode->IsInboundConn()));
1329+
m_node_states.emplace_hint(m_node_states.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(node.IsInboundConn()));
13081330
assert(m_txrequest.Count(nodeid) == 0);
13091331
}
1310-
PeerRef peer = std::make_shared<Peer>(nodeid);
1332+
PeerRef peer = std::make_shared<Peer>(nodeid, our_services);
13111333
{
13121334
LOCK(m_peer_mutex);
13131335
m_peer_map.emplace_hint(m_peer_map.end(), nodeid, peer);
13141336
}
1315-
if (!pnode->IsInboundConn()) {
1316-
PushNodeVersion(*pnode, *peer);
1337+
if (!node.IsInboundConn()) {
1338+
PushNodeVersion(node, *peer);
13171339
}
13181340
}
13191341

@@ -2843,6 +2865,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
28432865
m_connman.PushMessage(&pfrom, msg_maker.Make(NetMsgType::VERACK));
28442866

28452867
pfrom.nServices = nServices;
2868+
peer->m_their_services = nServices;
28462869
pfrom.SetAddrLocal(addrMe);
28472870
{
28482871
LOCK(pfrom.m_subver_mutex);

src/test/denialofservice_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
6565
/*inbound_onion=*/false};
6666
dummyNode1.SetCommonVersion(PROTOCOL_VERSION);
6767

68-
peerLogic->InitializeNode(&dummyNode1);
68+
peerLogic->InitializeNode(dummyNode1, dummyNode1.GetLocalServices());
6969
dummyNode1.fSuccessfullyConnected = true;
7070

7171
// This test requires that we have a chain with non-zero work.
@@ -124,7 +124,7 @@ static void AddRandomOutboundPeer(NodeId& id, std::vector<CNode*>& vNodes, PeerM
124124
CNode &node = *vNodes.back();
125125
node.SetCommonVersion(PROTOCOL_VERSION);
126126

127-
peerLogic.InitializeNode(&node);
127+
peerLogic.InitializeNode(node, node.GetLocalServices());
128128
node.fSuccessfullyConnected = true;
129129

130130
connman.AddTestNode(node);
@@ -302,7 +302,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
302302
ConnectionType::INBOUND,
303303
/*inbound_onion=*/false};
304304
nodes[0]->SetCommonVersion(PROTOCOL_VERSION);
305-
peerLogic->InitializeNode(nodes[0]);
305+
peerLogic->InitializeNode(*nodes[0], nodes[0]->GetLocalServices());
306306
nodes[0]->fSuccessfullyConnected = true;
307307
connman->AddTestNode(*nodes[0]);
308308
peerLogic->UnitTestMisbehaving(nodes[0]->GetId(), DISCOURAGEMENT_THRESHOLD); // Should be discouraged
@@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
325325
ConnectionType::INBOUND,
326326
/*inbound_onion=*/false};
327327
nodes[1]->SetCommonVersion(PROTOCOL_VERSION);
328-
peerLogic->InitializeNode(nodes[1]);
328+
peerLogic->InitializeNode(*nodes[1], nodes[1]->GetLocalServices());
329329
nodes[1]->fSuccessfullyConnected = true;
330330
connman->AddTestNode(*nodes[1]);
331331
peerLogic->UnitTestMisbehaving(nodes[1]->GetId(), DISCOURAGEMENT_THRESHOLD - 1);
@@ -363,7 +363,7 @@ BOOST_AUTO_TEST_CASE(peer_discouragement)
363363
ConnectionType::OUTBOUND_FULL_RELAY,
364364
/*inbound_onion=*/false};
365365
nodes[2]->SetCommonVersion(PROTOCOL_VERSION);
366-
peerLogic->InitializeNode(nodes[2]);
366+
peerLogic->InitializeNode(*nodes[2], nodes[2]->GetLocalServices());
367367
nodes[2]->fSuccessfullyConnected = true;
368368
connman->AddTestNode(*nodes[2]);
369369
peerLogic->UnitTestMisbehaving(nodes[2]->GetId(), DISCOURAGEMENT_THRESHOLD);
@@ -408,7 +408,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
408408
ConnectionType::INBOUND,
409409
/*inbound_onion=*/false};
410410
dummyNode.SetCommonVersion(PROTOCOL_VERSION);
411-
peerLogic->InitializeNode(&dummyNode);
411+
peerLogic->InitializeNode(dummyNode, dummyNode.GetLocalServices());
412412
dummyNode.fSuccessfullyConnected = true;
413413

414414
peerLogic->UnitTestMisbehaving(dummyNode.GetId(), DISCOURAGEMENT_THRESHOLD);

src/test/net_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
857857
*static_cast<TestChainState*>(&m_node.chainman->ActiveChainstate());
858858
chainstate.JumpOutOfIbd();
859859

860-
m_node.peerman->InitializeNode(&peer);
860+
m_node.peerman->InitializeNode(peer, peer.GetLocalServices());
861861

862862
std::atomic<bool> interrupt_dummy{false};
863863
std::chrono::microseconds time_received_dummy{0};

src/test/util/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void ConnmanTestMsg::Handshake(CNode& node,
2424
auto& connman{*this};
2525
const CNetMsgMaker mm{0};
2626

27-
peerman.InitializeNode(&node);
27+
peerman.InitializeNode(node, node.GetLocalServices());
2828

2929
CSerializedNetMsg msg_version{
3030
mm.Make(NetMsgType::VERSION,

0 commit comments

Comments
 (0)