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) 2025-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 mining on an alternate mainnet7Test mining related RPCs that involve difficulty adjustment, which8regtest doesn't have.10It uses an alternate mainnet chain. See data/README.md for how it was generated.12Mine one retarget period worth of blocks with a short interval in13order to maximally raise the difficulty. Verify this using the getmininginfo RPC.15"""17from test_framework.test_framework import BitcoinTestFramework18from test_framework.util import (19assert_equal,20)21from test_framework.blocktools import (22DIFF_1_N_BITS,23DIFF_1_TARGET,24DIFF_4_N_BITS,25DIFF_4_TARGET,26create_coinbase,27nbits_str,28target_str29)31from test_framework.messages import (32CBlock,33SEQUENCE_FINAL,34)36import json37import os39# See data/README.md40COINBASE_SCRIPT_PUBKEY="76a914eadbac7f36c37e39361168b7aaee3cb24a25312d88ac"42class MiningMainnetTest(BitcoinTestFramework):44def set_test_params(self):45self.num_nodes = 146self.setup_clean_chain = True47self.chain = "" # main49def add_options(self, parser):50parser.add_argument(51'--datafile',52default='data/mainnet_alt.json',53help='Block data file (default: %(default)s)',54)56def mine(self, height, prev_hash, blocks, node):57self.log.debug(f"height={height}")58block = CBlock()59block.nVersion = 0x2000000060block.hashPrevBlock = int(prev_hash, 16)61block.nTime = blocks['timestamps'][height - 1]62block.nBits = DIFF_1_N_BITS if height < 2016 else DIFF_4_N_BITS63block.nNonce = blocks['nonces'][height - 1]64block.vtx = [create_coinbase(height=height, script_pubkey=bytes.fromhex(COINBASE_SCRIPT_PUBKEY), halving_period=210000)]65# The alternate mainnet chain was mined with non-timelocked coinbase txs.66block.vtx[0].nLockTime = 067block.vtx[0].vin[0].nSequence = SEQUENCE_FINAL68block.hashMerkleRoot = block.calc_merkle_root()69block_hex = block.serialize(with_witness=False).hex()70self.log.debug(block_hex)71assert_equal(node.submitblock(block_hex), None)72prev_hash = node.getbestblockhash()73assert_equal(prev_hash, block.hash_hex)74return prev_hash77def run_test(self):78node = self.nodes[0]79# Clear disk space warning80node.stderr.seek(0)81node.stderr.truncate()82self.log.info("Load alternative mainnet blocks")83path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.options.datafile)84prev_hash = node.getbestblockhash()85blocks = None86with open(path) as f:87blocks = json.load(f)88n_blocks = len(blocks['timestamps'])89assert_equal(n_blocks, 2016)91# Mine up to the last block of the first retarget period92for i in range(2015):93prev_hash = self.mine(i + 1, prev_hash, blocks, node)95assert_equal(node.getblockcount(), 2015)97self.log.info("Check difficulty adjustment with getmininginfo")98mining_info = node.getmininginfo()99assert_equal(mining_info['difficulty'], 1)100assert_equal(mining_info['bits'], nbits_str(DIFF_1_N_BITS))101assert_equal(mining_info['target'], target_str(DIFF_1_TARGET))103assert_equal(mining_info['next']['height'], 2016)104assert_equal(mining_info['next']['difficulty'], 4)105assert_equal(mining_info['next']['bits'], nbits_str(DIFF_4_N_BITS))106assert_equal(mining_info['next']['target'], target_str(DIFF_4_TARGET))108# Mine first block of the second retarget period109height = 2016110prev_hash = self.mine(height, prev_hash, blocks, node)111assert_equal(node.getblockcount(), height)113mining_info = node.getmininginfo()114assert_equal(mining_info['difficulty'], 4)116self.log.info("getblock RPC should show historical target")117block_info = node.getblock(node.getblockhash(1))119assert_equal(block_info['difficulty'], 1)120assert_equal(block_info['bits'], nbits_str(DIFF_1_N_BITS))121assert_equal(block_info['target'], target_str(DIFF_1_TARGET))124if __name__ == '__main__':125MiningMainnetTest(__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.