LCOV - code coverage report
Current view: top level - src - chainparams.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 84.4 % 77 65
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 5 5
Branches: 41.3 % 138 57

             Branch data     Line data    Source code
       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                 :             : 
      24                 :             : using util::SplitString;
      25                 :             : 
      26                 :         692 : void ReadSigNetArgs(const ArgsManager& args, CChainParams::SigNetOptions& options)
      27                 :             : {
      28   [ +  -  -  + ]:         692 :     if (args.IsArgSet("-signetseednode")) {
      29         [ #  # ]:           0 :         options.seeds.emplace(args.GetArgs("-signetseednode"));
      30                 :             :     }
      31   [ +  -  +  + ]:         692 :     if (args.IsArgSet("-signetchallenge")) {
      32         [ +  - ]:           1 :         const auto signet_challenge = args.GetArgs("-signetchallenge");
      33         [ -  + ]:           1 :         if (signet_challenge.size() != 1) {
      34         [ #  # ]:           0 :             throw std::runtime_error("-signetchallenge cannot be multiple values.");
      35                 :             :         }
      36         [ +  - ]:           1 :         const auto val{TryParseHex<uint8_t>(signet_challenge[0])};
      37         [ -  + ]:           1 :         if (!val) {
      38   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("-signetchallenge must be hex, not '%s'.", signet_challenge[0]));
      39                 :             :         }
      40         [ +  - ]:           1 :         options.challenge.emplace(*val);
      41                 :           1 :     }
      42                 :         692 : }
      43                 :             : 
      44                 :         774 : void ReadRegTestArgs(const ArgsManager& args, CChainParams::RegTestOptions& options)
      45                 :             : {
      46   [ +  -  -  + ]:        1548 :     if (auto value = args.GetBoolArg("-fastprune")) options.fastprune = *value;
      47                 :             : 
      48   [ +  -  +  + ]:         777 :     for (const std::string& arg : args.GetArgs("-testactivationheight")) {
      49                 :           3 :         const auto found{arg.find('@')};
      50         [ -  + ]:           3 :         if (found == std::string::npos) {
      51   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid format (%s) for -testactivationheight=name@height.", arg));
      52                 :             :         }
      53                 :             : 
      54         [ +  - ]:           3 :         const auto value{arg.substr(found + 1)};
      55                 :           3 :         int32_t height;
      56   [ +  -  +  -  :           3 :         if (!ParseInt32(value, &height) || height < 0 || height >= std::numeric_limits<int>::max()) {
             +  -  +  - ]
      57   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid height value (%s) for -testactivationheight=name@height.", arg));
      58                 :             :         }
      59                 :             : 
      60         [ +  - ]:           3 :         const auto deployment_name{arg.substr(0, found)};
      61   [ +  -  +  - ]:           3 :         if (const auto buried_deployment = GetBuriedDeployment(deployment_name)) {
      62         [ +  - ]:           3 :             options.activation_heights[*buried_deployment] = height;
      63                 :             :         } else {
      64   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid name (%s) for -testactivationheight=name@height.", arg));
      65                 :             :         }
      66                 :         777 :     }
      67                 :             : 
      68   [ +  -  +  + ]:         774 :     if (!args.IsArgSet("-vbparams")) return;
      69                 :             : 
      70   [ +  -  +  + ]:           4 :     for (const std::string& strDeployment : args.GetArgs("-vbparams")) {
      71         [ +  - ]:           2 :         std::vector<std::string> vDeploymentParams = SplitString(strDeployment, ':');
      72   [ +  -  +  - ]:           2 :         if (vDeploymentParams.size() < 3 || 4 < vDeploymentParams.size()) {
      73         [ #  # ]:           0 :             throw std::runtime_error("Version bits parameters malformed, expecting deployment:start:end[:min_activation_height]");
      74                 :             :         }
      75                 :           2 :         CChainParams::VersionBitsParameters vbparams{};
      76   [ +  -  -  + ]:           2 :         if (!ParseInt64(vDeploymentParams[1], &vbparams.start_time)) {
      77   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid nStartTime (%s)", vDeploymentParams[1]));
      78                 :             :         }
      79   [ +  -  -  + ]:           2 :         if (!ParseInt64(vDeploymentParams[2], &vbparams.timeout)) {
      80   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid nTimeout (%s)", vDeploymentParams[2]));
      81                 :             :         }
      82         [ +  + ]:           2 :         if (vDeploymentParams.size() >= 4) {
      83   [ +  -  -  + ]:           1 :             if (!ParseInt32(vDeploymentParams[3], &vbparams.min_activation_height)) {
      84   [ #  #  #  # ]:           0 :                 throw std::runtime_error(strprintf("Invalid min_activation_height (%s)", vDeploymentParams[3]));
      85                 :             :             }
      86                 :             :         } else {
      87                 :           1 :             vbparams.min_activation_height = 0;
      88                 :             :         }
      89                 :           2 :         bool found = false;
      90         [ +  - ]:           2 :         for (int j=0; j < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; ++j) {
      91         [ +  - ]:           2 :             if (vDeploymentParams[0] == VersionBitsDeploymentInfo[j].name) {
      92         [ +  - ]:           2 :                 options.version_bits_parameters[Consensus::DeploymentPos(j)] = vbparams;
      93                 :           2 :                 found = true;
      94         [ +  - ]:           2 :                 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                 :           2 :         if (!found) {
      99   [ #  #  #  # ]:           0 :             throw std::runtime_error(strprintf("Invalid deployment (%s)", vDeploymentParams[0]));
     100                 :             :         }
     101                 :           4 :     }
     102                 :             : }
     103                 :             : 
     104                 :             : static std::unique_ptr<const CChainParams> globalChainParams;
     105                 :             : 
     106                 :       86517 : const CChainParams &Params() {
     107         [ -  + ]:       86517 :     assert(globalChainParams);
     108                 :       86517 :     return *globalChainParams;
     109                 :             : }
     110                 :             : 
     111                 :        4049 : std::unique_ptr<const CChainParams> CreateChainParams(const ArgsManager& args, const ChainType chain)
     112                 :             : {
     113   [ +  +  +  +  :        4049 :     switch (chain) {
                   +  - ]
     114                 :        1307 :     case ChainType::MAIN:
     115                 :        1307 :         return CChainParams::Main();
     116                 :         691 :     case ChainType::TESTNET:
     117                 :         691 :         return CChainParams::TestNet();
     118                 :         585 :     case ChainType::TESTNET4:
     119                 :         585 :         return CChainParams::TestNet4();
     120                 :         692 :     case ChainType::SIGNET: {
     121                 :         692 :         auto opts = CChainParams::SigNetOptions{};
     122         [ +  - ]:         692 :         ReadSigNetArgs(args, opts);
     123         [ +  - ]:         692 :         return CChainParams::SigNet(opts);
     124                 :         692 :     }
     125                 :         774 :     case ChainType::REGTEST: {
     126         [ +  - ]:         774 :         auto opts = CChainParams::RegTestOptions{};
     127         [ +  - ]:         774 :         ReadRegTestArgs(args, opts);
     128         [ +  - ]:         774 :         return CChainParams::RegTest(opts);
     129                 :         774 :     }
     130                 :             :     }
     131                 :           0 :     assert(false);
     132                 :             : }
     133                 :             : 
     134                 :        1107 : void SelectParams(const ChainType chain)
     135                 :             : {
     136                 :        1107 :     SelectBaseParams(chain);
     137                 :        1107 :     globalChainParams = CreateChainParams(gArgs, chain);
     138                 :        1107 : }
        

Generated by: LCOV version 2.0-1