Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
Show First 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | public: | ||||
template <typename Stream, typename Operation> | template <typename Stream, typename Operation> | ||||
inline void SerializationOp(Stream &s, Operation ser_action) { | inline void SerializationOp(Stream &s, Operation ser_action) { | ||||
READWRITE(cooldown); | READWRITE(cooldown); | ||||
READWRITE(votes); | READWRITE(votes); | ||||
} | } | ||||
}; | }; | ||||
class AvalanchePoll { | |||||
uint32_t round; | |||||
std::vector<CInv> invs; | |||||
public: | |||||
AvalanchePoll(uint32_t roundIn, std::vector<CInv> invsIn) | |||||
: round(roundIn), invs(invsIn) {} | |||||
const std::vector<CInv> &GetInvs() const { return invs; } | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(round); | |||||
READWRITE(invs); | |||||
} | |||||
}; | |||||
class AvalancheBlockUpdate { | class AvalancheBlockUpdate { | ||||
union { | union { | ||||
CBlockIndex *pindex; | CBlockIndex *pindex; | ||||
size_t raw; | size_t raw; | ||||
}; | }; | ||||
public: | public: | ||||
enum Status : uint8_t { | enum Status : uint8_t { | ||||
Show All 19 Lines | public: | ||||
} | } | ||||
}; | }; | ||||
typedef std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator> | typedef std::map<const CBlockIndex *, VoteRecord, CBlockIndexWorkComparator> | ||||
BlockVoteMap; | BlockVoteMap; | ||||
class AvalancheProcessor { | class AvalancheProcessor { | ||||
private: | private: | ||||
CConnman *connman; | |||||
/** | /** | ||||
* Blocks to run avalanche on. | * Blocks to run avalanche on. | ||||
*/ | */ | ||||
RWCollection<BlockVoteMap> vote_records; | RWCollection<BlockVoteMap> vote_records; | ||||
/** | /** | ||||
* Keep track of peers and queries sent. | |||||
*/ | |||||
struct RequestRecord { | |||||
private: | |||||
int64_t timestamp; | |||||
std::vector<CInv> invs; | |||||
public: | |||||
RequestRecord() : timestamp(0), invs() {} | |||||
RequestRecord(int64_t timestampIn, std::vector<CInv> invIn) | |||||
: timestamp(timestampIn), invs(std::move(invIn)) {} | |||||
int64_t GetTimestamp() const { return timestamp; } | |||||
const std::vector<CInv> &GetInvs() const { return invs; } | |||||
}; | |||||
std::atomic<uint32_t> round; | |||||
RWCollection<std::set<NodeId>> nodeids; | |||||
RWCollection<std::map<NodeId, RequestRecord>> queries; | |||||
/** | |||||
* Start stop machinery. | * Start stop machinery. | ||||
*/ | */ | ||||
std::atomic<bool> stopRequest; | std::atomic<bool> stopRequest; | ||||
bool running GUARDED_BY(cs_running); | bool running GUARDED_BY(cs_running); | ||||
CWaitableCriticalSection cs_running; | CWaitableCriticalSection cs_running; | ||||
std::condition_variable cond_running; | std::condition_variable cond_running; | ||||
public: | public: | ||||
AvalancheProcessor() : stopRequest(false), running(false) {} | AvalancheProcessor(CConnman *connmanIn) | ||||
: connman(connmanIn), stopRequest(false), running(false) {} | |||||
~AvalancheProcessor() { stopEventLoop(); } | ~AvalancheProcessor() { stopEventLoop(); } | ||||
bool addBlockToReconcile(const CBlockIndex *pindex); | bool addBlockToReconcile(const CBlockIndex *pindex); | ||||
bool isAccepted(const CBlockIndex *pindex) const; | bool isAccepted(const CBlockIndex *pindex) const; | ||||
bool registerVotes(const AvalancheResponse &response, | bool registerVotes(NodeId nodeid, const AvalancheResponse &response, | ||||
std::vector<AvalancheBlockUpdate> &updates); | std::vector<AvalancheBlockUpdate> &updates); | ||||
bool startEventLoop(CScheduler &scheduler); | bool startEventLoop(CScheduler &scheduler); | ||||
bool stopEventLoop(); | bool stopEventLoop(); | ||||
private: | private: | ||||
void runEventLoop(); | |||||
std::vector<CInv> getInvsForNextPoll() const; | std::vector<CInv> getInvsForNextPoll() const; | ||||
NodeId getSuitableNodeToQuery(); | |||||
friend struct AvalancheTest; | friend struct AvalancheTest; | ||||
}; | }; | ||||
#endif // BITCOIN_AVALANCHE_H | #endif // BITCOIN_AVALANCHE_H |