Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche.h
- This file was moved from src/avalanche_impl.h.
// Copyright (c) 2018 The Bitcoin developers | // Copyright (c) 2018 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_IMPL_H | #ifndef BITCOIN_AVALANCHE_H | ||||
#define BITCOIN_AVALANCHE_IMPL_H | #define BITCOIN_AVALANCHE_H | ||||
#include "net.h" // for NodeId | |||||
#include "protocol.h" // for CInv | |||||
#include "rwcollection.h" | |||||
#include "serialize.h" | |||||
#include "uint256.h" | |||||
#include <cstdint> | #include <cstdint> | ||||
#include <vector> | |||||
class Config; | |||||
class CBlockIndex; | |||||
class CScheduler; | |||||
namespace { | namespace { | ||||
/** | /** | ||||
* Finalization score. | * Finalization score. | ||||
*/ | */ | ||||
static int AVALANCHE_FINALIZATION_SCORE = 128; | static int AVALANCHE_FINALIZATION_SCORE = 128; | ||||
} | |||||
/** | /** | ||||
* Vote history. | * Vote history. | ||||
*/ | */ | ||||
struct VoteRecord { | struct VoteRecord { | ||||
private: | private: | ||||
// Historical record of votes. | // Historical record of votes. | ||||
uint16_t votes; | uint16_t votes; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | bool registerVote(bool vote) { | ||||
// The vote did not agree with our internal state, in that case, | // The vote did not agree with our internal state, in that case, | ||||
// reset confidence. | // reset confidence. | ||||
confidence = yes; | confidence = yes; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
}; | }; | ||||
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; } | |||||
bool IsValid() const { return error == 0; } | |||||
// serialization support | |||||
ADD_SERIALIZE_METHODS; | |||||
template <typename Stream, typename Operation> | |||||
inline void SerializationOp(Stream &s, Operation ser_action) { | |||||
READWRITE(error); | |||||
READWRITE(hash); | |||||
} | |||||
}; | |||||
class AvalancheResponse { | |||||
std::vector<AvalancheVote> votes; | |||||
public: | |||||
AvalancheResponse(std::vector<AvalancheVote> votesIn) : votes(votesIn) {} | |||||
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(votes); | |||||
} | } | ||||
}; | |||||
class AvalancheProcessor { | |||||
private: | |||||
/** | |||||
* Blocks to run avalanche on. | |||||
*/ | |||||
RWCollection<std::map<uint256, VoteRecord>> vote_records; | |||||
public: | |||||
AvalancheProcessor() {} | |||||
bool addBlockToReconcile(const CBlockIndex *pindex); | |||||
bool isAccepted(const CBlockIndex *pindex) const; | |||||
bool hasFinalized(const CBlockIndex *pindex) const; | |||||
bool registerVotes(const AvalancheResponse &response); | |||||
}; | |||||
#endif // BITCOIN_AVALANCHE_IMPL_H | #endif // BITCOIN_AVALANCHE_H |