Skip to content

Commit 5805b82

Browse files
committed
[net processing] Move PushNodeVersion into PeerManager
1 parent 7212db4 commit 5805b82

File tree

2 files changed

+33
-30
lines changed

2 files changed

+33
-30
lines changed

src/net_processing.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -432,32 +432,6 @@ static void UpdatePreferredDownload(const CNode& node, CNodeState* state) EXCLUS
432432
nPreferredDownload += state->fPreferredDownload;
433433
}
434434

435-
static void PushNodeVersion(CNode& pnode, CConnman& connman, int64_t nTime)
436-
{
437-
// Note that pnode->GetLocalServices() is a reflection of the local
438-
// services we were offering when the CNode object was created for this
439-
// peer.
440-
ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
441-
uint64_t nonce = pnode.GetLocalNonce();
442-
int nNodeStartingHeight = pnode.GetMyStartingHeight();
443-
NodeId nodeid = pnode.GetId();
444-
CAddress addr = pnode.addr;
445-
446-
CAddress addrYou = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ?
447-
addr :
448-
CAddress(CService(), addr.nServices);
449-
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
450-
451-
connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
452-
nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode.m_tx_relay != nullptr));
453-
454-
if (fLogIPs) {
455-
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
456-
} else {
457-
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
458-
}
459-
}
460-
461435
// Returns a bool indicating whether we requested this block.
462436
// Also used if a block was /not/ received and timed out or started with another peer
463437
static bool MarkBlockAsReceived(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) {
@@ -708,6 +682,32 @@ static void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vec
708682

709683
} // namespace
710684

685+
void PeerManager::PushNodeVersion(CNode& pnode, int64_t nTime)
686+
{
687+
// Note that pnode->GetLocalServices() is a reflection of the local
688+
// services we were offering when the CNode object was created for this
689+
// peer.
690+
ServiceFlags nLocalNodeServices = pnode.GetLocalServices();
691+
uint64_t nonce = pnode.GetLocalNonce();
692+
int nNodeStartingHeight = pnode.GetMyStartingHeight();
693+
NodeId nodeid = pnode.GetId();
694+
CAddress addr = pnode.addr;
695+
696+
CAddress addrYou = addr.IsRoutable() && !IsProxy(addr) && addr.IsAddrV1Compatible() ?
697+
addr :
698+
CAddress(CService(), addr.nServices);
699+
CAddress addrMe = CAddress(CService(), nLocalNodeServices);
700+
701+
m_connman.PushMessage(&pnode, CNetMsgMaker(INIT_PROTO_VERSION).Make(NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
702+
nonce, strSubVersion, nNodeStartingHeight, ::g_relay_txes && pnode.m_tx_relay != nullptr));
703+
704+
if (fLogIPs) {
705+
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
706+
} else {
707+
LogPrint(BCLog::NET, "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
708+
}
709+
}
710+
711711
void PeerManager::AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
712712
{
713713
AssertLockHeld(::cs_main); // For m_txrequest
@@ -759,7 +759,7 @@ void PeerManager::InitializeNode(CNode *pnode) {
759759
m_peer_map.emplace_hint(m_peer_map.end(), nodeid, std::move(peer));
760760
}
761761
if (!pnode->IsInboundConn()) {
762-
PushNodeVersion(*pnode, m_connman, GetTime());
762+
PushNodeVersion(*pnode, GetTime());
763763
}
764764
}
765765

@@ -2312,9 +2312,9 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
23122312
SeenLocal(addrMe);
23132313
}
23142314

2315-
// Be shy and don't send version until we hear
2316-
if (pfrom.IsInboundConn())
2317-
PushNodeVersion(pfrom, m_connman, GetAdjustedTime());
2315+
// Inbound peers send us their version message when they connect.
2316+
// We send our version message in response.
2317+
if (pfrom.IsInboundConn()) PushNodeVersion(pfrom, GetAdjustedTime());
23182318

23192319
// Change version
23202320
const int greatest_common_version = std::min(nVersion, PROTOCOL_VERSION);

src/net_processing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ class PeerManager final : public CValidationInterface, public NetEventsInterface
186186
void AddTxAnnouncement(const CNode& node, const GenTxid& gtxid, std::chrono::microseconds current_time)
187187
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
188188

189+
/** Send a version message to a peer */
190+
void PushNodeVersion(CNode& pnode, int64_t nTime);
191+
189192
const CChainParams& m_chainparams;
190193
CConnman& m_connman;
191194
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */

0 commit comments

Comments
 (0)