Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <avalanche/delegation.h> | #include <avalanche/delegation.h> | ||||
#include <avalanche/validation.h> | #include <avalanche/validation.h> | ||||
#include <net_processing.h> // For RelayProof | #include <net_processing.h> // For RelayProof | ||||
#include <random.h> | #include <random.h> | ||||
#include <validation.h> // For ChainstateActive() | #include <validation.h> // For ChainstateActive() | ||||
#include <cassert> | #include <cassert> | ||||
namespace avalanche { | namespace avalanche { | ||||
static bool isOrphanState(const ProofValidationState &state) { | |||||
return state.GetResult() == ProofValidationResult::MISSING_UTXO || | |||||
state.GetResult() == ProofValidationResult::HEIGHT_MISMATCH; | |||||
} | |||||
bool PeerManager::addNode(NodeId nodeid, const ProofId &proofid) { | bool PeerManager::addNode(NodeId nodeid, const ProofId &proofid) { | ||||
auto &pview = peers.get<proof_index>(); | auto &pview = peers.get<proof_index>(); | ||||
auto it = pview.find(proofid); | auto it = pview.find(proofid); | ||||
if (it == pview.end()) { | if (it == pview.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
return addOrUpdateNode(peers.project<0>(it), nodeid); | return addOrUpdateNode(peers.project<0>(it), nodeid); | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | ||||
it->nextRequestTime <= std::chrono::steady_clock::now()) { | it->nextRequestTime <= std::chrono::steady_clock::now()) { | ||||
return it->nodeid; | return it->nodeid; | ||||
} | } | ||||
} | } | ||||
return NO_NODE; | return NO_NODE; | ||||
} | } | ||||
static bool isOrphanState(const ProofValidationState &state) { | |||||
return state.GetResult() == ProofValidationResult::MISSING_UTXO || | |||||
state.GetResult() == ProofValidationResult::HEIGHT_MISMATCH; | |||||
} | |||||
void PeerManager::updatedBlockTip() { | void PeerManager::updatedBlockTip() { | ||||
std::vector<PeerId> invalidPeers; | std::vector<PeerId> invalidPeers; | ||||
std::vector<std::shared_ptr<Proof>> newOrphans; | std::vector<std::shared_ptr<Proof>> newOrphans; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | ||||
▲ Show 20 Lines • Show All 335 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
void PeerManager::removeUnbroadcastProof(const ProofId &proofid) { | void PeerManager::removeUnbroadcastProof(const ProofId &proofid) { | ||||
m_unbroadcast_proofids.erase(proofid); | m_unbroadcast_proofids.erase(proofid); | ||||
} | } | ||||
void PeerManager::broadcastProofs(const CConnman &connman) { | void PeerManager::broadcastProofs(const CConnman &connman) { | ||||
// For some reason SaltedProofIdHasher prevents the set from being swappable | // For some reason SaltedProofIdHasher prevents the set from being swappable | ||||
std::unordered_set<ProofId, SaltedProofIdHasher> | auto previous_unbroadcasted_proofids = std::move(m_unbroadcast_proofids); | ||||
previous_unbroadcasted_proofids = std::move(m_unbroadcast_proofids); | |||||
m_unbroadcast_proofids.clear(); | m_unbroadcast_proofids.clear(); | ||||
for (auto &proofid : previous_unbroadcasted_proofids) { | for (auto &proofid : previous_unbroadcasted_proofids) { | ||||
if (getProof(proofid)) { | if (getProof(proofid)) { | ||||
m_unbroadcast_proofids.insert(proofid); | m_unbroadcast_proofids.insert(proofid); | ||||
RelayProof(proofid, connman); | RelayProof(proofid, connman); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} // namespace avalanche | } // namespace avalanche |