| 140 | } |
| 141 | |
| 142 | int AbstractThresholdConditionChecker::GetStateSinceHeightFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const |
| 143 | { |
| 144 | int64_t start_time = BeginTime(params); |
| 145 | if (start_time == Consensus::BIP9Deployment::ALWAYS_ACTIVE) { |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | const ThresholdState initialState = GetStateFor(pindexPrev, params, cache); |
| 150 | |
| 151 | // BIP 9 about state DEFINED: "The genesis block is by definition in this state for each deployment." |
| 152 | if (initialState == ThresholdState::DEFINED) { |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | const int nPeriod = Period(params); |
| 157 | |
| 158 | // 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. |
| 159 | // To ease understanding of the following height calculation, it helps to remember that |
| 160 | // right now pindexPrev points to the block prior to the block that we are computing for, thus: |
| 161 | // if we are computing for the last block of a period, then pindexPrev points to the second to last block of the period, and |
| 162 | // if we are computing for the first block of a period, then pindexPrev points to the last block of the previous period. |
| 163 | // The parent of the genesis block is represented by nullptr. |
| 164 | pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod)); |
| 165 | |
| 166 | const CBlockIndex* previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); |
| 167 | |
| 168 | while (previousPeriodParent != nullptr && GetStateFor(previousPeriodParent, params, cache) == initialState) { |
| 169 | pindexPrev = previousPeriodParent; |
| 170 | previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); |
| 171 | } |
| 172 | |
| 173 | // Adjust the result because right now we point to the parent block. |
| 174 | return pindexPrev->nHeight + 1; |
| 175 | } |
| 176 | |
| 177 | namespace |
| 178 | { |
no test coverage detected