Skip to content

Commit 4258fd7

Browse files
committed
Merge #17091: tests: Add test for loadblock option and linearize scripts
89339d1 tests: Add test for loadblock option (Fabian Jahr) Pull request description: Fixes #17019 Was initially part of #17044 but as the test got larger it made sense to split it into its own commit as suggested in #17019 . This is testing the `-loadblock` option by using the scripts in `contrib/linearize` to generate a `bootstrap.dat` file and starting a disconnected node with it. So it is also testing the linearize scripts which were untested before and needed to be made available for the CI environment, hence they are added to `DIST_CONTRIB` in `Makefile.am`. ACKs for top commit: laanwj: ACK 89339d1 Tree-SHA512: aede0cd6e8b21194973f3633bc07fa2672d66a6f85dfe6a57cee2bb269a65d19ea49d5f9ed7914a173b3847c76e70257aa865f44bde170c1999d9655b4862d1c
2 parents 9dd6bbb + 89339d1 commit 4258fd7

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ DIST_CONTRIB = $(top_srcdir)/contrib/bitcoin-cli.bash-completion \
5252
$(top_srcdir)/contrib/bitcoind.bash-completion \
5353
$(top_srcdir)/contrib/debian/copyright \
5454
$(top_srcdir)/contrib/init \
55-
$(top_srcdir)/contrib/install_db4.sh
55+
$(top_srcdir)/contrib/install_db4.sh \
56+
$(top_srcdir)/contrib/linearize/linearize-data.py \
57+
$(top_srcdir)/contrib/linearize/linearize-hashes.py
58+
5659
DIST_SHARE = \
5760
$(top_srcdir)/share/genbuild.sh \
5861
$(top_srcdir)/share/rpcauth

test/functional/feature_loadblock.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2017-2019 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+
"""Test loadblock option
6+
7+
Test the option to start a node with the option loadblock which loads
8+
a serialized blockchain from a file (usually called bootstrap.dat).
9+
To generate that file this test uses the helper scripts available
10+
in contrib/linearize.
11+
"""
12+
13+
import os
14+
import subprocess
15+
import sys
16+
import tempfile
17+
import urllib
18+
19+
from test_framework.test_framework import (
20+
BitcoinTestFramework,
21+
)
22+
from test_framework.util import assert_equal, wait_until
23+
24+
25+
class LoadblockTest(BitcoinTestFramework):
26+
def set_test_params(self):
27+
self.setup_clean_chain = True
28+
self.num_nodes = 2
29+
30+
def run_test(self):
31+
self.nodes[1].setnetworkactive(state=False)
32+
self.nodes[0].generate(100)
33+
34+
# Parsing the url of our node to get settings for config file
35+
data_dir = self.nodes[0].datadir
36+
node_url = urllib.parse.urlparse(self.nodes[0].url)
37+
cfg_file = os.path.join(data_dir, "linearize.cfg")
38+
bootstrap_file = os.path.join(self.options.tmpdir, "bootstrap.dat")
39+
genesis_block = self.nodes[0].getblockhash(0)
40+
blocks_dir = os.path.join(data_dir, "regtest", "blocks")
41+
hash_list = tempfile.NamedTemporaryFile(dir=data_dir,
42+
mode='w',
43+
delete=False,
44+
encoding="utf-8")
45+
46+
self.log.info("Create linearization config file")
47+
with open(cfg_file, "a", encoding="utf-8") as cfg:
48+
cfg.write("datadir={}\n".format(data_dir))
49+
cfg.write("rpcuser={}\n".format(node_url.username))
50+
cfg.write("rpcpassword={}\n".format(node_url.password))
51+
cfg.write("port={}\n".format(node_url.port))
52+
cfg.write("host={}\n".format(node_url.hostname))
53+
cfg.write("output_file={}\n".format(bootstrap_file))
54+
cfg.write("max_height=100\n")
55+
cfg.write("netmagic=fabfb5da\n")
56+
cfg.write("input={}\n".format(blocks_dir))
57+
cfg.write("genesis={}\n".format(genesis_block))
58+
cfg.write("hashlist={}\n".format(hash_list.name))
59+
60+
base_dir = self.config["environment"]["SRCDIR"]
61+
linearize_dir = os.path.join(base_dir, "contrib", "linearize")
62+
63+
self.log.info("Run linearization of block hashes")
64+
linearize_hashes_file = os.path.join(linearize_dir, "linearize-hashes.py")
65+
subprocess.run([sys.executable, linearize_hashes_file, cfg_file],
66+
stdout=hash_list,
67+
check=True)
68+
69+
self.log.info("Run linearization of block data")
70+
linearize_data_file = os.path.join(linearize_dir, "linearize-data.py")
71+
subprocess.run([sys.executable, linearize_data_file, cfg_file],
72+
check=True)
73+
74+
self.log.info("Restart second, unsynced node with bootstrap file")
75+
self.stop_node(1)
76+
self.start_node(1, ["-loadblock=" + bootstrap_file])
77+
wait_until(lambda: self.nodes[1].getblockcount() == 100)
78+
79+
assert_equal(self.nodes[1].getblockchaininfo()['blocks'], 100)
80+
assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())
81+
82+
83+
if __name__ == '__main__':
84+
LoadblockTest().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
'feature_uacomment.py',
198198
'wallet_coinbase_category.py',
199199
'feature_filelock.py',
200+
'feature_loadblock.py',
200201
'p2p_dos_header_tree.py',
201202
'p2p_unrequested_blocks.py',
202203
'feature_includeconf.py',

0 commit comments

Comments
 (0)