feature_init.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2021-present The Bitcoin Core developers 3 # Distributed under the MIT software license, see the accompanying 4 # file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 """Stress tests related to node initialization.""" 6 from pathlib import Path 7 import platform 8 import shutil 9 10 from test_framework.test_framework import BitcoinTestFramework, SkipTest 11 from test_framework.test_node import ErrorMatch 12 from test_framework.util import assert_equal 13 14 15 class InitStressTest(BitcoinTestFramework): 16 """ 17 Ensure that initialization can be interrupted at a number of points and not impair 18 subsequent starts. 19 """ 20 21 def add_options(self, parser): 22 self.add_wallet_options(parser) 23 24 def set_test_params(self): 25 self.setup_clean_chain = False 26 self.num_nodes = 1 27 28 def run_test(self): 29 """ 30 - test terminating initialization after seeing a certain log line. 31 - test removing certain essential files to test startup error paths. 32 """ 33 # TODO: skip Windows for now since it isn't clear how to SIGTERM. 34 # 35 # Windows doesn't support `process.terminate()`. 36 # and other approaches (like below) don't work: 37 # 38 # os.kill(node.process.pid, signal.CTRL_C_EVENT) 39 if platform.system() == 'Windows': 40 raise SkipTest("can't SIGTERM on Windows") 41 42 self.stop_node(0) 43 node = self.nodes[0] 44 45 def sigterm_node(): 46 node.process.terminate() 47 node.process.wait() 48 49 def start_expecting_error(err_fragment): 50 node.assert_start_raises_init_error( 51 extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1', '-checkblocks=200', '-checklevel=4'], 52 expected_msg=err_fragment, 53 match=ErrorMatch.PARTIAL_REGEX, 54 ) 55 56 def check_clean_start(): 57 """Ensure that node restarts successfully after various interrupts.""" 58 node.start() 59 node.wait_for_rpc_connection() 60 assert_equal(200, node.getblockcount()) 61 62 lines_to_terminate_after = [ 63 b'Validating signatures for all blocks', 64 b'scheduler thread start', 65 b'Starting HTTP server', 66 b'Loading P2P addresses', 67 b'Loading banlist', 68 b'Loading block index', 69 b'Checking all blk files are present', 70 b'Loaded best chain:', 71 b'init message: Verifying blocks', 72 b'init message: Starting network threads', 73 b'net thread start', 74 b'addcon thread start', 75 b'initload thread start', 76 b'txindex thread start', 77 b'block filter index thread start', 78 b'coinstatsindex thread start', 79 b'msghand thread start', 80 b'net thread start', 81 b'addcon thread start', 82 ] 83 if self.is_wallet_compiled(): 84 lines_to_terminate_after.append(b'Verifying wallet') 85 86 for terminate_line in lines_to_terminate_after: 87 self.log.info(f"Starting node and will exit after line {terminate_line}") 88 with node.wait_for_debug_log([terminate_line]): 89 node.start(extra_args=['-txindex=1', '-blockfilterindex=1', '-coinstatsindex=1']) 90 self.log.debug("Terminating node after terminate line was found") 91 sigterm_node() 92 93 check_clean_start() 94 self.stop_node(0) 95 96 self.log.info("Test startup errors after removing certain essential files") 97 98 files_to_delete = { 99 'blocks/index/*.ldb': 'Error opening block database.', 100 'chainstate/*.ldb': 'Error opening block database.', 101 'blocks/blk*.dat': 'Error loading block database.', 102 } 103 104 files_to_perturb = { 105 'blocks/index/*.ldb': 'Error loading block database.', 106 'chainstate/*.ldb': 'Error opening block database.', 107 'blocks/blk*.dat': 'Corrupted block database detected.', 108 } 109 110 for file_patt, err_fragment in files_to_delete.items(): 111 target_files = list(node.chain_path.glob(file_patt)) 112 113 for target_file in target_files: 114 self.log.info(f"Deleting file to ensure failure {target_file}") 115 bak_path = str(target_file) + ".bak" 116 target_file.rename(bak_path) 117 118 start_expecting_error(err_fragment) 119 120 for target_file in target_files: 121 bak_path = str(target_file) + ".bak" 122 self.log.debug(f"Restoring file from {bak_path} and restarting") 123 Path(bak_path).rename(target_file) 124 125 check_clean_start() 126 self.stop_node(0) 127 128 self.log.info("Test startup errors after perturbing certain essential files") 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") 132 target_files = list(node.chain_path.glob(file_patt)) 133 134 for target_file in target_files: 135 self.log.info(f"Perturbing file to ensure failure {target_file}") 136 with open(target_file, "r+b") as tf: 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 tf.seek(150) 141 tf.write(b"1" * 200) 142 143 start_expecting_error(err_fragment) 144 145 shutil.rmtree(node.chain_path / "blocks") 146 shutil.rmtree(node.chain_path / "chainstate") 147 shutil.move(node.chain_path / "blocks_bak", node.chain_path / "blocks") 148 shutil.move(node.chain_path / "chainstate_bak", node.chain_path / "chainstate") 149 150 151 if __name__ == '__main__': 152 InitStressTest().main()