Inside Bitcoin
Opening the selected file…
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// Copyright (c) 2010 Satoshi Nakamoto2// Copyright (c) 2009-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.6#include <kernel/chainparams.h>8#include <chainparamsseeds.h>9#include <consensus/amount.h>10#include <consensus/merkle.h>11#include <consensus/params.h>12#include <crypto/hex_base.h>13#include <hash.h>14#include <kernel/messagestartchars.h>15#include <primitives/block.h>16#include <primitives/transaction.h>17#include <script/interpreter.h>18#include <script/script.h>19#include <script/verify_flags.h>20#include <uint256.h>21#include <util/chaintype.h>22#include <util/log.h>23#include <util/strencodings.h>25#include <algorithm>26#include <array>27#include <cassert>28#include <cstdint>29#include <cstring>30#include <iterator>31#include <map>32#include <span>33#include <utility>35using namespace util::hex_literals;37static CBlock CreateGenesisBlock(const char* pszTimestamp, const CScript& genesisOutputScript, uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)38{39CMutableTransaction txNew;40txNew.version = 1;41txNew.vin.resize(1);42txNew.vout.resize(1);43txNew.vin[0].scriptSig = CScript() << 486604799 << CScriptNum(4) << std::vector<unsigned char>((const unsigned char*)pszTimestamp, (const unsigned char*)pszTimestamp + strlen(pszTimestamp));44txNew.vout[0].nValue = genesisReward;45txNew.vout[0].scriptPubKey = genesisOutputScript;47CBlock genesis;48genesis.nTime = nTime;49genesis.nBits = nBits;50genesis.nNonce = nNonce;51genesis.nVersion = nVersion;52genesis.vtx.push_back(MakeTransactionRef(std::move(txNew)));53genesis.hashPrevBlock.SetNull();54genesis.hashMerkleRoot = BlockMerkleRoot(genesis);55return genesis;56}58/**59* Build the genesis block. Note that the output of its generation60* transaction cannot be spent since it did not originally exist in the61* database.62*63* CBlock(hash=000000000019d6, ver=1, hashPrevBlock=00000000000000, hashMerkleRoot=4a5e1e, nTime=1231006505, nBits=1d00ffff, nNonce=2083236893, vtx=1)64* CTransaction(hash=4a5e1e, ver=1, vin.size=1, vout.size=1, nLockTime=0)65* CTxIn(COutPoint(000000, -1), coinbase 04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73)66* CTxOut(nValue=50.00000000, scriptPubKey=0x5F1DF16B2B704C8A578D0B)67* vMerkleTree: 4a5e1e68*/69static CBlock CreateGenesisBlock(uint32_t nTime, uint32_t nNonce, uint32_t nBits, int32_t nVersion, const CAmount& genesisReward)70{71const char* pszTimestamp = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";72const CScript genesisOutputScript = CScript() << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f"_hex << OP_CHECKSIG;73return CreateGenesisBlock(pszTimestamp, genesisOutputScript, nTime, nNonce, nBits, nVersion, genesisReward);74}76void CChainParams::ApplyDeploymentOptions(const DeploymentOptions& opts)77{78for (const auto& [dep, height] : opts.activation_heights) {79switch (dep) {80case Consensus::BuriedDeployment::DEPLOYMENT_SEGWIT:81consensus.SegwitHeight = int{height};82break;83case Consensus::BuriedDeployment::DEPLOYMENT_HEIGHTINCB:84consensus.BIP34Height = int{height};85break;86case Consensus::BuriedDeployment::DEPLOYMENT_DERSIG:87consensus.BIP66Height = int{height};88break;89case Consensus::BuriedDeployment::DEPLOYMENT_CLTV:90consensus.BIP65Height = int{height};91break;92case Consensus::BuriedDeployment::DEPLOYMENT_CSV:93consensus.CSVHeight = int{height};94break;95}96}98for (const auto& [deployment_pos, version_bits_params] : opts.version_bits_parameters) {99consensus.vDeployments[deployment_pos].nStartTime = version_bits_params.start_time;100consensus.vDeployments[deployment_pos].nTimeout = version_bits_params.timeout;101consensus.vDeployments[deployment_pos].min_activation_height = version_bits_params.min_activation_height;102}103}105/**106* Main network on which people trade goods and services.107*/108class CMainParams : public CChainParams {109public:110CMainParams(const MainNetOptions& opts) {111m_chain_type = ChainType::MAIN;112consensus.signet_blocks = false;113consensus.signet_challenge.clear();114consensus.nSubsidyHalvingInterval = 210000;115consensus.script_flag_exceptions.emplace( // BIP16 exception116uint256{"00000000000002dc756eebf4f49723ed8d30cc28a5f108eb94b1ba88ac4f9c22"}, SCRIPT_VERIFY_NONE);117consensus.script_flag_exceptions.emplace( // Taproot exception118uint256{"0000000000000000000f14c35b2d841e986ab5441de8c585d5ffe55ea1e395ad"}, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS);119consensus.BIP34Height = 227931;120consensus.BIP34Hash = uint256{"000000000000024b89b42a942fe0d9fea3bb44ab7bd1b19115dd6a759c0808b8"};121consensus.BIP65Height = 388381; // 000000000000000004c2b624ed5d7756c508d90fd0da2c7c679febfa6c4735f0122consensus.BIP66Height = 363725; // 00000000000000000379eaa19dce8c9b722d46ae6a57c2f1a988119488b50931123consensus.CSVHeight = 419328; // 000000000000000004a1b34462cb8aeebd5799177f7a29cf28f2d1961716b5b5124consensus.SegwitHeight = 481824; // 0000000000000000001c8018d9cb3b742ef25114f27563e3fc4a1902167f9893125consensus.MinBIP9WarningHeight = 711648; // taproot activation height + miner confirmation window126consensus.powLimit = uint256{"00000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffff"};127consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks128consensus.nPowTargetSpacing = 10 * 60;129consensus.fPowAllowMinDifficultyBlocks = false;130consensus.enforce_BIP94 = false;131consensus.fPowNoRetargeting = false;132consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28;133consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nStartTime = Consensus::BIP9Deployment::NEVER_ACTIVE;134consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].nTimeout = Consensus::BIP9Deployment::NO_TIMEOUT;135consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].min_activation_height = 0; // No activation delay136consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].threshold = 1815; // 90%137consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].period = 2016;139ApplyDeploymentOptions(opts.dep_opts);141consensus.nMinimumChainWork = uint256{"000000000000000000000000000000000000000145ec036acc5ba740052af1a0"};142consensus.defaultAssumeValid = uint256{"00000000000000000000748969ec33043c0e52a763c6dd5193861f559f2c72e3"}; // 966143144/**145* The message start string is designed to be unlikely to occur in normal data.146* The characters are rarely used upper ASCII, not valid as UTF-8, and produce147* a large 32-bit integer with any alignment.148*/149pchMessageStart[0] = 0xf9;150pchMessageStart[1] = 0xbe;151pchMessageStart[2] = 0xb4;152pchMessageStart[3] = 0xd9;153nDefaultPort = 8333;154nPruneAfterHeight = 100000;155m_assumed_blockchain_size = 897;156m_assumed_chain_state_size = 14;158genesis = CreateGenesisBlock(1231006505, 2083236893, 0x1d00ffff, 1, 50 * COIN);159consensus.hashGenesisBlock = genesis.GetHash();160assert(consensus.hashGenesisBlock == uint256{"000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"});161assert(genesis.hashMerkleRoot == uint256{"4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"});163// Note that of those which support the service bits prefix, most only support a subset of164// possible options.165// This is fine at runtime as we'll fall back to using them as an addrfetch if they don't support the166// service bits we want, but we should get them updated to support all service bits wanted by any167// release ASAP to avoid it where possible.168vSeeds.emplace_back("dnsseed.bluematt.me."); // Matt Corallo, only supports x9169vSeeds.emplace_back("seed.bitcoin.jonasschnelli.ch."); // Jonas Schnelli, only supports x1, x5, x9, and xd170vSeeds.emplace_back("seed.btc.petertodd.net."); // Peter Todd, only supports x1, x5, x9, and xd171vSeeds.emplace_back("seed.bitcoin.sprovoost.nl."); // Sjors Provoost172vSeeds.emplace_back("dnsseed.emzy.de."); // Stephan Oeste173vSeeds.emplace_back("seed.bitcoin.wiz.biz."); // Jason Maurice174vSeeds.emplace_back("seed.mainnet.achownodes.xyz."); // Ava Chow, only supports x1, x5, x9, x49, x809, x849, xd, x400, x404, x408, x448, xc08, xc48, x40c176base58Prefixes[PUBKEY_ADDRESS] = std::vector<unsigned char>(1,0);177base58Prefixes[SCRIPT_ADDRESS] = std::vector<unsigned char>(1,5);178base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,128);179base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};180base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};182bech32_hrp = "bc";184vFixedSeeds = std::vector<uint8_t>(std::begin(chainparams_seed_main), std::end(chainparams_seed_main));186fDefaultConsistencyChecks = false;187m_is_mockable_chain = false;189m_assumeutxo_data = {190{191.height = 840'000,192.hash_serialized = AssumeutxoHash{uint256{"a2a5521b1b5ab65f67818e5e8eccabb7171a517f9e2382208f77687310768f96"}},193.m_chain_tx_count = 991032194,194.blockhash = uint256{"0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5"},195},196{197.height = 880'000,198.hash_serialized = AssumeutxoHash{uint256{"dbd190983eaf433ef7c15f78a278ae42c00ef52e0fd2a54953782175fbadcea9"}},199.m_chain_tx_count = 1145604538,200.blockhash = uint256{"000000000000000000010b17283c3c400507969a9c2afd1dcf2082ec5cca2880"},201},202{203.height = 910'000,204.hash_serialized = AssumeutxoHash{uint256{"4daf8a17b4902498c5787966a2b51c613acdab5df5db73f196fa59a4da2f1568"}},205.m_chain_tx_count = 1226586151,206.blockhash = uint256{"0000000000000000000108970acb9522ffd516eae17acddcb1bd16469194a821"},207},208{209.height = 935'000,210.hash_serialized = AssumeutxoHash{uint256{"e4b90ef9eae834f56c4b64d2d50143cee10ad87994c614d7d04125e2a6025050"}},211.m_chain_tx_count = 1305397408,212.blockhash = uint256{"0000000000000000000147034958af1652b2b91bba607beacc5e72a56f0fb5ee"},213},214{215.height = 965'000,216.hash_serialized = AssumeutxoHash{uint256{"4a8d794337118c0c615b574f817c7306c687584a537184b8d233df42bf477ec2"}},217.m_chain_tx_count = 1429611231,218.blockhash = uint256{"00000000000000000001595977e6000ce56129f5c9b4073e31ccc30b90b97da9"},219}220};222chainTxData = ChainTxData{223// Data from RPC: getchaintxstats 4096 00000000000000000000748969ec33043c0e52a763c6dd5193861f559f2c72e3224.nTime = 1788925573,225.tx_count = 1434982600,226.dTxRate = 7.851784479579372,227};229// Generated by headerssync-params.py on 2026-09-08.230m_headers_sync_params = HeadersSyncParams{231.commitment_period = 649,232.redownload_buffer_size = 15400, // 15400/649 = ~23.7 commitments233};234}235};237/**238* Testnet (v3): public test network which is reset from time to time.239*/240class CTestNetParams : public CChainParams {241public:242CTestNetParams(const TestNetOptions& opts) {243m_chain_type = ChainType::TESTNET;244consensus.signet_blocks = false;245consensus.signet_challenge.clear();246consensus.nSubsidyHalvingInterval = 210000;247consensus.script_flag_exceptions.emplace( // BIP16 exception248uint256{"00000000dd30457c001f4095d208cc1296b0eed002427aa599874af7a432b105"}, SCRIPT_VERIFY_NONE);249consensus.BIP34Height = 21111;250consensus.BIP34Hash = uint256{"0000000023b3a96d3484e5abb3755c413e7d41500f8e2a5c3f0dd01299cd8ef8"};