0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

Merge #17593: test: move more utility functions into test utility library

78e283e656 [test] move wallet helper functions into test library (Martin Zumsande)
f613e5dfda [test] move mining helper functions into test library (Martin Zumsande)
2cb4e8bdc7 [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 78e283e656 🔧

Tree-SHA512: f182a61e86e76c32bcb84e37f44904d3a4a9c5a321f7a8efdda5368a6623cb8b5a5384ec4f96e67f0357b0c22099f6e3ecd0ac4cb467e3fa3f3128f8d36edfb8
This commit is contained in:
MarcoFalke 2019-12-16 16:08:52 -05:00
commit 94c6f2bba4
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
12 changed files with 135 additions and 113 deletions

View file

@ -39,9 +39,7 @@ bench_bench_bitcoin_SOURCES = \
bench/bech32.cpp \
bench/lockedpool.cpp \
bench/poly1305.cpp \
bench/prevector.cpp \
test/util.h \
test/util.cpp
bench/prevector.cpp
nodist_bench_bench_bitcoin_SOURCES = $(GENERATED_BENCH_FILES)

View file

@ -10,22 +10,25 @@ EXTRA_LIBRARIES += \
TEST_UTIL_H = \
test/util/blockfilter.h \
test/util/logging.h \
test/util/mining.h \
test/util/setup_common.h \
test/util/str.h \
test/util/transaction_utils.h
test/util/transaction_utils.h \
test/util/wallet.h
libtest_util_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) $(MINIUPNPC_CPPFLAGS) $(EVENT_CFLAGS) $(EVENT_PTHREADS_CFLAGS)
libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libtest_util_a_SOURCES = \
test/util/blockfilter.cpp \
test/util/logging.cpp \
test/util/mining.cpp \
test/util/setup_common.cpp \
test/util/str.cpp \
test/util/transaction_utils.cpp \
test/util/wallet.cpp \
$(TEST_UTIL_H)
LIBTEST_UTIL += $(LIBBITCOIN_SERVER)
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)

View file

@ -5,7 +5,8 @@
#include <bench/bench.h>
#include <consensus/validation.h>
#include <crypto/sha256.h>
#include <test/util.h>
#include <test/util/mining.h>
#include <test/util/wallet.h>
#include <txmempool.h>
#include <validation.h>

View file

@ -6,7 +6,8 @@
#include <interfaces/chain.h>
#include <node/context.h>
#include <optional.h>
#include <test/util.h>
#include <test/util/mining.h>
#include <test/util/wallet.h>
#include <validationinterface.h>
#include <wallet/wallet.h>

View file

@ -4,8 +4,9 @@
#include <util/settings.h>
#include <test/util.h>
#include <test/util/setup_common.h>
#include <test/util/str.h>
#include <boost/test/unit_test.hpp>
#include <univalue.h>

View file

@ -1,70 +0,0 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_UTIL_H
#define BITCOIN_TEST_UTIL_H
#include <memory>
#include <string>
class CBlock;
class CScript;
class CTxIn;
class CWallet;
// Constants //
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
// Lower-level utils //
/** Returns the generated coin */
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
/** Prepare a block to be mined */
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
// RPC-like //
/** Import the address to the wallet */
void importaddress(CWallet& wallet, const std::string& address);
/** Returns a new address from the wallet */
std::string getnewaddress(CWallet& w);
/** Returns the generated coin */
CTxIn generatetoaddress(const std::string& address);
/**
* Increment a string. Useful to enumerate all fixed length strings with
* characters in [min_char, max_char].
*/
template <typename CharType, size_t StringLength>
bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
{
for (CharType& elem : string) {
bool has_next = elem != max_char;
elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
if (has_next) return true;
}
return false;
}
/**
* Iterate over string values and call function for each string without
* successive duplicate characters.
*/
template <typename CharType, size_t StringLength, typename Fn>
void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
int prev = -1;
bool skip_string = false;
for (CharType c : string) {
if (c == prev) skip_string = true;
if (skip_string || c < min_char || c > max_char) break;
prev = c;
}
if (!skip_string) fn();
}
}
#endif // BITCOIN_TEST_UTIL_H

View file

@ -2,48 +2,15 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util.h>
#include <test/util/mining.h>
#include <chainparams.h>
#include <consensus/merkle.h>
#include <key_io.h>
#include <miner.h>
#include <outputtype.h>
#include <pow.h>
#include <script/standard.h>
#include <validation.h>
#include <validationinterface.h>
#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#endif
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
#ifdef ENABLE_WALLET
std::string getnewaddress(CWallet& w)
{
constexpr auto output_type = OutputType::BECH32;
CTxDestination dest;
std::string error;
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
return EncodeDestination(dest);
}
void importaddress(CWallet& wallet, const std::string& address)
{
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
LOCK(wallet.cs_wallet);
AssertLockHeld(spk_man->cs_wallet);
const auto dest = DecodeDestination(address);
assert(IsValidDestination(dest));
const auto script = GetScriptForDestination(dest);
wallet.MarkDirty();
assert(!spk_man->HaveWatchOnly(script));
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
wallet.SetAddressBook(dest, /* label */ "", "receive");
}
#endif // ENABLE_WALLET
CTxIn generatetoaddress(const std::string& address)
{

24
src/test/util/mining.h Normal file
View file

@ -0,0 +1,24 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_UTIL_MINING_H
#define BITCOIN_TEST_UTIL_MINING_H
#include <memory>
#include <string>
class CBlock;
class CScript;
class CTxIn;
/** Returns the generated coin */
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
/** Prepare a block to be mined */
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
/** RPC-like helper function, returns the generated coin */
CTxIn generatetoaddress(const std::string& address);
#endif // BITCOIN_TEST_UTIL_MINING_H

View file

@ -9,4 +9,37 @@
bool CaseInsensitiveEqual(const std::string& s1, const std::string& s2);
/**
* Increment a string. Useful to enumerate all fixed length strings with
* characters in [min_char, max_char].
*/
template <typename CharType, size_t StringLength>
bool NextString(CharType (&string)[StringLength], CharType min_char, CharType max_char)
{
for (CharType& elem : string) {
bool has_next = elem != max_char;
elem = elem < min_char || elem >= max_char ? min_char : CharType(elem + 1);
if (has_next) return true;
}
return false;
}
/**
* Iterate over string values and call function for each string without
* successive duplicate characters.
*/
template <typename CharType, size_t StringLength, typename Fn>
void ForEachNoDup(CharType (&string)[StringLength], CharType min_char, CharType max_char, Fn&& fn) {
for (bool has_next = true; has_next; has_next = NextString(string, min_char, max_char)) {
int prev = -1;
bool skip_string = false;
for (CharType c : string) {
if (c == prev) skip_string = true;
if (skip_string || c < min_char || c > max_char) break;
prev = c;
}
if (!skip_string) fn();
}
}
#endif // BITCOIN_TEST_UTIL_STR_H

40
src/test/util/wallet.cpp Normal file
View file

@ -0,0 +1,40 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/wallet.h>
#include <key_io.h>
#include <outputtype.h>
#include <script/standard.h>
#ifdef ENABLE_WALLET
#include <wallet/wallet.h>
#endif
const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
#ifdef ENABLE_WALLET
std::string getnewaddress(CWallet& w)
{
constexpr auto output_type = OutputType::BECH32;
CTxDestination dest;
std::string error;
if (!w.GetNewDestination(output_type, "", dest, error)) assert(false);
return EncodeDestination(dest);
}
void importaddress(CWallet& wallet, const std::string& address)
{
auto spk_man = wallet.GetLegacyScriptPubKeyMan();
LOCK(wallet.cs_wallet);
AssertLockHeld(spk_man->cs_wallet);
const auto dest = DecodeDestination(address);
assert(IsValidDestination(dest));
const auto script = GetScriptForDestination(dest);
wallet.MarkDirty();
assert(!spk_man->HaveWatchOnly(script));
if (!spk_man->AddWatchOnly(script, 0 /* nCreateTime */)) assert(false);
wallet.SetAddressBook(dest, /* label */ "", "receive");
}
#endif // ENABLE_WALLET

24
src/test/util/wallet.h Normal file
View file

@ -0,0 +1,24 @@
// Copyright (c) 2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TEST_UTIL_WALLET_H
#define BITCOIN_TEST_UTIL_WALLET_H
#include <string>
class CWallet;
// Constants //
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
// RPC-like //
/** Import the address to the wallet */
void importaddress(CWallet& wallet, const std::string& address);
/** Returns a new address from the wallet */
std::string getnewaddress(CWallet& w);
#endif // BITCOIN_TEST_UTIL_WALLET_H

View file

@ -7,7 +7,7 @@
#include <clientversion.h>
#include <sync.h>
#include <test/util/setup_common.h>
#include <test/util.h>
#include <test/util/str.h>
#include <util/moneystr.h>
#include <util/strencodings.h>
#include <util/string.h>