Changeset View
Changeset View
Standalone View
Standalone View
src/net_processing.cpp
- This file is larger than 256 KB, so syntax highlighting is disabled by default.
Show First 20 Lines • Show All 1,672 Lines • ▼ Show 20 Lines | |||||
void PeerManagerImpl::UpdateAvalancheStatistics() const { | void PeerManagerImpl::UpdateAvalancheStatistics() const { | ||||
m_connman.ForEachNode([](CNode *pnode) { | m_connman.ForEachNode([](CNode *pnode) { | ||||
if (pnode->m_avalanche_state) { | if (pnode->m_avalanche_state) { | ||||
pnode->m_avalanche_state->updateAvailabilityScore(); | pnode->m_avalanche_state->updateAvailabilityScore(); | ||||
} | } | ||||
}); | }); | ||||
} | } | ||||
static bool isAvalancheOutboundOrManual(const CNode *pnode) { | |||||
return pnode->IsAvalancheOutboundConnection() || | |||||
(pnode->IsManualConn() && (pnode->nServices & NODE_AVALANCHE)); | |||||
} | |||||
void PeerManagerImpl::AvalanchePeriodicNetworking(CScheduler &scheduler) const { | void PeerManagerImpl::AvalanchePeriodicNetworking(CScheduler &scheduler) const { | ||||
const auto now = GetTime<std::chrono::seconds>(); | const auto now = GetTime<std::chrono::seconds>(); | ||||
std::vector<NodeId> avanode_outbound_ids; | std::vector<NodeId> avanode_outbound_ids; | ||||
if (!g_avalanche) { | if (!g_avalanche) { | ||||
// Not enabled or not ready yet, retry later | // Not enabled or not ready yet, retry later | ||||
goto scheduleLater; | goto scheduleLater; | ||||
} | } | ||||
m_connman.ForEachNode([&](CNode *pnode) { | m_connman.ForEachNode([&](CNode *pnode) { | ||||
// Build a list of the avalanche manual or outbound peers nodeids | // Build a list of the avalanche manual or outbound peers nodeids | ||||
if (isAvalancheOutboundOrManual(pnode)) { | if (pnode->m_avalanche_state && !pnode->IsInboundConn()) { | ||||
avanode_outbound_ids.push_back(pnode->GetId()); | avanode_outbound_ids.push_back(pnode->GetId()); | ||||
} | } | ||||
// If a proof radix tree timed out, cleanup | // If a proof radix tree timed out, cleanup | ||||
if (pnode->m_proof_relay && | if (pnode->m_proof_relay && | ||||
now > (pnode->m_proof_relay->lastSharedProofsUpdate.load() + | now > (pnode->m_proof_relay->lastSharedProofsUpdate.load() + | ||||
AVALANCHE_AVAPROOFS_TIMEOUT)) { | AVALANCHE_AVAPROOFS_TIMEOUT)) { | ||||
LogPrint(BCLog::AVALANCHE, | LogPrint(BCLog::AVALANCHE, | ||||
▲ Show 20 Lines • Show All 2,034 Lines • ▼ Show 20 Lines | if (msg_type == NetMsgType::VERACK) { | ||||
// proof INVs to this peer. This is used for filtering which | // proof INVs to this peer. This is used for filtering which | ||||
// INV can be requested for download. | // INV can be requested for download. | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
State(pfrom.GetId()) | State(pfrom.GetId()) | ||||
->m_recently_announced_proofs.insert( | ->m_recently_announced_proofs.insert( | ||||
localProof->getId()); | localProof->getId()); | ||||
} | } | ||||
} | } | ||||
// Send getavaaddr and getavaproofs to our avalanche outbound or | |||||
// manual connections | |||||
if (isAvalancheOutboundOrManual(&pfrom)) { | |||||
m_connman.PushMessage(&pfrom, | |||||
msgMaker.Make(NetMsgType::GETAVAADDR)); | |||||
WITH_LOCK(peer->m_addr_token_bucket_mutex, | |||||
peer->m_addr_token_bucket += GetMaxAddrToSend()); | |||||
if (pfrom.m_proof_relay && | |||||
!m_chainman.ActiveChainstate().IsInitialBlockDownload()) { | |||||
m_connman.PushMessage( | |||||
&pfrom, msgMaker.Make(NetMsgType::GETAVAPROOFS)); | |||||
pfrom.m_proof_relay->compactproofs_requested = true; | |||||
} | |||||
} | |||||
} | } | ||||
pfrom.fSuccessfullyConnected = true; | pfrom.fSuccessfullyConnected = true; | ||||
return; | return; | ||||
} | } | ||||
if (!pfrom.fSuccessfullyConnected) { | if (!pfrom.fSuccessfullyConnected) { | ||||
// Must have a verack message before anything else | // Must have a verack message before anything else | ||||
▲ Show 20 Lines • Show All 1,159 Lines • ▼ Show 20 Lines | if (msg_type == NetMsgType::AVAHELLO) { | ||||
pfrom.m_avalanche_state = std::make_unique<CNode::AvalancheState>(); | pfrom.m_avalanche_state = std::make_unique<CNode::AvalancheState>(); | ||||
avalanche::Delegation delegation; | avalanche::Delegation delegation; | ||||
vRecv >> delegation; | vRecv >> delegation; | ||||
// A delegation with an all zero limited id indicates that the peer has | // A delegation with an all zero limited id indicates that the peer has | ||||
// no proof, so we're done. | // no proof, so we're done. | ||||
if (delegation.getLimitedProofId() == uint256::ZERO) { | if (delegation.getLimitedProofId() != uint256::ZERO) { | ||||
return; | |||||
} | |||||
avalanche::DelegationState state; | avalanche::DelegationState state; | ||||
CPubKey &pubkey = pfrom.m_avalanche_state->pubkey; | CPubKey &pubkey = pfrom.m_avalanche_state->pubkey; | ||||
if (!delegation.verify(state, pubkey)) { | if (!delegation.verify(state, pubkey)) { | ||||
Misbehaving(pfrom, 100, "invalid-delegation"); | Misbehaving(pfrom, 100, "invalid-delegation"); | ||||
return; | return; | ||||
} | } | ||||
CHashWriter sighasher(SER_GETHASH, 0); | CHashWriter sighasher(SER_GETHASH, 0); | ||||
sighasher << delegation.getId(); | sighasher << delegation.getId(); | ||||
sighasher << pfrom.nRemoteHostNonce; | sighasher << pfrom.nRemoteHostNonce; | ||||
sighasher << pfrom.GetLocalNonce(); | sighasher << pfrom.GetLocalNonce(); | ||||
sighasher << pfrom.nRemoteExtraEntropy; | sighasher << pfrom.nRemoteExtraEntropy; | ||||
sighasher << pfrom.GetLocalExtraEntropy(); | sighasher << pfrom.GetLocalExtraEntropy(); | ||||
SchnorrSig sig; | SchnorrSig sig; | ||||
vRecv >> sig; | vRecv >> sig; | ||||
if (!pubkey.VerifySchnorr(sighasher.GetHash(), sig)) { | if (!pubkey.VerifySchnorr(sighasher.GetHash(), sig)) { | ||||
Misbehaving(pfrom, 100, "invalid-avahello-signature"); | Misbehaving(pfrom, 100, "invalid-avahello-signature"); | ||||
return; | return; | ||||
} | } | ||||
// If we don't know this proof already, add it to the tracker so it can | // If we don't know this proof already, add it to the tracker so it | ||||
// be requested. | // can be requested. | ||||
const avalanche::ProofId proofid(delegation.getProofId()); | const avalanche::ProofId proofid(delegation.getProofId()); | ||||
if (!AlreadyHaveProof(proofid)) { | if (!AlreadyHaveProof(proofid)) { | ||||
const bool preferred = isPreferredDownloadPeer(pfrom); | const bool preferred = isPreferredDownloadPeer(pfrom); | ||||
LOCK(cs_proofrequest); | LOCK(cs_proofrequest); | ||||
AddProofAnnouncement(pfrom, proofid, | AddProofAnnouncement(pfrom, proofid, | ||||
GetTime<std::chrono::microseconds>(), | GetTime<std::chrono::microseconds>(), | ||||
preferred); | preferred); | ||||
} | } | ||||
if (gArgs.GetBoolArg("-enableavalanchepeerdiscovery", | if (gArgs.GetBoolArg("-enableavalanchepeerdiscovery", | ||||
AVALANCHE_DEFAULT_PEER_DISCOVERY_ENABLED)) { | AVALANCHE_DEFAULT_PEER_DISCOVERY_ENABLED)) { | ||||
// Don't check the return value. If it fails we probably don't know | // Don't check the return value. If it fails we probably don't | ||||
// about the proof yet. | // know about the proof yet. | ||||
g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { | g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { | ||||
return pm.addNode(pfrom.GetId(), proofid); | return pm.addNode(pfrom.GetId(), proofid); | ||||
}); | }); | ||||
} | } | ||||
} | |||||
// Send getavaaddr and getavaproofs to our avalanche outbound or | |||||
// manual connections | |||||
if (!pfrom.IsInboundConn()) { | |||||
m_connman.PushMessage(&pfrom, | |||||
msgMaker.Make(NetMsgType::GETAVAADDR)); | |||||
WITH_LOCK(peer->m_addr_token_bucket_mutex, | |||||
peer->m_addr_token_bucket += GetMaxAddrToSend()); | |||||
if (pfrom.m_proof_relay && | |||||
!m_chainman.ActiveChainstate().IsInitialBlockDownload()) { | |||||
m_connman.PushMessage(&pfrom, | |||||
msgMaker.Make(NetMsgType::GETAVAPROOFS)); | |||||
pfrom.m_proof_relay->compactproofs_requested = true; | |||||
} | |||||
} | |||||
return; | return; | ||||
} | } | ||||
if (msg_type == NetMsgType::AVAPOLL) { | if (msg_type == NetMsgType::AVAPOLL) { | ||||
auto now = std::chrono::steady_clock::now(); | auto now = std::chrono::steady_clock::now(); | ||||
int64_t cooldown = | int64_t cooldown = | ||||
gArgs.GetArg("-avacooldown", AVALANCHE_DEFAULT_COOLDOWN); | gArgs.GetArg("-avacooldown", AVALANCHE_DEFAULT_COOLDOWN); | ||||
▲ Show 20 Lines • Show All 2,133 Lines • Show Last 20 Lines |