LCOV - code coverage report
Current view: top level - src - chainparams.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 91.0 % 78 71
Test Date: 2025-02-19 05:07:00 Functions: 100.0 % 5 5
Branches: 54.3 % 138 75

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

Generated by: LCOV version 2.0-1