Bitcoin Core  27.99.0
P2P Digital Currency
bitcoin-util.cpp
Go to the documentation of this file.
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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <arith_uint256.h>
10 #include <chain.h>
11 #include <chainparams.h>
12 #include <chainparamsbase.h>
13 #include <clientversion.h>
14 #include <common/args.h>
15 #include <common/system.h>
16 #include <compat/compat.h>
17 #include <core_io.h>
18 #include <streams.h>
19 #include <util/exception.h>
20 #include <util/strencodings.h>
21 #include <util/translation.h>
22 
23 #include <atomic>
24 #include <cstdio>
25 #include <functional>
26 #include <memory>
27 #include <thread>
28 
29 static const int CONTINUE_EXECUTION=-1;
30 
31 const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
32 
33 static void SetupBitcoinUtilArgs(ArgsManager &argsman)
34 {
35  SetupHelpOptions(argsman);
36 
37  argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
38 
39  argsman.AddCommand("grind", "Perform proof of work on hex header string");
40 
42 }
43 
44 // This function returns either one of EXIT_ codes when it's expected to stop the process or
45 // CONTINUE_EXECUTION when it's expected to continue further.
46 static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
47 {
49  std::string error;
50  if (!args.ParseParameters(argc, argv, error)) {
51  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
52  return EXIT_FAILURE;
53  }
54 
55  if (HelpRequested(args) || args.IsArgSet("-version")) {
56  // First part of help message is specific to this utility
57  std::string strUsage = PACKAGE_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n";
58 
59  if (args.IsArgSet("-version")) {
60  strUsage += FormatParagraph(LicenseInfo());
61  } else {
62  strUsage += "\n"
63  "Usage: bitcoin-util [options] [commands] Do stuff\n";
64  strUsage += "\n" + args.GetHelpMessage();
65  }
66 
67  tfm::format(std::cout, "%s", strUsage);
68 
69  if (argc < 2) {
70  tfm::format(std::cerr, "Error: too few parameters\n");
71  return EXIT_FAILURE;
72  }
73  return EXIT_SUCCESS;
74  }
75 
76  // Check for chain settings (Params() calls are only valid after this clause)
77  try {
79  } catch (const std::exception& e) {
80  tfm::format(std::cerr, "Error: %s\n", e.what());
81  return EXIT_FAILURE;
82  }
83 
84  return CONTINUE_EXECUTION;
85 }
86 
87 static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
88 {
89  arith_uint256 target;
90  bool neg, over;
91  target.SetCompact(nBits, &neg, &over);
92  if (target == 0 || neg || over) return;
93  header.nNonce = offset;
94 
95  uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
96  finish = finish - (finish % step) + offset;
97 
98  while (!found && header.nNonce < finish) {
99  const uint32_t next = (finish - header.nNonce < 5000*step) ? finish : header.nNonce + 5000*step;
100  do {
101  if (UintToArith256(header.GetHash()) <= target) {
102  if (!found.exchange(true)) {
103  proposed_nonce = header.nNonce;
104  }
105  return;
106  }
107  header.nNonce += step;
108  } while(header.nNonce != next);
109  }
110 }
111 
112 static int Grind(const std::vector<std::string>& args, std::string& strPrint)
113 {
114  if (args.size() != 1) {
115  strPrint = "Must specify block header to grind";
116  return EXIT_FAILURE;
117  }
118 
119  CBlockHeader header;
120  if (!DecodeHexBlockHeader(header, args[0])) {
121  strPrint = "Could not decode block header";
122  return EXIT_FAILURE;
123  }
124 
125  uint32_t nBits = header.nBits;
126  std::atomic<bool> found{false};
127  uint32_t proposed_nonce{};
128 
129  std::vector<std::thread> threads;
130  int n_tasks = std::max(1u, std::thread::hardware_concurrency());
131  threads.reserve(n_tasks);
132  for (int i = 0; i < n_tasks; ++i) {
133  threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
134  }
135  for (auto& t : threads) {
136  t.join();
137  }
138  if (found) {
139  header.nNonce = proposed_nonce;
140  } else {
141  strPrint = "Could not satisfy difficulty target";
142  return EXIT_FAILURE;
143  }
144 
145  DataStream ss{};
146  ss << header;
147  strPrint = HexStr(ss);
148  return EXIT_SUCCESS;
149 }
150 
152 {
155 
156  try {
157  int ret = AppInitUtil(args, argc, argv);
159  return ret;
160  }
161  } catch (const std::exception& e) {
162  PrintExceptionContinue(&e, "AppInitUtil()");
163  return EXIT_FAILURE;
164  } catch (...) {
165  PrintExceptionContinue(nullptr, "AppInitUtil()");
166  return EXIT_FAILURE;
167  }
168 
169  const auto cmd = args.GetCommand();
170  if (!cmd) {
171  tfm::format(std::cerr, "Error: must specify a command\n");
172  return EXIT_FAILURE;
173  }
174 
175  int ret = EXIT_FAILURE;
176  std::string strPrint;
177  try {
178  if (cmd->command == "grind") {
179  ret = Grind(cmd->args, strPrint);
180  } else {
181  assert(false); // unknown command should be caught earlier
182  }
183  } catch (const std::exception& e) {
184  strPrint = std::string("error: ") + e.what();
185  } catch (...) {
186  strPrint = "unknown error";
187  }
188 
189  if (strPrint != "") {
190  tfm::format(ret == 0 ? std::cout : std::cerr, "%s\n", strPrint);
191  }
192 
193  return ret;
194 }
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:659
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:664
ArgsManager gArgs
Definition: args.cpp:41
arith_uint256 UintToArith256(const uint256 &a)
#define PACKAGE_NAME
static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic< bool > &found, uint32_t &proposed_nonce)
static const int CONTINUE_EXECUTION
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
if(ret !=CONTINUE_EXECUTION)
const auto cmd
const std::function< std::string(const char *)> G_TRANSLATION_FUN
Translate string to current locale using Qt.
int ret
SetupEnvironment()
Definition: system.cpp:59
static int Grind(const std::vector< std::string > &args, std::string &strPrint)
static int AppInitUtil(ArgsManager &args, int argc, char *argv[])
MAIN_FUNCTION
std::string strPrint
return EXIT_SUCCESS
ArgsManager & args
Definition: bitcoind.cpp:268
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
Definition: args.cpp:340
@ ALLOW_ANY
disable validation
Definition: args.h:104
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
Definition: args.cpp:741
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:177
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:590
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
Definition: args.cpp:369
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
Definition: args.cpp:550
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:562
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:22
uint32_t nNonce
Definition: block.h:30
uint32_t nBits
Definition: block.h:29
uint256 GetHash() const
Definition: block.cpp:11
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
std::string FormatFullVersion()
std::string LicenseInfo()
Returns licensing information (for -version)
bool DecodeHexBlockHeader(CBlockHeader &, const std::string &hex_header)
Definition: core_read.cpp:204
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
Definition: exception.cpp:36
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1060
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
Format a paragraph of text to a fixed width, adding spaces for indentation to any added line.
assert(!tx.IsCoinBase())