1 #!/usr/bin/env python3
2 # Copyright (c) 2017-present The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test a pre-segwit node upgrading to segwit consensus"""
6 7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import (
9 assert_equal,
10 softfork_active,
11 )
12 import os
13 14 15 class SegwitUpgradeTest(BitcoinTestFramework):
16 def set_test_params(self):
17 self.setup_clean_chain = True
18 self.num_nodes = 1
19 self.extra_args = [["-testactivationheight=segwit@10"]]
20 21 def run_test(self):
22 """A pre-segwit node with insufficiently validated blocks needs to redownload blocks"""
23 24 self.log.info("Testing upgrade behaviour for pre-segwit node to segwit rules")
25 node = self.nodes[0]
26 27 # Node hasn't been used or connected yet
28 assert_equal(node.getblockcount(), 0)
29 30 assert not softfork_active(node, "segwit")
31 32 # Generate 8 blocks without witness data
33 self.generate(node, 8)
34 assert_equal(node.getblockcount(), 8)
35 36 self.stop_node(0)
37 # Restarting the node (with segwit activation height set to 5) should result in a shutdown
38 # because the blockchain consists of 3 insufficiently validated blocks per segwit consensus rules.
39 node.assert_start_raises_init_error(
40 extra_args=["-testactivationheight=segwit@5"],
41 expected_msg="Witness data for blocks after height 5 requires "
42 f"validation. Please restart with -reindex..{os.linesep}"
43 "Please restart with -reindex or -reindex-chainstate to recover.",
44 )
45 46 # As directed, the user restarts the node with -reindex
47 self.start_node(0, extra_args=["-reindex", "-testactivationheight=segwit@5"])
48 49 # With the segwit consensus rules, the node is able to validate only up to block 4
50 assert_equal(node.getblockcount(), 4)
51 52 # The upgraded node should now have segwit activated
53 assert softfork_active(node, "segwit")
54 55 56 if __name__ == '__main__':
57 SegwitUpgradeTest(__file__).main()
58