Skip to content

Commit f802edf

Browse files
ajtownsdarosior
andcommitted
versionbits: Limit live activation params and activation warnings per BIP323
Test bits are conserved. This only has an effect on the warnings. Co-Authored-By: Antoine Poinsot <[email protected]>
1 parent d3056bc commit f802edf

5 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎src/test/fuzz/versionbits.cpp‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <test/fuzz/FuzzedDataProvider.h>
1515
#include <test/fuzz/fuzz.h>
1616
#include <test/fuzz/util.h>
17+
#include <test/util/versionbits.h>
1718

1819
#include <cstdint>
1920
#include <limits>
@@ -31,7 +32,7 @@ class TestConditionChecker : public VersionBitsConditionChecker
3132
{
3233
assert(dep.period > 0);
3334
assert(dep.threshold <= dep.period);
34-
assert(0 <= dep.bit && dep.bit < 32 && dep.bit < VERSIONBITS_NUM_BITS);
35+
assert(0 <= dep.bit && dep.bit < 32 && dep.bit < VERSIONBITS_MAX_NUM_BITS);
3536
assert(0 <= dep.min_activation_height);
3637
}
3738

@@ -126,7 +127,7 @@ FUZZ_TARGET(versionbits, .init = initialize)
126127
assert(0 < dep.threshold && dep.threshold <= dep.period); // must be able to both pass and fail threshold!
127128

128129
// select deployment parameters: bit, start time, timeout
129-
dep.bit = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, VERSIONBITS_NUM_BITS - 1);
130+
dep.bit = fuzzed_data_provider.ConsumeIntegralInRange<int>(0, VERSIONBITS_MAX_NUM_BITS - 1);
130131

131132
if (always_active_test) {
132133
dep.nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE;

‎src/test/util/versionbits.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2026-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+
#ifndef BITCOIN_TEST_UTIL_VERSIONBITS_H
6+
#define BITCOIN_TEST_UTIL_VERSIONBITS_H
7+
8+
#include <versionbits.h>
9+
10+
/** Total possible bits available for versionbits per original BIP 9 specification */
11+
static constexpr int VERSIONBITS_MAX_NUM_BITS{29};
12+
13+
#endif // BITCOIN_TEST_UTIL_VERSIONBITS_H

‎src/test/versionbits_tests.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <test/util/random.h>
99
#include <test/util/common.h>
1010
#include <test/util/setup_common.h>
11+
#include <test/util/versionbits.h>
1112
#include <util/chaintype.h>
1213
#include <versionbits.h>
1314
#include <versionbits_impl.h>
@@ -454,9 +455,19 @@ BOOST_FIXTURE_TEST_CASE(versionbits_computeblockversion, BlockVersionTest)
454455
// not take precedence over STARTED/LOCKED_IN. So all softforks on
455456
// the same bit might overlap, even when non-overlapping start-end
456457
// times are picked.
457-
const uint32_t dep_mask{uint32_t{1} << chainParams->GetConsensus().vDeployments[dep].bit};
458+
const auto& dep_info = chainParams->GetConsensus().vDeployments[dep];
459+
const uint32_t dep_mask{uint32_t{1} << dep_info.bit};
458460
BOOST_CHECK(!(chain_all_vbits & dep_mask));
459461
chain_all_vbits |= dep_mask;
462+
BOOST_CHECK(0 <= dep_info.bit && dep_info.bit < VERSIONBITS_MAX_NUM_BITS);
463+
if (chain_type != ChainType::REGTEST) {
464+
if (dep == Consensus::DEPLOYMENT_TESTDUMMY) {
465+
BOOST_CHECK_EQUAL(dep_info.nStartTime, Consensus::BIP9Deployment::NEVER_ACTIVE);
466+
BOOST_CHECK_EQUAL(dep_info.nTimeout, Consensus::BIP9Deployment::NO_TIMEOUT);
467+
} else {
468+
BOOST_CHECK(dep_info.bit < VERSIONBITS_NUM_BITS);
469+
}
470+
}
460471
check_computeblockversion(vbcache, chainParams->GetConsensus(), dep);
461472
}
462473
}

‎src/versionbits.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4;
2121
static const int32_t VERSIONBITS_TOP_BITS = 0x20000000UL;
2222
/** What bitmask determines whether versionbits is in use */
2323
static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL;
24-
/** Total bits available for versionbits */
25-
static const int32_t VERSIONBITS_NUM_BITS = 29;
24+
/** Total bits available for versionbits (BIP 323) */
25+
static const int32_t VERSIONBITS_NUM_BITS = 5;
2626

2727
/** Opaque type for BIP9 state. See versionbits_impl.h for details. */
2828
enum class ThresholdState : uint8_t;

‎test/functional/feature_versionbits_warning.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
VB_PERIOD = 144 # versionbits period length for regtest
1919
VB_THRESHOLD = 108 # versionbits activation threshold for regtest
2020
VB_TOP_BITS = 0x20000000
21-
VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment
21+
VB_UNKNOWN_BIT = 3 # Choose a bit unassigned to any deployment
2222
VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)
2323

2424
WARN_UNKNOWN_RULES_ACTIVE = f"Unknown new rules activated (versionbit {VB_UNKNOWN_BIT})"

0 commit comments

Comments
 (0)