LCOV - code coverage report
Current view: top level - src - bitcoin-util.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 71.8 % 103 74
Test Date: 2024-07-07 05:05:19 Functions: 100.0 % 5 5
Branches: 40.1 % 142 57

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

Generated by: LCOV version 2.0-1