Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 136 Lines • ▼ Show 20 Lines | bool PeerManager::updateNextRequestTime(NodeId nodeid, TimePoint timeout) { | ||||
auto it = nodes.find(nodeid); | auto it = nodes.find(nodeid); | ||||
if (it == nodes.end()) { | if (it == nodes.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); | return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); | ||||
} | } | ||||
bool PeerManager::registerProof(const std::shared_ptr<Proof> &proof) { | bool PeerManager::registerProof(const ProofRef &proof) { | ||||
return !getProof(proof->getId()) && getPeerId(proof) != NO_PEER; | return !getProof(proof->getId()) && getPeerId(proof) != NO_PEER; | ||||
} | } | ||||
NodeId PeerManager::selectNode() { | NodeId PeerManager::selectNode() { | ||||
for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | ||||
const PeerId p = selectPeer(); | const PeerId p = selectPeer(); | ||||
// If we cannot find a peer, it may be due to the fact that it is | // If we cannot find a peer, it may be due to the fact that it is | ||||
Show All 17 Lines | |||||
static bool isOrphanState(const ProofValidationState &state) { | static bool isOrphanState(const ProofValidationState &state) { | ||||
return state.GetResult() == ProofValidationResult::MISSING_UTXO || | return state.GetResult() == ProofValidationResult::MISSING_UTXO || | ||||
state.GetResult() == ProofValidationResult::HEIGHT_MISMATCH; | 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<ProofRef> newOrphans; | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | ||||
for (const auto &p : peers) { | for (const auto &p : peers) { | ||||
ProofValidationState state; | ProofValidationState state; | ||||
if (!p.proof->verify(state, coins)) { | if (!p.proof->verify(state, coins)) { | ||||
Show All 11 Lines | for (auto &p : newOrphans) { | ||||
orphanProofs.addProof(p); | orphanProofs.addProof(p); | ||||
} | } | ||||
for (const auto &pid : invalidPeers) { | for (const auto &pid : invalidPeers) { | ||||
removePeer(pid); | removePeer(pid); | ||||
} | } | ||||
} | } | ||||
PeerId PeerManager::getPeerId(const std::shared_ptr<Proof> &proof) { | PeerId PeerManager::getPeerId(const ProofRef &proof) { | ||||
auto it = fetchOrCreatePeer(proof); | auto it = fetchOrCreatePeer(proof); | ||||
return it == peers.end() ? NO_PEER : it->peerid; | return it == peers.end() ? NO_PEER : it->peerid; | ||||
} | } | ||||
std::shared_ptr<Proof> PeerManager::getProof(const ProofId &proofid) const { | ProofRef PeerManager::getProof(const ProofId &proofid) const { | ||||
std::shared_ptr<Proof> proof = nullptr; | ProofRef proof = nullptr; | ||||
forPeer(proofid, [&](const Peer &p) { | forPeer(proofid, [&](const Peer &p) { | ||||
proof = p.proof; | proof = p.proof; | ||||
return true; | return true; | ||||
}); | }); | ||||
return proof; | return proof; | ||||
} | } | ||||
PeerManager::PeerSet::iterator | PeerManager::PeerSet::iterator | ||||
PeerManager::fetchOrCreatePeer(const std::shared_ptr<Proof> &proof) { | PeerManager::fetchOrCreatePeer(const ProofRef &proof) { | ||||
assert(proof); | assert(proof); | ||||
const ProofId &proofid = proof->getId(); | const ProofId &proofid = proof->getId(); | ||||
{ | { | ||||
// Check if we already know of that peer. | // Check if we already know of that peer. | ||||
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 peers.project<0>(it); | return peers.project<0>(it); | ||||
▲ Show 20 Lines • Show All 274 Lines • ▼ Show 20 Lines | PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | ||||
// We failed to find a slot, retry. | // We failed to find a slot, retry. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
bool PeerManager::isOrphan(const ProofId &id) const { | bool PeerManager::isOrphan(const ProofId &id) const { | ||||
return orphanProofs.getProof(id) != nullptr; | return orphanProofs.getProof(id) != nullptr; | ||||
} | } | ||||
std::shared_ptr<Proof> PeerManager::getOrphan(const ProofId &id) const { | ProofRef PeerManager::getOrphan(const ProofId &id) const { | ||||
return orphanProofs.getProof(id); | return orphanProofs.getProof(id); | ||||
} | } | ||||
void PeerManager::addUnbroadcastProof(const ProofId &proofid) { | void PeerManager::addUnbroadcastProof(const ProofId &proofid) { | ||||
// The proof should be known | // The proof should be known | ||||
if (getProof(proofid)) { | if (getProof(proofid)) { | ||||
m_unbroadcast_proofids.insert(proofid); | m_unbroadcast_proofids.insert(proofid); | ||||
} | } | ||||
} | } | ||||
void PeerManager::removeUnbroadcastProof(const ProofId &proofid) { | void PeerManager::removeUnbroadcastProof(const ProofId &proofid) { | ||||
m_unbroadcast_proofids.erase(proofid); | m_unbroadcast_proofids.erase(proofid); | ||||
} | } | ||||
} // namespace avalanche | } // namespace avalanche |