Skip to content

Commit 2bdce7f

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25514: net processing: Move CNode::nServices and CNode::nLocalServices to Peer
8d8eeb4 [net processing] Remove CNode::nLocalServices (John Newbery) 5961f8e [net] Return CService from GetLocalAddrForPeer and GetLocalAddress (dergoegge) d9079fe [net processing] Remove CNode::nServices (John Newbery) 7d1c036 [net processing] Replace fHaveWitness with CanServeWitnesses() (John Newbery) f65e83d [net processing] Remove fClient and m_limited_node (John Newbery) fc5eb52 [tests] Connect peer in outbound_slow_chain_eviction by sending p2p messages (John Newbery) 1f52c47 [net processing] Add m_our_services and m_their_services to Peer (John Newbery) Pull request description: Another step in #19398. Which services we offer to a peer and which services they offer to us is application layer data and should not be stored on `CNode`. This is also a prerequisite for adding `PeerManager` unit tests (See #25515). ACKs for top commit: MarcoFalke: ACK 8d8eeb4 🔑 jnewbery: utACK 8d8eeb4 mzumsande: Code Review ACK 8d8eeb4 Tree-SHA512: e772eb2a0a85db346dd7b453a41011a12756fc7cbfda6a9ef6daa9633b9a47b9770ab3dc02377690f9d02127301c3905ff22905977f758bf90b17a9a35b37523
2 parents 8d4a058 + 8d8eeb4 commit 2bdce7f

File tree

13 files changed

+199
-203
lines changed

13 files changed

+199
-203
lines changed

src/net.cpp

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -206,15 +206,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
206206
// Otherwise, return the unroutable 0.0.0.0 but filled in with
207207
// the normal parameters, since the IP may be changed to a useful
208208
// one by discovery.
209-
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices)
209+
CService GetLocalAddress(const CNetAddr& addrPeer)
210210
{
211-
CAddress ret(CService(CNetAddr(),GetListenPort()), nLocalServices);
211+
CService ret{CNetAddr(), GetListenPort()};
212212
CService addr;
213-
if (GetLocal(addr, paddrPeer))
214-
{
215-
ret = CAddress(addr, nLocalServices);
213+
if (GetLocal(addr, &addrPeer)) {
214+
ret = CService{addr};
216215
}
217-
ret.nTime = GetAdjustedTime();
218216
return ret;
219217
}
220218

@@ -233,35 +231,35 @@ bool IsPeerAddrLocalGood(CNode *pnode)
233231
IsReachable(addrLocal.GetNetwork());
234232
}
235233

236-
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode)
234+
std::optional<CService> GetLocalAddrForPeer(CNode& node)
237235
{
238-
CAddress addrLocal = GetLocalAddress(&pnode->addr, pnode->GetLocalServices());
236+
CService addrLocal{GetLocalAddress(node.addr)};
239237
if (gArgs.GetBoolArg("-addrmantest", false)) {
240238
// use IPv4 loopback during addrmantest
241-
addrLocal = CAddress(CService(LookupNumeric("127.0.0.1", GetListenPort())), pnode->GetLocalServices());
239+
addrLocal = CService(LookupNumeric("127.0.0.1", GetListenPort()));
242240
}
243241
// If discovery is enabled, sometimes give our peer the address it
244242
// tells us that it sees us as in case it has a better idea of our
245243
// address than we do.
246244
FastRandomContext rng;
247-
if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() ||
245+
if (IsPeerAddrLocalGood(&node) && (!addrLocal.IsRoutable() ||
248246
rng.randbits((GetnScore(addrLocal) > LOCAL_MANUAL) ? 3 : 1) == 0))
249247
{
250-
if (pnode->IsInboundConn()) {
248+
if (node.IsInboundConn()) {
251249
// For inbound connections, assume both the address and the port
252250
// as seen from the peer.
253-
addrLocal = CAddress{pnode->GetAddrLocal(), addrLocal.nServices, addrLocal.nTime};
251+
addrLocal = CService{node.GetAddrLocal()};
254252
} else {
255253
// For outbound connections, assume just the address as seen from
256254
// the peer and leave the port in `addrLocal` as returned by
257255
// `GetLocalAddress()` above. The peer has no way to observe our
258256
// listening port when we have initiated the connection.
259-
addrLocal.SetIP(pnode->GetAddrLocal());
257+
addrLocal.SetIP(node.GetAddrLocal());
260258
}
261259
}
262260
if (addrLocal.IsRoutable() || gArgs.GetBoolArg("-addrmantest", false))
263261
{
264-
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), pnode->GetId());
262+
LogPrint(BCLog::NET, "Advertising address %s to peer=%d\n", addrLocal.ToString(), node.GetId());
265263
return addrLocal;
266264
}
267265
// Address is unroutable. Don't advertise.
@@ -543,7 +541,6 @@ CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCo
543541
addr_bind = GetBindAddress(*sock);
544542
}
545543
CNode* pnode = new CNode(id,
546-
nLocalServices,
547544
std::move(sock),
548545
addrConnect,
549546
CalculateKeyedNetGroup(addrConnect),
@@ -603,7 +600,6 @@ Network CNode::ConnectedThroughNetwork() const
603600
void CNode::CopyStats(CNodeStats& stats)
604601
{
605602
stats.nodeid = this->GetId();
606-
X(nServices);
607603
X(addr);
608604
X(addrBind);
609605
stats.m_network = ConnectedThroughNetwork();
@@ -880,7 +876,7 @@ bool CConnman::AttemptToEvictConnection()
880876
.m_min_ping_time = node->m_min_ping_time,
881877
.m_last_block_time = node->m_last_block_time,
882878
.m_last_tx_time = node->m_last_tx_time,
883-
.fRelevantServices = HasAllDesirableServiceFlags(node->nServices),
879+
.fRelevantServices = node->m_has_all_wanted_services,
884880
.m_relay_txs = node->m_relays_txs.load(),
885881
.fBloomFilter = node->m_bloom_filter_loaded.load(),
886882
.nKeyedNetGroup = node->nKeyedNetGroup,
@@ -1014,7 +1010,6 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
10141010

10151011
const bool inbound_onion = std::find(m_onion_binds.begin(), m_onion_binds.end(), addr_bind) != m_onion_binds.end();
10161012
CNode* pnode = new CNode(id,
1017-
nodeServices,
10181013
std::move(sock),
10191014
addr,
10201015
CalculateKeyedNetGroup(addr),
@@ -1026,7 +1021,7 @@ void CConnman::CreateNodeFromAcceptedSocket(std::unique_ptr<Sock>&& sock,
10261021
pnode->AddRef();
10271022
pnode->m_permissionFlags = permissionFlags;
10281023
pnode->m_prefer_evict = discouraged;
1029-
m_msgproc->InitializeNode(pnode);
1024+
m_msgproc->InitializeNode(*pnode, nodeServices);
10301025

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

@@ -1964,7 +1959,7 @@ void CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
19641959
if (grantOutbound)
19651960
grantOutbound->MoveTo(pnode->grantOutbound);
19661961

1967-
m_msgproc->InitializeNode(pnode);
1962+
m_msgproc->InitializeNode(*pnode, nLocalServices);
19681963
{
19691964
LOCK(m_nodes_mutex);
19701965
m_nodes.push_back(pnode);
@@ -2708,7 +2703,10 @@ ServiceFlags CConnman::GetLocalServices() const
27082703

27092704
unsigned int CConnman::GetReceiveFloodSize() const { return nReceiveFloodSize; }
27102705

2711-
CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> sock, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion)
2706+
CNode::CNode(NodeId idIn, std::shared_ptr<Sock> sock, const CAddress& addrIn,
2707+
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
2708+
const CAddress& addrBindIn, const std::string& addrNameIn,
2709+
ConnectionType conn_type_in, bool inbound_onion)
27122710
: m_sock{sock},
27132711
m_connected{GetTime<std::chrono::seconds>()},
27142712
addr(addrIn),
@@ -2718,8 +2716,7 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> s
27182716
nKeyedNetGroup(nKeyedNetGroupIn),
27192717
id(idIn),
27202718
nLocalHostNonce(nLocalHostNonceIn),
2721-
m_conn_type(conn_type_in),
2722-
nLocalServices(nLocalServicesIn)
2719+
m_conn_type(conn_type_in)
27232720
{
27242721
if (inbound_onion) assert(conn_type_in == ConnectionType::INBOUND);
27252722

src/net.h

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ enum
144144
};
145145

146146
bool IsPeerAddrLocalGood(CNode *pnode);
147-
/** Returns a local address that we should advertise to this peer */
148-
std::optional<CAddress> GetLocalAddrForPeer(CNode *pnode);
147+
/** Returns a local address that we should advertise to this peer. */
148+
std::optional<CService> GetLocalAddrForPeer(CNode& node);
149149

150150
/**
151151
* Mark a network as reachable or unreachable (no automatic connects to it)
@@ -163,7 +163,7 @@ void RemoveLocal(const CService& addr);
163163
bool SeenLocal(const CService& addr);
164164
bool IsLocal(const CService& addr);
165165
bool GetLocal(CService &addr, const CNetAddr *paddrPeer = nullptr);
166-
CAddress GetLocalAddress(const CNetAddr *paddrPeer, ServiceFlags nLocalServices);
166+
CService GetLocalAddress(const CNetAddr& addrPeer);
167167

168168

169169
extern bool fDiscover;
@@ -187,7 +187,6 @@ class CNodeStats
187187
{
188188
public:
189189
NodeId nodeid;
190-
ServiceFlags nServices;
191190
std::chrono::seconds m_last_send;
192191
std::chrono::seconds m_last_recv;
193192
std::chrono::seconds m_last_tx_time;
@@ -346,7 +345,6 @@ class CNode
346345
std::unique_ptr<TransportSerializer> m_serializer;
347346

348347
NetPermissionFlags m_permissionFlags{NetPermissionFlags::None};
349-
std::atomic<ServiceFlags> nServices{NODE_NONE};
350348

351349
/**
352350
* Socket used for communication with the node.
@@ -399,8 +397,6 @@ class CNode
399397
bool HasPermission(NetPermissionFlags permission) const {
400398
return NetPermissions::HasFlag(m_permissionFlags, permission);
401399
}
402-
bool fClient{false}; // set by version message
403-
bool m_limited_node{false}; //after BIP159, set by version message
404400
/** fSuccessfullyConnected is set to true on receiving VERACK from the peer. */
405401
std::atomic_bool fSuccessfullyConnected{false};
406402
// Setting fDisconnect to true will cause the node to be disconnected the
@@ -484,6 +480,9 @@ class CNode
484480
// Peer selected us as (compact blocks) high-bandwidth peer (BIP152)
485481
std::atomic<bool> m_bip152_highbandwidth_from{false};
486482

483+
/** Whether this peer provides all services that we want. Used for eviction decisions */
484+
std::atomic_bool m_has_all_wanted_services{false};
485+
487486
/** Whether we should relay transactions to this peer (their version
488487
* message did not include fRelay=false and this is not a block-relay-only
489488
* connection). This only changes from false to true. It will never change
@@ -514,7 +513,10 @@ class CNode
514513
* criterium in CConnman::AttemptToEvictConnection. */
515514
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
516515

517-
CNode(NodeId id, ServiceFlags nLocalServicesIn, std::shared_ptr<Sock> sock, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion);
516+
CNode(NodeId id, std::shared_ptr<Sock> sock, const CAddress& addrIn,
517+
uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn,
518+
const CAddress& addrBindIn, const std::string& addrNameIn,
519+
ConnectionType conn_type_in, bool inbound_onion);
518520
CNode(const CNode&) = delete;
519521
CNode& operator=(const CNode&) = delete;
520522

@@ -572,11 +574,6 @@ class CNode
572574

573575
void CopyStats(CNodeStats& stats) EXCLUSIVE_LOCKS_REQUIRED(!m_subver_mutex, !m_addr_local_mutex, !cs_vSend, !cs_vRecv);
574576

575-
ServiceFlags GetLocalServices() const
576-
{
577-
return nLocalServices;
578-
}
579-
580577
std::string ConnectionTypeAsString() const { return ::ConnectionTypeAsString(m_conn_type); }
581578

582579
/** A ping-pong round trip has completed successfully. Update latest and minimum ping times. */
@@ -591,23 +588,6 @@ class CNode
591588
const ConnectionType m_conn_type;
592589
std::atomic<int> m_greatest_common_version{INIT_PROTO_VERSION};
593590

594-
//! 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.
609-
const ServiceFlags nLocalServices;
610-
611591
std::list<CNetMessage> vRecvMsg; // Used only by SocketHandler thread
612592

613593
// Our address, as reported by the peer
@@ -625,7 +605,7 @@ class NetEventsInterface
625605
{
626606
public:
627607
/** Initialize a peer (setup state, queue any initial messages) */
628-
virtual void InitializeNode(CNode* pnode) = 0;
608+
virtual void InitializeNode(CNode& node, ServiceFlags our_services) = 0;
629609

630610
/** Handle removal of a peer (clear state) */
631611
virtual void FinalizeNode(const CNode& node) = 0;
@@ -1035,16 +1015,14 @@ class CConnman
10351015
std::map<uint64_t, CachedAddrResponse> m_addr_response_caches;
10361016

10371017
/**
1038-
* Services this instance offers.
1018+
* Services this node offers.
10391019
*
1040-
* This data is replicated in each CNode instance we create during peer
1041-
* connection (in ConnectNode()) under a member also called
1042-
* nLocalServices.
1020+
* This data is replicated in each Peer instance we create.
10431021
*
10441022
* This data is not marked const, but after being set it should not
1045-
* change. See the note in CNode::nLocalServices documentation.
1023+
* change.
10461024
*
1047-
* \sa CNode::nLocalServices
1025+
* \sa Peer::our_services
10481026
*/
10491027
ServiceFlags nLocalServices;
10501028

0 commit comments

Comments
 (0)