MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / GetStateFor

Method GetStateFor

src/versionbits.cpp:7–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include "versionbits.h"
6
7ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, const Consensus::Params& params, ThresholdConditionCache& cache) const
8{
9 int nPeriod = Period(params);
10 int nThreshold = Threshold(params);
11 int64_t nTimeStart = BeginTime(params);
12 int64_t nTimeTimeout = EndTime(params);
13
14 // 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.
15 if (pindexPrev != NULL) {
16 pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod));
17 }
18
19 // Walk backwards in steps of nPeriod to find a pindexPrev whose information is known
20 std::vector<const CBlockIndex*> vToCompute;
21 while (cache.count(pindexPrev) == 0) {
22 if (pindexPrev == NULL) {
23 // The genesis block is by definition defined.
24 cache[pindexPrev] = THRESHOLD_DEFINED;
25 break;
26 }
27 if (pindexPrev->GetMedianTimePast() < nTimeStart) {
28 // Optimizaton: don't recompute down further, as we know every earlier block will be before the start time
29 cache[pindexPrev] = THRESHOLD_DEFINED;
30 break;
31 }
32 vToCompute.push_back(pindexPrev);
33 pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
34 }
35
36 // At this point, cache[pindexPrev] is known
37 assert(cache.count(pindexPrev));
38 ThresholdState state = cache[pindexPrev];
39
40 // Now walk forward and compute the state of descendants of pindexPrev
41 while (!vToCompute.empty()) {
42 ThresholdState stateNext = state;
43 pindexPrev = vToCompute.back();
44 vToCompute.pop_back();
45
46 switch (state) {
47 case THRESHOLD_DEFINED: {
48 if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) {
49 stateNext = THRESHOLD_FAILED;
50 } else if (pindexPrev->GetMedianTimePast() >= nTimeStart) {
51 stateNext = THRESHOLD_STARTED;
52 }
53 break;
54 }
55 case THRESHOLD_STARTED: {
56 if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) {
57 stateNext = THRESHOLD_FAILED;
58 break;
59 }
60 // We need to count
61 const CBlockIndex* pindexCount = pindexPrev;
62 int count = 0;
63 for (int i = 0; i < nPeriod; i++) {
64 if (Condition(pindexCount, params)) {

Callers 2

VersionBitsStateFunction · 0.45
UpdateTipFunction · 0.45

Calls 5

GetAncestorMethod · 0.80
countMethod · 0.80
GetMedianTimePastMethod · 0.80
push_backMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected