| 16 | } |
| 17 | |
| 18 | ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const |
| 19 | { |
| 20 | int nPeriod = Period(params); |
| 21 | int nThreshold = Threshold(params); |
| 22 | int min_activation_height = MinActivationHeight(params); |
| 23 | // ELEMENTS: We interpret this as block height, not block time! |
| 24 | int64_t nTimeStart = BeginTime(params); |
| 25 | int64_t nTimeTimeout = EndTime(params); |
| 26 | |
| 27 | // Check if this deployment is always active. |
| 28 | if (nTimeStart == Consensus::BIP9Deployment::ALWAYS_ACTIVE) { |
| 29 | return ThresholdState::ACTIVE; |
| 30 | } |
| 31 | |
| 32 | // Check if this deployment is never active. |
| 33 | if (nTimeStart == Consensus::BIP9Deployment::NEVER_ACTIVE) { |
| 34 | return ThresholdState::FAILED; |
| 35 | } |
| 36 | |
| 37 | // 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. |
| 38 | if (pindexPrev != nullptr) { |
| 39 | pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod)); |
| 40 | } |
| 41 | |
| 42 | // Walk backwards in steps of nPeriod to find a pindexPrev whose information is known |
| 43 | std::vector<const CBlockIndex*> vToCompute; |
| 44 | while (cache.count(pindexPrev) == 0) { |
| 45 | if (pindexPrev == nullptr) { |
| 46 | // The genesis block is by definition defined. |
| 47 | cache[pindexPrev] = ThresholdState::DEFINED; |
| 48 | break; |
| 49 | } |
| 50 | if (GetBIP9Time(pindexPrev, params) < nTimeStart) { |
| 51 | // Optimization: don't recompute down further, as we know every earlier block will be before the start time |
| 52 | cache[pindexPrev] = ThresholdState::DEFINED; |
| 53 | break; |
| 54 | } |
| 55 | vToCompute.push_back(pindexPrev); |
| 56 | pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod); |
| 57 | } |
| 58 | |
| 59 | // At this point, cache[pindexPrev] is known |
| 60 | assert(cache.count(pindexPrev)); |
| 61 | ThresholdState state = cache[pindexPrev]; |
| 62 | |
| 63 | // Now walk forward and compute the state of descendants of pindexPrev |
| 64 | while (!vToCompute.empty()) { |
| 65 | ThresholdState stateNext = state; |
| 66 | pindexPrev = vToCompute.back(); |
| 67 | vToCompute.pop_back(); |
| 68 | |
| 69 | switch (state) { |
| 70 | case ThresholdState::DEFINED: { |
| 71 | if (GetBIP9Time(pindexPrev, params) >= nTimeStart) { |
| 72 | stateNext = ThresholdState::STARTED; |
| 73 | } |
| 74 | break; |
| 75 | } |
no test coverage detected