Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 199 Lines • ▼ Show 20 Lines | PeerManager::getProofRegistrationTime(const ProofId &proofid) const { | ||||
auto &pview = peers.get<proof_index>(); | auto &pview = peers.get<proof_index>(); | ||||
auto it = pview.find(proofid); | auto it = pview.find(proofid); | ||||
return it == pview.end() ? std::chrono::seconds::max() | return it == pview.end() ? std::chrono::seconds::max() | ||||
: it->registration_time; | : it->registration_time; | ||||
} | } | ||||
PeerManager::PeerSet::iterator | PeerManager::PeerSet::iterator | ||||
PeerManager::fetchOrCreatePeer(const std::shared_ptr<Proof> &proof) { | PeerManager::fetchOrCreatePeer(const std::shared_ptr<Proof> &proof) { | ||||
assert(proof); | |||||
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(proof->getId()); | auto it = pview.find(proofid); | ||||
if (it != pview.end()) { | if (it != pview.end()) { | ||||
return peers.project<0>(it); | return peers.project<0>(it); | ||||
} | } | ||||
} | } | ||||
// Check the proof's validity. | // Check the proof's validity. | ||||
ProofValidationState state; | ProofValidationState state; | ||||
bool valid = [&](ProofValidationState &state) { | bool valid = [&](ProofValidationState &state) { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip(); | ||||
return proof->verify(state, coins); | return proof->verify(state, coins); | ||||
}(state); | }(state); | ||||
if (!valid) { | if (!valid) { | ||||
if (isOrphanState(state)) { | if (isOrphanState(state)) { | ||||
orphanProofs.addProof(proof); | orphanProofs.addProof(proof); | ||||
} | } | ||||
// Reject invalid proof. | // Reject invalid proof. | ||||
return peers.end(); | return peers.end(); | ||||
} | } | ||||
orphanProofs.removeProof(proof->getId()); | orphanProofs.removeProof(proofid); | ||||
// New peer means new peerid! | // New peer means new peerid! | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
// Attach UTXOs to this proof. | // Attach UTXOs to this proof. | ||||
std::unordered_set<PeerId> conflicting_peerids; | std::unordered_set<PeerId> conflicting_peerids; | ||||
for (const auto &s : proof->getStakes()) { | for (const auto &s : proof->getStakes()) { | ||||
auto p = utxos.emplace(s.getStake().getUTXO(), peerid); | auto p = utxos.emplace(s.getStake().getUTXO(), peerid); | ||||
▲ Show 20 Lines • Show All 259 Lines • Show Last 20 Lines |