Skip to content

Commit 94c6f2b

Browse files
author
MarcoFalke
committed
Merge #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 #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
2 parents f9fd3a2 + 78e283e commit 94c6f2b

File tree

12 files changed

+135
-113
lines changed

12 files changed

+135
-113
lines changed

src/Makefile.bench.include

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ bench_bench_bitcoin_SOURCES = \
3939
bench/bech32.cpp \
4040
bench/lockedpool.cpp \
4141
bench/poly1305.cpp \
42-
bench/prevector.cpp \
43-
test/util.h \
44-
test/util.cpp
42+
bench/prevector.cpp
4543

4644
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)
4745

src/Makefile.test_util.include

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

1719
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
1820
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
1921
libtest_util_a_SOURCES = \
2022
test/util/blockfilter.cpp \
2123
test/util/logging.cpp \
24+
test/util/mining.cpp \
2225
test/util/setup_common.cpp \
2326
test/util/str.cpp \
2427
test/util/transaction_utils.cpp \
28+
test/util/wallet.cpp \
2529
$(TEST_UTIL_H)
2630

2731
LIBTEST_UTIL += $(LIBBITCOIN_SERVER)
2832
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
2933
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
3034
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)
31-

src/bench/block_assemble.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include <bench/bench.h>
66
#include <consensus/validation.h>
77
#include <crypto/sha256.h>
8-
#include <test/util.h>
8+
#include <test/util/mining.h>
9+
#include <test/util/wallet.h>
910
#include <txmempool.h>
1011
#include <validation.h>
1112

src/bench/wallet_balance.cpp

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

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 & 70 deletions
This file was deleted.

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

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,15 @@
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>
99
#include <key_io.h>
1010
#include <miner.h>
11-
#include <outputtype.h>
1211
#include <pow.h>
1312
#include <script/standard.h>
1413
#include <validation.h>
15-
#include <validationinterface.h>
16-
#ifdef ENABLE_WALLET
17-
#include <wallet/wallet.h>
18-
#endif
19-
20-
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
21-
22-
#ifdef ENABLE_WALLET
23-
std::string getnewaddress(CWallet& w)
24-
{
25-
constexpr auto output_type = OutputType::BECH32;
26-
CTxDestination dest;
27-
std::string error;
28-
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
29-
30-
return EncodeDestination(dest);
31-
}
32-
33-
void importaddress(CWallet& wallet, const std::string& address)
34-
{
35-
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
36-
LOCK(wallet.cs_wallet);
37-
AssertLockHeld(spk_man->cs_wallet);
38-
const auto dest = DecodeDestination(address);
39-
assert(IsValidDestination(dest));
40-
const auto script = GetScriptForDestination(dest);
41-
wallet.MarkDirty();
42-
assert(!spk_man->HaveWatchOnly(script));
43-
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
44-
wallet.SetAddressBook(dest, /* label */ "", "receive");
45-
}
46-
#endif // ENABLE_WALLET
4714

4815
CTxIn generatetoaddress(const std::string& address)
4916
{

src/test/util/mining.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
15+
/** Returns the generated coin */
16+
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
17+
18+
/** Prepare a block to be mined */
19+
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
20+
21+
/** RPC-like helper function, returns the generated coin */
22+
CTxIn generatetoaddress(const std::string& address);
23+
24+
#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_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
15+
16+
#ifdef ENABLE_WALLET
17+
std::string getnewaddress(CWallet& w)
18+
{
19+
constexpr auto output_type = OutputType::BECH32;
20+
CTxDestination dest;
21+
std::string error;
22+
if (!w.GetNewDestination(output_type, "", 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+
LOCK(wallet.cs_wallet);
31+
AssertLockHeld(spk_man->cs_wallet);
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

0 commit comments

Comments
 (0)