Skip to content

Commit ca55613

Browse files
committed
test: Add functional test for bitcoin-chainstate
Adds basic coverage for successfully validating a mainnet block as well as some duplicate and invalid data.
1 parent 3f9c716 commit ca55613

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ function(create_test_config)
1919
set_configure_variable(WITH_BDB USE_BDB)
2020
set_configure_variable(BUILD_CLI BUILD_BITCOIN_CLI)
2121
set_configure_variable(BUILD_UTIL BUILD_BITCOIN_UTIL)
22+
set_configure_variable(BUILD_UTIL_CHAINSTATE BUILD_BITCOIN_CHAINSTATE)
2223
set_configure_variable(BUILD_WALLET_TOOL BUILD_BITCOIN_WALLET)
2324
set_configure_variable(BUILD_DAEMON BUILD_BITCOIND)
2425
set_configure_variable(BUILD_FUZZ_BINARY ENABLE_FUZZ_BINARY)

test/config.ini.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ RPCAUTH=@abs_top_srcdir@/share/rpcauth/rpcauth.py
1919
@USE_BDB_TRUE@USE_BDB=true
2020
@BUILD_BITCOIN_CLI_TRUE@ENABLE_CLI=true
2121
@BUILD_BITCOIN_UTIL_TRUE@ENABLE_BITCOIN_UTIL=true
22+
@BUILD_BITCOIN_CHAINSTATE_TRUE@ENABLE_BITCOIN_CHAINSTATE=true
2223
@BUILD_BITCOIN_WALLET_TRUE@ENABLE_WALLET_TOOL=true
2324
@BUILD_BITCOIND_TRUE@ENABLE_BITCOIND=true
2425
@ENABLE_FUZZ_BINARY_TRUE@ENABLE_FUZZ_BINARY=true

test/functional/test_framework/test_framework.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def wallet_argv(self):
8888
"Return argv array that should be used to invoke bitcoin-wallet"
8989
return self._argv(self.paths.bitcoinwallet)
9090

91+
def chainstate_argv(self):
92+
"Return argv array that should be used to invoke bitcoin-chainstate"
93+
return self._argv(self.paths.bitcoinchainstate)
94+
9195
def _argv(self, bin_path):
9296
"""Return argv array that should be used to invoke the command.
9397
Normally this will return binary paths directly from the paths object,
@@ -291,6 +295,7 @@ def get_binary_paths(self):
291295
"bitcoind": ("bitcoind", "BITCOIND"),
292296
"bitcoin-cli": ("bitcoincli", "BITCOINCLI"),
293297
"bitcoin-util": ("bitcoinutil", "BITCOINUTIL"),
298+
"bitcoin-chainstate": ("bitcoinchainstate", "BITCOINCHAINSTATE"),
294299
"bitcoin-wallet": ("bitcoinwallet", "BITCOINWALLET"),
295300
}
296301
for binary, [attribute_name, env_variable_name] in binaries.items():
@@ -1022,6 +1027,11 @@ def skip_if_no_bitcoin_util(self):
10221027
if not self.is_bitcoin_util_compiled():
10231028
raise SkipTest("bitcoin-util has not been compiled")
10241029

1030+
def skip_if_no_bitcoin_chainstate(self):
1031+
"""Skip the running test if bitcoin-chainstate has not been compiled."""
1032+
if not self.is_bitcoin_chainstate_compiled():
1033+
raise SkipTest("bitcoin-chainstate has not been compiled")
1034+
10251035
def skip_if_no_cli(self):
10261036
"""Skip the running test if bitcoin-cli has not been compiled."""
10271037
if not self.is_cli_compiled():
@@ -1073,6 +1083,10 @@ def is_bitcoin_util_compiled(self):
10731083
"""Checks whether bitcoin-util was compiled."""
10741084
return self.config["components"].getboolean("ENABLE_BITCOIN_UTIL")
10751085

1086+
def is_bitcoin_chainstate_compiled(self):
1087+
"""Checks whether bitcoin-chainstate was compiled."""
1088+
return self.config["components"].getboolean("ENABLE_BITCOIN_CHAINSTATE")
1089+
10761090
def is_zmq_compiled(self):
10771091
"""Checks whether the zmq module was compiled."""
10781092
return self.config["components"].getboolean("ENABLE_ZMQ")

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
'feature_bind_extra.py',
194194
'mempool_resurrect.py',
195195
'wallet_txn_doublespend.py --mineblock',
196+
'tool_bitcoin_chainstate.py',
196197
'tool_wallet.py --legacy-wallet',
197198
'tool_wallet.py --legacy-wallet --bdbro',
198199
'tool_wallet.py --legacy-wallet --bdbro --swap-bdb-endian',
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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()

0 commit comments

Comments
 (0)