@@ -45,6 +45,13 @@ def sigterm_node():
4545 node .process .terminate ()
4646 node .process .wait ()
4747
48+ def start_expecting_error (err_fragment ):
49+ node .assert_start_raises_init_error (
50+ extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' ],
51+ expected_msg = err_fragment ,
52+ match = ErrorMatch .PARTIAL_REGEX ,
53+ )
54+
4855 def check_clean_start ():
4956 """Ensure that node restarts successfully after various interrupts."""
5057 node .start ()
@@ -87,36 +94,27 @@ def check_clean_start():
8794
8895 self .log .info ("Test startup errors after removing certain essential files" )
8996
90- files_to_disturb = {
97+ files_to_delete = {
9198 'blocks/index/*.ldb' : 'Error opening block database.' ,
9299 'chainstate/*.ldb' : 'Error opening block database.' ,
93100 'blocks/blk*.dat' : 'Error loading block database.' ,
94101 }
95102
96- for file_patt , err_fragment in files_to_disturb .items ():
103+ files_to_perturb = {
104+ 'blocks/index/*.ldb' : 'Error opening block database.' ,
105+ 'chainstate/*.ldb' : 'Error opening block database.' ,
106+ 'blocks/blk*.dat' : 'Error opening block database.' ,
107+ }
108+
109+ for file_patt , err_fragment in files_to_delete .items ():
97110 target_files = list (node .chain_path .glob (file_patt ))
98111
99112 for target_file in target_files :
100- self .log .info (f"Tweaking file to ensure failure { target_file } " )
113+ self .log .info (f"Deleting file to ensure failure { target_file } " )
101114 bak_path = str (target_file ) + ".bak"
102115 target_file .rename (bak_path )
103116
104- # TODO: at some point, we should test perturbing the files instead of removing
105- # them, e.g.
106- #
107- # contents = target_file.read_bytes()
108- # tweaked_contents = bytearray(contents)
109- # tweaked_contents[50:250] = b'1' * 200
110- # target_file.write_bytes(bytes(tweaked_contents))
111- #
112- # At the moment I can't get this to work (bitcoind loads successfully?) so
113- # investigate doing this later.
114-
115- node .assert_start_raises_init_error (
116- extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' ],
117- expected_msg = err_fragment ,
118- match = ErrorMatch .PARTIAL_REGEX ,
119- )
117+ start_expecting_error (err_fragment )
120118
121119 for target_file in target_files :
122120 bak_path = str (target_file ) + ".bak"
@@ -126,6 +124,18 @@ def check_clean_start():
126124 check_clean_start ()
127125 self .stop_node (0 )
128126
127+ for file_patt , err_fragment in files_to_perturb .items ():
128+ target_files = list (node .chain_path .glob (file_patt ))
129+
130+ for target_file in target_files :
131+ self .log .info (f"Perturbing file to ensure failure { target_file } " )
132+ with open (target_file , "rb" ) as tf_read , open (target_file , "wb" ) as tf_write :
133+ contents = tf_read .read ()
134+ tweaked_contents = bytearray (contents )
135+ tweaked_contents [50 :250 ] = b'1' * 200
136+ tf_write .write (bytes (tweaked_contents ))
137+
138+ start_expecting_error (err_fragment )
129139
130140if __name__ == '__main__' :
131141 InitStressTest ().main ()
0 commit comments