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/validation.h> | #include <avalanche/validation.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <validation.h> // For ChainstateActive() | |||||
#include <cassert> | #include <cassert> | ||||
namespace avalanche { | namespace avalanche { | ||||
PeerId PeerManager::getPeer(const Proof &proof) { | PeerId PeerManager::getPeer(const Proof &proof) { | ||||
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 it->peerid; | return it->peerid; | ||||
} | } | ||||
{ | |||||
// Reject invalid proof. | // Reject invalid proof. | ||||
LOCK(cs_main); | |||||
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | |||||
ProofValidationState state; | ProofValidationState state; | ||||
if (!proof.verify(state)) { | if (!proof.verify(state, coins)) { | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
} | |||||
// We have no peer for this proof, time to create it. | // We have no peer for this proof, time to create it. | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
auto inserted = peers.emplace(peerid, uint32_t(slots.size()), proof); | auto inserted = peers.emplace(peerid, uint32_t(slots.size()), proof); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
const uint32_t score = proof.getScore(); | const uint32_t score = proof.getScore(); | ||||
const uint64_t start = slotCount; | const uint64_t start = slotCount; | ||||
▲ Show 20 Lines • Show All 235 Lines • Show Last 20 Lines |