55"""Stress tests related to node initialization."""
66import os
77from pathlib import Path
8+ import shutil
89
910from test_framework .test_framework import BitcoinTestFramework , SkipTest
1011from test_framework .test_node import ErrorMatch
@@ -44,7 +45,7 @@ def sigterm_node():
4445
4546 def start_expecting_error (err_fragment ):
4647 node .assert_start_raises_init_error (
47- extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' ],
48+ extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' , '-checkblocks=200' , '-checklevel=4' ],
4849 expected_msg = err_fragment ,
4950 match = ErrorMatch .PARTIAL_REGEX ,
5051 )
@@ -99,9 +100,9 @@ def check_clean_start():
99100 }
100101
101102 files_to_perturb = {
102- 'blocks/index/*.ldb' : 'Error opening block database.' ,
103+ 'blocks/index/*.ldb' : 'Error loading block database.' ,
103104 'chainstate/*.ldb' : 'Error opening block database.' ,
104- 'blocks/blk*.dat' : 'Error opening block database.' ,
105+ 'blocks/blk*.dat' : 'Corrupted block database detected .' ,
105106 }
106107
107108 for file_patt , err_fragment in files_to_delete .items ():
@@ -122,18 +123,31 @@ def check_clean_start():
122123 check_clean_start ()
123124 self .stop_node (0 )
124125
126+ self .log .info ("Test startup errors after perturbing certain essential files" )
125127 for file_patt , err_fragment in files_to_perturb .items ():
128+ shutil .copytree (node .chain_path / "blocks" , node .chain_path / "blocks_bak" )
129+ shutil .copytree (node .chain_path / "chainstate" , node .chain_path / "chainstate_bak" )
126130 target_files = list (node .chain_path .glob (file_patt ))
127131
128132 for target_file in target_files :
129133 self .log .info (f"Perturbing file to ensure failure { target_file } " )
130- with open (target_file , "rb" ) as tf_read , open ( target_file , "wb" ) as tf_write :
134+ with open (target_file , "rb" ) as tf_read :
131135 contents = tf_read .read ()
132136 tweaked_contents = bytearray (contents )
133- tweaked_contents [50 :250 ] = b'1' * 200
137+ # Since the genesis block is not checked by -checkblocks, the
138+ # perturbation window must be chosen such that a higher block
139+ # in blk*.dat is affected.
140+ tweaked_contents [150 :350 ] = b'1' * 200
141+ with open (target_file , "wb" ) as tf_write :
134142 tf_write .write (bytes (tweaked_contents ))
135143
136144 start_expecting_error (err_fragment )
137145
146+ shutil .rmtree (node .chain_path / "blocks" )
147+ shutil .rmtree (node .chain_path / "chainstate" )
148+ shutil .move (node .chain_path / "blocks_bak" , node .chain_path / "blocks" )
149+ shutil .move (node .chain_path / "chainstate_bak" , node .chain_path / "chainstate" )
150+
151+
138152if __name__ == '__main__' :
139153 InitStressTest ().main ()
0 commit comments