Skip to content

Commit a196c89

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#22270: test: Add bitcoin-util tests (+refactors)
fa4017e refactor: Pass grind args vector as const reference (MarcoFalke) fa08bc2 Remove gArgs from AppInitUtil (MarcoFalke) fa751a4 Remove unused OptionsCategory arg from AddCommand (MarcoFalke) fa3c1ee Remove unused includes from bitcoin-util (MarcoFalke) fa30492 test: Add bitcoin-util tests (MarcoFalke) fa831e7 build_msvc: Add bitcoin-util.exe (MarcoFalke) Pull request description: bitcoin-util has no tests See https://marcofalke.github.io/btc_cov/total.coverage/src/bitcoin-util.cpp.gcov.html (Coverage report showing 0%) ACKs for top commit: klementtan: Code review and tested ACK fa4017e theStack: Tested ACK fa4017e jamesob: reACK fa4017e ([`jamesob/ackr/22270.1.MarcoFalke.test_add_bitcoin_util_te`](https://github.com/jamesob/bitcoin/tree/ackr/22270.1.MarcoFalke.test_add_bitcoin_util_te)) Tree-SHA512: 68e2791239bc48d28fbb6394155c39ea0357a96ec7e4896ca579feeef1a803657165a0ef9fa3cf6e2a381e5b0ca0dafa1b594158303a04997db784201d8dd66d
2 parents 7317e14 + fa4017e commit a196c89

File tree

7 files changed

+96
-34
lines changed

7 files changed

+96
-34
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="..\common.init.vcxproj" />
4+
<PropertyGroup Label="Globals">
5+
<ProjectGuid>{D3022AF6-AD33-4CE3-B358-87CB6A1B29CF}</ProjectGuid>
6+
</PropertyGroup>
7+
<PropertyGroup Label="Configuration">
8+
<ConfigurationType>Application</ConfigurationType>
9+
<OutDir>$(SolutionDir)$(Platform)\$(Configuration)\</OutDir>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<ClCompile Include="..\..\src\bitcoin-util.cpp" />
13+
</ItemGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\libbitcoinconsensus\libbitcoinconsensus.vcxproj">
16+
<Project>{2b384fa8-9ee1-4544-93cb-0d733c25e8ce}</Project>
17+
</ProjectReference>
18+
<ProjectReference Include="..\libbitcoin_common\libbitcoin_common.vcxproj">
19+
<Project>{7c87e378-df58-482e-aa2f-1bc129bc19ce}</Project>
20+
</ProjectReference>
21+
<ProjectReference Include="..\libbitcoin_crypto\libbitcoin_crypto.vcxproj">
22+
<Project>{6190199c-6cf4-4dad-bfbd-93fa72a760c1}</Project>
23+
</ProjectReference>
24+
<ProjectReference Include="..\libbitcoin_util\libbitcoin_util.vcxproj">
25+
<Project>{b53a5535-ee9d-4c6f-9a26-f79ee3bc3754}</Project>
26+
</ProjectReference>
27+
<ProjectReference Include="..\libunivalue\libunivalue.vcxproj">
28+
<Project>{5724ba7d-a09a-4ba8-800b-c4c1561b3d69}</Project>
29+
</ProjectReference>
30+
<ProjectReference Include="..\libsecp256k1\libsecp256k1.vcxproj">
31+
<Project>{bb493552-3b8c-4a8c-bf69-a6e7a51d2ea6}</Project>
32+
</ProjectReference>
33+
</ItemGroup>
34+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
35+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
36+
<Import Project="..\common.vcxproj" />
37+
</Project>

build_msvc/bitcoin.sln

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bench_bitcoin", "bench_bitc
3232
EndProject
3333
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitcoin-tx", "bitcoin-tx\bitcoin-tx.vcxproj", "{D3022AF6-AD33-4CE3-B358-87CB6A1B29CF}"
3434
EndProject
35+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitcoin-util", "bitcoin-util\bitcoin-util.vcxproj", "{D3022AF6-AD33-4CE3-B358-87CB6A1B29CF}"
36+
EndProject
3537
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "bitcoin-wallet", "bitcoin-wallet\bitcoin-wallet.vcxproj", "{84DE8790-EDE3-4483-81AC-C32F15E861F4}"
3638
EndProject
3739
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libbitcoin_wallet_tool", "libbitcoin_wallet_tool\libbitcoin_wallet_tool.vcxproj", "{F91AC55E-6F5E-4C58-9AC5-B40DB7DEEF93}"

src/bitcoin-util.cpp

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,19 @@
77
#endif
88

99
#include <arith_uint256.h>
10+
#include <chain.h>
11+
#include <chainparams.h>
12+
#include <chainparamsbase.h>
1013
#include <clientversion.h>
11-
#include <coins.h>
12-
#include <consensus/consensus.h>
1314
#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>
15+
#include <streams.h>
2516
#include <util/system.h>
2617
#include <util/translation.h>
2718

2819
#include <atomic>
20+
#include <cstdio>
2921
#include <functional>
3022
#include <memory>
31-
#include <stdio.h>
3223
#include <thread>
3324

3425
#include <boost/algorithm/string.hpp>
@@ -43,29 +34,29 @@ static void SetupBitcoinUtilArgs(ArgsManager &argsman)
4334

4435
argsman.AddArg("-version", "Print version and exit", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
4536

46-
argsman.AddCommand("grind", "Perform proof of work on hex header string", OptionsCategory::COMMANDS);
37+
argsman.AddCommand("grind", "Perform proof of work on hex header string");
4738

4839
SetupChainParamsBaseOptions(argsman);
4940
}
5041

5142
// This function returns either one of EXIT_ codes when it's expected to stop the process or
5243
// CONTINUE_EXECUTION when it's expected to continue further.
53-
static int AppInitUtil(int argc, char* argv[])
44+
static int AppInitUtil(ArgsManager& args, int argc, char* argv[])
5445
{
55-
SetupBitcoinUtilArgs(gArgs);
46+
SetupBitcoinUtilArgs(args);
5647
std::string error;
57-
if (!gArgs.ParseParameters(argc, argv, error)) {
48+
if (!args.ParseParameters(argc, argv, error)) {
5849
tfm::format(std::cerr, "Error parsing command line arguments: %s\n", error);
5950
return EXIT_FAILURE;
6051
}
6152

62-
if (HelpRequested(gArgs) || gArgs.IsArgSet("-version")) {
53+
if (HelpRequested(args) || args.IsArgSet("-version")) {
6354
// First part of help message is specific to this utility
6455
std::string strUsage = PACKAGE_NAME " bitcoin-util utility version " + FormatFullVersion() + "\n";
65-
if (!gArgs.IsArgSet("-version")) {
56+
if (!args.IsArgSet("-version")) {
6657
strUsage += "\n"
6758
"Usage: bitcoin-util [options] [commands] Do stuff\n";
68-
strUsage += "\n" + gArgs.GetHelpMessage();
59+
strUsage += "\n" + args.GetHelpMessage();
6960
}
7061

7162
tfm::format(std::cout, "%s", strUsage);
@@ -79,7 +70,7 @@ static int AppInitUtil(int argc, char* argv[])
7970

8071
// Check for chain settings (Params() calls are only valid after this clause)
8172
try {
82-
SelectParams(gArgs.GetChainName());
73+
SelectParams(args.GetChainName());
8374
} catch (const std::exception& e) {
8475
tfm::format(std::cerr, "Error: %s\n", e.what());
8576
return EXIT_FAILURE;
@@ -114,7 +105,7 @@ static void grind_task(uint32_t nBits, CBlockHeader& header_orig, uint32_t offse
114105
}
115106
}
116107

117-
static int Grind(std::vector<std::string> args, std::string& strPrint)
108+
static int Grind(const std::vector<std::string>& args, std::string& strPrint)
118109
{
119110
if (args.size() != 1) {
120111
strPrint = "Must specify block header to grind";
@@ -160,12 +151,14 @@ __declspec(dllexport) int main(int argc, char* argv[])
160151
int main(int argc, char* argv[])
161152
#endif
162153
{
154+
ArgsManager& args = gArgs;
163155
SetupEnvironment();
164156

165157
try {
166-
int ret = AppInitUtil(argc, argv);
167-
if (ret != CONTINUE_EXECUTION)
158+
int ret = AppInitUtil(args, argc, argv);
159+
if (ret != CONTINUE_EXECUTION) {
168160
return ret;
161+
}
169162
} catch (const std::exception& e) {
170163
PrintExceptionContinue(&e, "AppInitUtil()");
171164
return EXIT_FAILURE;
@@ -174,7 +167,7 @@ int main(int argc, char* argv[])
174167
return EXIT_FAILURE;
175168
}
176169

177-
const auto cmd = gArgs.GetCommand();
170+
const auto cmd = args.GetCommand();
178171
if (!cmd) {
179172
tfm::format(std::cerr, "Error: must specify a command\n");
180173
return EXIT_FAILURE;

src/bitcoin-wallet.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ static void SetupWalletToolArgs(ArgsManager& argsman)
3333
argsman.AddArg("-format=<format>", "The format of the wallet file to create. Either \"bdb\" or \"sqlite\". Only used with 'createfromdump'", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
3434
argsman.AddArg("-printtoconsole", "Send trace/debug info to console (default: 1 when no -debug is true, 0 otherwise).", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
3535

36-
argsman.AddCommand("info", "Get wallet info", OptionsCategory::COMMANDS);
37-
argsman.AddCommand("create", "Create new wallet file", OptionsCategory::COMMANDS);
38-
argsman.AddCommand("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.", OptionsCategory::COMMANDS);
39-
argsman.AddCommand("dump", "Print out all of the wallet key-value records", OptionsCategory::COMMANDS);
40-
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records", OptionsCategory::COMMANDS);
36+
argsman.AddCommand("info", "Get wallet info");
37+
argsman.AddCommand("create", "Create new wallet file");
38+
argsman.AddCommand("salvage", "Attempt to recover private keys from a corrupt wallet. Warning: 'salvage' is experimental.");
39+
argsman.AddCommand("dump", "Print out all of the wallet key-value records");
40+
argsman.AddCommand("createfromdump", "Create new wallet file from dumped records");
4141
}
4242

4343
static bool WalletAppInit(ArgsManager& args, int argc, char* argv[])

src/util/system.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,14 +620,14 @@ void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strV
620620
m_settings.forced_settings[SettingName(strArg)] = strValue;
621621
}
622622

623-
void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, const OptionsCategory& cat)
623+
void ArgsManager::AddCommand(const std::string& cmd, const std::string& help)
624624
{
625625
Assert(cmd.find('=') == std::string::npos);
626626
Assert(cmd.at(0) != '-');
627627

628628
LOCK(cs_args);
629629
m_accept_any_command = false; // latch to false
630-
std::map<std::string, Arg>& arg_map = m_available_args[cat];
630+
std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
631631
auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
632632
Assert(ret.second); // Fail on duplicate commands
633633
}

src/util/system.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class ArgsManager
374374
/**
375375
* Add subcommand
376376
*/
377-
void AddCommand(const std::string& cmd, const std::string& help, const OptionsCategory& cat);
377+
void AddCommand(const std::string& cmd, const std::string& help);
378378

379379
/**
380380
* Add many hidden arguments

test/util/data/bitcoin-util-test.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,34 @@
11
[
2+
{ "exec": "./bitcoin-util",
3+
"args": ["foo"],
4+
"return_code": 1,
5+
"error_txt": "Error parsing command line arguments: Invalid command 'foo'",
6+
"description": ""
7+
},
8+
{ "exec": "./bitcoin-util",
9+
"args": ["help"],
10+
"return_code": 1,
11+
"error_txt": "Error parsing command line arguments: Invalid command 'help'",
12+
"description": "`help` raises an error. Use `-help`"
13+
},
14+
{ "exec": "./bitcoin-util",
15+
"args": ["grind"],
16+
"return_code": 1,
17+
"error_txt": "Must specify block header to grind",
18+
"description": ""
19+
},
20+
{ "exec": "./bitcoin-util",
21+
"args": ["grind", "1", "2"],
22+
"return_code": 1,
23+
"error_txt": "Must specify block header to grind",
24+
"description": ""
25+
},
26+
{ "exec": "./bitcoin-util",
27+
"args": ["grind", "aa"],
28+
"return_code": 1,
29+
"error_txt": "Could not decode block header",
30+
"description": ""
31+
},
232
{ "exec": "./bitcoin-tx",
333
"args": ["-create", "nversion=1"],
434
"output_cmp": "blanktxv1.hex",

0 commit comments

Comments
 (0)