feature_loadblock.py raw

   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  
  29      def run_test(self):
  30          self.nodes[1].setnetworkactive(state=False)
  31          self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op)
  32  
  33          # Parsing the url of our node to get settings for config file
  34          data_dir = self.nodes[0].datadir_path
  35          node_url = urllib.parse.urlparse(self.nodes[0].url)
  36          cfg_file = data_dir / "linearize.cfg"
  37          bootstrap_file = Path(self.options.tmpdir) / "bootstrap.dat"
  38          genesis_block = self.nodes[0].getblockhash(0)
  39          blocks_dir = self.nodes[0].blocks_path
  40          hash_list = tempfile.NamedTemporaryFile(dir=data_dir,
  41                                                  mode='w',
  42                                                  delete=False,
  43          )
  44  
  45          self.log.info("Create linearization config file")
  46          with open(cfg_file, "a") as cfg:
  47              cfg.write(f"datadir={data_dir}\n")
  48              cfg.write(f"rpcuser={node_url.username}\n")
  49              cfg.write(f"rpcpassword={node_url.password}\n")
  50              cfg.write(f"port={node_url.port}\n")
  51              cfg.write(f"host={node_url.hostname}\n")
  52              cfg.write(f"output_file={bootstrap_file}\n")
  53              cfg.write("max_height=100\n")
  54              cfg.write("netmagic=fabfb5da\n")
  55              cfg.write(f"input={blocks_dir}\n")
  56              cfg.write(f"genesis={genesis_block}\n")
  57              cfg.write(f"hashlist={hash_list.name}\n")
  58  
  59          base_dir = self.config["environment"]["SRCDIR"]
  60          linearize_dir = Path(base_dir) / "contrib" / "linearize"
  61  
  62          self.log.info("Run linearization of block hashes")
  63          linearize_hashes_file = linearize_dir / "linearize-hashes.py"
  64          subprocess.run([sys.executable, linearize_hashes_file, cfg_file],
  65                         stdout=hash_list,
  66                         check=True)
  67  
  68          self.log.info("Run linearization of block data")
  69          linearize_data_file = linearize_dir / "linearize-data.py"
  70          subprocess.run([sys.executable, linearize_data_file, cfg_file],
  71                         check=True)
  72  
  73          self.log.info("Restart second, unsynced node with bootstrap file")
  74          self.restart_node(1, extra_args=[f"-loadblock={bootstrap_file}"])
  75          assert_equal(self.nodes[1].getblockcount(), 100)  # start_node is blocking on all block files being imported
  76  
  77          assert_equal(self.nodes[1].getblockchaininfo()['blocks'], 100)
  78          assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())
  79  
  80  
  81  if __name__ == '__main__':
  82      LoadblockTest(__file__).main()
  83