Coverage Report

Created: 2024-08-21 05:08

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