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) 2017-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 a pre-segwit node upgrading to segwit consensus"""7from test_framework.test_framework import BitcoinTestFramework8from test_framework.util import (9assert_equal,10softfork_active,11)12import os15class SegwitUpgradeTest(BitcoinTestFramework):16def set_test_params(self):17self.setup_clean_chain = True18self.num_nodes = 119self.extra_args = [["-testactivationheight=segwit@10"]]21def run_test(self):22"""A pre-segwit node with insufficiently validated blocks needs to redownload blocks"""24self.log.info("Testing upgrade behaviour for pre-segwit node to segwit rules")25node = self.nodes[0]27# Node hasn't been used or connected yet28assert_equal(node.getblockcount(), 0)30assert not softfork_active(node, "segwit")32# Generate 8 blocks without witness data33self.generate(node, 8)34assert_equal(node.getblockcount(), 8)36self.stop_node(0)37# Restarting the node (with segwit activation height set to 5) should result in a shutdown38# because the blockchain consists of 3 insufficiently validated blocks per segwit consensus rules.39node.assert_start_raises_init_error(40extra_args=["-testactivationheight=segwit@5"],41expected_msg="Witness data for blocks after height 5 requires "42f"validation. Please restart with -reindex..{os.linesep}"43"Please restart with -reindex or -reindex-chainstate to recover.",44)46# As directed, the user restarts the node with -reindex47self.start_node(0, extra_args=["-reindex", "-testactivationheight=segwit@5"])49# With the segwit consensus rules, the node is able to validate only up to block 450assert_equal(node.getblockcount(), 4)52# The upgraded node should now have segwit activated53assert softfork_active(node, "segwit")56if __name__ == '__main__':57SegwitUpgradeTest(__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.