Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/versionbits.cpp
Line
Count
Source
1
// Copyright (c) 2016-present The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#include <consensus/params.h>
6
#include <deploymentinfo.h>
7
#include <kernel/chainparams.h>
8
#include <util/check.h>
9
#include <versionbits.h>
10
#include <versionbits_impl.h>
11
12
using enum ThresholdState;
13
14
std::string StateName(ThresholdState state)
15
41.2k
{
16
41.2k
    switch (state) {
17
9.22k
    case DEFINED: return "defined";
18
7.81k
    case STARTED: return "started";
19
2.97k
    case LOCKED_IN: return "locked_in";
20
9.38k
    case ACTIVE: return "active";
21
11.8k
    case FAILED: return "failed";
22
41.2k
    }
23
0
    return "invalid";
24
41.2k
}
25
26
ThresholdState AbstractThresholdConditionChecker::GetStateFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const
27
3.15M
{
28
3.15M
    int nPeriod = Period();
29
3.15M
    int nThreshold = Threshold();
30
3.15M
    int min_activation_height = MinActivationHeight();
31
3.15M
    int64_t nTimeStart = BeginTime();
32
3.15M
    int64_t nTimeTimeout = EndTime();
33
34
    // Check if this deployment is always active.
35
3.15M
    if (nTimeStart == Consensus::BIP9Deployment::ALWAYS_ACTIVE) {
36
6.84k
        return ThresholdState::ACTIVE;
37
6.84k
    }
38
39
    // Check if this deployment is never active.
40
3.14M
    if (nTimeStart == Consensus::BIP9Deployment::NEVER_ACTIVE) {
41
7.00k
        return ThresholdState::FAILED;
42
7.00k
    }
43
44
    // 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.
45
3.13M
    if (pindexPrev != nullptr) {
46
3.13M
        pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod));
47
3.13M
    }
48
49
    // Walk backwards in steps of nPeriod to find a pindexPrev whose information is known
50
3.13M
    std::vector<const CBlockIndex*> vToCompute;
51
3.19M
    while (!cache.contains(pindexPrev)) {
52
83.2k
        if (pindexPrev == nullptr) {
53
            // The genesis block is by definition defined.
54
20.3k
            cache[pindexPrev] = ThresholdState::DEFINED;
55
20.3k
            break;
56
20.3k
        }
57
62.9k
        if (pindexPrev->GetMedianTimePast() < nTimeStart) {
58
            // Optimization: don't recompute down further, as we know every earlier block will be before the start time
59
2.85k
            cache[pindexPrev] = ThresholdState::DEFINED;
60
2.85k
            break;
61
2.85k
        }
62
60.0k
        vToCompute.push_back(pindexPrev);
63
60.0k
        pindexPrev = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
64
60.0k
    }
65
66
    // At this point, cache[pindexPrev] is known
67
3.13M
    assert(cache.contains(pindexPrev));
68
3.13M
    ThresholdState state = cache[pindexPrev];
69
70
    // Now walk forward and compute the state of descendants of pindexPrev
71
3.19M
    while (!vToCompute.empty()) {
72
60.0k
        ThresholdState stateNext = state;
73
60.0k
        pindexPrev = vToCompute.back();
74
60.0k
        vToCompute.pop_back();
75
76
60.0k
        switch (state) {
77
14.7k
            case ThresholdState::DEFINED: {
78
14.7k
                if (pindexPrev->GetMedianTimePast() >= nTimeStart) {
79
14.7k
                    stateNext = ThresholdState::STARTED;
80
14.7k
                }
81
14.7k
                break;
82
0
            }
83
18.0k
            case ThresholdState::STARTED: {
84
                // We need to count
85
18.0k
                const CBlockIndex* pindexCount = pindexPrev;
86
18.0k
                int count = 0;
87
5.85M
                for (int i = 0; i < nPeriod; i++) {
88
5.84M
                    if (Condition(pindexCount)) {
89
1.48M
                        count++;
90
1.48M
                    }
91
5.84M
                    pindexCount = pindexCount->pprev;
92
5.84M
                }
93
18.0k
                if (count >= nThreshold) {
94
1.26k
                    stateNext = ThresholdState::LOCKED_IN;
95
16.7k
                } else if (pindexPrev->GetMedianTimePast() >= nTimeTimeout) {
96
1.49k
                    stateNext = ThresholdState::FAILED;
97
1.49k
                }
98
18.0k
                break;
99
0
            }
100
8.81k
            case ThresholdState::LOCKED_IN: {
101
                // Progresses into ACTIVE provided activation height will have been reached.
102
8.81k
                if (pindexPrev->nHeight + 1 >= min_activation_height) {
103
933
                    stateNext = ThresholdState::ACTIVE;
104
933
                }
105
8.81k
                break;
106
0
            }
107
7.16k
            case ThresholdState::FAILED:
108
18.4k
            case ThresholdState::ACTIVE: {
109
                // Nothing happens, these are terminal states.
110
18.4k
                break;
111
7.16k
            }
112
60.0k
        }
113
60.0k
        cache[pindexPrev] = state = stateNext;
114
60.0k
    }
115
116
3.13M
    return state;
117
3.13M
}
118
119
BIP9Stats AbstractThresholdConditionChecker::GetStateStatisticsFor(const CBlockIndex* pindex, std::vector<bool>* signalling_blocks) const
120
34
{
121
34
    BIP9Stats stats = {};
122
123
34
    stats.period = Period();
124
34
    stats.threshold = Threshold();
125
126
34
    if (pindex == nullptr) return stats;
127
128
    // Find how many blocks are in the current period
129
34
    int blocks_in_period = 1 + (pindex->nHeight % stats.period);
130
131
    // Reset signalling_blocks
132
34
    if (signalling_blocks) {
133
34
        signalling_blocks->assign(blocks_in_period, false);
134
34
    }
135
136
    // Count from current block to beginning of period
137
34
    int elapsed = 0;
138
34
    int count = 0;
139
34
    const CBlockIndex* currentIndex = pindex;
140
3.52k
    do {
141
3.52k
        ++elapsed;
142
3.52k
        --blocks_in_period;
143
3.52k
        if (Condition(currentIndex)) {
144
2.71k
            ++count;
145
2.71k
            if (signalling_blocks) signalling_blocks->at(blocks_in_period) = true;
146
2.71k
        }
147
3.52k
        currentIndex = currentIndex->pprev;
148
3.52k
    } while(blocks_in_period > 0);
149
150
34
    stats.elapsed = elapsed;
151
34
    stats.count = count;
152
34
    stats.possible = (stats.period - stats.threshold ) >= (stats.elapsed - count);
153
154
34
    return stats;
155
34
}
156
157
int AbstractThresholdConditionChecker::GetStateSinceHeightFor(const CBlockIndex* pindexPrev, ThresholdConditionCache& cache) const
158
26.5k
{
159
26.5k
    int64_t start_time = BeginTime();
160
26.5k
    if (start_time == Consensus::BIP9Deployment::ALWAYS_ACTIVE || start_time == Consensus::BIP9Deployment::NEVER_ACTIVE) {
161
13.2k
        return 0;
162
13.2k
    }
163
164
13.3k
    const ThresholdState initialState = GetStateFor(pindexPrev, cache);
165
166
    // BIP 9 about state DEFINED: "The genesis block is by definition in this state for each deployment."
167
13.3k
    if (initialState == ThresholdState::DEFINED) {
168
4.32k
        return 0;
169
4.32k
    }
170
171
9.01k
    const int nPeriod = Period();
172
173
    // 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.
174
    // To ease understanding of the following height calculation, it helps to remember that
175
    // right now pindexPrev points to the block prior to the block that we are computing for, thus:
176
    // if we are computing for the last block of a period, then pindexPrev points to the second to last block of the period, and
177
    // if we are computing for the first block of a period, then pindexPrev points to the last block of the previous period.
178
    // The parent of the genesis block is represented by nullptr.
179
9.01k
    pindexPrev = Assert(pindexPrev->GetAncestor(pindexPrev->nHeight - ((pindexPrev->nHeight + 1) % nPeriod)));
180
181
9.01k
    const CBlockIndex* previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
182
183
29.8k
    while (previousPeriodParent != nullptr && GetStateFor(previousPeriodParent, cache) == initialState) {
184
20.8k
        pindexPrev = previousPeriodParent;
185
20.8k
        previousPeriodParent = pindexPrev->GetAncestor(pindexPrev->nHeight - nPeriod);
186
20.8k
    }
187
188
    // Adjust the result because right now we point to the parent block.
189
9.01k
    return pindexPrev->nHeight + 1;
190
13.3k
}
191
192
BIP9Info VersionBitsCache::Info(const CBlockIndex& block_index, const Consensus::Params& params, Consensus::DeploymentPos id)
193
91
{
194
91
    BIP9Info result;
195
196
91
    VersionBitsConditionChecker checker(params, id);
197
198
91
    ThresholdState current_state, next_state;
199
200
91
    {
201
91
        LOCK(m_mutex);
202
91
        current_state = checker.GetStateFor(block_index.pprev, m_caches[id]);
203
91
        next_state = checker.GetStateFor(&block_index, m_caches[id]);
204
91
        result.since = checker.GetStateSinceHeightFor(block_index.pprev, m_caches[id]);
205
91
    }
206
207
91
    result.current_state = StateName(current_state);
208
91
    result.next_state = StateName(next_state);
209
210
91
    const bool has_signal = (STARTED == current_state || LOCKED_IN == current_state);
211
91
    if (has_signal) {
212
34
        result.stats.emplace(checker.GetStateStatisticsFor(&block_index, &result.signalling_blocks));
213
34
        if (LOCKED_IN == current_state) {
214
2
            result.stats->threshold = 0;
215
2
            result.stats->possible = false;
216
2
        }
217
34
    }
218
219
91
    if (current_state == ACTIVE) {
220
0
        result.active_since = result.since;
221
91
    } else if (next_state == ACTIVE) {
222
1
        result.active_since = block_index.nHeight + 1;
223
1
    }
224
225
91
    return result;
226
91
}
227
228
BIP9GBTStatus VersionBitsCache::GBTStatus(const CBlockIndex& block_index, const Consensus::Params& params)
229
2.08k
{
230
2.08k
    BIP9GBTStatus result;
231
232
2.08k
    LOCK(m_mutex);
233
4.16k
    for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
234
2.08k
        auto pos = static_cast<Consensus::DeploymentPos>(i);
235
2.08k
        VersionBitsConditionChecker checker(params, pos);
236
2.08k
        ThresholdState state = checker.GetStateFor(&block_index, m_caches[pos]);
237
2.08k
        const VBDeploymentInfo& vbdepinfo = VersionBitsDeploymentInfo[pos];
238
2.08k
        BIP9GBTStatus::Info gbtinfo{.bit=params.vDeployments[pos].bit, .mask=checker.Mask(), .gbt_optional_rule=vbdepinfo.gbt_optional_rule};
239
240
2.08k
        switch (state) {
241
8
        case DEFINED:
242
14
        case FAILED:
243
            // Not exposed to GBT
244
14
            break;
245
45
        case STARTED:
246
45
            result.signalling.try_emplace(vbdepinfo.name, gbtinfo);
247
45
            break;
248
20
        case LOCKED_IN:
249
20
            result.locked_in.try_emplace(vbdepinfo.name, gbtinfo);
250
20
            break;
251
2.00k
        case ACTIVE:
252
2.00k
            result.active.try_emplace(vbdepinfo.name, gbtinfo);
253
2.00k
            break;
254
2.08k
        }
255
2.08k
    }
256
2.08k
    return result;
257
2.08k
}
258
259
bool VersionBitsCache::IsActiveAfter(const CBlockIndex* pindexPrev, const Consensus::Params& params, Consensus::DeploymentPos pos)
260
1.46k
{
261
1.46k
    LOCK(m_mutex);
262
1.46k
    return ThresholdState::ACTIVE == VersionBitsConditionChecker(params, pos).GetStateFor(pindexPrev, m_caches[pos]);
263
1.46k
}
264
265
static int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params, std::array<ThresholdConditionCache, Consensus::MAX_VERSION_BITS_DEPLOYMENTS>& caches)
266
72.2k
{
267
72.2k
    int32_t nVersion = VERSIONBITS_TOP_BITS;
268
269
144k
    for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
270
72.2k
        Consensus::DeploymentPos pos = static_cast<Consensus::DeploymentPos>(i);
271
72.2k
        VersionBitsConditionChecker checker(params, pos);
272
72.2k
        ThresholdState state = checker.GetStateFor(pindexPrev, caches[pos]);
273
72.2k
        if (state == ThresholdState::LOCKED_IN || state == ThresholdState::STARTED) {
274
38.8k
            nVersion |= checker.Mask();
275
38.8k
        }
276
72.2k
    }
277
278
72.2k
    return nVersion;
279
72.2k
}
280
281
int32_t VersionBitsCache::ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
282
46.8k
{
283
46.8k
    LOCK(m_mutex);
284
46.8k
    return ::ComputeBlockVersion(pindexPrev, params, m_caches);
285
46.8k
}
286
287
void VersionBitsCache::Clear()
288
1.19k
{
289
1.19k
    LOCK(m_mutex);
290
2.39k
    for (unsigned int d = 0; d < Consensus::MAX_VERSION_BITS_DEPLOYMENTS; d++) {
291
1.19k
        m_caches[d].clear();
292
1.19k
    }
293
1.19k
}
294
295
namespace {
296
/**
297
 * Threshold condition checker that triggers when unknown versionbits are seen on the network.
298
 */
299
class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
300
{
301
private:
302
    const Consensus::Params& m_params;
303
    std::array<ThresholdConditionCache, Consensus::MAX_VERSION_BITS_DEPLOYMENTS>& m_caches;
304
    int m_bit;
305
    int period{2016};
306
    int threshold{1815}; // 90% threshold used in BIP 341
307
308
public:
309
    explicit WarningBitsConditionChecker(const CChainParams& chainparams, std::array<ThresholdConditionCache, Consensus::MAX_VERSION_BITS_DEPLOYMENTS>& caches, int bit)
310
3.00M
    : m_params{chainparams.GetConsensus()}, m_caches{caches}, m_bit(bit)
311
3.00M
    {
312
3.00M
        if (chainparams.IsTestChain()) {
313
3.00M
            period = chainparams.GetConsensus().DifficultyAdjustmentInterval();
314
3.00M
            threshold = period * 3 / 4; // 75% for test nets per BIP9 suggestion
315
3.00M
        }
316
3.00M
    }
317
318
3.00M
    int64_t BeginTime() const override { return 0; }
319
3.00M
    int64_t EndTime() const override { return std::numeric_limits<int64_t>::max(); }
320
3.00M
    int Period() const override { return period; }
321
3.00M
    int Threshold() const override { return threshold; }
322
323
    bool Condition(const CBlockIndex* pindex) const override
324
2.03M
    {
325
2.03M
        return pindex->nHeight >= m_params.MinBIP9WarningHeight &&
326
2.03M
               ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
327
2.03M
               ((pindex->nVersion >> m_bit) & 1) != 0 &&
328
2.03M
               ((::ComputeBlockVersion(pindex->pprev, m_params, m_caches) >> m_bit) & 1) == 0;
329
2.03M
    }
330
};
331
} // anonymous namespace
332
333
std::vector<std::pair<int, bool>> VersionBitsCache::CheckUnknownActivations(const CBlockIndex* pindex, const CChainParams& chainparams)
334
103k
{
335
103k
    LOCK(m_mutex);
336
103k
    std::vector<std::pair<int, bool>> result;
337
3.10M
    for (int bit = 0; bit < VERSIONBITS_NUM_BITS; ++bit) {
338
3.00M
        WarningBitsConditionChecker checker(chainparams, m_caches, bit);
339
3.00M
        ThresholdState state = checker.GetStateFor(pindex, m_warning_caches.at(bit));
340
3.00M
        if (state == ACTIVE || state == LOCKED_IN) {
341
148
            result.emplace_back(bit, state == ACTIVE);
342
148
        }
343
3.00M
    }
344
103k
    return result;
345
103k
}