LCOV - code coverage report
Current view: top level - src - chainparams.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 29 81 35.8 %
Date: 2023-11-12 01:39:15 Functions: 5 5 100.0 %
Branches: 24 169 14.2 %

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

Generated by: LCOV version 1.14