Bitcoin Core  29.1.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 #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/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 
30 
31 static void SetupBitcoinUtilArgs(ArgsManager &argsman)
32 {
33  SetupHelpOptions(argsman);
34 
35  argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
36 
37  argsman.AddCommand("grind", "Perform proof of work on hex header string");
38 
40 }
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 static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
45 {
47  std::string error;
48  if (!args.ParseParameters(argc, argv, error)) {
49  tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
50  return EXIT_FAILURE;
51  }
52 
53  if (HelpRequested(args) || args.GetBoolArg("-version", false)) {
54  // First part of help message is specific to this utility
55  std::string strUsage = CLIENT_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n";
56 
57  if (args.GetBoolArg("-version", false)) {
58  strUsage += FormatParagraph(LicenseInfo());
59  } else {
60  strUsage += "\n"
61  "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"
62  "\n"
63  "Usage: bitcoin-util [options] [command]\n"
64  "or: bitcoin-util [options] grind <hex-block-header>\n";
65  strUsage += "\n" + args.GetHelpMessage();
66  }
67 
68  tfm::format(std::cout, "%s", strUsage);
69 
70  if (argc < 2) {
71  tfm::format(std::cerr, "Error: too few parameters\n");
72  return EXIT_FAILURE;
73  }
74  return EXIT_SUCCESS;
75  }
76 
77  // Check for chain settings (Params() calls are only valid after this clause)
78  try {
80  } catch (const std::exception& e) {
81  tfm::format(std::cerr, "Error: %s\n", e.what());
82  return EXIT_FAILURE;
83  }
84 
85  return CONTINUE_EXECUTION;
86 }
87 
88 static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic<bool>& found, uint32_t& proposed_nonce)
89 {
90  arith_uint256 target;
91  bool neg, over;
92  target.SetCompact(nBits, &neg, &over);
93  if (target == 0 || neg || over) return;
94  header.nNonce = offset;
95 
96  uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
97  finish = finish - (finish % step) + offset;
98 
99  while (!found && header.nNonce < finish) {
100  const uint32_t next = (finish - header.nNonce < 5000*step) ? finish : header.nNonce + 5000*step;
101  do {
102  if (UintToArith256(header.GetHash()) <= target) {
103  if (!found.exchange(true)) {
104  proposed_nonce = header.nNonce;
105  }
106  return;
107  }
108  header.nNonce += step;
109  } while(header.nNonce != next);
110  }
111 }
112 
113 static int Grind(const std::vector<std::string>& args, std::string& strPrint)
114 {
115  if (args.size() != 1) {
116  strPrint = "Must specify block header to grind";
117  return EXIT_FAILURE;
118  }
119 
120  CBlockHeader header;
121  if (!DecodeHexBlockHeader(header, args[0])) {
122  strPrint = "Could not decode block header";
123  return EXIT_FAILURE;
124  }
125 
126  uint32_t nBits = header.nBits;
127  std::atomic<bool> found{false};
128  uint32_t proposed_nonce{};
129 
130  std::vector<std::thread> threads;
131  int n_tasks = std::max(1u, std::thread::hardware_concurrency());
132  threads.reserve(n_tasks);
133  for (int i = 0; i < n_tasks; ++i) {
134  threads.emplace_back(grind_task, nBits, header, i, n_tasks, std::ref(found), std::ref(proposed_nonce));
135  }
136  for (auto& t : threads) {
137  t.join();
138  }
139  if (found) {
140  header.nNonce = proposed_nonce;
141  } else {
142  strPrint = "Could not satisfy difficulty target";
143  return EXIT_FAILURE;
144  }
145 
146  DataStream ss{};
147  ss << header;
148  strPrint = HexStr(ss);
149  return EXIT_SUCCESS;
150 }
151 
153 {
156 
157  try {
158  int ret = AppInitUtil(args, argc, argv);
160  return ret;
161  }
162  } catch (const std::exception& e) {
163  PrintExceptionContinue(&e, "AppInitUtil()");
164  return EXIT_FAILURE;
165  } catch (...) {
166  PrintExceptionContinue(nullptr, "AppInitUtil()");
167  return EXIT_FAILURE;
168  }
169 
170  const auto cmd = args.GetCommand();
171  if (!cmd) {
172  tfm::format(std::cerr, "Error: must specify a command\n");
173  return EXIT_FAILURE;
174  }
175 
176  int ret = EXIT_FAILURE;
177  std::string strPrint;
178  try {
179  if (cmd->command == "grind") {
180  ret = Grind(cmd->args, strPrint);
181  } else {
182  assert(false); // unknown command should be caught earlier
183  }
184  } catch (const std::exception& e) {
185  strPrint = std::string("error: ") + e.what();
186  } catch (...) {
187  strPrint = "unknown error";
188  }
189 
190  if (strPrint != "") {
191  tfm::format(ret == 0 ? std::cout : std::cerr, "%s\n", strPrint);
192  }
193 
194  return ret;
195 }
uint32_t nNonce
Definition: block.h:30
void SetupChainParamsBaseOptions(ArgsManager &argsman)
Set the arguments for chainparams.
return EXIT_SUCCESS
assert(!tx.IsCoinBase())
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
std::string strPrint
const auto cmd
SetupEnvironment()
Definition: system.cpp:59
int ret
bool ParseParameters(int argc, const char *const argv[], std::string &error)
Definition: args.cpp:179
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
std::string LicenseInfo()
Returns licensing information (for -version)
static const int CONTINUE_EXECUTION
bool DecodeHexBlockHeader(CBlockHeader &, const std::string &hex_header)
Definition: core_read.cpp:206
void format(std::ostream &out, FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1079
static int Grind(const std::vector< std::string > &args, std::string &strPrint)
void PrintExceptionContinue(const std::exception *pex, std::string_view thread_name)
Definition: exception.cpp:36
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
Definition: args.cpp:774
disable validation
Definition: args.h:106
arith_uint256 UintToArith256(const uint256 &a)
std::string GetHelpMessage() const
Get the help string.
Definition: args.cpp:609
void AddCommand(const std::string &cmd, const std::string &help)
Add subcommand.
Definition: args.cpp:552
ArgsManager & args
Definition: bitcoind.cpp:277
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:146
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
Definition: translation.h:16
void AddArg(const std::string &name, const std::string &help, unsigned int flags, const OptionsCategory &cat)
Add argument.
Definition: args.cpp:564
if(ret !=CONTINUE_EXECUTION)
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...
std::optional< const Command > GetCommand() const
Get the command and command args (returns std::nullopt if no command provided)
Definition: args.cpp:342
256-bit unsigned big integer.
std::string FormatFullVersion()
ArgsManager gArgs
Definition: args.cpp:42
uint256 GetHash() const
Definition: block.cpp:11
void SetupHelpOptions(ArgsManager &args)
Add help options to the args manager.
Definition: args.cpp:689
bool HelpRequested(const ArgsManager &args)
Definition: args.cpp:684
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
static int AppInitUtil(ArgsManager &args, int argc, char *argv[])
const TranslateFn G_TRANSLATION_FUN
Translate string to current locale using Qt.
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...
static void grind_task(uint32_t nBits, CBlockHeader header, uint32_t offset, uint32_t step, std::atomic< bool > &found, uint32_t &proposed_nonce)
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
uint32_t nBits
Definition: block.h:29
MAIN_FUNCTION