Coverage Report

Created: 2025-06-10 13:21

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