Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2017-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.5"""Test loadblock option7Test the option to start a node with the option loadblock which loads8a serialized blockchain from a file (usually called bootstrap.dat).9To generate that file this test uses the helper scripts available10in contrib/linearize.11"""13from pathlib import Path14import subprocess15import sys16import tempfile17import urllib19from test_framework.blocktools import COINBASE_MATURITY20from test_framework.test_framework import BitcoinTestFramework21from test_framework.util import assert_equal24class LoadblockTest(BitcoinTestFramework):25def set_test_params(self):26self.setup_clean_chain = True27self.num_nodes = 229def run_test(self):30self.nodes[1].setnetworkactive(state=False)31self.generate(self.nodes[0], COINBASE_MATURITY, sync_fun=self.no_op)33# Parsing the url of our node to get settings for config file34data_dir = self.nodes[0].datadir_path35node_url = urllib.parse.urlparse(self.nodes[0].url)36cfg_file = data_dir / "linearize.cfg"37bootstrap_file = Path(self.options.tmpdir) / "bootstrap.dat"38genesis_block = self.nodes[0].getblockhash(0)39blocks_dir = self.nodes[0].blocks_path40hash_list = tempfile.NamedTemporaryFile(dir=data_dir,41mode='w',42delete=False,43)45self.log.info("Create linearization config file")46with open(cfg_file, "a") as cfg:47cfg.write(f"datadir={data_dir}\n")48cfg.write(f"rpcuser={node_url.username}\n")49cfg.write(f"rpcpassword={node_url.password}\n")50cfg.write(f"port={node_url.port}\n")51cfg.write(f"host={node_url.hostname}\n")52cfg.write(f"output_file={bootstrap_file}\n")53cfg.write("max_height=100\n")54cfg.write("netmagic=fabfb5da\n")55cfg.write(f"input={blocks_dir}\n")56cfg.write(f"genesis={genesis_block}\n")57cfg.write(f"hashlist={hash_list.name}\n")59base_dir = self.config["environment"]["SRCDIR"]60linearize_dir = Path(base_dir) / "contrib" / "linearize"62self.log.info("Run linearization of block hashes")63linearize_hashes_file = linearize_dir / "linearize-hashes.py"64subprocess.run([sys.executable, linearize_hashes_file, cfg_file],65stdout=hash_list,66check=True)68self.log.info("Run linearization of block data")69linearize_data_file = linearize_dir / "linearize-data.py"70subprocess.run([sys.executable, linearize_data_file, cfg_file],71check=True)73self.log.info("Restart second, unsynced node with bootstrap file")74self.restart_node(1, extra_args=[f"-loadblock={bootstrap_file}"])75assert_equal(self.nodes[1].getblockcount(), 100) # start_node is blocking on all block files being imported77assert_equal(self.nodes[1].getblockchaininfo()['blocks'], 100)78assert_equal(self.nodes[0].getbestblockhash(), self.nodes[1].getbestblockhash())81if __name__ == '__main__':82LoadblockTest(__file__).main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.