Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
Show First 20 Lines • Show All 516 Lines • ▼ Show 20 Lines | private: | ||||
void | void | ||||
AddProofAnnouncement(const CNode &node, const avalanche::ProofId &proofid, | AddProofAnnouncement(const CNode &node, const avalanche::ProofId &proofid, | ||||
std::chrono::microseconds current_time, bool preferred) | std::chrono::microseconds current_time, bool preferred) | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_proofrequest); | EXCLUSIVE_LOCKS_REQUIRED(cs_proofrequest); | ||||
/** Send a version message to a peer */ | /** Send a version message to a peer */ | ||||
void PushNodeVersion(const Config &config, CNode &pnode, int64_t nTime); | void PushNodeVersion(const Config &config, CNode &pnode, int64_t nTime); | ||||
/** Send a ping message every PING_INTERVAL or if requested via RPC. */ | /** | ||||
* Send a ping message every PING_INTERVAL or if requested via RPC. May mark | |||||
* the peer to be disconnected if a ping has timed out. | |||||
*/ | |||||
void MaybeSendPing(CNode &node_to); | void MaybeSendPing(CNode &node_to); | ||||
const CChainParams &m_chainparams; | const CChainParams &m_chainparams; | ||||
CConnman &m_connman; | CConnman &m_connman; | ||||
/** | /** | ||||
* Pointer to this node's banman. May be nullptr - check existence before | * Pointer to this node's banman. May be nullptr - check existence before | ||||
* dereferencing. | * dereferencing. | ||||
*/ | */ | ||||
▲ Show 20 Lines • Show All 4,928 Lines • ▼ Show 20 Lines | if (!fImporting && !fReindex && m_connman.GetNetworkActive() && | ||||
m_connman.SetTryNewOutboundPeer(true); | m_connman.SetTryNewOutboundPeer(true); | ||||
} else if (m_connman.GetTryNewOutboundPeer()) { | } else if (m_connman.GetTryNewOutboundPeer()) { | ||||
m_connman.SetTryNewOutboundPeer(false); | m_connman.SetTryNewOutboundPeer(false); | ||||
} | } | ||||
m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL; | m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL; | ||||
} | } | ||||
void PeerManagerImpl::MaybeSendPing(CNode &node_to) { | void PeerManagerImpl::MaybeSendPing(CNode &node_to) { | ||||
// Use mockable time for ping timeouts. | |||||
// This means that setmocktime may cause pings to time out. | |||||
auto now = GetTime<std::chrono::microseconds>(); | |||||
if (m_connman.RunInactivityChecks(node_to) && node_to.nPingNonceSent && | |||||
now > node_to.m_ping_start.load() + | |||||
std::chrono::seconds{TIMEOUT_INTERVAL}) { | |||||
LogPrint(BCLog::NET, "ping timeout: %fs peer=%d\n", | |||||
0.000001 * | |||||
count_microseconds(now - node_to.m_ping_start.load()), | |||||
node_to.GetId()); | |||||
node_to.fDisconnect = true; | |||||
return; | |||||
} | |||||
const CNetMsgMaker msgMaker(node_to.GetCommonVersion()); | const CNetMsgMaker msgMaker(node_to.GetCommonVersion()); | ||||
bool pingSend = false; | bool pingSend = false; | ||||
if (node_to.fPingQueued) { | if (node_to.fPingQueued) { | ||||
// RPC ping request by user | // RPC ping request by user | ||||
pingSend = true; | pingSend = true; | ||||
} | } | ||||
if (node_to.nPingNonceSent == 0 && | if (node_to.nPingNonceSent == 0 && | ||||
node_to.m_ping_start.load() + PING_INTERVAL < | now > node_to.m_ping_start.load() + PING_INTERVAL) { | ||||
GetTime<std::chrono::microseconds>()) { | |||||
// Ping automatically sent as a latency probe & keepalive. | // Ping automatically sent as a latency probe & keepalive. | ||||
pingSend = true; | pingSend = true; | ||||
} | } | ||||
if (pingSend) { | if (pingSend) { | ||||
uint64_t nonce = 0; | uint64_t nonce = 0; | ||||
while (nonce == 0) { | while (nonce == 0) { | ||||
GetRandBytes((uint8_t *)&nonce, sizeof(nonce)); | GetRandBytes((uint8_t *)&nonce, sizeof(nonce)); | ||||
} | } | ||||
node_to.fPingQueued = false; | node_to.fPingQueued = false; | ||||
node_to.m_ping_start = GetTime<std::chrono::microseconds>(); | node_to.m_ping_start = now; | ||||
if (node_to.GetCommonVersion() > BIP0031_VERSION) { | if (node_to.GetCommonVersion() > BIP0031_VERSION) { | ||||
node_to.nPingNonceSent = nonce; | node_to.nPingNonceSent = nonce; | ||||
m_connman.PushMessage(&node_to, | m_connman.PushMessage(&node_to, | ||||
msgMaker.Make(NetMsgType::PING, nonce)); | msgMaker.Make(NetMsgType::PING, nonce)); | ||||
} else { | } else { | ||||
// Peer is too old to support ping command with nonce, pong will | // Peer is too old to support ping command with nonce, pong will | ||||
// never arrive. | // never arrive. | ||||
node_to.nPingNonceSent = 0; | node_to.nPingNonceSent = 0; | ||||
Show All 39 Lines | bool PeerManagerImpl::SendMessages(const Config &config, CNode *pto) { | ||||
} | } | ||||
// If we get here, the outgoing message serialization version is set and | // If we get here, the outgoing message serialization version is set and | ||||
// can't change. | // can't change. | ||||
const CNetMsgMaker msgMaker(pto->GetCommonVersion()); | const CNetMsgMaker msgMaker(pto->GetCommonVersion()); | ||||
MaybeSendPing(*pto); | MaybeSendPing(*pto); | ||||
// MaybeSendPing may have marked peer for disconnection | |||||
if (pto->fDisconnect) { | |||||
return true; | |||||
} | |||||
auto current_time = GetTime<std::chrono::microseconds>(); | auto current_time = GetTime<std::chrono::microseconds>(); | ||||
bool fFetch; | bool fFetch; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
CNodeState &state = *State(pto->GetId()); | CNodeState &state = *State(pto->GetId()); | ||||
▲ Show 20 Lines • Show All 769 Lines • Show Last 20 Lines |