|
| 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() |
0 commit comments