feature_prune_stale_fork.py raw
1 #!/usr/bin/env python3
2 # Copyright (c) 2026-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 node restart with a pruned stale-fork block whose parent has no transactions."""
6
7 from test_framework.blocktools import create_empty_fork
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import assert_equal, assert_raises_rpc_error
10
11
12 class FeaturePruneStaleForkTest(BitcoinTestFramework):
13 def set_test_params(self):
14 self.num_nodes = 1
15 self.extra_args = [["-prune=1", "-fastprune"]]
16
17 def run_test(self):
18 node = self.nodes[0]
19
20 self.log.info("Create a 2-block stale fork: parent has no transactions, child has transactions")
21 [side_parent, side_child] = create_empty_fork(node, 2)
22
23 node.submitheader(side_parent.serialize().hex())
24 node.submitblock(side_child.serialize().hex())
25 assert_equal(node.getblockheader(side_parent.hash_hex)["nTx"], 0)
26 assert_equal(node.getblockheader(side_child.hash_hex)["nTx"], 1)
27
28 self.log.info("Advance and prune so the stale-fork child's block data is removed from disk")
29 self.generate(node, 500)
30 node.pruneblockchain(node.getblockcount() - 100)
31 assert_raises_rpc_error(-1, "Block not available (pruned data)", node.getblock, side_child.hash_hex)
32
33 self.log.info("Restart and mine; node must reload cleanly after the stale-fork child was pruned")
34 self.restart_node(0)
35 self.generate(node, 1)
36
37
38 if __name__ == '__main__':
39 FeaturePruneStaleForkTest(__file__).main()
40