Dogecoin Core  1.14.2
P2P Digital Currency
versionbits.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016 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 #include "versionbits.h"
6 
7 #include "consensus/params.h"
8 
10  {
11  /*.name =*/ "testdummy",
12  /*.gbt_force =*/ true,
13  },
14  {
15  /*.name =*/ "csv",
16  /*.gbt_force =*/ true,
17  },
18  {
19  /*.name =*/ "segwit",
20  /*.gbt_force =*/ true,
21  }
22 };
23 
25 {
26  int nPeriod = Period(params);
27  int nThreshold = Threshold(params);
28  int64_t nTimeStart = BeginTime(params);
29  int64_t nTimeTimeout = EndTime(params);
30 
31  // A block's state is always the same as that of the first of its period, so it is computed based on a pindexPrev whose height equals a multiple of nPeriod - 1.
32  if (pindexPrev != NULL) {
33  pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod));
34  }
35 
36  // Walk backwards in steps of nPeriod to find a pindexPrev whose information is known
37  std::vector<const CBlockIndex*> vToCompute;
38  while (cache.count(pindexPrev) == 0) {
39  if (pindexPrev == NULL) {
40  // The genesis block is by definition defined.
41  cache[pindexPrev] = THRESHOLD_DEFINED;
42  break;
43  }
44  if (pindexPrev->GetMedianTimePast() < nTimeStart) {
45  // Optimization: don't recompute down further, as we know every earlier block will be before the start time
46  cache[pindexPrev] = THRESHOLD_DEFINED;
47  break;
48  }
49  vToCompute.push_back(pindexPrev);
50  pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
51  }
52 
53  // At this point, cache[pindexPrev] is known
54  assert(cache.count(pindexPrev));
55  ThresholdState state = cache[pindexPrev];
56 
57  // Now walk forward and compute the state of descendants of pindexPrev
58  while (!vToCompute.empty()) {
59  ThresholdState stateNext = state;
60  pindexPrev = vToCompute.back();
61  vToCompute.pop_back();
62 
63  switch (state) {
64  case THRESHOLD_DEFINED: {
65  if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) {
66  stateNext = THRESHOLD_FAILED;
67  } else if (pindexPrev->GetMedianTimePast() >= nTimeStart) {
68  stateNext = THRESHOLD_STARTED;
69  }
70  break;
71  }
72  case THRESHOLD_STARTED: {
73  if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) {
74  stateNext = THRESHOLD_FAILED;
75  break;
76  }
77  // We need to count
78  const CBlockIndex* pindexCount = pindexPrev;
79  int count = 0;
80  for (int i = 0; i < nPeriod; i++) {
81  if (Condition(pindexCount, params)) {
82  count++;
83  }
84  pindexCount = pindexCount->pprev;
85  }
86  if (count >= nThreshold) {
87  stateNext = THRESHOLD_LOCKED_IN;
88  }
89  break;
90  }
91  case THRESHOLD_LOCKED_IN: {
92  // Always progresses into ACTIVE.
93  stateNext = THRESHOLD_ACTIVE;
94  break;
95  }
96  case THRESHOLD_FAILED:
97  case THRESHOLD_ACTIVE: {
98  // Nothing happens, these are terminal states.
99  break;
100  }
101  }
102  cache[pindexPrev] = state = stateNext;
103  }
104 
105  return state;
106 }
107 
109 {
110  const ThresholdState initialState = GetStateFor(pindexPrev, params, cache);
111 
112  // BIP 9 about state DEFINED: "The genesis block is by definition in this state for each deployment."
113  if (initialState == THRESHOLD_DEFINED) {
114  return 0;
115  }
116 
117  const int nPeriod = Period(params);
118 
119  // A block's state is always the same as that of the first of its period, so it is computed based on a pindexPrev whose height equals a multiple of nPeriod - 1.
120  // To ease understanding of the following height calculation, it helps to remember that
121  // right now pindexPrev points to the block prior to the block that we are computing for, thus:
122  // if we are computing for the last block of a period, then pindexPrev points to the second to last block of the period, and
123  // if we are computing for the first block of a period, then pindexPrev points to the last block of the previous period.
124  // The parent of the genesis block is represented by NULL.
125  pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod));
126 
127  const CBlockIndex* previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
128 
129  while (previousPeriodParent != NULL && GetStateFor(previousPeriodParent, params, cache) == initialState) {
130  pindexPrev = previousPeriodParent;
131  previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
132  }
133 
134  // Adjust the result because right now we point to the parent block.
135  return pindexPrev->nHeight + 1;
136 }
137 
138 namespace
139 {
143 class VersionBitsConditionChecker : public AbstractThresholdConditionChecker {
144 private:
145  const Consensus::DeploymentPos id;
146 
147 protected:
148  int64_t BeginTime(const Consensus::Params& params) const { return params.vDeployments[id].nStartTime; }
149  int64_t EndTime(const Consensus::Params& params) const { return params.vDeployments[id].nTimeout; }
150  int Period(const Consensus::Params& params) const { return params.nMinerConfirmationWindow; }
151  int Threshold(const Consensus::Params& params) const { return params.nRuleChangeActivationThreshold; }
152 
153  bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const
154  {
155  return (((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) && (pindex->nVersion & Mask(params)) != 0);
156  }
157 
158 public:
159  VersionBitsConditionChecker(Consensus::DeploymentPos id_) : id(id_) {}
160  uint32_t Mask(const Consensus::Params& params) const { return ((uint32_t)1) << params.vDeployments[id].bit; }
161 };
162 
163 }
164 
166 {
167  return VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, cache.caches[pos]);
168 }
169 
171 {
172  return VersionBitsConditionChecker(pos).GetStateSinceHeightFor(pindexPrev, params, cache.caches[pos]);
173 }
174 
176 {
177  return VersionBitsConditionChecker(pos).Mask(params);
178 }
179 
181 {
182  for (unsigned int d = 0; d < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; d++) {
183  caches[d].clear();
184  }
185 }
Abstract class that implements BIP9-style threshold logic, and caches results.
Definition: versionbits.h:45
virtual bool Condition(const CBlockIndex *pindex, const Consensus::Params &params) const =0
virtual int Period(const Consensus::Params &params) const =0
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, const Consensus::Params &params, ThresholdConditionCache &cache) const
Definition: versionbits.cpp:24
virtual int64_t EndTime(const Consensus::Params &params) const =0
int GetStateSinceHeightFor(const CBlockIndex *pindexPrev, const Consensus::Params &params, ThresholdConditionCache &cache) const
virtual int64_t BeginTime(const Consensus::Params &params) const =0
virtual int Threshold(const Consensus::Params &params) const =0
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:164
int nVersion
block header
Definition: chain.h:197
int64_t GetMedianTimePast() const
Definition: chain.h:285
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:112
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:170
DeploymentPos
Definition: params.h:16
@ MAX_VERSION_BITS_DEPLOYMENTS
Definition: params.h:21
int bit
Bit position to select the particular bit in nVersion.
Definition: params.h:29
int64_t nTimeout
Timeout/expiry MedianTime for the deployment attempt.
Definition: params.h:33
int64_t nStartTime
Start MedianTime for version bits miner confirmation.
Definition: params.h:31
Parameters that influence chain consensus.
Definition: params.h:39
uint32_t nMinerConfirmationWindow
Definition: params.h:59
BIP9Deployment vDeployments[MAX_VERSION_BITS_DEPLOYMENTS]
Definition: params.h:60
uint32_t nRuleChangeActivationThreshold
Minimum blocks including miner confirmation of the total of 2016 blocks in a retargeting period,...
Definition: params.h:58
ThresholdConditionCache caches[Consensus::MAX_VERSION_BITS_DEPLOYMENTS]
Definition: versionbits.h:61
uint32_t VersionBitsMask(const Consensus::Params &params, Consensus::DeploymentPos pos)
const struct BIP9DeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS]
Definition: versionbits.cpp:9
int VersionBitsStateSinceHeight(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
ThresholdState VersionBitsState(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
std::map< const CBlockIndex *, ThresholdState > ThresholdConditionCache
Definition: versionbits.h:31
ThresholdState
Definition: versionbits.h:20
@ THRESHOLD_LOCKED_IN
Definition: versionbits.h:23
@ THRESHOLD_DEFINED
Definition: versionbits.h:21
@ THRESHOLD_ACTIVE
Definition: versionbits.h:24
@ THRESHOLD_STARTED
Definition: versionbits.h:22
@ THRESHOLD_FAILED
Definition: versionbits.h:25