From f802edf57cc844f9a1708b395906a974dded451c Mon Sep 17 00:00:00 2001 From: Anthony Towns Date: Tue, 10 Mar 2026 17:55:12 +1000 Subject: [PATCH] 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 --- src/test/fuzz/versionbits.cpp | 5 +++-- src/test/util/versionbits.h | 13 +++++++++++++ src/test/versionbits_tests.cpp | 13 ++++++++++++- src/versionbits.h | 4 ++-- test/functional/feature_versionbits_warning.py | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/test/util/versionbits.h diff --git a/src/test/fuzz/versionbits.cpp b/src/test/fuzz/versionbits.cpp index a2085e6a409..0ee86e6e58b 100644 --- a/src/test/fuzz/versionbits.cpp +++ b/src/test/fuzz/versionbits.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -31,7 +32,7 @@ public: { assert(dep.period > 0); assert(dep.threshold <= dep.period); - assert(0 <= dep.bit && dep.bit < 32 && dep.bit < VERSIONBITS_NUM_BITS); + assert(0 <= dep.bit && dep.bit < 32 && dep.bit < VERSIONBITS_MAX_NUM_BITS); assert(0 <= dep.min_activation_height); } @@ -126,7 +127,7 @@ FUZZ_TARGET(versionbits, .init = initialize) assert(0 < dep.threshold && dep.threshold <= dep.period); // must be able to both pass and fail threshold! // select deployment parameters: bit, start time, timeout - dep.bit = fuzzed_data_provider.ConsumeIntegralInRange(0, VERSIONBITS_NUM_BITS - 1); + dep.bit = fuzzed_data_provider.ConsumeIntegralInRange(0, VERSIONBITS_MAX_NUM_BITS - 1); if (always_active_test) { dep.nStartTime = Consensus::BIP9Deployment::ALWAYS_ACTIVE; diff --git a/src/test/util/versionbits.h b/src/test/util/versionbits.h new file mode 100644 index 00000000000..478b7882fab --- /dev/null +++ b/src/test/util/versionbits.h @@ -0,0 +1,13 @@ +// Copyright (c) 2026-present The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_TEST_UTIL_VERSIONBITS_H +#define BITCOIN_TEST_UTIL_VERSIONBITS_H + +#include + +/** Total possible bits available for versionbits per original BIP 9 specification */ +static constexpr int VERSIONBITS_MAX_NUM_BITS{29}; + +#endif // BITCOIN_TEST_UTIL_VERSIONBITS_H diff --git a/src/test/versionbits_tests.cpp b/src/test/versionbits_tests.cpp index ad792053117..77384c5f2f1 100644 --- a/src/test/versionbits_tests.cpp +++ b/src/test/versionbits_tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -454,9 +455,19 @@ BOOST_FIXTURE_TEST_CASE(versionbits_computeblockversion, BlockVersionTest) // not take precedence over STARTED/LOCKED_IN. So all softforks on // the same bit might overlap, even when non-overlapping start-end // times are picked. - const uint32_t dep_mask{uint32_t{1} << chainParams->GetConsensus().vDeployments[dep].bit}; + const auto& dep_info = chainParams->GetConsensus().vDeployments[dep]; + const uint32_t dep_mask{uint32_t{1} << dep_info.bit}; BOOST_CHECK(!(chain_all_vbits & dep_mask)); chain_all_vbits |= dep_mask; + BOOST_CHECK(0 <= dep_info.bit && dep_info.bit < VERSIONBITS_MAX_NUM_BITS); + if (chain_type != ChainType::REGTEST) { + if (dep == Consensus::DEPLOYMENT_TESTDUMMY) { + BOOST_CHECK_EQUAL(dep_info.nStartTime, Consensus::BIP9Deployment::NEVER_ACTIVE); + BOOST_CHECK_EQUAL(dep_info.nTimeout, Consensus::BIP9Deployment::NO_TIMEOUT); + } else { + BOOST_CHECK(dep_info.bit < VERSIONBITS_NUM_BITS); + } + } check_computeblockversion(vbcache, chainParams->GetConsensus(), dep); } } diff --git a/src/versionbits.h b/src/versionbits.h index 59b0cbeee71..0ac17287da9 100644 --- a/src/versionbits.h +++ b/src/versionbits.h @@ -21,8 +21,8 @@ static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION = 4; static const int32_t VERSIONBITS_TOP_BITS = 0x20000000UL; /** What bitmask determines whether versionbits is in use */ static const int32_t VERSIONBITS_TOP_MASK = 0xE0000000UL; -/** Total bits available for versionbits */ -static const int32_t VERSIONBITS_NUM_BITS = 29; +/** Total bits available for versionbits (BIP 323) */ +static const int32_t VERSIONBITS_NUM_BITS = 5; /** Opaque type for BIP9 state. See versionbits_impl.h for details. */ enum class ThresholdState : uint8_t; diff --git a/test/functional/feature_versionbits_warning.py b/test/functional/feature_versionbits_warning.py index 79f512adea0..1f858dc734f 100755 --- a/test/functional/feature_versionbits_warning.py +++ b/test/functional/feature_versionbits_warning.py @@ -18,7 +18,7 @@ from test_framework.test_framework import BitcoinTestFramework VB_PERIOD = 144 # versionbits period length for regtest VB_THRESHOLD = 108 # versionbits activation threshold for regtest VB_TOP_BITS = 0x20000000 -VB_UNKNOWN_BIT = 27 # Choose a bit unassigned to any deployment +VB_UNKNOWN_BIT = 3 # Choose a bit unassigned to any deployment VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT) WARN_UNKNOWN_RULES_ACTIVE = f"Unknown new rules activated (versionbit {VB_UNKNOWN_BIT})"