/ test / functional / feature_loadblock.py
feature_loadblock.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2017-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  """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  from pathlib import Path
14  import subprocess
15  import sys
16  import tempfile
17  import urllib
18  
19  from test_framework.blocktools import COINBASE_MATURITY
20  from test_framework.test_framework import BitcoinTestFramework
21  from test_framework.util import assert_equal
22  
23  
24  class LoadblockTest(BitcoinTestFramework):
25      def set_test_params(self):
26          self.setup_clean_chain = True
27          self.num_nodes = 2
28          self.supports_cli = False
29  
30      def run_test(self):
31          self.nodes[1].setnetworkactive(state=False)
32          self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op)
33  
34          # Parsing the url of our node to get settings for config file
35          data_dir = self.nodes[0].datadir_path
36          node_url = urllib.parse.urlparse(self.nodes[0].url)
37          cfg_file = data_dir / "linearize.cfg"
38          bootstrap_file = Path(self.options.tmpdir) / "bootstrap.dat"
39          genesis_block = self.nodes[0].getblockhash(0)
40          blocks_dir = self.nodes[0].blocks_path
41          hash_list = tempfile.NamedTemporaryFile(dir=data_dir,
42                                                  mode='w',
43                                                  delete=False,
44          )
45  
46          self.log.info("Create linearization config file")
47          with open(cfg_file, "a") as cfg:
48              cfg.write(f"datadir={data_dir}\n")
49              cfg.write(f"rpcuser={node_url.username}\n")
50              cfg.write(f"rpcpassword={node_url.password}\n")
51              cfg.write(f"port={node_url.port}\n")
52              cfg.write(f"host={node_url.hostname}\n")
53              cfg.write(f"output_file={bootstrap_file}\n")
54              cfg.write("max_height=100\n")
55              cfg.write("netmagic=fabfb5da\n")
56              cfg.write(f"input={blocks_dir}\n")
57              cfg.write(f"genesis={genesis_block}\n")
58              cfg.write(f"hashlist={hash_list.name}\n")
59  
60          base_dir = self.config["environment"]["SRCDIR"]
61          linearize_dir = Path(base_dir) / "contrib" / "linearize"
62  
63          self.log.info("Run linearization of block hashes")
64          linearize_hashes_file = 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 = 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.restart_node(1, extra_args=[f"-loadblock={bootstrap_file}"])
76          assert_equal(self.nodes[1].getblockcount(), 100)  # start_node is blocking on all block files being imported
77  
78          assert_equal(self.nodes[1].getblockchaininfo()['blocks'], 100)
79          assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())
80  
81  
82  if __name__ == '__main__':
83      LoadblockTest(__file__).main()