LCOV - code coverage report
Current view: top level - src - bitcoin-util.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 0.0 % 153 0
Test Date: 2026-11-23 06:11:08 Functions: 0.0 % 6 0
Branches: 0.0 % 288 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <bitcoin-build-config.h>
       6                 :             : 
       7                 :             : #include <arith_uint256.h>
       8                 :             : #include <chain.h>
       9                 :             : #include <chainparams.h>
      10                 :             : #include <chainparamsbase.h>
      11                 :             : #include <clientversion.h>
      12                 :             : #include <common/args.h>
      13                 :             : #include <common/license_info.h>
      14                 :             : #include <common/system.h>
      15                 :             : #include <compat/compat.h>
      16                 :             : #include <core_io.h>
      17                 :             : #include <crypto/sha256.h>
      18                 :             : #include <streams.h>
      19                 :             : #include <univalue.h>
      20                 :             : #include <util/check.h>
      21                 :             : #include <util/exception.h>
      22                 :             : #include <util/strencodings.h>
      23                 :             : #include <util/translation.h>
      24                 :             : 
      25                 :             : #include <atomic>
      26                 :             : #include <cstdio>
      27                 :             : #include <functional>
      28                 :             : #include <memory>
      29                 :             : #include <thread>
      30                 :             : 
      31                 :             : static const int CONTINUE_EXECUTION=-1;
      32                 :             : 
      33                 :             : const TranslateFn G_TRANSLATION_FUN{nullptr};
      34                 :             : 
      35                 :           0 : static void SetupBitcoinUtilArgs(ArgsManager &argsman)
      36                 :             : {
      37                 :           0 :     SetupHelpOptions(argsman);
      38                 :             : 
      39   [ #  #  #  # ]:           0 :     argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
      40                 :             : 
      41   [ #  #  #  #  :           0 :     argsman.AddCommand("grind", "Perform proof of work on hex header string");
                   #  # ]
      42   [ #  #  #  #  :           0 :     argsman.AddCommand("getchainparams", "Get hardcoded parameters for the selected chain");
                   #  # ]
      43                 :             : 
      44                 :           0 :     SetupChainParamsBaseOptions(argsman);
      45                 :           0 : }
      46                 :             : 
      47                 :             : // This function returns either one of EXIT_ codes when it's expected to stop the process or
      48                 :             : // CONTINUE_EXECUTION when it's expected to continue further.
      49                 :           0 : static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
      50                 :             : {
      51                 :           0 :     SetupBitcoinUtilArgs(args);
      52         [ #  # ]:           0 :     std::string error;
      53   [ #  #  #  # ]:           0 :     if (!args.ParseParameters(argc, argv, error)) {
      54         [ #  # ]:           0 :         tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
      55                 :             :         return EXIT_FAILURE;
      56                 :             :     }
      57                 :             : 
      58   [ #  #  #  #  :           0 :     if (HelpRequested(args) || args.GetBoolArg("-version", false)) {
          #  #  #  #  #  
                #  #  # ]
      59                 :             :         // First part of help message is specific to this utility
      60   [ #  #  #  # ]:           0 :         std::string strUsage = CLIENT_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n";
      61                 :             : 
      62   [ #  #  #  #  :           0 :         if (args.GetBoolArg("-version", false)) {
                   #  # ]
      63   [ #  #  #  # ]:           0 :             strUsage += FormatParagraph(LicenseInfo());
      64                 :             :         } else {
      65         [ #  # ]:           0 :             strUsage += "\n"
      66                 :             :                 "The bitcoin-util tool provides bitcoin related functionality that does not rely on the ability to access a running node. Available [commands] are listed below.\n"
      67                 :             :                 "\n"
      68                 :             :                 "Usage:  bitcoin-util [options] [command]\n"
      69                 :           0 :                 "or:     bitcoin-util [options] grind <hex-block-header>\n";
      70   [ #  #  #  # ]:           0 :             strUsage += "\n" + args.GetHelpMessage();
      71                 :             :         }
      72                 :             : 
      73         [ #  # ]:           0 :         tfm::format(std::cout, "%s", strUsage);
      74                 :             : 
      75         [ #  # ]:           0 :         if (argc < 2) {
      76         [ #  # ]:           0 :             tfm::format(std::cerr, "Error: too few parameters\n");
      77                 :             :             return EXIT_FAILURE;
      78                 :             :         }
      79                 :             :         return EXIT_SUCCESS;
      80                 :           0 :     }
      81                 :             : 
      82                 :             :     // Check for chain settings (Params() calls are only valid after this clause)
      83                 :           0 :     try {
      84   [ #  #  #  # ]:           0 :         SelectParams(args.GetChainType());
      85         [ -  - ]:           0 :     } catch (const std::exception& e) {
      86         [ -  - ]:           0 :         tfm::format(std::cerr, "Error: %s\n", e.what());
      87                 :           0 :         return EXIT_FAILURE;
      88                 :           0 :     }
      89                 :             : 
      90                 :             :     return CONTINUE_EXECUTION;
      91                 :           0 : }
      92                 :             : 
      93                 :           0 : static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
      94                 :             : {
      95                 :           0 :     arith_uint256 target;
      96                 :           0 :     bool neg, over;
      97                 :           0 :     target.SetCompact(nBits, &neg, &over);
      98   [ #  #  #  #  :           0 :     if (target == 0 || neg || over) return;
                   #  # ]
      99                 :           0 :     header.nNonce = offset;
     100                 :             : 
     101                 :           0 :     uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
     102                 :           0 :     finish = finish - (finish % step) + offset;
     103                 :             : 
     104   [ #  #  #  # ]:           0 :     while (!found && header.nNonce < finish) {
     105         [ #  # ]:           0 :         const uint32_t next = (finish - header.nNonce < 5000*step) ? finish : header.nNonce + 5000*step;
     106                 :           0 :         do {
     107         [ #  # ]:           0 :             if (UintToArith256(header.GetHash()) <= target) {
     108         [ #  # ]:           0 :                 if (!found.exchange(true)) {
     109                 :           0 :                     proposed_nonce = header.nNonce;
     110                 :             :                 }
     111                 :           0 :                 return;
     112                 :             :             }
     113                 :           0 :             header.nNonce += step;
     114         [ #  # ]:           0 :         } while(header.nNonce != next);
     115                 :             :     }
     116                 :             : }
     117                 :             : 
     118                 :           0 : static int Grind(const std::vector<std::string>& args, std::string& strPrint)
     119                 :             : {
     120   [ #  #  #  # ]:           0 :     if (args.size() != 1) {
     121                 :           0 :         strPrint = "Must specify block header to grind";
     122                 :           0 :         return EXIT_FAILURE;
     123                 :             :     }
     124                 :             : 
     125                 :           0 :     CBlockHeader header;
     126         [ #  # ]:           0 :     if (!DecodeHexBlockHeader(header, args[0])) {
     127                 :           0 :         strPrint = "Could not decode block header";
     128                 :           0 :         return EXIT_FAILURE;
     129                 :             :     }
     130                 :             : 
     131                 :           0 :     uint32_t nBits = header.nBits;
     132                 :           0 :     std::atomic<bool> found{false};
     133                 :           0 :     uint32_t proposed_nonce{};
     134                 :             : 
     135                 :           0 :     std::vector<std::thread> threads;
     136         [ #  # ]:           0 :     int n_tasks = std::max(1u, std::thread::hardware_concurrency());
     137         [ #  # ]:           0 :     threads.reserve(n_tasks);
     138         [ #  # ]:           0 :     for (int i = 0; i < n_tasks; ++i) {
     139         [ #  # ]:           0 :         threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
     140                 :             :     }
     141         [ #  # ]:           0 :     for (auto& t : threads) {
     142         [ #  # ]:           0 :         t.join();
     143                 :             :     }
     144         [ #  # ]:           0 :     if (found) {
     145                 :           0 :         header.nNonce = proposed_nonce;
     146                 :             :     } else {
     147         [ #  # ]:           0 :         strPrint = "Could not satisfy difficulty target";
     148                 :             :         return EXIT_FAILURE;
     149                 :             :     }
     150                 :             : 
     151                 :           0 :     DataStream ss{};
     152         [ #  # ]:           0 :     ss << header;
     153   [ #  #  #  # ]:           0 :     strPrint = HexStr(ss);
     154                 :           0 :     return EXIT_SUCCESS;
     155                 :           0 : }
     156                 :             : 
     157                 :           0 : static int GetChainParams(const std::vector<std::string>& args, std::string& strPrint)
     158                 :             : {
     159         [ #  # ]:           0 :     if (!args.empty()) {
     160                 :           0 :         strPrint = "getchainparams does not take arguments";
     161                 :           0 :         return EXIT_FAILURE;
     162                 :             :     }
     163                 :             : 
     164                 :           0 :     const auto& params = Params();
     165                 :           0 :     const auto& consensus = params.GetConsensus();
     166                 :             : 
     167                 :           0 :     UniValue result{UniValue::VOBJ};
     168   [ #  #  #  #  :           0 :     result.pushKV("chain", params.GetChainTypeString());
             #  #  #  # ]
     169   [ #  #  #  #  :           0 :     result.pushKV("test_chain", params.IsTestChain());
                   #  # ]
     170   [ #  #  #  #  :           0 :     result.pushKV("genesis", consensus.hashGenesisBlock.GetHex());
             #  #  #  # ]
     171   [ #  #  #  #  :           0 :     result.pushKV("subsidy_halving_interval", consensus.nSubsidyHalvingInterval);
                   #  # ]
     172                 :             : 
     173         [ #  # ]:           0 :     if (consensus.signet_blocks) {
     174                 :           0 :         UniValue signet{UniValue::VOBJ};
     175   [ #  #  #  #  :           0 :         signet.pushKV("challenge", HexStr(consensus.signet_challenge));
          #  #  #  #  #  
                      # ]
     176   [ #  #  #  #  :           0 :         result.pushKV("signet", signet);
                   #  # ]
     177                 :           0 :     }
     178                 :             : 
     179                 :           0 :     {
     180                 :           0 :         UniValue pow{UniValue::VOBJ};
     181   [ #  #  #  #  :           0 :         pow.pushKV("limit", consensus.powLimit.ToString());
             #  #  #  # ]
     182         [ #  # ]:           0 :         if (!consensus.fPowNoRetargeting) {
     183   [ #  #  #  #  :           0 :             pow.pushKV("target_spacing", TicksSeconds(consensus.PowTargetSpacing()));
                   #  # ]
     184   [ #  #  #  #  :           0 :             pow.pushKV("difficulty_retarget_interval", consensus.DifficultyAdjustmentInterval());
                   #  # ]
     185                 :           0 :             std::string mindiff_blocks = (consensus.fPowAllowMinDifficultyBlocks ?
     186   [ #  #  #  #  :           0 :                   (consensus.enforce_BIP94 ? "bip94" : "yes") : "no");
                   #  # ]
     187   [ #  #  #  #  :           0 :             pow.pushKV("mindiff_blocks", mindiff_blocks);
                   #  # ]
     188                 :           0 :         }
     189   [ #  #  #  #  :           0 :         result.pushKV("pow", pow);
                   #  # ]
     190                 :           0 :     }
     191                 :             : 
     192                 :           0 :     {
     193                 :           0 :         UniValue net{UniValue::VOBJ};
     194   [ #  #  #  #  :           0 :         net.pushKV("default_port", params.GetDefaultPort());
                   #  # ]
     195   [ #  #  #  #  :           0 :         net.pushKV("magic", HexStr(params.MessageStart()));
             #  #  #  # ]
     196                 :           0 :         UniValue dns{UniValue::VARR};
     197         [ #  # ]:           0 :         for (const auto& seed : params.DNSSeeds()) {
     198   [ #  #  #  # ]:           0 :             dns.push_back(seed);
     199                 :             :         }
     200   [ #  #  #  #  :           0 :         net.pushKV("dns_seeds", dns);
                   #  # ]
     201   [ #  #  #  #  :           0 :         result.pushKV("net", net);
                   #  # ]
     202                 :           0 :     }
     203                 :             : 
     204                 :           0 :     {
     205                 :           0 :         UniValue addr{UniValue::VOBJ};
     206   [ #  #  #  #  :           0 :         addr.pushKV("bech32_hrp", params.Bech32HRP());
                   #  # ]
     207   [ #  #  #  #  :           0 :         result.pushKV("addresses", addr);
                   #  # ]
     208                 :           0 :     }
     209                 :             : 
     210         [ #  # ]:           0 :     strPrint = result.write(/*prettyIndent=*/2);
     211                 :           0 :     return EXIT_SUCCESS;
     212                 :           0 : }
     213                 :             : 
     214                 :           0 : MAIN_FUNCTION
     215                 :             : {
     216                 :           0 :     ArgsManager& args = gArgs;
     217                 :           0 :     SetupEnvironment();
     218                 :           0 :     SHA256AutoDetect();
     219                 :             : 
     220                 :           0 :     try {
     221         [ #  # ]:           0 :         int ret = AppInitUtil(args, argc, argv);
     222         [ #  # ]:           0 :         if (ret != CONTINUE_EXECUTION) {
     223                 :             :             return ret;
     224                 :             :         }
     225         [ -  - ]:           0 :     } catch (const std::exception& e) {
     226         [ -  - ]:           0 :         PrintExceptionContinue(&e, "AppInitUtil()");
     227                 :           0 :         return EXIT_FAILURE;
     228                 :           0 :     } catch (...) {
     229         [ -  - ]:           0 :         PrintExceptionContinue(nullptr, "AppInitUtil()");
     230                 :           0 :         return EXIT_FAILURE;
     231                 :           0 :     }
     232                 :             : 
     233                 :           0 :     const auto cmd = args.GetCommand();
     234         [ #  # ]:           0 :     if (!cmd) {
     235         [ #  # ]:           0 :         tfm::format(std::cerr, "Error: must specify a command\n");
     236                 :             :         return EXIT_FAILURE;
     237                 :             :     }
     238                 :             : 
     239                 :           0 :     int ret = EXIT_FAILURE;
     240         [ #  # ]:           0 :     std::string strPrint;
     241                 :           0 :     try {
     242         [ #  # ]:           0 :         if (cmd->command == "grind") {
     243         [ #  # ]:           0 :             ret = Grind(cmd->args, strPrint);
     244         [ #  # ]:           0 :         } else if (cmd->command == "getchainparams") {
     245         [ #  # ]:           0 :             ret = GetChainParams(cmd->args, strPrint);
     246                 :             :         } else {
     247                 :           0 :             AssertUnreachable(); // unknown command should be caught earlier
     248                 :             :         }
     249         [ -  - ]:           0 :     } catch (const std::exception& e) {
     250         [ -  - ]:           0 :         strPrint = std::string("error: ") + e.what();
     251                 :           0 :     } catch (...) {
     252         [ -  - ]:           0 :         strPrint = "unknown error";
     253         [ -  - ]:           0 :     }
     254                 :             : 
     255         [ #  # ]:           0 :     if (strPrint != "") {
     256   [ #  #  #  # ]:           0 :         tfm::format(ret == 0 ? std::cout : std::cerr, "%s\n", strPrint);
     257                 :             :     }
     258                 :             : 
     259                 :           0 :     return ret;
     260                 :           0 : }
        

Generated by: LCOV version 2.0-1