@@ -45,6 +45,13 @@ def sigterm_node():
45
45
node .process .terminate ()
46
46
node .process .wait ()
47
47
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
+
48
55
def check_clean_start ():
49
56
"""Ensure that node restarts successfully after various interrupts."""
50
57
node .start ()
@@ -87,36 +94,27 @@ def check_clean_start():
87
94
88
95
self .log .info ("Test startup errors after removing certain essential files" )
89
96
90
- files_to_disturb = {
97
+ files_to_delete = {
91
98
'blocks/index/*.ldb' : 'Error opening block database.' ,
92
99
'chainstate/*.ldb' : 'Error opening block database.' ,
93
100
'blocks/blk*.dat' : 'Error loading block database.' ,
94
101
}
95
102
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 ():
97
110
target_files = list (node .chain_path .glob (file_patt ))
98
111
99
112
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 } " )
101
114
bak_path = str (target_file ) + ".bak"
102
115
target_file .rename (bak_path )
103
116
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 )
120
118
121
119
for target_file in target_files :
122
120
bak_path = str (target_file ) + ".bak"
@@ -126,6 +124,18 @@ def check_clean_start():
126
124
check_clean_start ()
127
125
self .stop_node (0 )
128
126
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 )
129
139
130
140
if __name__ == '__main__' :
131
141
InitStressTest ().main ()
0 commit comments