/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 <tinyformat.h> |
13 | | #include <util/chaintype.h> |
14 | | #include <util/log.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 | | static void HandleDeploymentArgs(const ArgsManager& args, CChainParams::DeploymentOptions& options) |
27 | 162 | { |
28 | 162 | for (const std::string& arg : args.GetArgs("-testactivationheight")) { Branch (28:33): [True: 0, False: 162]
|
29 | 0 | const auto found{arg.find('@')}; |
30 | 0 | if (found == std::string::npos) { Branch (30:13): [True: 0, False: 0]
|
31 | 0 | throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg)); |
32 | 0 | } |
33 | | |
34 | 0 | const auto value{arg.substr(found + 1)}; |
35 | 0 | const auto height{ToIntegral<int32_t>(value)}; |
36 | 0 | if (!height || *height < 0 || *height >= std::numeric_limits<int>::max()) { Branch (36:13): [True: 0, False: 0]
Branch (36:24): [True: 0, False: 0]
Branch (36:39): [True: 0, False: 0]
|
37 | 0 | throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg)); |
38 | 0 | } |
39 | | |
40 | 0 | const auto deployment_name{arg.substr(0, found)}; |
41 | 0 | if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) { Branch (41:24): [True: 0, False: 0]
|
42 | 0 | options.activation_heights[*buried_deployment] = *height; |
43 | 0 | } else { |
44 | 0 | throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg)); |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | 162 | for (const std::string& strDeployment : args.GetArgs("-vbparams")) { Branch (48:43): [True: 0, False: 162]
|
49 | 0 | std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':'); |
50 | 0 | if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) { Branch (50:13): [True: 0, False: 0]
Branch (50:45): [True: 0, False: 0]
|
51 | 0 | throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]"); |
52 | 0 | } |
53 | 0 | CChainParams::VersionBitsParameters vbparams{}; |
54 | 0 | const auto start_time{ToIntegral<int64_t>(vDeploymentParams[1])}; |
55 | 0 | if (!start_time) { Branch (55:13): [True: 0, False: 0]
|
56 | 0 | throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1])); |
57 | 0 | } |
58 | 0 | vbparams.start_time = *start_time; |
59 | 0 | const auto timeout{ToIntegral<int64_t>(vDeploymentParams[2])}; |
60 | 0 | if (!timeout) { Branch (60:13): [True: 0, False: 0]
|
61 | 0 | throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2])); |
62 | 0 | } |
63 | 0 | vbparams.timeout = *timeout; |
64 | 0 | if (vDeploymentParams.size() >= 4) { Branch (64:13): [True: 0, False: 0]
|
65 | 0 | const auto min_activation_height{ToIntegral<int64_t>(vDeploymentParams[3])}; |
66 | 0 | if (!min_activation_height) { Branch (66:17): [True: 0, False: 0]
|
67 | 0 | throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3])); |
68 | 0 | } |
69 | 0 | vbparams.min_activation_height = *min_activation_height; |
70 | 0 | } else { |
71 | 0 | vbparams.min_activation_height = 0; |
72 | 0 | } |
73 | 0 | bool found = false; |
74 | 0 | for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) { Branch (74:23): [True: 0, False: 0]
|
75 | 0 | if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) { Branch (75:17): [True: 0, False: 0]
|
76 | 0 | options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams; |
77 | 0 | found = true; |
78 | 0 | LogInfo("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d", |
79 | 0 | vDeploymentParams[0], vbparams.start_time, vbparams.timeout, vbparams.min_activation_height); |
80 | 0 | break; |
81 | 0 | } |
82 | 0 | } |
83 | 0 | if (!found) { Branch (83:13): [True: 0, False: 0]
|
84 | 0 | throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0])); |
85 | 0 | } |
86 | 0 | } |
87 | 162 | } |
88 | | |
89 | | void ReadMainNetArgs(const ArgsManager& args, CChainParams::MainNetOptions& options) |
90 | 27 | { |
91 | 27 | HandleDeploymentArgs(args, options.dep_opts); |
92 | 27 | } |
93 | | |
94 | | void ReadTestNetArgs(const ArgsManager& args, CChainParams::TestNetOptions& options) |
95 | 54 | { |
96 | 54 | HandleDeploymentArgs(args, options.dep_opts); |
97 | 54 | } |
98 | | |
99 | | void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options) |
100 | 27 | { |
101 | 27 | if (!args.GetArgs("-signetseednode").empty()) { Branch (101:9): [True: 0, False: 27]
|
102 | 0 | options.seeds.emplace(args.GetArgs("-signetseednode")); |
103 | 0 | } |
104 | 27 | if (!args.GetArgs("-signetchallenge").empty()) { Branch (104:9): [True: 0, False: 27]
|
105 | 0 | const auto signet_challenge = args.GetArgs("-signetchallenge"); |
106 | 0 | if (signet_challenge.size() != 1) { Branch (106:13): [True: 0, False: 0]
|
107 | 0 | throw std::runtime_error("-signetchallenge cannot be multiple values."); |
108 | 0 | } |
109 | 0 | const auto val{TryParseHex<uint8_t>(signet_challenge[0])}; |
110 | 0 | if (!val) { Branch (110:13): [True: 0, False: 0]
|
111 | 0 | throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0])); |
112 | 0 | } |
113 | 0 | options.challenge.emplace(*val); |
114 | 0 | } |
115 | 27 | HandleDeploymentArgs(args, options.dep_opts); |
116 | 27 | } |
117 | | |
118 | | void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options) |
119 | 54 | { |
120 | 54 | if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value; Branch (120:14): [True: 0, False: 54]
|
121 | 54 | if (HasTestOption(args, "bip94")) options.enforce_bip94 = true; Branch (121:9): [True: 0, False: 54]
|
122 | | |
123 | 54 | HandleDeploymentArgs(args, options.dep_opts); |
124 | 54 | } |
125 | | |
126 | | static std::unique_ptr<const CChainParams> globalChainParams; |
127 | | |
128 | 364k | const CChainParams &Params() { |
129 | 364k | assert(globalChainParams); Branch (129:5): [True: 364k, False: 0]
|
130 | 364k | return *globalChainParams; |
131 | 364k | } |
132 | | |
133 | | std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain) |
134 | 162 | { |
135 | 162 | switch (chain) { Branch (135:13): [True: 0, False: 162]
|
136 | 27 | case ChainType::MAIN: { Branch (136:5): [True: 27, False: 135]
|
137 | 27 | auto opts = CChainParams::MainNetOptions{}; |
138 | 27 | ReadMainNetArgs(args, opts); |
139 | 27 | return CChainParams::Main(opts); |
140 | 0 | } |
141 | 27 | case ChainType::TESTNET: { Branch (141:5): [True: 27, False: 135]
|
142 | 27 | auto opts = CChainParams::TestNetOptions{}; |
143 | 27 | ReadTestNetArgs(args, opts); |
144 | 27 | return CChainParams::TestNet(opts); |
145 | 0 | } |
146 | 27 | case ChainType::TESTNET4: { Branch (146:5): [True: 27, False: 135]
|
147 | 27 | auto opts = CChainParams::TestNetOptions{}; |
148 | 27 | ReadTestNetArgs(args, opts); |
149 | 27 | return CChainParams::TestNet4(opts); |
150 | 0 | } |
151 | 27 | case ChainType::SIGNET: { Branch (151:5): [True: 27, False: 135]
|
152 | 27 | auto opts = CChainParams::SigNetOptions{}; |
153 | 27 | ReadSigNetArgs(args, opts); |
154 | 27 | return CChainParams::SigNet(opts); |
155 | 0 | } |
156 | 54 | case ChainType::REGTEST: { Branch (156:5): [True: 54, False: 108]
|
157 | 54 | auto opts = CChainParams::RegTestOptions{}; |
158 | 54 | ReadRegTestArgs(args, opts); |
159 | 54 | return CChainParams::RegTest(opts); |
160 | 0 | } |
161 | 162 | } |
162 | 162 | assert(false); Branch (162:5): [Folded - Ignored]
|
163 | 0 | } |
164 | | |
165 | | void SelectParams(const ChainType chain) |
166 | 27 | { |
167 | 27 | SelectBaseParams(chain); |
168 | 27 | globalChainParams = CreateChainParams(gArgs, chain); |
169 | 27 | } |