Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show All 9 Lines | |||||
#include <validation.h> // For ChainstateActive() | #include <validation.h> // For ChainstateActive() | ||||
#include <cassert> | #include <cassert> | ||||
namespace avalanche { | namespace avalanche { | ||||
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); | ProofValidationState validationState; | ||||
auto it = fetchOrCreatePeer(proof, validationState); | |||||
if (it == peers.end()) { | if (it == peers.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
const PeerId peerid = it->peerid; | const PeerId peerid = it->peerid; | ||||
DelegationState state; | DelegationState state; | ||||
CPubKey pubkey; | CPubKey pubkey; | ||||
▲ Show 20 Lines • Show All 149 Lines • ▼ Show 20 Lines | void PeerManager::updatedBlockTip() { | ||||
} | } | ||||
for (const auto &pid : invalidPeers) { | for (const auto &pid : invalidPeers) { | ||||
removePeer(pid); | removePeer(pid); | ||||
} | } | ||||
} | } | ||||
PeerId PeerManager::getPeerId(const Proof &proof) { | PeerId PeerManager::getPeerId(const Proof &proof) { | ||||
auto it = fetchOrCreatePeer(proof); | ProofValidationState state; | ||||
return getPeerId(proof, state); | |||||
} | |||||
PeerId PeerManager::getPeerId(const Proof &proof, ProofValidationState &state) { | |||||
auto it = fetchOrCreatePeer(proof, state); | |||||
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) { | |||||
{ | { | ||||
// 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 247 Lines • Show Last 20 Lines |