Bitcoin Core  31.0.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 <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 
27 {
28  if (!args.GetArgs("-signetseednode").empty()) {
29  options.seeds.emplace(args.GetArgs("-signetseednode"));
30  }
31  if (!args.GetArgs("-signetchallenge").empty()) {
32  const auto signet_challenge = args.GetArgs("-signetchallenge");
33  if (signet_challenge.size() != 1) {
34  throw std::runtime_error("-signetchallenge cannot be multiple values.");
35  }
36  const auto val{TryParseHex<uint8_t>(signet_challenge[0])};
37  if (!val) {
38  throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0]));
39  }
40  options.challenge.emplace(*val);
41  }
42 }
43 
45 {
46  if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
47  if (HasTestOption(args, "bip94")) options.enforce_bip94 = true;
48 
49  for (const std::string& arg : args.GetArgs("-testactivationheight")) {
50  const auto found{arg.find('@')};
51  if (found == std::string::npos) {
52  throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
53  }
54 
55  const auto value{arg.substr(found + 1)};
56  const auto height{ToIntegral<int32_t>(value)};
57  if (!height || *height < 0 || *height >= std::numeric_limits<int>::max()) {
58  throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
59  }
60 
61  const auto deployment_name{arg.substr(0, found)};
62  if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
63  options.activation_heights[*buried_deployment] = *height;
64  } else {
65  throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
66  }
67  }
68 
69  for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
70  std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
71  if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
72  throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
73  }
75  const auto start_time{ToIntegral<int64_t>(vDeploymentParams[1])};
76  if (!start_time) {
77  throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
78  }
79  vbparams.start_time = *start_time;
80  const auto timeout{ToIntegral<int64_t>(vDeploymentParams[2])};
81  if (!timeout) {
82  throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
83  }
84  vbparams.timeout = *timeout;
85  if (vDeploymentParams.size() >= 4) {
86  const auto min_activation_height{ToIntegral<int64_t>(vDeploymentParams[3])};
87  if (!min_activation_height) {
88  throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
89  }
90  vbparams.min_activation_height = *min_activation_height;
91  } else {
92  vbparams.min_activation_height = 0;
93  }
94  bool found = false;
95  for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
96  if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
98  found = true;
99  LogInfo("Setting version bits activation parameters for %s to start=%ld, timeout=%ld, min_activation_height=%d",
100  vDeploymentParams[0], vbparams.start_time, vbparams.timeout, vbparams.min_activation_height);
101  break;
102  }
103  }
104  if (!found) {
105  throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
106  }
107  }
108 }
109 
110 static std::unique_ptr<const CChainParams> globalChainParams;
111 
114  return *globalChainParams;
115 }
116 
117 std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
118 {
119  switch (chain) {
120  case ChainType::MAIN:
121  return CChainParams::Main();
122  case ChainType::TESTNET:
123  return CChainParams::TestNet();
124  case ChainType::TESTNET4:
125  return CChainParams::TestNet4();
126  case ChainType::SIGNET: {
127  auto opts = CChainParams::SigNetOptions{};
128  ReadSigNetArgs(args, opts);
129  return CChainParams::SigNet(opts);
130  }
131  case ChainType::REGTEST: {
132  auto opts = CChainParams::RegTestOptions{};
133  ReadRegTestArgs(args, opts);
134  return CChainParams::RegTest(opts);
135  }
136  }
137  assert(false);
138 }
139 
140 void SelectParams(const ChainType chain)
141 {
142  SelectBaseParams(chain);
144 }
static std::unique_ptr< const CChainParams > TestNet4()
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:149
assert(!tx.IsCoinBase())
std::unordered_map< Consensus::DeploymentPos, VersionBitsParameters > version_bits_parameters
Definition: chainparams.h:151
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
DeploymentPos
Definition: params.h:34
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
Definition: chainparams.h:76
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:515
void ReadRegTestArgs(const ArgsManager &args, CChainParams::RegTestOptions &options)
Definition: chainparams.cpp:44
void ReadSigNetArgs(const ArgsManager &args, CChainParams::SigNetOptions &options)
Definition: chainparams.cpp:26
static std::unique_ptr< const CChainParams > Main()
static std::unique_ptr< const CChainParams > RegTest(const RegTestOptions &options)
ChainType
Definition: chaintype.h:11
ArgsManager & args
Definition: bitcoind.cpp:277
VersionBitsParameters holds activation parameters.
Definition: chainparams.h:141
const char * name
Definition: rest.cpp:48
#define LogInfo(...)
Definition: log.h:95
RegTestOptions holds configurations for creating a regtest CChainParams.
Definition: chainparams.h:150
const std::array< VBDeploymentInfo, Consensus::MAX_VERSION_BITS_DEPLOYMENTS > VersionBitsDeploymentInfo
for(const CTxIn &txin :tx.vin)
Definition: validation.cpp:405
static std::unique_ptr< const CChainParams > globalChainParams
std::optional< std::vector< std::string > > seeds
Definition: chainparams.h:135
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:749
SigNetOptions holds configurations for creating a signet CChainParams.
Definition: chainparams.h:133
ArgsManager gArgs
Definition: args.cpp:40
if(!SetupNetworking())
std::unordered_map< Consensus::BuriedDeployment, int > activation_heights
Definition: chainparams.h:152
static std::unique_ptr< const CChainParams > SigNet(const SigNetOptions &options)
void SelectBaseParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain.
static std::unique_ptr< const CChainParams > TestNet()
const CChainParams & Params()
Return the currently selected parameters.
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const ChainType chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: args.cpp:366
std::optional< std::vector< uint8_t > > challenge
Definition: chainparams.h:134
std::optional< Consensus::BuriedDeployment > GetBuriedDeployment(const std::string_view name)
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.