Bitcoin
versionbits.h
Go to the documentation of this file.
1 // Copyright (c) 2016-2018 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_VERSIONBITS_H
6 #define BITCOIN_VERSIONBITS_H
7 
8 #include <chain.h>
9 #include <map>
10 
14 static const int32_t VERSIONBITS_TOP_BITS = 0x20000000UL;
16 static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
18 static const int32_t VERSIONBITS_NUM_BITS = 29;
19 
20 enum class ThresholdState {
21  DEFINED,
22  STARTED,
23  LOCKED_IN,
24  ACTIVE,
25  FAILED,
26 };
27 
28 // A map that gives the state for blocks whose height is a multiple of Period().
29 // The map is indexed by the block's parent, however, so all keys in the map
30 // will either be nullptr or a block with (height + 1) % Period() == 0.
31 typedef std::map<const CBlockIndex*, ThresholdState> ThresholdConditionCache;
32 
33 struct BIP9Stats {
34  int period;
35  int threshold;
36  int elapsed;
37  int count;
38  bool possible;
39 };
40 
45 protected:
46  virtual bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const =0;
47  virtual int64_t BeginTime(const Consensus::Params& params) const =0;
48  virtual int64_t EndTime(const Consensus::Params& params) const =0;
49  virtual int Period(const Consensus::Params& params) const =0;
50  virtual int Threshold(const Consensus::Params& params) const =0;
51 
52 public:
53  BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, const Consensus::Params& params) const;
54  // Note that the functions below take a pindexPrev as input: they compute information for block B based on its parent.
55  ThresholdState GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
56  int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const;
57 };
58 
60 {
62 
63  void Clear();
64 };
65 
70 
71 #endif // BITCOIN_VERSIONBITS_H
virtual bool Condition(const CBlockIndex *pindex, const Consensus::Params &params) const =0
bool possible
Definition: versionbits.h:38
int elapsed
Definition: versionbits.h:36
virtual int Period(const Consensus::Params &params) const =0
int period
Definition: versionbits.h:34
static const int32_t VERSIONBITS_TOP_MASK
Definition: versionbits.h:16
virtual int64_t BeginTime(const Consensus::Params &params) const =0
ThresholdConditionCache caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS]
Definition: versionbits.h:61
int count
Definition: versionbits.h:37
Definition: params.h:49
Definition: versionbits.h:44
ThresholdState
Definition: versionbits.h:20
void Clear()
Definition: versionbits.cpp:209
Definition: versionbits.h:59
DeploymentPos
Definition: params.h:16
BIP9Stats VersionBitsStatistics(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos)
Definition: versionbits.cpp:194
int threshold
Definition: versionbits.h:35
virtual int Threshold(const Consensus::Params &params) const =0
unsigned int uint32_t
Definition: stdint.h:21
ThresholdState VersionBitsState(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
Definition: versionbits.cpp:189
Definition: versionbits.h:33
std::map< const CBlockIndex *, ThresholdState > ThresholdConditionCache
Definition: versionbits.h:31
int VersionBitsStateSinceHeight(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
Definition: versionbits.cpp:199
static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION
Definition: versionbits.h:12
static const int32_t VERSIONBITS_TOP_BITS
Definition: versionbits.h:14
BIP9Stats GetStateStatisticsFor(const CBlockIndex *pindex, const Consensus::Params &params) const
Definition: versionbits.cpp:98
Definition: chain.h:139
signed long long int64_t
Definition: stdint.h:18
int GetStateSinceHeightFor(const CBlockIndex *pindexPrev, const Consensus::Params &params, ThresholdConditionCache &cache) const
Definition: versionbits.cpp:127
signed int int32_t
Definition: stdint.h:17
uint32_t VersionBitsMask(const Consensus::Params &params, Consensus::DeploymentPos pos)
Definition: versionbits.cpp:204
static const int32_t VERSIONBITS_NUM_BITS
Definition: versionbits.h:18
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, const Consensus::Params &params, ThresholdConditionCache &cache) const
Definition: versionbits.cpp:8
virtual int64_t EndTime(const Consensus::Params &params) const =0