Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2016-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test version bits warning system.7Generate chains with block versions that appear to be signalling unknown8soft-forks, and test that warning alerts are generated.9"""10import os11import re13from test_framework.blocktools import create_block14from test_framework.messages import msg_block15from test_framework.p2p import P2PInterface16from test_framework.test_framework import BitcoinTestFramework18VB_PERIOD = 144 # versionbits period length for regtest19VB_THRESHOLD = 108 # versionbits activation threshold for regtest20VB_TOP_BITS = 0x2000000022# Choose a bit unassigned to any deployment, or start the23# node with the deployment matching this bit disabled.24VB_UNKNOWN_BIT = 325VB_UNKNOWN_VERSION = VB_TOP_BITS | (1 << VB_UNKNOWN_BIT)26VB_IGNORED_BIT = 527VB_IGNORED_VERSION = VB_TOP_BITS | (1 << VB_IGNORED_BIT)29WARN_UNKNOWN_RULES_ACTIVE = f"Unknown new rules activated (versionbit {VB_UNKNOWN_BIT})"30VB_PATTERN = re.compile("Unknown new rules activated.*versionbit")32class VersionBitsWarningTest(BitcoinTestFramework):33def set_test_params(self):34self.setup_clean_chain = True35self.num_nodes = 137def setup_network(self):38self.alert_filename = os.path.join(self.options.tmpdir, "alert.txt")39# Open and close to create zero-length file40with open(self.alert_filename, 'w'):41pass42self.extra_args = [[f"-alertnotify=echo %s >> \"{self.alert_filename}\""]]43self.setup_nodes()45def send_blocks_with_version(self, peer, numblocks, version):46"""Send numblocks blocks to peer with version set"""47tip = self.nodes[0].getbestblockhash()48height = self.nodes[0].getblockcount()49block_time = self.nodes[0].getblockheader(tip)["time"] + 150tip = int(tip, 16)52for _ in range(numblocks):53block = create_block(tip, height=height + 1, ntime=block_time, version=version)54block.solve()55peer.send_without_ping(msg_block(block))56block_time += 157height += 158tip = block.hash_int59peer.sync_with_ping()61def versionbits_in_alert_file(self):62"""Test that the versionbits warning has been written to the alert file."""63with open(self.alert_filename, 'r') as f:64alert_text = f.read()65return VB_PATTERN.search(alert_text) is not None67def run_test(self):68node = self.nodes[0]69peer = node.add_p2p_connection(P2PInterface())71node_deterministic_address = node.get_deterministic_priv_key().address72# Mine one period worth of blocks73self.generatetoaddress(node, VB_PERIOD, node_deterministic_address)75self.log.info("Check that there is no warning if previous VB_BLOCKS have <VB_THRESHOLD blocks with unknown versionbits version.")76# Build one period of blocks with < VB_THRESHOLD blocks signaling some unknown bit77self.send_blocks_with_version(peer, VB_THRESHOLD - 1, VB_UNKNOWN_VERSION)78self.generatetoaddress(node, VB_PERIOD - VB_THRESHOLD + 1, node_deterministic_address)80# Check that we're not getting any versionbit-related errors in get*info()81assert not VB_PATTERN.match(",".join(node.getmininginfo()["warnings"]))82assert not VB_PATTERN.match(",".join(node.getnetworkinfo()["warnings"]))84self.log.info("Check that there is no warning if previous VB_BLOCKS have VB_PERIOD blocks with ignored versionbits version.")85# Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit86self.send_blocks_with_version(peer, VB_THRESHOLD, VB_IGNORED_VERSION)87self.generatetoaddress(node, VB_PERIOD - VB_THRESHOLD, node_deterministic_address)89# Move the ignored deployment state to ACTIVE and make sure we're out of IBD.90self.generatetoaddress(node, VB_PERIOD, node_deterministic_address)91self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'])93# Check that we're not getting any versionbit-related warnings in get*info()94assert not VB_PATTERN.match(", ".join(node.getmininginfo()["warnings"]))95assert not VB_PATTERN.match(", ".join(node.getnetworkinfo()["warnings"]))97self.log.info("Check that there is a warning if previous VB_BLOCKS have >=VB_THRESHOLD blocks with unknown versionbits version.")98# Build one period of blocks with VB_THRESHOLD blocks signaling some unknown bit99self.send_blocks_with_version(peer, VB_THRESHOLD, VB_UNKNOWN_VERSION)100self.generatetoaddress(node, VB_PERIOD - VB_THRESHOLD, node_deterministic_address)102# Mine a period worth of expected blocks so the generic block-version warning103# is cleared. This will move the versionbit state to ACTIVE.104self.generatetoaddress(node, VB_PERIOD, node_deterministic_address)106# Stop-start the node. This is required because bitcoind will only warn once about unknown versions or unknown rules activating.107self.restart_node(0)109# Generating one block guarantees that we'll get out of IBD110self.generatetoaddress(node, 1, node_deterministic_address)111self.wait_until(lambda: not node.getblockchaininfo()['initialblockdownload'])112# Generating one more block will be enough to generate an error.113self.generatetoaddress(node, 1, node_deterministic_address)114# Check that get*info() shows the versionbits unknown rules warning115assert WARN_UNKNOWN_RULES_ACTIVE in ",".join(node.getmininginfo()["warnings"])116assert WARN_UNKNOWN_RULES_ACTIVE in ",".join(node.getnetworkinfo()["warnings"])117# Check that the alert file shows the versionbits unknown rules warning118self.wait_until(lambda: self.versionbits_in_alert_file())120if __name__ == '__main__':121VersionBitsWarningTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.