tool_bitcoin_chainstate.py
1 #!/usr/bin/env python3 2 # Copyright (c) 2022-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 6 import subprocess 7 8 from test_framework.test_framework import BitcoinTestFramework 9 10 class BitcoinChainstateTest(BitcoinTestFramework): 11 def skip_test_if_missing_module(self): 12 self.skip_if_no_bitcoin_chainstate() 13 14 def set_test_params(self): 15 self.setup_clean_chain = True 16 self.chain = "" 17 self.num_nodes = 1 18 # Set prune to avoid disk space warning. 19 self.extra_args = [["-prune=550"]] 20 21 def add_block(self, datadir, input, expected_stderr): 22 proc = subprocess.Popen( 23 self.get_binaries().chainstate_argv() + [datadir], 24 stdin=subprocess.PIPE, 25 stdout=subprocess.PIPE, 26 stderr=subprocess.PIPE, 27 text=True 28 ) 29 stdout, stderr = proc.communicate(input=input + "\n", timeout=5) 30 self.log.debug("STDOUT: {0}".format(stdout.strip("\n"))) 31 self.log.info("STDERR: {0}".format(stderr.strip("\n"))) 32 33 if expected_stderr not in stderr: 34 raise AssertionError(f"Expected stderr output {expected_stderr} does not partially match stderr:\n{stderr}") 35 36 def run_test(self): 37 node = self.nodes[0] 38 datadir = node.cli.datadir 39 node.stop_node() 40 41 self.log.info(f"Testing bitcoin-chainstate {self.get_binaries().chainstate_argv()} with datadir: {datadir}") 42 block_one = "010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000" 43 self.add_block(datadir, block_one, "Block has not yet been rejected") 44 self.add_block(datadir, block_one, "duplicate") 45 self.add_block(datadir, "00", "Block decode failed") 46 self.add_block(datadir, "", "Empty line found") 47 48 if __name__ == "__main__": 49 BitcoinChainstateTest(__file__).main()