Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/processor.h
- This file was moved from src/avalanche/avalanche.h.
// Copyright (c) 2018-2019 The Bitcoin developers | // Copyright (c) 2018-2019 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_AVALANCHE_H | #ifndef BITCOIN_AVALANCHE_PROCESSOR_H | ||||
#define BITCOIN_AVALANCHE_AVALANCHE_H | #define BITCOIN_AVALANCHE_PROCESSOR_H | ||||
#include <avalanche/protocol.h> | |||||
#include <blockindexworkcomparator.h> | #include <blockindexworkcomparator.h> | ||||
#include <eventloop.h> | #include <eventloop.h> | ||||
#include <key.h> | #include <key.h> | ||||
#include <net.h> | #include <net.h> | ||||
#include <protocol.h> // for CInv | |||||
#include <rwcollection.h> | #include <rwcollection.h> | ||||
#include <serialize.h> | |||||
#include <uint256.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/member.hpp> | #include <boost/multi_index/member.hpp> | ||||
#include <boost/multi_index/ordered_index.hpp> | #include <boost/multi_index/ordered_index.hpp> | ||||
#include <boost/multi_index_container.hpp> | #include <boost/multi_index_container.hpp> | ||||
#include <atomic> | #include <atomic> | ||||
▲ Show 20 Lines • Show All 114 Lines • ▼ Show 20 Lines | private: | ||||
/** | /** | ||||
* Add the node to the quorum. | * Add the node to the quorum. | ||||
* Returns true if the node was added, false if the node already was in the | * Returns true if the node was added, false if the node already was in the | ||||
* quorum. | * quorum. | ||||
*/ | */ | ||||
bool addNodeToQuorum(NodeId nodeid); | bool addNodeToQuorum(NodeId nodeid); | ||||
}; | }; | ||||
class AvalancheVote { | |||||
uint32_t error; | |||||
uint256 hash; | |||||
public: | |||||
AvalancheVote() : error(-1), hash() {} | |||||
AvalancheVote(uint32_t errorIn, uint256 hashIn) | |||||
: error(errorIn), hash(hashIn) {} | |||||
const uint256 &GetHash() const { return hash; } | |||||
uint32_t GetError() const { return error; } | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(error); | |||||
READWRITE(hash); | |||||
} | |||||
}; | |||||
class AvalancheResponse { | |||||
uint64_t round; | |||||
uint32_t cooldown; | |||||
std::vector<AvalancheVote> votes; | |||||
public: | |||||
AvalancheResponse() : round(-1), cooldown(-1) {} | |||||
AvalancheResponse(uint64_t roundIn, uint32_t cooldownIn, | |||||
std::vector<AvalancheVote> votesIn) | |||||
: round(roundIn), cooldown(cooldownIn), votes(votesIn) {} | |||||
uint64_t getRound() const { return round; } | |||||
uint32_t getCooldown() const { return cooldown; } | |||||
const std::vector<AvalancheVote> &GetVotes() const { return votes; } | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(round); | |||||
READWRITE(cooldown); | |||||
READWRITE(votes); | |||||
} | |||||
}; | |||||
class AvalanchePoll { | |||||
uint64_t round; | |||||
std::vector<CInv> invs; | |||||
public: | |||||
AvalanchePoll(uint64_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; | ||||
uintptr_t raw; | uintptr_t raw; | ||||
}; | }; | ||||
static const size_t STATUS_BITS = 2; | static const size_t STATUS_BITS = 2; | ||||
static const uintptr_t MASK = (1 << STATUS_BITS) - 1; | static const uintptr_t MASK = (1 << STATUS_BITS) - 1; | ||||
▲ Show 20 Lines • Show All 115 Lines • ▼ Show 20 Lines | public: | ||||
void setQueryTimeoutDuration(std::chrono::milliseconds d) { | void setQueryTimeoutDuration(std::chrono::milliseconds d) { | ||||
queryTimeoutDuration = d; | queryTimeoutDuration = d; | ||||
} | } | ||||
bool addBlockToReconcile(const CBlockIndex *pindex); | bool addBlockToReconcile(const CBlockIndex *pindex); | ||||
bool isAccepted(const CBlockIndex *pindex) const; | bool isAccepted(const CBlockIndex *pindex) const; | ||||
int getConfidence(const CBlockIndex *pindex) const; | int getConfidence(const CBlockIndex *pindex) const; | ||||
// TDOD: Refactor the API to remove the dependency on avalanche/protocol.h | |||||
void sendResponse(CNode *pfrom, AvalancheResponse response) const; | void sendResponse(CNode *pfrom, AvalancheResponse response) const; | ||||
bool registerVotes(NodeId nodeid, const AvalancheResponse &response, | bool registerVotes(NodeId nodeid, const AvalancheResponse &response, | ||||
std::vector<AvalancheBlockUpdate> &updates); | std::vector<AvalancheBlockUpdate> &updates); | ||||
bool addPeer(NodeId nodeid, int64_t score, CPubKey pubkey); | bool addPeer(NodeId nodeid, int64_t score, CPubKey pubkey); | ||||
CPubKey getPubKey(NodeId nodeid) const; | CPubKey getPubKey(NodeId nodeid) const; | ||||
CPubKey getSessionPubKey() const { return sessionKey.GetPubKey(); } | CPubKey getSessionPubKey() const { return sessionKey.GetPubKey(); } | ||||
Show All 9 Lines | private: | ||||
friend struct AvalancheTest; | friend struct AvalancheTest; | ||||
}; | }; | ||||
/** | /** | ||||
* Global avalanche instance. | * Global avalanche instance. | ||||
*/ | */ | ||||
extern std::unique_ptr<AvalancheProcessor> g_avalanche; | extern std::unique_ptr<AvalancheProcessor> g_avalanche; | ||||
#endif // BITCOIN_AVALANCHE_AVALANCHE_H | #endif // BITCOIN_AVALANCHE_PROCESSOR_H |