Skip to content

Commit 13762bc

Browse files
committed
Add bitcoin-util command line utility
1 parent 95d5d5e commit 13762bc

File tree

6 files changed

+290
-2
lines changed

6 files changed

+290
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ src/bitcoin-cli
88
src/bitcoin-gui
99
src/bitcoin-node
1010
src/bitcoin-tx
11+
src/bitcoin-util
1112
src/bitcoin-wallet
1213
src/test/fuzz/*
1314
!src/test/fuzz/*.*

Makefile.am

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ BITCOIND_BIN=$(top_builddir)/src/$(BITCOIN_DAEMON_NAME)$(EXEEXT)
2424
BITCOIN_QT_BIN=$(top_builddir)/src/qt/$(BITCOIN_GUI_NAME)$(EXEEXT)
2525
BITCOIN_CLI_BIN=$(top_builddir)/src/$(BITCOIN_CLI_NAME)$(EXEEXT)
2626
BITCOIN_TX_BIN=$(top_builddir)/src/$(BITCOIN_TX_NAME)$(EXEEXT)
27+
BITCOIN_UTIL_BIN=$(top_builddir)/src/$(BITCOIN_UTIL_NAME)$(EXEEXT)
2728
BITCOIN_WALLET_BIN=$(top_builddir)/src/$(BITCOIN_WALLET_TOOL_NAME)$(EXEEXT)
2829
BITCOIN_NODE_BIN=$(top_builddir)/src/$(BITCOIN_MP_NODE_NAME)$(EXEEXT)
2930
BITCOIN_GUI_BIN=$(top_builddir)/src/$(BITCOIN_MP_GUI_NAME)$(EXEEXT)
@@ -78,6 +79,7 @@ $(BITCOIN_WIN_INSTALLER): all-recursive
7879
STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $(BITCOIN_CLI_BIN) $(top_builddir)/release
7980
STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $(BITCOIN_TX_BIN) $(top_builddir)/release
8081
STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $(BITCOIN_WALLET_BIN) $(top_builddir)/release
82+
STRIPPROG="$(STRIP)" $(INSTALL_STRIP_PROGRAM) $(BITCOIN_UTIL_BIN) $(top_builddir)/release
8183
@test -f $(MAKENSIS) && echo 'OutFile "$@"' | cat $(top_builddir)/share/setup.nsi - | $(MAKENSIS) -V2 - || \
8284
echo error: could not build $@
8385
@echo built $@
@@ -171,6 +173,9 @@ $(BITCOIN_CLI_BIN): FORCE
171173
$(BITCOIN_TX_BIN): FORCE
172174
$(MAKE) -C src $(@F)
173175

176+
$(BITCOIN_UTIL_BIN): FORCE
177+
$(MAKE) -C src $(@F)
178+
174179
$(BITCOIN_WALLET_BIN): FORCE
175180
$(MAKE) -C src $(@F)
176181

configure.ac

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ BITCOIN_DAEMON_NAME=bitcoind
2323
BITCOIN_GUI_NAME=bitcoin-qt
2424
BITCOIN_CLI_NAME=bitcoin-cli
2525
BITCOIN_TX_NAME=bitcoin-tx
26+
BITCOIN_UTIL_NAME=bitcoin-util
2627
BITCOIN_WALLET_TOOL_NAME=bitcoin-wallet
2728
dnl Multi Process
2829
BITCOIN_MP_NODE_NAME=bitcoin-node
@@ -553,7 +554,7 @@ CPPFLAGS="$CPPFLAGS -DHAVE_BUILD_INFO -D__STDC_FORMAT_MACROS"
553554

554555
AC_ARG_WITH([utils],
555556
[AS_HELP_STRING([--with-utils],
556-
[build bitcoin-cli bitcoin-tx bitcoin-wallet (default=yes)])],
557+
[build bitcoin-cli bitcoin-tx bitcoin-util bitcoin-wallet (default=yes)])],
557558
[build_bitcoin_utils=$withval],
558559
[build_bitcoin_utils=yes])
559560

@@ -575,6 +576,12 @@ AC_ARG_ENABLE([util-wallet],
575576
[build_bitcoin_wallet=$enableval],
576577
[build_bitcoin_wallet=$build_bitcoin_utils])
577578

579+
AC_ARG_ENABLE([util-util],
580+
[AS_HELP_STRING([--enable-util-util],
581+
[build bitcoin-util])],
582+
[build_bitcoin_util=$enableval],
583+
[build_bitcoin_util=$build_bitcoin_utils])
584+
578585
AC_ARG_WITH([libs],
579586
[AS_HELP_STRING([--with-libs],
580587
[build libraries (default=yes)])],
@@ -1185,6 +1192,7 @@ if test "x$enable_fuzz" = "xyes"; then
11851192
build_bitcoin_utils=no
11861193
build_bitcoin_cli=no
11871194
build_bitcoin_tx=no
1195+
build_bitcoin_util=no
11881196
build_bitcoin_wallet=no
11891197
build_bitcoind=no
11901198
build_bitcoin_libs=no
@@ -1401,7 +1409,7 @@ fi
14011409
dnl univalue check
14021410

14031411
need_bundled_univalue=yes
1404-
if test x$build_bitcoin_wallet$build_bitcoin_cli$build_bitcoin_tx$build_bitcoind$bitcoin_enable_qt$use_tests$use_bench = xnonononononono; then
1412+
if test x$build_bitcoin_wallet$build_bitcoin_cli$build_bitcoin_tx$build_bitcoin_util$build_bitcoind$bitcoin_enable_qt$use_tests$use_bench = xnononononononono; then
14051413
need_bundled_univalue=no
14061414
else
14071415
if test x$system_univalue != xno; then
@@ -1484,6 +1492,10 @@ AC_MSG_CHECKING([whether to build bitcoin-wallet])
14841492
AM_CONDITIONAL([BUILD_BITCOIN_WALLET], [test x$build_bitcoin_wallet = xyes])
14851493
AC_MSG_RESULT($build_bitcoin_wallet)
14861494

1495+
AC_MSG_CHECKING([whether to build bitcoin-util])
1496+
AM_CONDITIONAL([BUILD_BITCOIN_UTIL], [test x$build_bitcoin_util = xyes])
1497+
AC_MSG_RESULT($build_bitcoin_util)
1498+
14871499
AC_MSG_CHECKING([whether to build libraries])
14881500
AM_CONDITIONAL([BUILD_BITCOIN_LIBS], [test x$build_bitcoin_libs = xyes])
14891501
if test x$build_bitcoin_libs = xyes; then
@@ -1659,6 +1671,7 @@ AC_SUBST(BITCOIN_DAEMON_NAME)
16591671
AC_SUBST(BITCOIN_GUI_NAME)
16601672
AC_SUBST(BITCOIN_CLI_NAME)
16611673
AC_SUBST(BITCOIN_TX_NAME)
1674+
AC_SUBST(BITCOIN_UTIL_NAME)
16621675
AC_SUBST(BITCOIN_WALLET_TOOL_NAME)
16631676
AC_SUBST(BITCOIN_MP_NODE_NAME)
16641677
AC_SUBST(BITCOIN_MP_GUI_NAME)

src/Makefile.am

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,21 @@ endif
9292
if BUILD_BITCOIN_CLI
9393
bin_PROGRAMS += bitcoin-cli
9494
endif
95+
9596
if BUILD_BITCOIN_TX
9697
bin_PROGRAMS += bitcoin-tx
9798
endif
99+
98100
if ENABLE_WALLET
99101
if BUILD_BITCOIN_WALLET
100102
bin_PROGRAMS += bitcoin-wallet
101103
endif
102104
endif
103105

106+
if BUILD_BITCOIN_UTIL
107+
bin_PROGRAMS += bitcoin-util
108+
endif
109+
104110
.PHONY: FORCE check-symbols check-security
105111
# bitcoin core #
106112
BITCOIN_CORE_H = \
@@ -660,6 +666,27 @@ bitcoin_wallet_SOURCES += bitcoin-wallet-res.rc
660666
endif
661667
#
662668

669+
# bitcoin-util binary #
670+
bitcoin_util_SOURCES = bitcoin-util.cpp
671+
bitcoin_util_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
672+
bitcoin_util_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
673+
bitcoin_util_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
674+
675+
if TARGET_WINDOWS
676+
bitcoin_util_SOURCES += bitcoin-util-res.rc
677+
endif
678+
679+
bitcoin_util_LDADD = \
680+
$(LIBBITCOIN_COMMON) \
681+
$(LIBBITCOIN_UTIL) \
682+
$(LIBUNIVALUE) \
683+
$(LIBBITCOIN_CONSENSUS) \
684+
$(LIBBITCOIN_CRYPTO) \
685+
$(LIBSECP256K1)
686+
687+
bitcoin_util_LDADD += $(BOOST_LIBS)
688+
#
689+
663690
# bitcoinconsensus library #
664691
if BUILD_BITCOIN_LIBS
665692
include_HEADERS = script/bitcoinconsensus.h

src/bitcoin-util-res.rc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <windows.h> // needed for VERSIONINFO
2+
#include "clientversion.h" // holds the needed client version information
3+
4+
#define VER_PRODUCTVERSION CLIENT_VERSION_MAJOR,CLIENT_VERSION_MINOR,CLIENT_VERSION_BUILD
5+
#define VER_PRODUCTVERSION_STR STRINGIZE(CLIENT_VERSION_MAJOR) "." STRINGIZE(CLIENT_VERSION_MINOR) "." STRINGIZE(CLIENT_VERSION_BUILD)
6+
#define VER_FILEVERSION VER_PRODUCTVERSION
7+
#define VER_FILEVERSION_STR VER_PRODUCTVERSION_STR
8+
9+
VS_VERSION_INFO VERSIONINFO
10+
FILEVERSION VER_FILEVERSION
11+
PRODUCTVERSION VER_PRODUCTVERSION
12+
FILEOS VOS_NT_WINDOWS32
13+
FILETYPE VFT_APP
14+
BEGIN
15+
BLOCK "StringFileInfo"
16+
BEGIN
17+
BLOCK "040904E4" // U.S. English - multilingual (hex)
18+
BEGIN
19+
VALUE "CompanyName", "Bitcoin"
20+
VALUE "FileDescription", "bitcoin-util (CLI Bitcoin utility)"
21+
VALUE "FileVersion", VER_FILEVERSION_STR
22+
VALUE "InternalName", "bitcoin-util"
23+
VALUE "LegalCopyright", COPYRIGHT_STR
24+
VALUE "LegalTrademarks1", "Distributed under the MIT software license, see the accompanying file COPYING or http://www.opensource.org/licenses/mit-license.php."
25+
VALUE "OriginalFilename", "bitcoin-util.exe"
26+
VALUE "ProductName", "bitcoin-util"
27+
VALUE "ProductVersion", VER_PRODUCTVERSION_STR
28+
END
29+
END
30+
31+
BLOCK "VarFileInfo"
32+
BEGIN
33+
VALUE "Translation", 0x0, 1252 // language neutral - multilingual (decimal)
34+
END
35+
END

src/bitcoin-util.cpp

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
// Copyright (c) 2009-2020 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)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <arith_uint256.h>
10+
#include <clientversion.h>
11+
#include <coins.h>
12+
#include <consensus/consensus.h>
13+
#include <core_io.h>
14+
#include <key_io.h>
15+
#include <policy/rbf.h>
16+
#include <primitives/transaction.h>
17+
#include <script/script.h>
18+
#include <script/sign.h>
19+
#include <script/signingprovider.h>
20+
#include <univalue.h>
21+
#include <util/moneystr.h>
22+
#include <util/rbf.h>
23+
#include <util/strencodings.h>
24+
#include <util/string.h>
25+
#include <util/system.h>
26+
#include <util/translation.h>
27+
28+
#include <functional>
29+
#include <memory>
30+
#include <stdio.h>
31+
#include <thread>
32+
33+
#include <boost/algorithm/string.hpp>
34+
35+
static const int CONTINUE_EXECUTION=-1;
36+
37+
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
38+
39+
static void SetupBitcoinUtilArgs(ArgsManager &argsman)
40+
{
41+
SetupHelpOptions(argsman);
42+
43+
SetupChainParamsBaseOptions(argsman);
44+
}
45+
46+
// This function returns either one of EXIT_ codes when it's expected to stop the process or
47+
// CONTINUE_EXECUTION when it's expected to continue further.
48+
static int AppInitUtil(int argc, char* argv[])
49+
{
50+
SetupBitcoinUtilArgs(gArgs);
51+
std::string error;
52+
if (!gArgs.ParseParameters(argc, argv, error)) {
53+
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
54+
return EXIT_FAILURE;
55+
}
56+
57+
// Check for chain settings (Params() calls are only valid after this clause)
58+
try {
59+
SelectParams(gArgs.GetChainName());
60+
} catch (const std::exception& e) {
61+
tfm::format(std::cerr, "Error: %s\n", e.what());
62+
return EXIT_FAILURE;
63+
}
64+
65+
if (argc < 2 || HelpRequested(gArgs)) {
66+
// First part of help message is specific to this utility
67+
std::string strUsage = PACKAGE_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n\n" +
68+
"Usage: bitcoin-util [options] [commands] Do stuff\n" +
69+
"\n";
70+
strUsage += gArgs.GetHelpMessage();
71+
72+
tfm::format(std::cout, "%s", strUsage);
73+
74+
if (argc < 2) {
75+
tfm::format(std::cerr, "Error: too few parameters\n");
76+
return EXIT_FAILURE;
77+
}
78+
return EXIT_SUCCESS;
79+
}
80+
return CONTINUE_EXECUTION;
81+
}
82+
83+
static void grind_task(uint32_t nBits, CBlockHeader& header_orig, uint32_t offset, uint32_t step, std::atomic<bool>& found)
84+
{
85+
arith_uint256 target;
86+
bool neg, over;
87+
target.SetCompact(nBits, &neg, &over);
88+
if (target == 0 || neg || over) return;
89+
CBlockHeader header = header_orig; // working copy
90+
header.nNonce = offset;
91+
92+
uint32_t finish = std::numeric_limits<uint32_t>::max() - step;
93+
finish = finish - (finish % step) + offset;
94+
95+
while (!found && header.nNonce < finish) {
96+
const uint32_t next = (finish - header.nNonce < 5000*step) ? finish : header.nNonce + 5000*step;
97+
do {
98+
if (UintToArith256(header.GetHash()) <= target) {
99+
if (!found.exchange(true)) {
100+
header_orig.nNonce = header.nNonce;
101+
}
102+
return;
103+
}
104+
header.nNonce += step;
105+
} while(header.nNonce != next);
106+
}
107+
}
108+
109+
static int Grind(int argc, char* argv[], std::string& strPrint)
110+
{
111+
if (argc != 1) {
112+
strPrint = "Must specify block header to grind";
113+
return 1;
114+
}
115+
116+
CBlockHeader header;
117+
if (!DecodeHexBlockHeader(header, argv[0])) {
118+
strPrint = "Could not decode block header";
119+
return 1;
120+
}
121+
122+
uint32_t nBits = header.nBits;
123+
std::atomic<bool> found{false};
124+
125+
std::vector<std::thread> threads;
126+
int n_tasks = std::max(1u, std::thread::hardware_concurrency());
127+
for (int i = 0; i < n_tasks; ++i) {
128+
threads.emplace_back( grind_task, nBits, std::ref(header), i, n_tasks, std::ref(found) );
129+
}
130+
for (auto& t : threads) {
131+
t.join();
132+
}
133+
if (!found) {
134+
strPrint = "Could not satisfy difficulty target";
135+
return 1;
136+
}
137+
138+
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
139+
ss << header;
140+
strPrint = HexStr(ss);
141+
return 0;
142+
}
143+
144+
static int CommandLineUtil(int argc, char* argv[])
145+
{
146+
if (argc <= 1) return 1;
147+
148+
std::string strPrint;
149+
int nRet = 0;
150+
151+
try {
152+
while (argc > 1 && IsSwitchChar(argv[1][0]) && (argv[1][1] != 0)) {
153+
--argc;
154+
++argv;
155+
}
156+
157+
char* command = argv[1];
158+
if (strcmp(command, "grind") == 0) {
159+
nRet = Grind(argc-2, argv+2, strPrint);
160+
} else {
161+
strPrint = strprintf("Unknown command %s", command);
162+
nRet = 1;
163+
}
164+
}
165+
catch (const std::exception& e) {
166+
strPrint = std::string("error: ") + e.what();
167+
nRet = EXIT_FAILURE;
168+
}
169+
catch (...) {
170+
PrintExceptionContinue(nullptr, "CommandLineUtil()");
171+
throw;
172+
}
173+
174+
if (strPrint != "") {
175+
tfm::format(nRet == 0 ? std::cout : std::cerr, "%s\n", strPrint);
176+
}
177+
return nRet;
178+
}
179+
180+
int main(int argc, char* argv[])
181+
{
182+
SetupEnvironment();
183+
184+
try {
185+
int ret = AppInitUtil(argc, argv);
186+
if (ret != CONTINUE_EXECUTION)
187+
return ret;
188+
}
189+
catch (const std::exception& e) {
190+
PrintExceptionContinue(&e, "AppInitUtil()");
191+
return EXIT_FAILURE;
192+
} catch (...) {
193+
PrintExceptionContinue(nullptr, "AppInitUtil()");
194+
return EXIT_FAILURE;
195+
}
196+
197+
int ret = EXIT_FAILURE;
198+
try {
199+
ret = CommandLineUtil(argc, argv);
200+
}
201+
catch (const std::exception& e) {
202+
PrintExceptionContinue(&e, "CommandLineUtil()");
203+
} catch (...) {
204+
PrintExceptionContinue(nullptr, "CommandLineUtil()");
205+
}
206+
return ret;
207+
}

0 commit comments

Comments
 (0)