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
@@ -47,7 +48,7 @@ def sigterm_node():
47
48
48
49
def start_expecting_error (err_fragment ):
49
50
node .assert_start_raises_init_error (
50
- extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' ],
51
+ extra_args = ['-txindex=1' , '-blockfilterindex=1' , '-coinstatsindex=1' , '-checkblocks=200' , '-checklevel=4' ],
51
52
expected_msg = err_fragment ,
52
53
match = ErrorMatch .PARTIAL_REGEX ,
53
54
)
@@ -101,9 +102,9 @@ def check_clean_start():
101
102
}
102
103
103
104
files_to_perturb = {
104
- 'blocks/index/*.ldb' : 'Error opening block database.' ,
105
+ 'blocks/index/*.ldb' : 'Error loading block database.' ,
105
106
'chainstate/*.ldb' : 'Error opening block database.' ,
106
- 'blocks/blk*.dat' : 'Error opening block database.' ,
107
+ 'blocks/blk*.dat' : 'Corrupted block database detected .' ,
107
108
}
108
109
109
110
for file_patt , err_fragment in files_to_delete .items ():
@@ -124,18 +125,31 @@ def check_clean_start():
124
125
check_clean_start ()
125
126
self .stop_node (0 )
126
127
128
+ self .log .info ("Test startup errors after perturbing certain essential files" )
127
129
for file_patt , err_fragment in files_to_perturb .items ():
130
+ shutil .copytree (node .chain_path / "blocks" , node .chain_path / "blocks_bak" )
131
+ shutil .copytree (node .chain_path / "chainstate" , node .chain_path / "chainstate_bak" )
128
132
target_files = list (node .chain_path .glob (file_patt ))
129
133
130
134
for target_file in target_files :
131
135
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 :
136
+ with open (target_file , "rb" ) as tf_read :
133
137
contents = tf_read .read ()
134
138
tweaked_contents = bytearray (contents )
135
- tweaked_contents [50 :250 ] = b'1' * 200
139
+ # Since the genesis block is not checked by -checkblocks, the
140
+ # perturbation window must be chosen such that a higher block
141
+ # in blk*.dat is affected.
142
+ tweaked_contents [150 :350 ] = b'1' * 200
143
+ with open (target_file , "wb" ) as tf_write :
136
144
tf_write .write (bytes (tweaked_contents ))
137
145
138
146
start_expecting_error (err_fragment )
139
147
148
+ shutil .rmtree (node .chain_path / "blocks" )
149
+ shutil .rmtree (node .chain_path / "chainstate" )
150
+ shutil .move (node .chain_path / "blocks_bak" , node .chain_path / "blocks" )
151
+ shutil .move (node .chain_path / "chainstate_bak" , node .chain_path / "chainstate" )
152
+
153
+
140
154
if __name__ == '__main__' :
141
155
InitStressTest ().main ()
0 commit comments