Skip to content

Commit 0f49e59

Browse files
MarcoFalkeknst
authored andcommitted
Merge bitcoin#17593: test: move more utility functions into test utility library
78e283e [test] move wallet helper functions into test library (Martin Zumsande) f613e5d [test] move mining helper functions into test library (Martin Zumsande) 2cb4e8b [test] move string helper functions into test library (Martin Zumsande) Pull request description: This disbands `test/util.h` and `test/util.cpp` and moves the content into the test utility library recently created in bitcoin#17542, so that all test utility functions are in one place. The content of the original files are split into three modules: 1) string helper functions go to `test/util/str` 2) mining helper functions go to the newly created `test/util/mining` 3) wallet helper functions go to the newly created `test/util/wallet` ACKs for top commit: MarcoFalke: ACK 78e283e 🔧 Tree-SHA512: f182a61e86e76c32bcb84e37f44904d3a4a9c5a321f7a8efdda5368a6623cb8b5a5384ec4f96e67f0357b0c22099f6e3ecd0ac4cb467e3fa3f3128f8d36edfb8
1 parent a3c9cf8 commit 0f49e59

15 files changed

+141
-117
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ bench_bench_dash_SOURCES = \
4747
bench/lockedpool.cpp \
4848
bench/poly1305.cpp \
4949
bench/prevector.cpp \
50-
bench/string_cast.cpp \
51-
test/util.cpp \
52-
test/util.h
50+
bench/string_cast.cpp
5351

5452
nodist_bench_bench_dash_SOURCES = $(GENERATED_BENCH_FILES)
5553

src/Makefile.test_fuzz.include

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ TEST_FUZZ_H = \
1111
test/fuzz/fuzz.h \
1212
test/fuzz/FuzzedDataProvider.h \
1313
test/fuzz/util.h \
14-
test/util.h
14+
test/util/mining.h
1515

1616
libtest_fuzz_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
1717
libtest_fuzz_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1818
libtest_fuzz_a_SOURCES = \
1919
test/fuzz/fuzz.cpp \
20-
test/util.cpp \
20+
test/util/mining.cpp \
2121
$(TEST_FUZZ_H)
2222

2323
LIBTEST_FUZZ += $(LIBBITCOIN_SERVER)

src/Makefile.test_util.include

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,27 @@ EXTRA_LIBRARIES += \
1010
TEST_UTIL_H = \
1111
test/util/blockfilter.h \
1212
test/util/logging.h \
13+
test/util/mining.h \
1314
test/util/net.h \
1415
test/util/setup_common.h \
1516
test/util/str.h \
16-
test/util/transaction_utils.h
17+
test/util/transaction_utils.h \
18+
test/util/wallet.h
1719

1820
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
1921
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
2022
libtest_util_a_SOURCES = \
2123
test/util/blockfilter.cpp \
2224
test/util/logging.cpp \
25+
test/util/mining.cpp \
2326
test/util/net.cpp \
2427
test/util/setup_common.cpp \
2528
test/util/str.cpp \
2629
test/util/transaction_utils.cpp \
30+
test/util/wallet.cpp \
2731
$(TEST_UTIL_H)
2832

2933
LIBTEST_UTIL += $(LIBBITCOIN_SERVER)
3034
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
3135
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
3236
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)
33-

src/bench/block_assemble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include <consensus/consensus.h>
77
#include <consensus/validation.h>
88
#include <script/standard.h>
9-
#include <test/util.h>
9+
#include <test/util/mining.h>
10+
#include <test/util/wallet.h>
1011
#include <test/util/setup_common.h>
1112
#include <txmempool.h>
1213
#include <validation.h>

src/bench/wallet_balance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include <bench/bench.h>
66
#include <interfaces/chain.h>
77
#include <node/context.h>
8-
#include <test/util.h>
8+
#include <test/util/mining.h>
99
#include <test/util/setup_common.h>
10+
#include <test/util/wallet.h>
1011
#include <validationinterface.h>
1112
#include <wallet/wallet.h>
1213

src/test/fuzz/process_message.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include <streams.h>
1414
#include <test/fuzz/FuzzedDataProvider.h>
1515
#include <test/fuzz/fuzz.h>
16-
#include <test/util.h>
16+
#include <test/util/mining.h>
1717
#include <test/util/setup_common.h>
1818
#include <validationinterface.h>
1919
#include <version.h>

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include <test/fuzz/FuzzedDataProvider.h>
1010
#include <test/fuzz/fuzz.h>
1111
#include <test/fuzz/util.h>
12-
#include <test/util.h>
1312
#include <test/util/net.h>
13+
#include <test/util/mining.h>
1414
#include <test/util/setup_common.h>
1515
#include <validation.h>
1616
#include <validationinterface.h>

src/test/settings_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
#include <util/settings.h>
66

7-
#include <test/util.h>
87
#include <test/util/setup_common.h>
8+
#include <test/util/str.h>
9+
910

1011
#include <boost/test/unit_test.hpp>
1112
#include <univalue.h>

src/test/util.h

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/test/util.cpp renamed to src/test/util/mining.cpp

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <test/util.h>
5+
#include <test/util/mining.h>
66

77
#include <chainparams.h>
88
#include <consensus/merkle.h>
@@ -20,37 +20,6 @@
2020
#include <spork.h>
2121
#include <validation.h>
2222
#include <util/check.h>
23-
#ifdef ENABLE_WALLET
24-
#include <wallet/wallet.h>
25-
#endif
26-
27-
const std::string ADDRESS_B58T_UNSPENDABLE = "yXXXXXXXXXXXXXXXXXXXXXXXXXXXVd2rXU";
28-
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
29-
30-
#ifdef ENABLE_WALLET
31-
std::string getnewaddress(CWallet& w)
32-
{
33-
CTxDestination dest;
34-
std::string error;
35-
if (!w.GetNewDestination("", dest, error)) assert(false);
36-
37-
return EncodeDestination(dest);
38-
}
39-
40-
void importaddress(CWallet& wallet, const std::string& address)
41-
{
42-
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
43-
assert(spk_man != nullptr);
44-
LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
45-
const auto dest = DecodeDestination(address);
46-
assert(IsValidDestination(dest));
47-
const auto script = GetScriptForDestination(dest);
48-
wallet.MarkDirty();
49-
assert(!spk_man->HaveWatchOnly(script));
50-
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
51-
wallet.SetAddressBook(dest, /* label */ "", "receive");
52-
}
53-
#endif // ENABLE_WALLET
5423

5524
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
5625
{

src/test/util/mining.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2019 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+
#ifndef BITCOIN_TEST_UTIL_MINING_H
6+
#define BITCOIN_TEST_UTIL_MINING_H
7+
8+
#include <memory>
9+
#include <string>
10+
11+
class CBlock;
12+
class CScript;
13+
class CTxIn;
14+
struct NodeContext;
15+
16+
/** Returns the generated coin */
17+
CTxIn MineBlock(const NodeContext&, const CScript& coinbase_scriptPubKey);
18+
19+
/** Prepare a block to be mined */
20+
std::shared_ptr<CBlock> PrepareBlock(const NodeContext&, const CScript& coinbase_scriptPubKey);
21+
22+
/** RPC-like helper function, returns the generated coin */
23+
CTxIn generatetoaddress(const NodeContext&, const std::string& address);
24+
25+
#endif // BITCOIN_TEST_UTIL_MINING_H

src/test/util/str.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,37 @@
99

1010
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2);
1111

12+
/**
13+
* Increment a string. Useful to enumerate all fixed length strings with
14+
* characters in [min_char, max_char].
15+
*/
16+
template <typename CharType, size_t StringLength>
17+
bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
18+
{
19+
for (CharType& elem : string) {
20+
bool has_next = elem != max_char;
21+
elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
22+
if (has_next) return true;
23+
}
24+
return false;
25+
}
26+
27+
/**
28+
* Iterate over string values and call function for each string without
29+
* successive duplicate characters.
30+
*/
31+
template <typename CharType, size_t StringLength, typename Fn>
32+
void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
33+
for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
34+
int prev = -1;
35+
bool skip_string = false;
36+
for (CharType c : string) {
37+
if (c == prev) skip_string = true;
38+
if (skip_string || c < min_char || c > max_char) break;
39+
prev = c;
40+
}
41+
if (!skip_string) fn();
42+
}
43+
}
44+
1245
#endif // BITCOIN_TEST_UTIL_STR_H

src/test/util/wallet.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2019 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 <test/util/wallet.h>
6+
7+
#include <key_io.h>
8+
#include <outputtype.h>
9+
#include <script/standard.h>
10+
#ifdef ENABLE_WALLET
11+
#include <wallet/wallet.h>
12+
#endif
13+
14+
const std::string ADDRESS_B58T_UNSPENDABLE = "yXXXXXXXXXXXXXXXXXXXXXXXXXXXVd2rXU";
15+
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
16+
17+
#ifdef ENABLE_WALLET
18+
std::string getnewaddress(CWallet& w)
19+
{
20+
CTxDestination dest;
21+
std::string error;
22+
if (!w.GetNewDestination("", dest, error)) assert(false);
23+
24+
return EncodeDestination(dest);
25+
}
26+
27+
void importaddress(CWallet& wallet, const std::string& address)
28+
{
29+
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
30+
assert(spk_man != nullptr);
31+
LOCK2(wallet.cs_wallet, spk_man->cs_KeyStore);
32+
const auto dest = DecodeDestination(address);
33+
assert(IsValidDestination(dest));
34+
const auto script = GetScriptForDestination(dest);
35+
wallet.MarkDirty();
36+
assert(!spk_man->HaveWatchOnly(script));
37+
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
38+
wallet.SetAddressBook(dest, /* label */ "", "receive");
39+
}
40+
#endif // ENABLE_WALLET

src/test/util/wallet.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2019 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+
#ifndef BITCOIN_TEST_UTIL_WALLET_H
6+
#define BITCOIN_TEST_UTIL_WALLET_H
7+
8+
#include <string>
9+
10+
class CWallet;
11+
12+
// Constants //
13+
14+
extern const std::string ADDRESS_B58T_UNSPENDABLE;
15+
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
16+
17+
// RPC-like //
18+
19+
/** Import the address to the wallet */
20+
void importaddress(CWallet& wallet, const std::string& address);
21+
/** Returns a new address from the wallet */
22+
std::string getnewaddress(CWallet& w);
23+
24+
25+
#endif // BITCOIN_TEST_UTIL_WALLET_H

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <hash.h> // For Hash()
99
#include <key.h> // For CKey
1010
#include <sync.h>
11-
#include <test/util.h>
1211
#include <test/util/logging.h>
1312
#include <test/util/setup_common.h>
13+
#include <test/util/str.h>
1414
#include <uint256.h>
1515
#include <util/getuniquepath.h>
1616
#include <util/message.h> // For MessageSign(), MessageVerify(), MESSAGE_MAGIC

0 commit comments

Comments
 (0)