Coverage Report

Created: 2026-06-16 16:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/kernel/chainparams.h
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present 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
6
#ifndef BITCOIN_KERNEL_CHAINPARAMS_H
7
#define BITCOIN_KERNEL_CHAINPARAMS_H
8
9
#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>
16
17
#include <cstddef>
18
#include <cstdint>
19
#include <memory>
20
#include <optional>
21
#include <string>
22
#include <unordered_map>
23
#include <vector>
24
25
struct AssumeutxoHash : public BaseHash<uint256> {
26
32.5k
    explicit AssumeutxoHash(const uint256& hash) : BaseHash(hash) {}
27
};
28
29
/**
30
 * Holds configuration for use during UTXO snapshot load and validation. The contents
31
 * here are security critical, since they dictate which UTXO snapshots are recognized
32
 * as valid.
33
 */
34
struct AssumeutxoData {
35
    int height;
36
37
    //! The expected hash of the deserialized UTXO set.
38
    AssumeutxoHash hash_serialized;
39
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.
44
    uint64_t m_chain_tx_count;
45
46
    //! The hash of the base block for this snapshot. Used to refer to assumeutxo data
47
    //! prior to having a loaded blockindex.
48
    uint256 blockhash;
49
};
50
51
/**
52
 * Holds various statistics on transactions within a chain. Used to estimate
53
 * verification progress during chain sync.
54
 *
55
 * See also: CChainParams::TxData, GuessVerificationProgress.
56
 */
57
struct ChainTxData {
58
    int64_t nTime;    //!< UNIX timestamp of last known number of transactions
59
    uint64_t tx_count; //!< total number of transactions between genesis and that timestamp
60
    double dTxRate;   //!< estimated number of transactions per second after that timestamp
61
};
62
63
//! Configuration for headers sync memory usage.
64
struct HeadersSyncParams {
65
    //! Distance in blocks between header commitments.
66
    size_t commitment_period{0};
67
    //! Minimum number of validated headers to accumulate in the redownload
68
    //! buffer before feeding them into the permanent block index.
69
    size_t redownload_buffer_size{0};
70
};
71
72
/**
73
 * CChainParams defines various tweakable parameters of a given instance of the
74
 * Bitcoin system.
75
 */
76
class CChainParams
77
{
78
public:
79
    enum Base58Type {
80
        PUBKEY_ADDRESS,
81
        SCRIPT_ADDRESS,
82
        SECRET_KEY,
83
        EXT_PUBLIC_KEY,
84
        EXT_SECRET_KEY,
85
86
        MAX_BASE58_TYPES
87
    };
88
89
7.22M
    const Consensus::Params& GetConsensus() const { return consensus; }
90
378k
    const MessageStartChars& MessageStart() const { return pchMessageStart; }
91
22.2k
    uint16_t GetDefaultPort() const { return nDefaultPort; }
92
    std::vector<int> GetAvailableSnapshotHeights() const;
93
94
5.45k
    const CBlock& GenesisBlock() const { return genesis; }
95
    /** Default value for -checkmempool and -checkblockindex argument */
96
9.62k
    bool DefaultConsistencyChecks() const { return fDefaultConsistencyChecks; }
97
    /** If this chain is exclusively used for testing */
98
528k
    bool IsTestChain() const { return m_chain_type != ChainType::MAIN; }
99
    /** If this chain allows time to be mocked */
100
1.37k
    bool IsMockableChain() const { return m_is_mockable_chain; }
101
139
    uint64_t PruneAfterHeight() const { return nPruneAfterHeight; }
102
    /** Minimum free space (in GB) needed for data directory */
103
425
    uint64_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*/
105
0
    uint64_t AssumedChainStateSize() const { return m_assumed_chain_state_size; }
106
    /** Whether it is possible to mine blocks on demand (no retargeting) */
107
45.3k
    bool MineBlocksOnDemand() const { return consensus.fPowNoRetargeting; }
108
    /** Return the chain type string */
109
16.3k
    std::string GetChainTypeString() const { return ChainTypeToString(m_chain_type); }
110
    /** Return the chain type */
111
18.3k
    ChainType GetChainType() const { return m_chain_type; }
112
    /** Return the list of hostnames to look up for DNS seeds */
113
13
    const std::vector<std::string>& DNSSeeds() const { return vSeeds; }
114
218k
    const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
115
114k
    const std::string& Bech32HRP() const { return bech32_hrp; }
116
3
    const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
117
9
    const HeadersSyncParams& HeadersSync() const { return m_headers_sync_params; }
118
119
    std::optional<AssumeutxoData> AssumeutxoForHeight(int height) const
120
62
    {
121
134
        return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.height == height; });
122
62
    }
123
    std::optional<AssumeutxoData> AssumeutxoForBlockhash(const uint256& blockhash) const
124
67
    {
125
159
        return FindFirst(m_assumeutxo_data, [&](const auto& d) { return d.blockhash == blockhash; });
126
67
    }
127
128
309k
    const ChainTxData& TxData() const { return chainTxData; }
129
130
    /**
131
     * VersionBitsParameters holds activation parameters
132
     */
133
    struct VersionBitsParameters {
134
        int64_t start_time;
135
        int64_t timeout;
136
        int min_activation_height;
137
    };
138
139
    struct DeploymentOptions {
140
        std::unordered_map<Consensus::DeploymentPos, VersionBitsParameters> version_bits_parameters{};
141
        std::unordered_map<Consensus::BuriedDeployment, int> activation_heights{};
142
    };
143
144
    /**
145
     * SigNetOptions holds configurations for creating a signet CChainParams.
146
     */
147
    struct SigNetOptions {
148
        DeploymentOptions dep_opts{};
149
        std::optional<std::vector<uint8_t>> challenge{};
150
        std::optional<std::vector<std::string>> seeds{};
151
    };
152
153
    /**
154
     * RegTestOptions holds configurations for creating a regtest CChainParams.
155
     */
156
    struct RegTestOptions {
157
        DeploymentOptions dep_opts{};
158
        bool fastprune{false};
159
        bool enforce_bip94{false};
160
    };
161
162
    struct MainNetOptions {
163
        DeploymentOptions dep_opts{};
164
    };
165
166
    struct TestNetOptions {
167
        DeploymentOptions dep_opts{};
168
    };
169
170
    static std::unique_ptr<const CChainParams> RegTest(const RegTestOptions& options);
171
8
    static std::unique_ptr<const CChainParams> RegTest() { const RegTestOptions opts{}; return RegTest(opts); }
172
    static std::unique_ptr<const CChainParams> SigNet(const SigNetOptions& options);
173
8
    static std::unique_ptr<const CChainParams> SigNet() { const SigNetOptions opts{}; return SigNet(opts); }
174
    static std::unique_ptr<const CChainParams> Main(const MainNetOptions& options);
175
9
    static std::unique_ptr<const CChainParams> Main() { const MainNetOptions opts{}; return Main(opts); }
176
    static std::unique_ptr<const CChainParams> TestNet(const TestNetOptions& options);
177
8
    static std::unique_ptr<const CChainParams> TestNet() { const TestNetOptions opts{}; return TestNet(opts); }
178
    static std::unique_ptr<const CChainParams> TestNet4(const TestNetOptions& options);
179
8
    static std::unique_ptr<const CChainParams> TestNet4() { const TestNetOptions opts{}; return TestNet4(opts); }
180
181
protected:
182
11.8k
    CChainParams() = default;
183
184
    Consensus::Params consensus;
185
    MessageStartChars pchMessageStart;
186
    uint16_t nDefaultPort;
187
    uint64_t nPruneAfterHeight;
188
    uint64_t m_assumed_blockchain_size;
189
    uint64_t m_assumed_chain_state_size;
190
    std::vector<std::string> vSeeds;
191
    std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];
192
    std::string bech32_hrp;
193
    ChainType m_chain_type;
194
    CBlock genesis;
195
    std::vector<uint8_t> vFixedSeeds;
196
    bool fDefaultConsistencyChecks;
197
    bool m_is_mockable_chain;
198
    std::vector<AssumeutxoData> m_assumeutxo_data;
199
    ChainTxData chainTxData;
200
    HeadersSyncParams m_headers_sync_params;
201
202
    void ApplyDeploymentOptions(const DeploymentOptions& opts);
203
};
204
205
std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& pchMessageStart);
206
207
#endif // BITCOIN_KERNEL_CHAINPARAMS_H