LCOV - code coverage report
Current view: top level - src - chainparams.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 62.1 % 103 64
Test Date: 2026-06-12 03:37:55 Functions: 100.0 % 8 8
Branches: 25.9 % 174 45

             Branch data     Line data    Source code
       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                 :             : 
      24                 :             : using util::SplitString;
      25                 :             : 
      26                 :        8802 : static void HandleDeploymentArgs(const ArgsManager& args, CChainParams::DeploymentOptions& options)
      27                 :             : {
      28   [ +  -  +  + ]:       10172 :     for (const std::string& arg : args.GetArgs("-testactivationheight")) {
      29                 :        1370 :         const auto found{arg.find('@')};
      30         [ -  + ]:        1370 :         if (found == std::string::npos) {
      31   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
      32                 :             :         }
      33                 :             : 
      34         [ +  - ]:        1370 :         const auto value{arg.substr(found + 1)};
      35         [ -  + ]:        1370 :         const auto height{ToIntegral<int32_t>(value)};
      36   [ +  -  +  -  :        1370 :         if (!height || *height < 0 || *height >= std::numeric_limits<int>::max()) {
                   -  + ]
      37   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
      38                 :             :         }
      39                 :             : 
      40         [ +  - ]:        1370 :         const auto deployment_name{arg.substr(0, found)};
      41   [ -  +  +  -  :        1370 :         if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
                   +  - ]
      42         [ +  - ]:        1370 :             options.activation_heights[*buried_deployment] = *height;
      43                 :             :         } else {
      44   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
      45                 :             :         }
      46                 :       10172 :     }
      47                 :             : 
      48   [ +  -  -  + ]:        8802 :     for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
      49   [ #  #  #  # ]:           0 :         std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
      50   [ #  #  #  #  :           0 :         if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
                   #  # ]
      51         [ #  # ]:           0 :             throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
      52                 :             :         }
      53                 :           0 :         CChainParams::VersionBitsParameters vbparams{};
      54         [ #  # ]:           0 :         const auto start_time{ToIntegral<int64_t>(vDeploymentParams[1])};
      55         [ #  # ]:           0 :         if (!start_time) {
      56   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
      57                 :             :         }
      58         [ #  # ]:           0 :         vbparams.start_time = *start_time;
      59         [ #  # ]:           0 :         const auto timeout{ToIntegral<int64_t>(vDeploymentParams[2])};
      60         [ #  # ]:           0 :         if (!timeout) {
      61   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
      62                 :             :         }
      63         [ #  # ]:           0 :         vbparams.timeout = *timeout;
      64   [ #  #  #  # ]:           0 :         if (vDeploymentParams.size() >= 4) {
      65         [ #  # ]:           0 :             const auto min_activation_height{ToIntegral<int64_t>(vDeploymentParams[3])};
      66         [ #  # ]:           0 :             if (!min_activation_height) {
      67   [ #  #  #  # ]:           0 :                 throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
      68                 :             :             }
      69                 :           0 :             vbparams.min_activation_height = *min_activation_height;
      70                 :             :         } else {
      71                 :           0 :             vbparams.min_activation_height = 0;
      72                 :             :         }
      73                 :           0 :         bool found = false;
      74         [ #  # ]:           0 :         for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
      75         [ #  # ]:           0 :             if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
      76         [ #  # ]:           0 :                 options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
      77                 :           0 :                 found = true;
      78         [ #  # ]:           0 :                 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                 :           0 :         if (!found) {
      84   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
      85                 :             :         }
      86                 :        8802 :     }
      87                 :        8802 : }
      88                 :             : 
      89                 :        1476 : void ReadMainNetArgs(const ArgsManager& args, CChainParams::MainNetOptions& options)
      90                 :             : {
      91                 :        1476 :     HandleDeploymentArgs(args, options.dep_opts);
      92                 :        1476 : }
      93                 :             : 
      94                 :        2926 : void ReadTestNetArgs(const ArgsManager& args, CChainParams::TestNetOptions& options)
      95                 :             : {
      96                 :        2926 :     HandleDeploymentArgs(args, options.dep_opts);
      97                 :        2926 : }
      98                 :             : 
      99                 :        1464 : void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options)
     100                 :             : {
     101   [ +  -  -  + ]:        1464 :     if (!args.GetArgs("-signetseednode").empty()) {
     102         [ #  # ]:           0 :         options.seeds.emplace(args.GetArgs("-signetseednode"));
     103                 :             :     }
     104   [ +  -  -  + ]:        1464 :     if (!args.GetArgs("-signetchallenge").empty()) {
     105         [ #  # ]:           0 :         const auto signet_challenge = args.GetArgs("-signetchallenge");
     106   [ #  #  #  # ]:           0 :         if (signet_challenge.size() != 1) {
     107         [ #  # ]:           0 :             throw std::runtime_error("-signetchallenge cannot be multiple values.");
     108                 :             :         }
     109   [ #  #  #  # ]:           0 :         const auto val{TryParseHex<uint8_t>(signet_challenge[0])};
     110         [ #  # ]:           0 :         if (!val) {
     111   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0]));
     112                 :             :         }
     113         [ #  # ]:           0 :         options.challenge.emplace(*val);
     114                 :           0 :     }
     115                 :        1464 :     HandleDeploymentArgs(args, options.dep_opts);
     116                 :        1464 : }
     117                 :             : 
     118                 :        2936 : void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
     119                 :             : {
     120   [ +  -  -  + ]:        5872 :     if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
     121   [ +  -  -  + ]:        2936 :     if (HasTestOption(args, "bip94")) options.enforce_bip94 = true;
     122                 :             : 
     123                 :        2936 :     HandleDeploymentArgs(args, options.dep_opts);
     124                 :        2936 : }
     125                 :             : 
     126                 :             : static std::unique_ptr<const CChainParams> globalChainParams;
     127                 :             : 
     128                 :     7663033 : const CChainParams &Params() {
     129         [ -  + ]:     7663033 :     assert(globalChainParams);
     130                 :     7663033 :     return *globalChainParams;
     131                 :             : }
     132                 :             : 
     133                 :        8802 : std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
     134                 :             : {
     135   [ +  +  +  +  :        8802 :     switch (chain) {
                   +  - ]
     136                 :        1476 :     case ChainType::MAIN: {
     137         [ +  - ]:        1476 :         auto opts = CChainParams::MainNetOptions{};
     138         [ +  - ]:        1476 :         ReadMainNetArgs(args, opts);
     139         [ +  - ]:        1476 :         return CChainParams::Main(opts);
     140                 :        1476 :     }
     141                 :        1463 :     case ChainType::TESTNET: {
     142         [ +  - ]:        1463 :         auto opts = CChainParams::TestNetOptions{};
     143         [ +  - ]:        1463 :         ReadTestNetArgs(args, opts);
     144         [ +  - ]:        1463 :         return CChainParams::TestNet(opts);
     145                 :        1463 :     }
     146                 :        1463 :     case ChainType::TESTNET4: {
     147         [ +  - ]:        1463 :         auto opts = CChainParams::TestNetOptions{};
     148         [ +  - ]:        1463 :         ReadTestNetArgs(args, opts);
     149         [ +  - ]:        1463 :         return CChainParams::TestNet4(opts);
     150                 :        1463 :     }
     151                 :        1464 :     case ChainType::SIGNET: {
     152         [ +  - ]:        1464 :         auto opts = CChainParams::SigNetOptions{};
     153         [ +  - ]:        1464 :         ReadSigNetArgs(args, opts);
     154         [ +  - ]:        1464 :         return CChainParams::SigNet(opts);
     155                 :        1464 :     }
     156                 :        2936 :     case ChainType::REGTEST: {
     157         [ +  - ]:        2936 :         auto opts = CChainParams::RegTestOptions{};
     158         [ +  - ]:        2936 :         ReadRegTestArgs(args, opts);
     159         [ +  - ]:        2936 :         return CChainParams::RegTest(opts);
     160                 :        2936 :     }
     161                 :             :     }
     162                 :           0 :     assert(false);
     163                 :             : }
     164                 :             : 
     165                 :        1484 : void SelectParams(const ChainType chain)
     166                 :             : {
     167                 :        1484 :     SelectBaseParams(chain);
     168                 :        1484 :     globalChainParams = CreateChainParams(gArgs, chain);
     169                 :        1484 : }
        

Generated by: LCOV version 2.0-1