MCPcopy Create free account
hub / github.com/ElementsProject/elements / GetStateSinceHeightFor

Method GetStateSinceHeightFor

src/versionbits.cpp:150–183  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

StateSinceHeightMethod · 0.45

Calls 1

GetAncestorMethod · 0.80

Tested by

no test coverage detected