Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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 <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
25
27{
28 if (args.IsArgSet("-signetseednode")) {
29 options.seeds.emplace(args.GetArgs("-signetseednode"));
30 }
31 if (args.IsArgSet("-signetchallenge")) {
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
48 for (const std::string& arg : args.GetArgs("-testactivationheight")) {
49 const auto found{arg.find('@')};
50 if (found == std::string::npos) {
51 throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
52 }
53
54 const auto value{arg.substr(found + 1)};
55 int32_t height;
56 if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
57 throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
58 }
59
60 const auto deployment_name{arg.substr(0, found)};
61 if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
62 options.activation_heights[*buried_deployment] = height;
63 } else {
64 throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
65 }
66 }
67
68 if (!args.IsArgSet("-vbparams")) return;
69
70 for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
71 std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
72 if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
73 throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
74 }
76 if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
77 throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
78 }
79 if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
80 throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
81 }
82 if (vDeploymentParams.size() >= 4) {
83 if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
84 throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
85 }
86 } else {
87 vbparams.min_activation_height = 0;
88 }
89 bool found = false;
90 for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
91 if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
93 found = true;
94 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);
95 break;
96 }
97 }
98 if (!found) {
99 throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
100 }
101 }
102}
103
104static std::unique_ptr<const CChainParams> globalChainParams;
105
110
111std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
112{
113 switch (chain) {
114 case ChainType::MAIN:
115 return CChainParams::Main();
117 return CChainParams::TestNet();
119 return CChainParams::TestNet4();
120 case ChainType::SIGNET: {
121 auto opts = CChainParams::SigNetOptions{};
122 ReadSigNetArgs(args, opts);
123 return CChainParams::SigNet(opts);
124 }
125 case ChainType::REGTEST: {
126 auto opts = CChainParams::RegTestOptions{};
127 ReadRegTestArgs(args, opts);
128 return CChainParams::RegTest(opts);
129 }
130 }
131 assert(false);
132}
133
134void SelectParams(const ChainType chain)
135{
136 SelectBaseParams(chain);
138}
ArgsManager gArgs
Definition args.cpp:41
ArgsManager & args
Definition bitcoind.cpp:270
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)
void ReadRegTestArgs(const ArgsManager &args, CChainParams::RegTestOptions &options)
void SelectBaseParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain.
ChainType
Definition chaintype.h:11
std::vector< std::string > GetArgs(const std::string &strArg) const
Return a vector of strings of the given argument.
Definition args.cpp:361
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition args.cpp:370
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition args.cpp:506
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition chainparams.h:81
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 > TestNet4()
static std::unique_ptr< const CChainParams > SigNet(const SigNetOptions &options)
std::optional< Consensus::BuriedDeployment > GetBuriedDeployment(const std::string_view name)
const struct VBDeploymentInfo VersionBitsDeploymentInfo[Consensus::MAX_VERSION_BITS_DEPLOYMENTS]
#define LogPrintf(...)
Definition logging.h:274
DeploymentPos
Definition params.h:32
@ MAX_VERSION_BITS_DEPLOYMENTS
Definition params.h:36
std::vector< std::string > SplitString(std::string_view str, char sep)
Definition string.h:59
const char * name
Definition rest.cpp:49
RegTestOptions holds configurations for creating a regtest CChainParams.
std::unordered_map< Consensus::DeploymentPos, VersionBitsParameters > version_bits_parameters
std::unordered_map< Consensus::BuriedDeployment, int > activation_heights
SigNetOptions holds configurations for creating a signet CChainParams.
std::optional< std::vector< uint8_t > > challenge
std::optional< std::vector< std::string > > seeds
VersionBitsParameters holds activation parameters.
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
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.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
assert(!tx.IsCoinBase())