Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/chainparams.cpp
Line
Count
Source
1
// Copyright (c) 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
#include <chainparams.h>
7
8
#include <chainparamsbase.h>
9
#include <common/args.h>
10
#include <consensus/params.h>
11
#include <deploymentinfo.h>
12
#include <logging.h>
13
#include <tinyformat.h>
14
#include <util/chaintype.h>
15
#include <util/strencodings.h>
16
#include <util/string.h>
17
18
#include <cassert>
19
#include <cstdint>
20
#include <limits>
21
#include <stdexcept>
22
#include <vector>
23
24
using util::SplitString;
25
26
void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options)
27
1.96k
{
28
1.96k
    if (!args.GetArgs("-signetseednode").empty()) {
29
0
        options.seeds.emplace(args.GetArgs("-signetseednode"));
30
0
    }
31
1.96k
    if (!args.GetArgs("-signetchallenge").empty()) {
32
12
        const auto signet_challenge = args.GetArgs("-signetchallenge");
33
12
        if (signet_challenge.size() != 1) {
34
1
            throw std::runtime_error("-signetchallenge cannot be multiple values.");
35
1
        }
36
11
        const auto val{TryParseHex<uint8_t>(signet_challenge[0])};
37
11
        if (!val) {
38
1
            throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0]));
39
1
        }
40
10
        options.challenge.emplace(*val);
41
10
    }
42
1.96k
}
43
44
void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
45
3.21k
{
46
3.21k
    if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
47
3.21k
    if (HasTestOption(args, "bip94")) options.enforce_bip94 = true;
48
49
3.21k
    for (const std::string& arg : args.GetArgs("-testactivationheight")) {
50
37
        const auto found{arg.find('@')};
51
37
        if (found == std::string::npos) {
52
2
            throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
53
2
        }
54
55
35
        const auto value{arg.substr(found + 1)};
56
35
        const auto height{ToIntegral<int32_t>(value)};
57
35
        if (!height || *height < 0 || *height >= std::numeric_limits<int>::max()) {
58
2
            throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
59
2
        }
60
61
33
        const auto deployment_name{arg.substr(0, found)};
62
33
        if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
63
31
            options.activation_heights[*buried_deployment] = *height;
64
31
        } else {
65
2
            throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
66
2
        }
67
33
    }
68
69
3.20k
    for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
70
2
        std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
71
2
        if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
72
0
            throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
73
0
        }
74
2
        CChainParams::VersionBitsParameters vbparams{};
75
2
        const auto start_time{ToIntegral<int64_t>(vDeploymentParams[1])};
76
2
        if (!start_time) {
77
0
            throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
78
0
        }
79
2
        vbparams.start_time = *start_time;
80
2
        const auto timeout{ToIntegral<int64_t>(vDeploymentParams[2])};
81
2
        if (!timeout) {
82
0
            throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
83
0
        }
84
2
        vbparams.timeout = *timeout;
85
2
        if (vDeploymentParams.size() >= 4) {
86
1
            const auto min_activation_height{ToIntegral<int64_t>(vDeploymentParams[3])};
87
1
            if (!min_activation_height) {
88
0
                throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
89
0
            }
90
1
            vbparams.min_activation_height = *min_activation_height;
91
1
        } else {
92
1
            vbparams.min_activation_height = 0;
93
1
        }
94
2
        bool found = false;
95
2
        for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
96
2
            if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
97
2
                options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
98
2
                found = true;
99
2
                LogInfo("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d",
100
2
                        vDeploymentParams[0], vbparams.start_time, vbparams.timeout, vbparams.min_activation_height);
101
2
                break;
102
2
            }
103
2
        }
104
2
        if (!found) {
105
0
            throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
106
0
        }
107
2
    }
108
3.20k
}
109
110
static std::unique_ptr<const CChainParams> globalChainParams;
111
112
367k
const CChainParams &Params() {
113
367k
    assert(globalChainParams);
114
367k
    return *globalChainParams;
115
367k
}
116
117
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
118
11.6k
{
119
11.6k
    switch (chain) {
120
2.65k
    case ChainType::MAIN:
121
2.65k
        return CChainParams::Main();
122
1.91k
    case ChainType::TESTNET:
123
1.91k
        return CChainParams::TestNet();
124
1.88k
    case ChainType::TESTNET4:
125
1.88k
        return CChainParams::TestNet4();
126
1.96k
    case ChainType::SIGNET: {
127
1.96k
        auto opts = CChainParams::SigNetOptions{};
128
1.96k
        ReadSigNetArgs(args, opts);
129
1.96k
        return CChainParams::SigNet(opts);
130
0
    }
131
3.21k
    case ChainType::REGTEST: {
132
3.21k
        auto opts = CChainParams::RegTestOptions{};
133
3.21k
        ReadRegTestArgs(args, opts);
134
3.21k
        return CChainParams::RegTest(opts);
135
0
    }
136
11.6k
    }
137
11.6k
    assert(false);
138
0
}
139
140
void SelectParams(const ChainType chain)
141
2.38k
{
142
2.38k
    SelectBaseParams(chain);
143
2.38k
    globalChainParams = CreateChainParams(gArgs, chain);
144
2.38k
}