Bitcoin Core  24.99.0
P2P Digital Currency
chainparams.cpp
Go to the documentation of this file.
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 <chainparamsseeds.h>
9 #include <consensus/merkle.h>
10 #include <deploymentinfo.h>
11 #include <hash.h> // for signet block challenge hash
12 #include <script/interpreter.h>
13 #include <util/string.h>
14 #include <util/system.h>
15 
16 #include <assert.h>
17 
19 {
20  if (args.IsArgSet("-signetseednode")) {
21  options.seeds.emplace(args.GetArgs("-signetseednode"));
22  }
23  if (args.IsArgSet("-signetchallenge")) {
24  const auto signet_challenge = args.GetArgs("-signetchallenge");
25  if (signet_challenge.size() != 1) {
26  throw std::runtime_error(strprintf("%s: -signetchallenge cannot be multiple values.", __func__));
27  }
28  options.challenge.emplace(ParseHex(signet_challenge[0]));
29  }
30 }
31 
33 {
34  if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
35 
36  for (const std::string& arg : args.GetArgs("-testactivationheight")) {
37  const auto found{arg.find('@')};
38  if (found == std::string::npos) {
39  throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
40  }
41 
42  const auto value{arg.substr(found + 1)};
43  int32_t height;
44  if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
45  throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
46  }
47 
48  const auto deployment_name{arg.substr(0, found)};
49  if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
50  options.activation_heights[*buried_deployment] = height;
51  } else {
52  throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
53  }
54  }
55 
56  if (!args.IsArgSet("-vbparams")) return;
57 
58  for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
59  std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
60  if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
61  throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
62  }
64  if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
65  throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
66  }
67  if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
68  throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
69  }
70  if (vDeploymentParams.size() >= 4) {
71  if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
72  throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
73  }
74  } else {
75  vbparams.min_activation_height = 0;
76  }
77  bool found = false;
78  for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
79  if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
81  found = true;
82  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);
83  break;
84  }
85  }
86  if (!found) {
87  throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
88  }
89  }
90 }
91 
92 static std::unique_ptr<const CChainParams> globalChainParams;
93 
94 const CChainParams &Params() {
96  return *globalChainParams;
97 }
98 
99 std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const std::string& chain)
100 {
101  if (chain == CBaseChainParams::MAIN) {
102  return CChainParams::Main();
103  } else if (chain == CBaseChainParams::TESTNET) {
104  return CChainParams::TestNet();
105  } else if (chain == CBaseChainParams::SIGNET) {
106  auto opts = CChainParams::SigNetOptions{};
107  ReadSigNetArgs(args, opts);
108  return CChainParams::SigNet(opts);
109  } else if (chain == CBaseChainParams::REGTEST) {
110  auto opts = CChainParams::RegTestOptions{};
111  ReadRegTestArgs(args, opts);
112  return CChainParams::RegTest(opts);
113  }
114  throw std::runtime_error(strprintf("%s: Unknown chain %s.", __func__, chain));
115 }
116 
117 void SelectParams(const std::string& network)
118 {
119  SelectBaseParams(network);
121 }
std::unique_ptr< const CChainParams > CreateChainParams(const ArgsManager &args, const std::string &chain)
Creates and returns a std::unique_ptr<CChainParams> of the chosen chain.
Definition: chainparams.cpp:99
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
static std::unique_ptr< const CChainParams > globalChainParams
Definition: chainparams.cpp:92
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:94
void ReadSigNetArgs(const ArgsManager &args, CChainParams::SigNetOptions &options)
Definition: chainparams.cpp:18
void ReadRegTestArgs(const ArgsManager &args, CChainParams::RegTestOptions &options)
Definition: chainparams.cpp:32
void SelectBaseParams(const std::string &chain)
Sets the params returned by Params() to those for the given network.
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition: system.cpp:470
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: system.cpp:479
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: system.cpp:615
static const std::string REGTEST
static const std::string TESTNET
static const std::string SIGNET
static const std::string MAIN
Chain name strings.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:76
static std::unique_ptr< const CChainParams > Main()
static std::unique_ptr< const CChainParams > RegTest(const RegTestOptions &options)
static std::unique_ptr< const CChainParams > TestNet()
static std::unique_ptr< const CChainParams > SigNet(const SigNetOptions &options)
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS]
std::optional< Consensus::BuriedDeployment > GetBuriedDeployment(const std::string_view name)
#define LogPrintf(...)
Definition: logging.h:236
DeploymentPos
Definition: params.h:32
@ MAX_VERSION_BITS_DEPLOYMENTS
Definition: params.h:36
ArgsManager args
const char * name
Definition: rest.cpp:46
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:65
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition: string.h:21
RegTestOptions holds configurations for creating a regtest CChainParams.
Definition: chainparams.h:152
std::unordered_map< Consensus::DeploymentPos, VersionBitsParameters > version_bits_parameters
Definition: chainparams.h:153
std::unordered_map< Consensus::BuriedDeployment, int > activation_heights
Definition: chainparams.h:154
SigNetOptions holds configurations for creating a signet CChainParams.
Definition: chainparams.h:135
std::optional< std::vector< uint8_t > > challenge
Definition: chainparams.h:136
std::optional< std::vector< std::string > > seeds
Definition: chainparams.h:137
VersionBitsParameters holds activation parameters.
Definition: chainparams.h:143
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1162
bool ParseInt32(std::string_view str, int32_t *out)
Convert string to signed 32-bit integer with strict parse error feedback.
bool ParseInt64(std::string_view str, int64_t *out)
Convert string to signed 64-bit integer with strict parse error feedback.
ArgsManager gArgs
Definition: system.cpp:73
assert(!tx.IsCoinBase())