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 % 103 0
Test Date: 2026-04-25 06:24:37 Functions: 0.0 % 5 0
Branches: 0.0 % 142 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> // IWYU pragma: keep
       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 <streams.h>
      18                 :             : #include <util/exception.h>
      19                 :             : #include <util/strencodings.h>
      20                 :             : #include <util/translation.h>
      21                 :             : 
      22                 :             : #include <atomic>
      23                 :             : #include <cstdio>
      24                 :             : #include <functional>
      25                 :             : #include <memory>
      26                 :             : #include <thread>
      27                 :             : 
      28                 :             : static const int CONTINUE_EXECUTION=-1;
      29                 :             : 
      30                 :             : const TranslateFn G_TRANSLATION_FUN{nullptr};
      31                 :             : 
      32                 :           0 : static void SetupBitcoinUtilArgs(ArgsManager &argsman)
      33                 :             : {
      34                 :           0 :     SetupHelpOptions(argsman);
      35                 :             : 
      36   [ #  #  #  # ]:           0 :     argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
      37                 :             : 
      38   [ #  #  #  # ]:           0 :     argsman.AddCommand("grind", "Perform proof of work on hex header string");
      39                 :             : 
      40                 :           0 :     SetupChainParamsBaseOptions(argsman);
      41                 :           0 : }
      42                 :             : 
      43                 :             : // This function returns either one of EXIT_ codes when it's expected to stop the process or
      44                 :             : // CONTINUE_EXECUTION when it's expected to continue further.
      45                 :           0 : static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
      46                 :             : {
      47                 :           0 :     SetupBitcoinUtilArgs(args);
      48         [ #  # ]:           0 :     std::string error;
      49   [ #  #  #  # ]:           0 :     if (!args.ParseParameters(argc, argv, error)) {
      50         [ #  # ]:           0 :         tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
      51                 :             :         return EXIT_FAILURE;
      52                 :             :     }
      53                 :             : 
      54   [ #  #  #  #  :           0 :     if (HelpRequested(args) || args.GetBoolArg("-version", false)) {
          #  #  #  #  #  
                #  #  # ]
      55                 :             :         // First part of help message is specific to this utility
      56   [ #  #  #  # ]:           0 :         std::string strUsage = CLIENT_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n";
      57                 :             : 
      58   [ #  #  #  #  :           0 :         if (args.GetBoolArg("-version", false)) {
                   #  # ]
      59   [ #  #  #  # ]:           0 :             strUsage += FormatParagraph(LicenseInfo());
      60                 :             :         } else {
      61         [ #  # ]:           0 :             strUsage += "\n"
      62                 :             :                 "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"
      63                 :             :                 "\n"
      64                 :             :                 "Usage:  bitcoin-util [options] [command]\n"
      65                 :           0 :                 "or:     bitcoin-util [options] grind <hex-block-header>\n";
      66   [ #  #  #  # ]:           0 :             strUsage += "\n" + args.GetHelpMessage();
      67                 :             :         }
      68                 :             : 
      69         [ #  # ]:           0 :         tfm::format(std::cout, "%s", strUsage);
      70                 :             : 
      71         [ #  # ]:           0 :         if (argc < 2) {
      72         [ #  # ]:           0 :             tfm::format(std::cerr, "Error: too few parameters\n");
      73                 :             :             return EXIT_FAILURE;
      74                 :             :         }
      75                 :             :         return EXIT_SUCCESS;
      76                 :           0 :     }
      77                 :             : 
      78                 :             :     // Check for chain settings (Params() calls are only valid after this clause)
      79                 :           0 :     try {
      80   [ #  #  #  # ]:           0 :         SelectParams(args.GetChainType());
      81         [ -  - ]:           0 :     } catch (const std::exception& e) {
      82         [ -  - ]:           0 :         tfm::format(std::cerr, "Error: %s\n", e.what());
      83                 :           0 :         return EXIT_FAILURE;
      84                 :           0 :     }
      85                 :             : 
      86                 :             :     return CONTINUE_EXECUTION;
      87                 :           0 : }
      88                 :             : 
      89                 :           0 : static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
      90                 :             : {
      91                 :           0 :     arith_uint256 target;
      92                 :           0 :     bool neg, over;
      93                 :           0 :     target.SetCompact(nBits, &neg, &over);
      94   [ #  #  #  #  :           0 :     if (target == 0 || neg || over) return;
                   #  # ]
      95                 :           0 :     header.nNonce = offset;
      96                 :             : 
      97                 :           0 :     uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
      98                 :           0 :     finish = finish - (finish % step) + offset;
      99                 :             : 
     100   [ #  #  #  # ]:           0 :     while (!found && header.nNonce < finish) {
     101         [ #  # ]:           0 :         const uint32_t next = (finish - header.nNonce < 5000*step) ? finish : header.nNonce + 5000*step;
     102                 :           0 :         do {
     103         [ #  # ]:           0 :             if (UintToArith256(header.GetHash()) <= target) {
     104         [ #  # ]:           0 :                 if (!found.exchange(true)) {
     105                 :           0 :                     proposed_nonce = header.nNonce;
     106                 :             :                 }
     107                 :           0 :                 return;
     108                 :             :             }
     109                 :           0 :             header.nNonce += step;
     110         [ #  # ]:           0 :         } while(header.nNonce != next);
     111                 :             :     }
     112                 :             : }
     113                 :             : 
     114                 :           0 : static int Grind(const std::vector<std::string>& args, std::string& strPrint)
     115                 :             : {
     116   [ #  #  #  # ]:           0 :     if (args.size() != 1) {
     117                 :           0 :         strPrint = "Must specify block header to grind";
     118                 :           0 :         return EXIT_FAILURE;
     119                 :             :     }
     120                 :             : 
     121                 :           0 :     CBlockHeader header;
     122         [ #  # ]:           0 :     if (!DecodeHexBlockHeader(header, args[0])) {
     123                 :           0 :         strPrint = "Could not decode block header";
     124                 :           0 :         return EXIT_FAILURE;
     125                 :             :     }
     126                 :             : 
     127                 :           0 :     uint32_t nBits = header.nBits;
     128                 :           0 :     std::atomic<bool> found{false};
     129                 :           0 :     uint32_t proposed_nonce{};
     130                 :             : 
     131                 :           0 :     std::vector<std::thread> threads;
     132         [ #  # ]:           0 :     int n_tasks = std::max(1u, std::thread::hardware_concurrency());
     133         [ #  # ]:           0 :     threads.reserve(n_tasks);
     134         [ #  # ]:           0 :     for (int i = 0; i < n_tasks; ++i) {
     135         [ #  # ]:           0 :         threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
     136                 :             :     }
     137         [ #  # ]:           0 :     for (auto& t : threads) {
     138         [ #  # ]:           0 :         t.join();
     139                 :             :     }
     140         [ #  # ]:           0 :     if (found) {
     141                 :           0 :         header.nNonce = proposed_nonce;
     142                 :             :     } else {
     143         [ #  # ]:           0 :         strPrint = "Could not satisfy difficulty target";
     144                 :             :         return EXIT_FAILURE;
     145                 :             :     }
     146                 :             : 
     147                 :           0 :     DataStream ss{};
     148         [ #  # ]:           0 :     ss << header;
     149   [ #  #  #  # ]:           0 :     strPrint = HexStr(ss);
     150                 :           0 :     return EXIT_SUCCESS;
     151                 :           0 : }
     152                 :             : 
     153                 :           0 : MAIN_FUNCTION
     154                 :             : {
     155                 :           0 :     ArgsManager& args = gArgs;
     156                 :           0 :     SetupEnvironment();
     157                 :             : 
     158                 :           0 :     try {
     159         [ #  # ]:           0 :         int ret = AppInitUtil(args, argc, argv);
     160         [ #  # ]:           0 :         if (ret != CONTINUE_EXECUTION) {
     161                 :             :             return ret;
     162                 :             :         }
     163         [ -  - ]:           0 :     } catch (const std::exception& e) {
     164         [ -  - ]:           0 :         PrintExceptionContinue(&e, "AppInitUtil()");
     165                 :           0 :         return EXIT_FAILURE;
     166                 :           0 :     } catch (...) {
     167         [ -  - ]:           0 :         PrintExceptionContinue(nullptr, "AppInitUtil()");
     168                 :           0 :         return EXIT_FAILURE;
     169                 :           0 :     }
     170                 :             : 
     171                 :           0 :     const auto cmd = args.GetCommand();
     172         [ #  # ]:           0 :     if (!cmd) {
     173         [ #  # ]:           0 :         tfm::format(std::cerr, "Error: must specify a command\n");
     174                 :             :         return EXIT_FAILURE;
     175                 :             :     }
     176                 :             : 
     177                 :           0 :     int ret = EXIT_FAILURE;
     178         [ #  # ]:           0 :     std::string strPrint;
     179                 :           0 :     try {
     180         [ #  # ]:           0 :         if (cmd->command == "grind") {
     181         [ #  # ]:           0 :             ret = Grind(cmd->args, strPrint);
     182                 :             :         } else {
     183                 :           0 :             assert(false); // unknown command should be caught earlier
     184                 :             :         }
     185         [ -  - ]:           0 :     } catch (const std::exception& e) {
     186         [ -  - ]:           0 :         strPrint = std::string("error: ") + e.what();
     187                 :           0 :     } catch (...) {
     188         [ -  - ]:           0 :         strPrint = "unknown error";
     189         [ -  - ]:           0 :     }
     190                 :             : 
     191         [ #  # ]:           0 :     if (strPrint != "") {
     192   [ #  #  #  # ]:           0 :         tfm::format(ret == 0 ? std::cout : std::cerr, "%s\n", strPrint);
     193                 :             :     }
     194                 :             : 
     195                 :           0 :     return ret;
     196                 :           0 : }
        

Generated by: LCOV version 2.0-1