Coverage Report

Created: 2025-03-27 15:33

/root/bitcoin/src/chainparams.cpp
Line
Count
Source (jump to first uncovered line)
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
824
{
28
824
    if (!args.GetArgs("-signetseednode").empty()) {
29
0
        options.seeds.emplace(args.GetArgs("-signetseednode"));
30
0
    }
31
824
    if (!args.GetArgs("-signetchallenge").empty()) {
32
0
        const auto signet_challenge = args.GetArgs("-signetchallenge");
33
0
        if (signet_challenge.size() != 1) {
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) {
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
824
}
43
44
void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
45
1.64k
{
46
1.64k
    if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
47
1.64k
    if (HasTestOption(args, "bip94")) options.enforce_bip94 = true;
48
49
1.64k
    for (const std::string& arg : args.GetArgs("-testactivationheight")) {
50
1.64k
        const auto found{arg.find('@')};
51
1.64k
        if (found == std::string::npos) {
52
0
            throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
53
0
        }
54
55
1.64k
        const auto value{arg.substr(found + 1)};
56
1.64k
        int32_t height;
57
1.64k
        if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
58
0
            throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
59
0
        }
60
61
1.64k
        const auto deployment_name{arg.substr(0, found)};
62
1.64k
        if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
63
1.64k
            options.activation_heights[*buried_deployment] = height;
64
1.64k
        } else {
65
0
            throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
66
0
        }
67
1.64k
    }
68
69
1.64k
    for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
70
0
        std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
71
0
        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
0
        CChainParams::VersionBitsParameters vbparams{};
75
0
        if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
76
0
            throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
77
0
        }
78
0
        if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
79
0
            throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
80
0
        }
81
0
        if (vDeploymentParams.size() >= 4) {
82
0
            if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
83
0
                throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
84
0
            }
85
0
        } else {
86
0
            vbparams.min_activation_height = 0;
87
0
        }
88
0
        bool found = false;
89
0
        for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
90
0
            if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
91
0
                options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
92
0
                found = true;
93
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);
94
0
                break;
95
0
            }
96
0
        }
97
0
        if (!found) {
98
0
            throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
99
0
        }
100
0
    }
101
1.64k
}
102
103
static std::unique_ptr<const CChainParams> globalChainParams;
104
105
4.33M
const CChainParams &Params() {
106
4.33M
    assert(globalChainParams);
107
4.33M
    return *globalChainParams;
108
4.33M
}
109
110
std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
111
4.94k
{
112
4.94k
    switch (chain) {
113
824
    case ChainType::MAIN:
114
824
        return CChainParams::Main();
115
824
    case ChainType::TESTNET:
116
824
        return CChainParams::TestNet();
117
824
    case ChainType::TESTNET4:
118
824
        return CChainParams::TestNet4();
119
824
    case ChainType::SIGNET: {
120
824
        auto opts = CChainParams::SigNetOptions{};
121
824
        ReadSigNetArgs(args, opts);
122
824
        return CChainParams::SigNet(opts);
123
0
    }
124
1.64k
    case ChainType::REGTEST: {
125
1.64k
        auto opts = CChainParams::RegTestOptions{};
126
1.64k
        ReadRegTestArgs(args, opts);
127
1.64k
        return CChainParams::RegTest(opts);
128
0
    }
129
4.94k
    }
130
0
    assert(false);
131
0
}
132
133
void SelectParams(const ChainType chain)
134
824
{
135
824
    SelectBaseParams(chain);
136
824
    globalChainParams = CreateChainParams(gArgs, chain);
137
824
}