Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
// 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. | ||||
#ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | #ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#define BITCOIN_AVALANCHE_PEERMANAGER_H | #define BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#include <avalanche/node.h> | #include <avalanche/node.h> | ||||
#include <avalanche/orphanproofpool.h> | #include <avalanche/orphanproofpool.h> | ||||
#include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
#include <avalanche/proofpool.h> | |||||
#include <coins.h> | #include <coins.h> | ||||
#include <pubkey.h> | #include <pubkey.h> | ||||
#include <salteduint256hasher.h> | #include <salteduint256hasher.h> | ||||
#include <util/time.h> | #include <util/time.h> | ||||
#include <boost/multi_index/composite_key.hpp> | #include <boost/multi_index/composite_key.hpp> | ||||
#include <boost/multi_index/hashed_index.hpp> | #include <boost/multi_index/hashed_index.hpp> | ||||
#include <boost/multi_index/mem_fun.hpp> | #include <boost/multi_index/mem_fun.hpp> | ||||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | struct Peer { | ||||
Peer(PeerId peerid_, ProofRef proof_) | Peer(PeerId peerid_, ProofRef proof_) | ||||
: peerid(peerid_), proof(std::move(proof_)), | : peerid(peerid_), proof(std::move(proof_)), | ||||
registration_time(GetTime<std::chrono::seconds>()) {} | registration_time(GetTime<std::chrono::seconds>()) {} | ||||
const ProofId &getProofId() const { return proof->getId(); } | const ProofId &getProofId() const { return proof->getId(); } | ||||
uint32_t getScore() const { return proof->getScore(); } | uint32_t getScore() const { return proof->getScore(); } | ||||
}; | }; | ||||
struct ProofPoolEntry { | struct proof_index { | ||||
size_t utxoIndex; | |||||
ProofRef proof; | |||||
const COutPoint &getUTXO() const { | |||||
return proof->getStakes().at(utxoIndex).getStake().getUTXO(); | |||||
} | |||||
ProofPoolEntry(size_t _utxoIndex, const ProofRef &_proof) | |||||
: utxoIndex(_utxoIndex), proof(_proof) {} | |||||
}; | |||||
struct by_utxo; | |||||
template <typename StructWithProof> struct proof_index { | |||||
using result_type = ProofId; | using result_type = ProofId; | ||||
result_type operator()(const StructWithProof &s) const { | result_type operator()(const Peer &p) const { return p.proof->getId(); } | ||||
return s.proof->getId(); | |||||
} | |||||
}; | }; | ||||
struct next_request_time {}; | struct next_request_time {}; | ||||
struct PendingNode { | struct PendingNode { | ||||
ProofId proofid; | ProofId proofid; | ||||
NodeId nodeid; | NodeId nodeid; | ||||
Show All 15 Lines | class PeerManager { | ||||
* Several nodes can make an avalanche peer. In this case, all nodes are | * Several nodes can make an avalanche peer. In this case, all nodes are | ||||
* considered interchangeable parts of the same peer. | * considered interchangeable parts of the same peer. | ||||
*/ | */ | ||||
using PeerSet = boost::multi_index_container< | using PeerSet = boost::multi_index_container< | ||||
Peer, bmi::indexed_by< | Peer, bmi::indexed_by< | ||||
// index by peerid | // index by peerid | ||||
bmi::hashed_unique<bmi::member<Peer, PeerId, &Peer::peerid>>, | bmi::hashed_unique<bmi::member<Peer, PeerId, &Peer::peerid>>, | ||||
// index by proof | // index by proof | ||||
bmi::hashed_unique<bmi::tag<by_proofid>, proof_index<Peer>, | bmi::hashed_unique<bmi::tag<by_proofid>, proof_index, | ||||
SaltedProofIdHasher>>>; | SaltedProofIdHasher>>>; | ||||
PeerId nextPeerId = 0; | PeerId nextPeerId = 0; | ||||
PeerSet peers; | PeerSet peers; | ||||
/** | |||||
* Map a proof to each utxo. A proof can be mapped with several utxos. | |||||
*/ | |||||
using ProofPool = boost::multi_index_container< | |||||
ProofPoolEntry, | |||||
bmi::indexed_by< | |||||
// index by utxo | |||||
bmi::hashed_unique< | |||||
bmi::tag<by_utxo>, | |||||
bmi::const_mem_fun<ProofPoolEntry, const COutPoint &, | |||||
&ProofPoolEntry::getUTXO>, | |||||
SaltedOutpointHasher>, | |||||
// index by proofid | |||||
bmi::hashed_non_unique<bmi::tag<by_proofid>, | |||||
proof_index<ProofPoolEntry>, | |||||
SaltedProofIdHasher>>>; | |||||
ProofPool validProofPool; | ProofPool validProofPool; | ||||
using NodeSet = boost::multi_index_container< | using NodeSet = boost::multi_index_container< | ||||
Node, | Node, | ||||
bmi::indexed_by< | bmi::indexed_by< | ||||
// index by nodeid | // index by nodeid | ||||
bmi::hashed_unique<bmi::member<Node, NodeId, &Node::nodeid>>, | bmi::hashed_unique<bmi::member<Node, NodeId, &Node::nodeid>>, | ||||
// sorted by peerid/nextRequestTime | // sorted by peerid/nextRequestTime | ||||
▲ Show 20 Lines • Show All 147 Lines • Show Last 20 Lines |