Bitcoin Core  31.0.0
P2P Digital Currency
versionbits_impl.h
Go to the documentation of this file.
1 // Copyright (c) 2016-present 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_IMPL_H
6 #define BITCOIN_VERSIONBITS_IMPL_H
7 
8 #include <chain.h>
9 #include <sync.h>
10 #include <versionbits.h>
11 
17 enum class ThresholdState : uint8_t {
18  DEFINED, // First state that each softfork starts out as. The genesis block is by definition in this state for each deployment.
19  STARTED, // For blocks past the starttime.
20  LOCKED_IN, // For at least one retarget period after the first retarget period with STARTED blocks of which at least threshold have the associated bit set in nVersion, until min_activation_height is reached.
21  ACTIVE, // For all blocks after the LOCKED_IN retarget period (final state)
22  FAILED, // For all blocks once the first retarget period after the timeout time is hit, if LOCKED_IN wasn't already reached (final state)
23 };
24 
26 std::string StateName(ThresholdState state);
27 
32 protected:
33  virtual bool Condition(const CBlockIndex* pindex) const =0;
34  virtual int64_t BeginTime() const =0;
35  virtual int64_t EndTime() const =0;
36  virtual int MinActivationHeight() const { return 0; }
37  virtual int Period() const =0;
38  virtual int Threshold() const =0;
39 
40 public:
41  virtual ~AbstractThresholdConditionChecker() = default;
42 
46  BIP9Stats GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks = nullptr) const;
49  ThresholdState GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
51  int GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const;
52 };
53 
58 private:
60 
61 protected:
62  int64_t BeginTime() const override { return dep.nStartTime; }
63  int64_t EndTime() const override { return dep.nTimeout; }
64  int MinActivationHeight() const override { return dep.min_activation_height; }
65  int Period() const override { return dep.period; }
66  int Threshold() const override { return dep.threshold; }
67 
68  bool Condition(const CBlockIndex* pindex) const override
69  {
70  return Condition(pindex->nVersion);
71  }
72 
73 public:
76 
77  uint32_t Mask() const { return (uint32_t{1}) << dep.bit; }
78 
79  bool Condition(int32_t nVersion) const
80  {
81  return (((nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) && (nVersion & Mask()) != 0);
82  }
83 };
84 
85 #endif // BITCOIN_VERSIONBITS_IMPL_H
virtual int64_t BeginTime() const =0
uint32_t threshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period...
Definition: params.h:64
int min_activation_height
If lock in occurs, delay activation until at least this block height.
Definition: params.h:56
Opaque type for BIP9 state.
Definition: versionbits.h:36
std::array< BIP9Deployment, MAX_VERSION_BITS_DEPLOYMENTS > vDeployments
Definition: params.h:110
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
DeploymentPos
Definition: params.h:34
static const int32_t VERSIONBITS_TOP_MASK
What bitmask determines whether versionbits is in use.
Definition: versionbits.h:23
int64_t BeginTime() const override
VersionBitsConditionChecker(const Consensus::Params &params, Consensus::DeploymentPos id)
virtual bool Condition(const CBlockIndex *pindex) const =0
int Threshold() const override
uint32_t period
Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInter...
Definition: params.h:58
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the state for pindex A based on parent pindexPrev B.
Definition: versionbits.cpp:26
virtual int MinActivationHeight() const
const Consensus::BIP9Deployment & dep
int MinActivationHeight() const override
Struct for each individual consensus rule change using BIP9.
Definition: params.h:45
Class to implement versionbits logic.
std::string StateName(ThresholdState state)
Get a string with the state name.
Definition: versionbits.cpp:14
int64_t nStartTime
Start MedianTime for version bits miner confirmation.
Definition: params.h:49
Abstract class that implements BIP9-style threshold logic, and caches results.
Parameters that influence chain consensus.
Definition: params.h:84
bool Condition(int32_t nVersion) const
virtual int Period() const =0
virtual int Threshold() const =0
bool Condition(const CBlockIndex *pindex) const override
int64_t EndTime() const override
int32_t nVersion
block header
Definition: chain.h:140
int64_t nTimeout
Timeout/expiry MedianTime for the deployment attempt.
Definition: params.h:51
BIP9Stats GetStateStatisticsFor(const CBlockIndex *pindex, std::vector< bool > *signalling_blocks=nullptr) const
Returns the numerical statistics of an in-progress BIP9 softfork in the period including pindex If pr...
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
Definition: versionbits.h:21
virtual ~AbstractThresholdConditionChecker()=default
int Period() const override
int GetStateSinceHeightFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the height since when the ThresholdState has started for pindex A based on parent pindexPrev ...
virtual int64_t EndTime() const =0
int bit
Bit position to select the particular bit in nVersion.
Definition: params.h:47
VersionBitsConditionChecker(const Consensus::BIP9Deployment &dep)