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) 2009-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#ifndef BITCOIN_KERNEL_CHAINPARAMS_H7#define BITCOIN_KERNEL_CHAINPARAMS_H9#include <consensus/params.h>10#include <kernel/messagestartchars.h>11#include <primitives/block.h>12#include <uint256.h>13#include <util/chaintype.h>14#include <util/hash_type.h>15#include <util/vector.h>17#include <cstddef>18#include <cstdint>19#include <memory>20#include <optional>21#include <string>22#include <unordered_map>23#include <vector>25struct AssumeutxoHash : public BaseHash<uint256> {26explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}27};29/**30* Holds configuration for use during UTXO snapshot load and validation. The contents31* here are security critical, since they dictate which UTXO snapshots are recognized32* as valid.33*/34struct AssumeutxoData {35int height;37//! The expected hash of the deserialized UTXO set.38AssumeutxoHash hash_serialized;40//! Used to populate the m_chain_tx_count value, which is used during BlockManager::LoadBlockIndex().41//!42//! We need to hardcode the value here because this is computed cumulatively using block data,43//! which we do not necessarily have at the time of snapshot load.44uint64_t m_chain_tx_count;46//! The hash of the base block for this snapshot. Used to refer to assumeutxo data47//! prior to having a loaded blockindex.48uint256 blockhash;49};51/**52* Holds various statistics on transactions within a chain. Used to estimate53* verification progress during chain sync.54*55* See also: CChainParams::TxData, GuessVerificationProgress.56*/57struct ChainTxData {58int64_t nTime; //!< UNIX timestamp of last known number of transactions59uint64_t tx_count; //!< total number of transactions between genesis and that timestamp60double dTxRate; //!< estimated number of transactions per second after that timestamp61};63//! Configuration for headers sync memory usage.64struct HeadersSyncParams {65//! Distance in blocks between header commitments.66size_t commitment_period{0};67//! Minimum number of validated headers to accumulate in the redownload68//! buffer before feeding them into the permanent block index.69size_t redownload_buffer_size{0};70};72/**73* CChainParams defines various tweakable parameters of a given instance of the74* Bitcoin system.75*/76class CChainParams77{78public:79enum Base58Type {80PUBKEY_ADDRESS,81SCRIPT_ADDRESS,82SECRET_KEY,83EXT_PUBLIC_KEY,84EXT_SECRET_KEY,86MAX_BASE58_TYPES87};89const Consensus::Params& GetConsensus() const { return consensus; }90const MessageStartChars& MessageStart() const { return pchMessageStart; }91uint16_t GetDefaultPort() const { return nDefaultPort; }92std::vector<int> GetAvailableSnapshotHeights() const;94const CBlock& GenesisBlock() const { return genesis; }95/** Default value for -checkmempool and -checkblockindex argument */96bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }97/** If this chain is exclusively used for testing */98bool IsTestChain() const { return m_chain_type != ChainType::MAIN; }99/** If this chain allows time to be mocked */100bool IsMockableChain() const { return m_is_mockable_chain; }101uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }102/** Minimum free space (in GB) needed for data directory */103uint64_t AssumedBlockchainSize() const { return m_assumed_blockchain_size; }104/** Minimum free space (in GB) needed for data directory when pruned; Does not include prune target*/105uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }106/** Whether it is possible to mine blocks on demand (no retargeting) */107bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }108/** Return the chain type string */109std::string GetChainTypeString() const { return ChainTypeToString(m_chain_type); }110/** Return the chain type */111ChainType GetChainType() const { return m_chain_type; }112/** Return the list of hostnames to look up for DNS seeds */113const std::vector<std::string>& DNSSeeds() const { return vSeeds; }114const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }115const std::string& Bech32HRP() const { return bech32_hrp; }116const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }117const HeadersSyncParams& HeadersSync() const { return m_headers_sync_params; }119std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const120{121return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.height == height; });122}123std::optional<AssumeutxoData> AssumeutxoForBlockhash(const uint256& blockhash) const124{125return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.blockhash == blockhash; });126}128const ChainTxData& TxData() const { return chainTxData; }130/**131* VersionBitsParameters holds activation parameters132*/133struct VersionBitsParameters {134int64_t start_time;135int64_t timeout;136int min_activation_height;137};139struct DeploymentOptions {140std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{};141std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{};142};144/**145* SigNetOptions holds configurations for creating a signet CChainParams.146*/147struct SigNetOptions {148DeploymentOptions dep_opts{};149std::optional<std::vector<uint8_t>> challenge{};150std::optional<std::vector<std::string>> seeds{};151};153/**154* RegTestOptions holds configurations for creating a regtest CChainParams.155*/156struct RegTestOptions {157DeploymentOptions dep_opts{};158bool fastprune{false};159bool enforce_bip94{false};160};162struct MainNetOptions {163DeploymentOptions dep_opts{};164};166struct TestNetOptions {167DeploymentOptions dep_opts{};168};170static std::unique_ptr<const CChainParams> RegTest(const RegTestOptions& options);171static std::unique_ptr<const CChainParams> RegTest() { const RegTestOptions opts{}; return RegTest(opts); }172static std::unique_ptr<const CChainParams> SigNet(const SigNetOptions& options);173static std::unique_ptr<const CChainParams> SigNet() { const SigNetOptions opts{}; return SigNet(opts); }174static std::unique_ptr<const CChainParams> Main(const MainNetOptions& options);175static std::unique_ptr<const CChainParams> Main() { const MainNetOptions opts{}; return Main(opts); }176static std::unique_ptr<const CChainParams> TestNet(const TestNetOptions& options);177static std::unique_ptr<const CChainParams> TestNet() { const TestNetOptions opts{}; return TestNet(opts); }178static std::unique_ptr<const CChainParams> TestNet4(const TestNetOptions& options);179static std::unique_ptr<const CChainParams> TestNet4() { const TestNetOptions opts{}; return TestNet4(opts); }181protected:182CChainParams() = default;184Consensus::Params consensus;185MessageStartChars pchMessageStart;186uint16_t nDefaultPort;187uint64_t nPruneAfterHeight;188uint64_t m_assumed_blockchain_size;189uint64_t m_assumed_chain_state_size;190std::vector<std::string> vSeeds;191std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];192std::string bech32_hrp;193ChainType m_chain_type;194CBlock genesis;195std::vector<uint8_t> vFixedSeeds;196bool fDefaultConsistencyChecks;197bool m_is_mockable_chain;198std::vector<AssumeutxoData> m_assumeutxo_data;199ChainTxData chainTxData;200HeadersSyncParams m_headers_sync_params;202void ApplyDeploymentOptions(const DeploymentOptions& opts);203};205std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);207#endif // BITCOIN_KERNEL_CHAINPARAMS_H