Skip to content

Commit 3f44f9a

Browse files
committed
test: Add coverage for m_blocks_unlinked invariant in LoadBlockIndex
A pruned stale-fork block whose parent doesn't have any transactions shouldn't be added to m_blocks_unlinked when starting up a node.
1 parent 0e4b0ba commit 3f44f9a

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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()

‎test/functional/test_runner.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@
369369
'feature_blocksdir.py',
370370
'wallet_startup.py',
371371
'feature_remove_pruned_files_on_startup.py',
372+
'feature_prune_stale_fork.py',
372373
'p2p_i2p_ports.py',
373374
'p2p_i2p_sessions.py',
374375
'feature_presegwit_node_upgrade.py',

0 commit comments

Comments
 (0)