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 <random.h> | #include <random.h> | ||||
#include <validation.h> // For ChainstateActive() | #include <validation.h> // For ChainstateActive() | ||||
#include <cassert> | #include <cassert> | ||||
namespace avalanche { | namespace avalanche { | ||||
PeerManager::PeerManager() { | |||||
recentProofRejects.reset(new CRollingBloomFilter(120000, 0.000001)); | |||||
} | |||||
bool PeerManager::addNode(NodeId nodeid, const Proof &proof, | bool PeerManager::addNode(NodeId nodeid, const Proof &proof, | ||||
const Delegation &delegation) { | const Delegation &delegation) { | ||||
auto it = fetchOrCreatePeer(proof); | auto it = fetchOrCreatePeer(proof); | ||||
if (it == peers.end()) { | if (it == peers.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
const PeerId peerid = it->peerid; | const PeerId peerid = it->peerid; | ||||
▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | |||||
PeerId PeerManager::getPeerId(const Proof &proof) { | PeerId PeerManager::getPeerId(const Proof &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; | ||||
} | } | ||||
PeerManager::PeerSet::iterator | PeerManager::PeerSet::iterator | ||||
PeerManager::fetchOrCreatePeer(const Proof &proof) { | PeerManager::fetchOrCreatePeer(const Proof &proof) { | ||||
ProofValidationState state; | |||||
return fetchOrCreatePeer(proof, state); | |||||
} | |||||
PeerManager::PeerSet::iterator | |||||
PeerManager::fetchOrCreatePeer(const Proof &proof, | |||||
ProofValidationState &state) { | |||||
{ | { | ||||
// 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(proof.getId()); | auto it = pview.find(proof.getId()); | ||||
if (it != pview.end()) { | if (it != pview.end()) { | ||||
return peers.project<0>(it); | return peers.project<0>(it); | ||||
} | } | ||||
} | } | ||||
{ | { | ||||
// Reject invalid proof. | // Reject invalid proof. | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | ||||
ProofValidationState state; | |||||
if (!proof.verify(state, coins)) { | if (!proof.verify(state, coins)) { | ||||
return peers.end(); | return peers.end(); | ||||
} | } | ||||
} | } | ||||
// New peer means new peerid! | // New peer means new peerid! | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
▲ Show 20 Lines • Show All 238 Lines • ▼ Show 20 Lines | std::vector<NodeId> PeerManager::getNodeIdsForPeer(PeerId peerId) const { | ||||
auto &nview = nodes.get<next_request_time>(); | auto &nview = nodes.get<next_request_time>(); | ||||
auto nodeRange = nview.equal_range(peerId); | auto nodeRange = nview.equal_range(peerId); | ||||
for (auto it = nodeRange.first; it != nodeRange.second; ++it) { | for (auto it = nodeRange.first; it != nodeRange.second; ++it) { | ||||
nodeids.emplace_back(it->nodeid); | nodeids.emplace_back(it->nodeid); | ||||
} | } | ||||
return nodeids; | return nodeids; | ||||
} | } | ||||
const Proof *PeerManager::getProof(const ProofId proofId) const { | |||||
auto &peerView = peers.get<proof_index>(); | |||||
auto peerIt = peerView.find(proofId); | |||||
if (peerIt != peerView.end()) { | |||||
return &peerIt->proof; | |||||
} | |||||
auto proofIt = orphanProofs.find(proofId); | |||||
if (proofIt != orphanProofs.end()) { | |||||
return &proofIt->second; | |||||
} | |||||
return nullptr; | |||||
} | |||||
bool PeerManager::addProof(Proof &&proof) { | |||||
// skip proof verification if we already did it previously | |||||
if (recentProofRejects->contains(proof.getId()) || | |||||
getProof(proof.getId())) { | |||||
return false; | |||||
} | |||||
ProofValidationState state; | |||||
// Store good proof in a new peer | |||||
if (fetchOrCreatePeer(proof, state) != peers.end()) { | |||||
return true; | |||||
} | |||||
// Keep track of orphan proofs in case they become valid later | |||||
if (state.GetResult() == ProofValidationResult::MISSING_UTXO) { | |||||
return orphanProofs | |||||
.insert(std::make_pair(proof.getId(), std::move(proof))) | |||||
.second; | |||||
} | |||||
// Keep track of bad proofs to avoid requesting or relaying them | |||||
recentProofRejects->insert(proof.getId()); | |||||
return false; | |||||
} | |||||
} // namespace avalanche | } // namespace avalanche |