Branch data Line data Source code
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 : : #include <consensus/params.h> 6 : : #include <util/check.h> 7 : : #include <versionbits.h> 8 : : 9 : 138754 : ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const 10 : : { 11 : 138754 : int nPeriod = Period(params); 12 : 138754 : int nThreshold = Threshold(params); 13 : 138754 : int min_activation_height = MinActivationHeight(params); 14 : 138754 : int64_t nTimeStart = BeginTime(params); 15 : 138754 : int64_t nTimeTimeout = EndTime(params); 16 : : 17 : : // Check if this deployment is always active. 18 [ + + ]: 138754 : if (nTimeStart == Consensus::BIP9Deployment::ALWAYS_ACTIVE) { 19 : 54617 : return ThresholdState::ACTIVE; 20 : : } 21 : : 22 : : // Check if this deployment is never active. 23 [ + + ]: 84137 : if (nTimeStart == Consensus::BIP9Deployment::NEVER_ACTIVE) { 24 : 594 : return ThresholdState::FAILED; 25 : : } 26 : : 27 : : // 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. 28 [ + + ]: 83543 : if (pindexPrev != nullptr) { 29 : 83521 : pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod)); 30 : 83521 : } 31 : : 32 : : // Walk backwards in steps of nPeriod to find a pindexPrev whose information is known 33 : 83543 : std::vector<const CBlockIndex*> vToCompute; 34 [ + - + + ]: 84725 : while (cache.count(pindexPrev) == 0) { 35 [ + + ]: 1893 : if (pindexPrev == nullptr) { 36 : : // The genesis block is by definition defined. 37 [ + - ]: 666 : cache[pindexPrev] = ThresholdState::DEFINED; 38 : 666 : break; 39 : : } 40 [ + - + + ]: 1227 : if (pindexPrev->GetMedianTimePast() < nTimeStart) { 41 : : // Optimization: don't recompute down further, as we know every earlier block will be before the start time 42 [ + - ]: 45 : cache[pindexPrev] = ThresholdState::DEFINED; 43 : 45 : break; 44 : : } 45 [ + - ]: 1182 : vToCompute.push_back(pindexPrev); 46 [ + - ]: 1182 : pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); 47 : : } 48 : : 49 : : // At this point, cache[pindexPrev] is known 50 [ + - - + ]: 83543 : assert(cache.count(pindexPrev)); 51 [ + - ]: 83543 : ThresholdState state = cache[pindexPrev]; 52 : : 53 : : // Now walk forward and compute the state of descendants of pindexPrev 54 [ + + ]: 84725 : while (!vToCompute.empty()) { 55 : 1182 : ThresholdState stateNext = state; 56 : 1182 : pindexPrev = vToCompute.back(); 57 : 1182 : vToCompute.pop_back(); 58 : : 59 [ + + - + : 1182 : switch (state) { + ] 60 : : case ThresholdState::DEFINED: { 61 [ + - - + ]: 185 : if (pindexPrev->GetMedianTimePast() >= nTimeStart) { 62 : 185 : stateNext = ThresholdState::STARTED; 63 : 185 : } 64 : 185 : break; 65 : : } 66 : : case ThresholdState::STARTED: { 67 : : // We need to count 68 : 194 : const CBlockIndex* pindexCount = pindexPrev; 69 : 194 : int count = 0; 70 [ + + ]: 7186 : for (int i = 0; i < nPeriod; i++) { 71 [ + - + + ]: 6992 : if (Condition(pindexCount, params)) { 72 : 2433 : count++; 73 : 2433 : } 74 : 7165 : pindexCount = pindexCount->pprev; 75 : 6992 : } 76 [ + + ]: 194 : if (count >= nThreshold) { 77 : 48 : stateNext = ThresholdState::LOCKED_IN; 78 [ + - + + ]: 194 : } else if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) { 79 : 21 : stateNext = ThresholdState::FAILED; 80 : 21 : } 81 : 194 : break; 82 : : } 83 : : case ThresholdState::LOCKED_IN: { 84 : : // Progresses into ACTIVE provided activation height will have been reached. 85 [ + + ]: 179 : if (pindexPrev->nHeight + 1 >= min_activation_height) { 86 : 37 : stateNext = ThresholdState::ACTIVE; 87 : 37 : } 88 : 179 : break; 89 : : } 90 : : case ThresholdState::FAILED: 91 : : case ThresholdState::ACTIVE: { 92 : : // Nothing happens, these are terminal states. 93 : 624 : break; 94 : : } 95 : : } 96 [ + - ]: 1182 : cache[pindexPrev] = state = stateNext; 97 : : } 98 : : 99 : 83543 : return state; 100 : 138754 : } 101 : : 102 : 6993 : BIP9Stats AbstractThresholdConditionChecker::GetStateStatisticsFor(const CBlockIndex* pindex, const Consensus::Params& params, std::vector<bool>* signalling_blocks) const 103 : : { 104 : 6993 : BIP9Stats stats = {}; 105 : : 106 : 6993 : stats.period = Period(params); 107 : 6993 : stats.threshold = Threshold(params); 108 : : 109 [ - + ]: 6993 : if (pindex == nullptr) return stats; 110 : : 111 : : // Find how many blocks are in the current period 112 : 6993 : int blocks_in_period = 1 + (pindex->nHeight % stats.period); 113 : : 114 : : // Reset signalling_blocks 115 [ + + ]: 6993 : if (signalling_blocks) { 116 : 3441 : signalling_blocks->assign(blocks_in_period, false); 117 : 3441 : } 118 : : 119 : : // Count from current block to beginning of period 120 : 6993 : int elapsed = 0; 121 : 6993 : int count = 0; 122 : 6993 : const CBlockIndex* currentIndex = pindex; 123 : 6993 : do { 124 : 113664 : ++elapsed; 125 : 113664 : --blocks_in_period; 126 [ + + ]: 113664 : if (Condition(currentIndex, params)) { 127 : 49637 : ++count; 128 [ + + ]: 49637 : if (signalling_blocks) signalling_blocks->at(blocks_in_period) = true; 129 : 49637 : } 130 : 113664 : currentIndex = currentIndex->pprev; 131 [ + + ]: 113664 : } while(blocks_in_period > 0); 132 : : 133 : 6993 : stats.elapsed = elapsed; 134 : 6993 : stats.count = count; 135 : 6993 : stats.possible = (stats.period - stats.threshold ) >= (stats.elapsed - count); 136 : : 137 : 6993 : return stats; 138 : 6993 : } 139 : : 140 : 3669 : int AbstractThresholdConditionChecker::GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const 141 : : { 142 : 3669 : int64_t start_time = BeginTime(params); 143 [ + + + + ]: 3669 : if (start_time == Consensus::BIP9Deployment::ALWAYS_ACTIVE || start_time == Consensus::BIP9Deployment::NEVER_ACTIVE) { 144 : 862 : return 0; 145 : : } 146 : : 147 : 2807 : const ThresholdState initialState = GetStateFor(pindexPrev, params, cache); 148 : : 149 : : // BIP 9 about state DEFINED: "The genesis block is by definition in this state for each deployment." 150 [ + + ]: 2807 : if (initialState == ThresholdState::DEFINED) { 151 : 521 : return 0; 152 : : } 153 : : 154 : 2286 : const int nPeriod = Period(params); 155 : : 156 : : // 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. 157 : : // To ease understanding of the following height calculation, it helps to remember that 158 : : // right now pindexPrev points to the block prior to the block that we are computing for, thus: 159 : : // if we are computing for the last block of a period, then pindexPrev points to the second to last block of the period, and 160 : : // if we are computing for the first block of a period, then pindexPrev points to the last block of the previous period. 161 : : // The parent of the genesis block is represented by nullptr. 162 : 2286 : pindexPrev = Assert(pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod))); 163 : : 164 : 2286 : const CBlockIndex* previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); 165 : : 166 [ + + + + ]: 23909 : while (previousPeriodParent != nullptr && GetStateFor(previousPeriodParent, params, cache) == initialState) { 167 : 21623 : pindexPrev = previousPeriodParent; 168 : 21623 : previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); 169 : : } 170 : : 171 : : // Adjust the result because right now we point to the parent block. 172 : 2286 : return pindexPrev->nHeight + 1; 173 : 3669 : } 174 : : 175 : : namespace 176 : : { 177 : : /** 178 : : * Class to implement versionbits logic. 179 : : */ 180 : : class VersionBitsConditionChecker : public AbstractThresholdConditionChecker { 181 : : private: 182 : : const Consensus::DeploymentPos id; 183 : : 184 : : protected: 185 : 108714 : int64_t BeginTime(const Consensus::Params& params) const override { return params.vDeployments[id].nStartTime; } 186 : 108708 : int64_t EndTime(const Consensus::Params& params) const override { return params.vDeployments[id].nTimeout; } 187 : 108708 : int MinActivationHeight(const Consensus::Params& params) const override { return params.vDeployments[id].min_activation_height; } 188 : 108708 : int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; } 189 : 108708 : int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; } 190 : : 191 : 1008 : bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override 192 : : { 193 [ - + ]: 1008 : return (((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) && (pindex->nVersion & Mask(params)) != 0); 194 : : } 195 : : 196 : : public: 197 : 118653 : explicit VersionBitsConditionChecker(Consensus::DeploymentPos id_) : id(id_) {} 198 : 10949 : uint32_t Mask(const Consensus::Params& params) const { return (uint32_t{1}) << params.vDeployments[id].bit; } 199 : : }; 200 : : 201 : : } // namespace 202 : : 203 : 8 : ThresholdState VersionBitsCache::State(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) 204 : : { 205 : 8 : LOCK(m_mutex); 206 [ + - + - ]: 8 : return VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, m_caches[pos]); 207 : 8 : } 208 : : 209 : 0 : BIP9Stats VersionBitsCache::Statistics(const CBlockIndex* pindex, const Consensus::Params& params, Consensus::DeploymentPos pos, std::vector<bool>* signalling_blocks) 210 : : { 211 : 0 : return VersionBitsConditionChecker(pos).GetStateStatisticsFor(pindex, params, signalling_blocks); 212 : : } 213 : : 214 : 6 : int VersionBitsCache::StateSinceHeight(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos) 215 : : { 216 : 6 : LOCK(m_mutex); 217 [ + - + - ]: 6 : return VersionBitsConditionChecker(pos).GetStateSinceHeightFor(pindexPrev, params, m_caches[pos]); 218 : 6 : } 219 : : 220 : 9941 : uint32_t VersionBitsCache::Mask(const Consensus::Params& params, Consensus::DeploymentPos pos) 221 : : { 222 : 9941 : return VersionBitsConditionChecker(pos).Mask(params); 223 : : } 224 : : 225 : 54349 : int32_t VersionBitsCache::ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params) 226 : : { 227 : 54349 : LOCK(m_mutex); 228 : 54349 : int32_t nVersion = VERSIONBITS_TOP_BITS; 229 : : 230 [ + + ]: 163047 : for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) { 231 : 108698 : Consensus::DeploymentPos pos = static_cast<Consensus::DeploymentPos>(i); 232 [ + - + - ]: 108698 : ThresholdState state = VersionBitsConditionChecker(pos).GetStateFor(pindexPrev, params, m_caches[pos]); 233 [ + + + + ]: 108698 : if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) { 234 [ + - ]: 9941 : nVersion |= Mask(params, pos); 235 : 9941 : } 236 : 108698 : } 237 : : 238 : 54349 : return nVersion; 239 : 54349 : } 240 : : 241 : 836 : void VersionBitsCache::Clear() 242 : : { 243 : 836 : LOCK(m_mutex); 244 [ + + ]: 2508 : for (unsigned int d = 0; d < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; d++) { 245 : 1672 : m_caches[d].clear(); 246 : 1672 : } 247 : 836 : }