Bitcoin Core 31.99.0
P2P Digital Currency
chainparams.cpp
Go to the documentation of this file.
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
25
27{
28 for (const std::string& arg : args.GetArgs("-testactivationheight")) {
29 const auto found{arg.find('@')};
30 if (found == std::string::npos) {
31 throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
32 }
33
34 const auto value{arg.substr(found + 1)};
35 const auto height{ToIntegral<int32_t>(value)};
36 if (!height || *height < 0 || *height >= std::numeric_limits<int>::max()) {
37 throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
38 }
39
40 const auto deployment_name{arg.substr(0, found)};
41 if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
42 options.activation_heights[*buried_deployment] = *height;
43 } else {
44 throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
45 }
46 }
47
48 for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
49 std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
50 if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
51 throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
52 }
54 const auto start_time{ToIntegral<int64_t>(vDeploymentParams[1])};
55 if (!start_time) {
56 throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
57 }
58 vbparams.start_time = *start_time;
59 const auto timeout{ToIntegral<int64_t>(vDeploymentParams[2])};
60 if (!timeout) {
61 throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
62 }
63 vbparams.timeout = *timeout;
64 if (vDeploymentParams.size() >= 4) {
65 const auto min_activation_height{ToIntegral<int64_t>(vDeploymentParams[3])};
66 if (!min_activation_height) {
67 throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
68 }
69 vbparams.min_activation_height = *min_activation_height;
70 } else {
71 vbparams.min_activation_height = 0;
72 }
73 bool found = false;
74 for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
75 if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
77 found = true;
78 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 if (!found) {
84 throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
85 }
86 }
87}
88
90{
92}
93
95{
97}
98
100{
101 if (!args.GetArgs("-signetseednode").empty()) {
102 options.seeds.emplace(args.GetArgs("-signetseednode"));
103 }
104 if (!args.GetArgs("-signetchallenge").empty()) {
105 const auto signet_challenge = args.GetArgs("-signetchallenge");
106 if (signet_challenge.size() != 1) {
107 throw std::runtime_error("-signetchallenge cannot be multiple values.");
108 }
109 const auto val{TryParseHex<uint8_t>(signet_challenge[0])};
110 if (!val) {
111 throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0]));
112 }
113 options.challenge.emplace(*val);
114 }
116}
117
119{
120 if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
121 if (HasTestOption(args, "bip94")) options.enforce_bip94 = true;
122
124}
125
126static std::unique_ptr<const CChainParams> globalChainParams;
127
130 return *globalChainParams;
131}
132
133std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
134{
135 switch (chain) {
136 case ChainType::MAIN: {
137 auto opts = CChainParams::MainNetOptions{};
138 ReadMainNetArgs(args, opts);
139 return CChainParams::Main(opts);
140 }
141 case ChainType::TESTNET: {
142 auto opts = CChainParams::TestNetOptions{};
143 ReadTestNetArgs(args, opts);
144 return CChainParams::TestNet(opts);
145 }
146 case ChainType::TESTNET4: {
147 auto opts = CChainParams::TestNetOptions{};
148 ReadTestNetArgs(args, opts);
149 return CChainParams::TestNet4(opts);
150 }
151 case ChainType::SIGNET: {
152 auto opts = CChainParams::SigNetOptions{};
153 ReadSigNetArgs(args, opts);
154 return CChainParams::SigNet(opts);
155 }
156 case ChainType::REGTEST: {
157 auto opts = CChainParams::RegTestOptions{};
158 ReadRegTestArgs(args, opts);
159 return CChainParams::RegTest(opts);
160 }
161 }
162 assert(false);
163}
164
165void SelectParams(const ChainType chain)
166{
167 SelectBaseParams(chain);
169}
ArgsManager gArgs
Definition: args.cpp:40
bool HasTestOption(const ArgsManager &args, const std::string &test_option)
Checks if a particular test option is present in -test command-line arg options.
Definition: args.cpp:845
ArgsManager & args
Definition: bitcoind.cpp:280
void ReadTestNetArgs(const ArgsManager &args, CChainParams::TestNetOptions &options)
Definition: chainparams.cpp:94
static void HandleDeploymentArgs(const ArgsManager &args, CChainParams::DeploymentOptions &options)
Definition: chainparams.cpp:26
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
static std::unique_ptr< const CChainParams > globalChainParams
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
const CChainParams & Params()
Return the currently selected parameters.
void ReadSigNetArgs(const ArgsManager &args, CChainParams::SigNetOptions &options)
Definition: chainparams.cpp:99
void ReadRegTestArgs(const ArgsManager &args, CChainParams::RegTestOptions &options)
void ReadMainNetArgs(const ArgsManager &args, CChainParams::MainNetOptions &options)
Definition: chainparams.cpp:89
void SelectBaseParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain.
ChainType
Definition: chaintype.h:12
std::vector< std::string > GetArgs(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return a vector of strings of the given argument.
Definition: args.cpp:424
bool GetBoolArg(const std::string &strArg, bool fDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return boolean argument or default value.
Definition: args.cpp:573
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:77
static std::unique_ptr< const CChainParams > TestNet4()
Definition: chainparams.h:179
static std::unique_ptr< const CChainParams > Main()
Definition: chainparams.h:175
static std::unique_ptr< const CChainParams > TestNet()
Definition: chainparams.h:177
static std::unique_ptr< const CChainParams > SigNet()
Definition: chainparams.h:173
static std::unique_ptr< const CChainParams > RegTest()
Definition: chainparams.h:171
std::optional< Consensus::BuriedDeployment > GetBuriedDeployment(const std::string_view name)
const std::array< VBDeploymentInfo, Consensus::MAX_VERSION_BITS_DEPLOYMENTS > VersionBitsDeploymentInfo
#define LogInfo(...)
Definition: log.h:125
DeploymentPos
Definition: params.h:37
@ MAX_VERSION_BITS_DEPLOYMENTS
Definition: params.h:41
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:150
const char * name
Definition: rest.cpp:49
std::unordered_map< Consensus::BuriedDeployment, int > activation_heights
Definition: chainparams.h:141
std::unordered_map< Consensus::DeploymentPos, VersionBitsParameters > version_bits_parameters
Definition: chainparams.h:140
DeploymentOptions dep_opts
Definition: chainparams.h:163
RegTestOptions holds configurations for creating a regtest CChainParams.
Definition: chainparams.h:156
DeploymentOptions dep_opts
Definition: chainparams.h:157
SigNetOptions holds configurations for creating a signet CChainParams.
Definition: chainparams.h:147
DeploymentOptions dep_opts
Definition: chainparams.h:148
std::optional< std::vector< uint8_t > > challenge
Definition: chainparams.h:149
std::optional< std::vector< std::string > > seeds
Definition: chainparams.h:150
DeploymentOptions dep_opts
Definition: chainparams.h:167
VersionBitsParameters holds activation parameters.
Definition: chainparams.h:133
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
assert(!tx.IsCoinBase())