Bitcoin Core 29.99.0
P2P Digital Currency
versionbits_impl.h
Go to the documentation of this file.
1// Copyright (c) 2016-2022 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
17enum 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
26std::string StateName(ThresholdState state);
27
32protected:
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
40public:
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
58private:
60
61protected:
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
73public:
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
Abstract class that implements BIP9-style threshold logic, and caches results.
virtual bool Condition(const CBlockIndex *pindex) const =0
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, ThresholdConditionCache &cache) const
Returns the state for pindex A based on parent pindexPrev B.
Definition: versionbits.cpp:26
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 int MinActivationHeight() const
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...
virtual int Period() const =0
virtual ~AbstractThresholdConditionChecker()=default
virtual int Threshold() const =0
virtual int64_t BeginTime() const =0
virtual int64_t EndTime() const =0
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:141
int32_t nVersion
block header
Definition: chain.h:187
Class to implement versionbits logic.
int Threshold() const override
int64_t EndTime() const override
bool Condition(const CBlockIndex *pindex) const override
const Consensus::BIP9Deployment & dep
VersionBitsConditionChecker(const Consensus::BIP9Deployment &dep)
int Period() const override
VersionBitsConditionChecker(const Consensus::Params &params, Consensus::DeploymentPos id)
int64_t BeginTime() const override
bool Condition(int32_t nVersion) const
int MinActivationHeight() const override
DeploymentPos
Definition: params.h:33
Display status of an in-progress BIP9 softfork.
Definition: versionbits.h:36
Struct for each individual consensus rule change using BIP9.
Definition: params.h:44
int min_activation_height
If lock in occurs, delay activation until at least this block height.
Definition: params.h:55
int bit
Bit position to select the particular bit in nVersion.
Definition: params.h:46
uint32_t period
Period of blocks to check signalling in (usually retarget period, ie params.DifficultyAdjustmentInter...
Definition: params.h:57
uint32_t threshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,...
Definition: params.h:63
int64_t nTimeout
Timeout/expiry MedianTime for the deployment attempt.
Definition: params.h:50
int64_t nStartTime
Start MedianTime for version bits miner confirmation.
Definition: params.h:48
Parameters that influence chain consensus.
Definition: params.h:83
std::map< const CBlockIndex *, ThresholdState > ThresholdConditionCache
Definition: versionbits.h:33
static const int32_t VERSIONBITS_TOP_BITS
What bits to set in version for versionbits blocks.
Definition: versionbits.h:21
static const int32_t VERSIONBITS_TOP_MASK
What bitmask determines whether versionbits is in use.
Definition: versionbits.h:23
ThresholdState
BIP 9 defines a finite-state-machine to deploy a softfork in multiple stages.
std::string StateName(ThresholdState state)
Get a string with the state name.
Definition: versionbits.cpp:14