5
5
"""Stress tests related to node initialization."""
6
6
import os
7
7
from pathlib import Path
8
+ import shutil
8
9
9
10
from test_framework .test_framework import BitcoinTestFramework , SkipTest
10
11
from test_framework .test_node import ErrorMatch
@@ -44,7 +45,7 @@ def sigterm_node():
44
45
45
46
def start_expecting_error (err_fragment ):
46
47
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' ],
48
49
expected_msg = err_fragment ,
49
50
match = ErrorMatch .PARTIAL_REGEX ,
50
51
)
@@ -99,9 +100,9 @@ def check_clean_start():
99
100
}
100
101
101
102
files_to_perturb = {
102
- 'blocks/index/*.ldb' : 'Error opening block database.' ,
103
+ 'blocks/index/*.ldb' : 'Error loading block database.' ,
103
104
'chainstate/*.ldb' : 'Error opening block database.' ,
104
- 'blocks/blk*.dat' : 'Error opening block database.' ,
105
+ 'blocks/blk*.dat' : 'Corrupted block database detected .' ,
105
106
}
106
107
107
108
for file_patt , err_fragment in files_to_delete .items ():
@@ -122,18 +123,31 @@ def check_clean_start():
122
123
check_clean_start ()
123
124
self .stop_node (0 )
124
125
126
+ self .log .info ("Test startup errors after perturbing certain essential files" )
125
127
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" )
126
130
target_files = list (node .chain_path .glob (file_patt ))
127
131
128
132
for target_file in target_files :
129
133
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 :
131
135
contents = tf_read .read ()
132
136
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 :
134
142
tf_write .write (bytes (tweaked_contents ))
135
143
136
144
start_expecting_error (err_fragment )
137
145
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
+
138
152
if __name__ == '__main__' :
139
153
InitStressTest ().main ()
0 commit comments