| 9327 | } |
| 9328 | |
| 9329 | bool PeerManagerImpl::ReceivedAvalancheProof(CNode &node, Peer &peer, |
| 9330 | const avalanche::ProofRef &proof) { |
| 9331 | assert(proof != nullptr); |
| 9332 | |
| 9333 | const avalanche::ProofId &proofid = proof->getId(); |
| 9334 | |
| 9335 | AddKnownProof(peer, proofid); |
| 9336 | |
| 9337 | if (m_chainman.IsInitialBlockDownload()) { |
| 9338 | // We cannot reliably verify proofs during IBD, so bail out early and |
| 9339 | // keep the inventory as pending so it can be requested when the node |
| 9340 | // has synced. |
| 9341 | return true; |
| 9342 | } |
| 9343 | |
| 9344 | const NodeId nodeid = node.GetId(); |
| 9345 | |
| 9346 | const bool isStaker = WITH_LOCK(node.cs_avalanche_pubkey, |
| 9347 | return node.m_avalanche_pubkey.has_value()); |
| 9348 | auto saveProofIfStaker = [this, isStaker](const CNode &node, |
| 9349 | const avalanche::ProofId &proofid, |
| 9350 | const NodeId nodeid) -> bool { |
| 9351 | if (isStaker) { |
| 9352 | return m_avalanche->withPeerManager( |
| 9353 | [&](avalanche::PeerManager &pm) { |
| 9354 | return pm.saveRemoteProof(proofid, nodeid, true); |
| 9355 | }); |
| 9356 | } |
| 9357 | |
| 9358 | return false; |
| 9359 | }; |
| 9360 | |
| 9361 | { |
| 9362 | LOCK(cs_proofrequest); |
| 9363 | m_proofrequest.ReceivedResponse(nodeid, proofid); |
| 9364 | |
| 9365 | if (AlreadyHaveProof(proofid)) { |
| 9366 | m_proofrequest.ForgetInvId(proofid); |
| 9367 | saveProofIfStaker(node, proofid, nodeid); |
| 9368 | return true; |
| 9369 | } |
| 9370 | } |
| 9371 | |
| 9372 | // registerProof should not be called while cs_proofrequest because it |
| 9373 | // holds cs_main and that creates a potential deadlock during shutdown |
| 9374 | |
| 9375 | avalanche::ProofRegistrationState state; |
| 9376 | if (m_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { |
| 9377 | return pm.registerProof(proof, state); |
| 9378 | })) { |
| 9379 | WITH_LOCK(cs_proofrequest, m_proofrequest.ForgetInvId(proofid)); |
| 9380 | RelayProof(proofid); |
| 9381 | |
| 9382 | node.m_last_proof_time = GetTime<std::chrono::seconds>(); |
| 9383 | |
| 9384 | LogPrint(BCLog::NET, "New avalanche proof: peer=%d, proofid %s\n", |
| 9385 | nodeid, proofid.ToString()); |
| 9386 | } |
nothing calls this directly
no test coverage detected