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