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

Method GetStateFor

src/versionbits.cpp:23–110  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 2

UpdateTipFunction · 0.45
VersionBitsStateFunction · 0.45

Calls 6

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

Tested by

no test coverage detected