2013-05-07 15:16:25 +02:00
// Copyright (c) 2010 Satoshi Nakamoto
2022-12-24 23:49:50 +00:00
// Copyright (c) 2009-2022 The Bitcoin Core developers
2014-10-25 17:24:16 +08:00
// Distributed under the MIT software license, see the accompanying
2013-05-07 15:16:25 +02:00
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2017-11-10 13:57:53 +13:00
# include <chainparams.h>
2013-04-13 00:13:08 -05:00
2023-04-18 13:40:47 +02:00
# include <chainparamsbase.h>
2023-03-23 12:23:29 +01:00
# include <common/args.h>
2023-05-25 19:24:08 +02:00
# include <consensus/params.h>
2021-03-11 12:17:22 +10:00
# include <deploymentinfo.h>
2023-03-23 12:23:29 +01:00
# include <logging.h>
2023-05-25 19:24:08 +02:00
# include <tinyformat.h>
2023-04-17 22:20:59 +02:00
# include <util/chaintype.h>
2023-05-25 19:24:08 +02:00
# include <util/strencodings.h>
2021-09-11 13:02:47 +02:00
# include <util/string.h>
2013-05-07 15:16:25 +02:00
2023-05-25 19:24:08 +02:00
# include <cassert>
# include <cstdint>
# include <limits>
# include <stdexcept>
# include <vector>
2014-08-28 22:56:53 +02:00
2022-03-09 00:13:36 -05:00
void ReadSigNetArgs ( const ArgsManager & args , CChainParams : : SigNetOptions & options )
{
if ( args . IsArgSet ( " -signetseednode " ) ) {
options . seeds . emplace ( args . GetArgs ( " -signetseednode " ) ) ;
}
if ( args . IsArgSet ( " -signetchallenge " ) ) {
const auto signet_challenge = args . GetArgs ( " -signetchallenge " ) ;
if ( signet_challenge . size ( ) ! = 1 ) {
2023-05-25 19:24:08 +02:00
throw std : : runtime_error ( " -signetchallenge cannot be multiple values. " ) ;
2022-03-09 00:13:36 -05:00
}
2023-05-25 19:24:08 +02:00
const auto val { TryParseHex < uint8_t > ( signet_challenge [ 0 ] ) } ;
if ( ! val ) {
throw std : : runtime_error ( strprintf ( " -signetchallenge must be hex, not '%s'. " , signet_challenge [ 0 ] ) ) ;
}
options . challenge . emplace ( * val ) ;
2022-03-09 00:13:36 -05:00
}
}
2022-03-09 16:11:39 -05:00
void ReadRegTestArgs ( const ArgsManager & args , CChainParams : : RegTestOptions & options )
2018-06-16 22:38:13 +02:00
{
2022-03-09 16:11:39 -05:00
if ( auto value = args . GetBoolArg ( " -fastprune " ) ) options . fastprune = * value ;
2021-08-27 12:54:24 +02:00
for ( const std : : string & arg : args . GetArgs ( " -testactivationheight " ) ) {
const auto found { arg . find ( ' @ ' ) } ;
if ( found = = std : : string : : npos ) {
throw std : : runtime_error ( strprintf ( " Invalid format (%s) for -testactivationheight=name@height. " , arg ) ) ;
}
2022-03-09 16:11:39 -05:00
2021-08-27 12:54:24 +02:00
const auto value { arg . substr ( found + 1 ) } ;
int32_t height ;
if ( ! ParseInt32 ( value , & height ) | | height < 0 | | height > = std : : numeric_limits < int > : : max ( ) ) {
throw std : : runtime_error ( strprintf ( " Invalid height value (%s) for -testactivationheight=name@height. " , arg ) ) ;
}
2022-03-09 16:11:39 -05:00
const auto deployment_name { arg . substr ( 0 , found ) } ;
if ( const auto buried_deployment = GetBuriedDeployment ( deployment_name ) ) {
options . activation_heights [ * buried_deployment ] = height ;
2021-08-27 12:54:24 +02:00
} else {
throw std : : runtime_error ( strprintf ( " Invalid name (%s) for -testactivationheight=name@height. " , arg ) ) ;
2019-05-20 14:59:07 -04:00
}
}
2018-06-16 22:38:13 +02:00
if ( ! args . IsArgSet ( " -vbparams " ) ) return ;
for ( const std : : string & strDeployment : args . GetArgs ( " -vbparams " ) ) {
2021-09-11 13:02:47 +02:00
std : : vector < std : : string > vDeploymentParams = SplitString ( strDeployment , ' : ' ) ;
2021-03-06 18:18:49 +10:00
if ( vDeploymentParams . size ( ) < 3 | | 4 < vDeploymentParams . size ( ) ) {
throw std : : runtime_error ( " Version bits parameters malformed, expecting deployment:start:end[:min_activation_height] " ) ;
2018-06-16 22:38:13 +02:00
}
2022-03-09 16:11:39 -05:00
CChainParams : : VersionBitsParameters vbparams { } ;
if ( ! ParseInt64 ( vDeploymentParams [ 1 ] , & vbparams . start_time ) ) {
2018-06-16 22:38:13 +02:00
throw std : : runtime_error ( strprintf ( " Invalid nStartTime (%s) " , vDeploymentParams [ 1 ] ) ) ;
}
2022-03-09 16:11:39 -05:00
if ( ! ParseInt64 ( vDeploymentParams [ 2 ] , & vbparams . timeout ) ) {
2018-06-16 22:38:13 +02:00
throw std : : runtime_error ( strprintf ( " Invalid nTimeout (%s) " , vDeploymentParams [ 2 ] ) ) ;
}
2022-03-09 16:11:39 -05:00
if ( vDeploymentParams . size ( ) > = 4 ) {
if ( ! ParseInt32 ( vDeploymentParams [ 3 ] , & vbparams . min_activation_height ) ) {
throw std : : runtime_error ( strprintf ( " Invalid min_activation_height (%s) " , vDeploymentParams [ 3 ] ) ) ;
}
} else {
vbparams . min_activation_height = 0 ;
2021-03-06 18:18:49 +10:00
}
2018-06-16 22:38:13 +02:00
bool found = false ;
for ( int j = 0 ; j < ( int ) Consensus : : MAX_VERSION_BITS_DEPLOYMENTS ; + + j ) {
if ( vDeploymentParams [ 0 ] = = VersionBitsDeploymentInfo [ j ] . name ) {
2022-03-09 16:11:39 -05:00
options . version_bits_parameters [ Consensus : : DeploymentPos ( j ) ] = vbparams ;
2018-06-16 22:38:13 +02:00
found = true ;
2022-03-09 16:11:39 -05:00
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 ) ;
2018-06-16 22:38:13 +02:00
break ;
}
}
if ( ! found ) {
throw std : : runtime_error ( strprintf ( " Invalid deployment (%s) " , vDeploymentParams [ 0 ] ) ) ;
}
}
}
static std : : unique_ptr < const CChainParams > globalChainParams ;
2013-05-07 15:16:25 +02:00
const CChainParams & Params ( ) {
2015-05-22 03:50:01 +02:00
assert ( globalChainParams ) ;
return * globalChainParams ;
2013-05-07 15:16:25 +02:00
}
2023-04-17 22:20:59 +02:00
std : : unique_ptr < const CChainParams > CreateChainParams ( const ArgsManager & args , const ChainType chain )
2015-06-30 21:39:49 +02:00
{
2023-04-17 22:20:59 +02:00
switch ( chain ) {
case ChainType : : MAIN :
2022-03-10 17:21:05 -05:00
return CChainParams : : Main ( ) ;
2023-04-17 22:20:59 +02:00
case ChainType : : TESTNET :
2022-03-10 17:21:05 -05:00
return CChainParams : : TestNet ( ) ;
2023-04-17 22:20:59 +02:00
case ChainType : : SIGNET : {
2022-03-09 00:13:36 -05:00
auto opts = CChainParams : : SigNetOptions { } ;
ReadSigNetArgs ( args , opts ) ;
2022-03-10 17:21:05 -05:00
return CChainParams : : SigNet ( opts ) ;
2023-04-17 22:20:59 +02:00
}
case ChainType : : REGTEST : {
2022-03-09 16:11:39 -05:00
auto opts = CChainParams : : RegTestOptions { } ;
ReadRegTestArgs ( args , opts ) ;
2022-03-10 17:21:05 -05:00
return CChainParams : : RegTest ( opts ) ;
2020-03-05 15:58:30 +09:00
}
2023-04-17 22:20:59 +02:00
}
2023-05-09 22:03:50 +02:00
assert ( false ) ;
2015-05-22 03:50:01 +02:00
}
2023-04-17 22:20:59 +02:00
void SelectParams ( const ChainType chain )
2015-06-30 21:39:49 +02:00
{
2023-04-17 22:20:59 +02:00
SelectBaseParams ( chain ) ;
globalChainParams = CreateChainParams ( gArgs , chain ) ;
2014-08-02 19:54:57 +01:00
}