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