Bitcoin Core 28.99.0
P2P Digital Currency
setup_common.h
Go to the documentation of this file.
1// Copyright (c) 2015-present 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_SETUP_COMMON_H
6#define BITCOIN_TEST_UTIL_SETUP_COMMON_H
7
8#include <common/args.h> // IWYU pragma: export
9#include <kernel/context.h>
10#include <key.h>
11#include <node/caches.h>
12#include <node/context.h> // IWYU pragma: export
13#include <optional>
14#include <ostream>
16#include <pubkey.h>
17#include <stdexcept>
18#include <test/util/random.h>
19#include <util/chaintype.h> // IWYU pragma: export
20#include <util/check.h>
21#include <util/fs.h>
23#include <util/string.h>
24#include <util/vector.h>
25
26#include <functional>
27#include <type_traits>
28#include <vector>
29
30class arith_uint256;
31class CFeeRate;
32class Chainstate;
34class uint160;
35class uint256;
36
38extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
39
41extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
42
44extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
45
46static constexpr CAmount CENT{1000000};
47
49void SetupCommonTestArgs(ArgsManager& argsman);
50
51struct TestOpts {
52 std::vector<const char*> extra_args{};
55 bool setup_net{true};
57 bool min_validation_cache{false}; // Equivalent of -maxsigcachebytes=0
58};
59
65 node::NodeContext m_node; // keep as first member to be destructed last
66
70 {
73 }
74
75 explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {});
77
99};
100
109 std::function<void()> m_make_chainman{};
110
111 explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {});
113
114 // Supplies a chainstate, if one is needed
116};
117
121 explicit TestingSetup(
122 const ChainType chainType = ChainType::MAIN,
123 TestOpts = {});
124};
125
130};
131
132class CBlock;
134class CScript;
135
141 const ChainType chain_type = ChainType::REGTEST,
142 TestOpts = {});
143
149 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
150 const CScript& scriptPubKey,
151 Chainstate* chainstate = nullptr);
152
158 const std::vector<CMutableTransaction>& txns,
159 const CScript& scriptPubKey,
160 Chainstate& chainstate);
161
163 void mineBlocks(int num_blocks);
164
178 std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
179 const std::vector<COutPoint>& inputs,
180 int input_height,
181 const std::vector<CKey>& input_signing_keys,
182 const std::vector<CTxOut>& outputs,
183 const std::optional<CFeeRate>& feerate,
184 const std::optional<uint32_t>& fee_output);
195 CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
196 const std::vector<COutPoint>& inputs,
197 int input_height,
198 const std::vector<CKey>& input_signing_keys,
199 const std::vector<CTxOut>& outputs,
200 bool submit = true);
201
214 uint32_t input_vout,
215 int input_height,
216 CKey input_signing_key,
217 CScript output_destination,
218 CAmount output_amount = CAmount(1 * COIN),
219 bool submit = true);
220
232 std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
233
243 void MockMempoolMinFee(const CFeeRate& target_feerate);
244
245 std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
246 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
247};
248
253template <class T = const BasicTestingSetup>
254std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
255{
256 opts.extra_args = Cat(
257 {
258 "-nodebuglogfile",
259 "-nodebug",
260 },
261 opts.extra_args);
262
263 return std::make_unique<T>(chain_type, opts);
264}
265
267
268// Make types usable in BOOST_CHECK_* @{
269namespace std {
270template <typename T> requires std::is_enum_v<T>
271inline std::ostream& operator<<(std::ostream& os, const T& e)
272{
273 return os << static_cast<std::underlying_type_t<T>>(e);
274}
275
276template <typename T>
277inline std::ostream& operator<<(std::ostream& os, const std::optional<T>& v)
278{
279 return v ? os << *v
280 : os << "std::nullopt";
281}
282} // namespace std
283
284std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
285std::ostream& operator<<(std::ostream& os, const uint160& num);
286std::ostream& operator<<(std::ostream& os, const uint256& num);
287// @}
288
295{
296public:
297 explicit HasReason(std::string_view reason) : m_reason(reason) {}
298 bool operator()(std::string_view s) const { return s.find(m_reason) != std::string_view::npos; }
299 bool operator()(const std::exception& e) const { return (*this)(e.what()); }
300
301private:
302 const std::string m_reason;
303};
304
305#endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
ChainType
Definition: chaintype.h:11
Definition: block.h:69
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
Definition: feerate.h:33
An encapsulated private key.
Definition: key.h:35
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:505
Fast randomness source.
Definition: random.h:377
void Reseed(const uint256 &seed) noexcept
Reseed with explicit seed (only for testing).
Definition: random.cpp:709
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:295
bool operator()(std::string_view s) const
Definition: setup_common.h:298
const std::string m_reason
Definition: setup_common.h:302
bool operator()(const std::exception &e) const
Definition: setup_common.h:299
HasReason(std::string_view reason)
Definition: setup_common.h:297
256-bit unsigned big integer.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
160-bit opaque blob.
Definition: uint256.h:178
256-bit opaque blob.
Definition: uint256.h:190
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
std::ostream & operator<<(std::ostream &os, const T &e)
Definition: setup_common.h:271
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition: random.h:454
static constexpr CAmount CENT
Definition: setup_common.h:46
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:28
void SetupCommonTestArgs(ArgsManager &argsman)
Register common test args.
const std::function< std::vector< const char * >()> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:36
CBlock getBlock13b8a()
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.
Definition: setup_common.h:254
std::ostream & operator<<(std::ostream &os, const arith_uint256 &num)
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: bench.cpp:47
Basic testing setup.
Definition: setup_common.h:63
fs::path m_path_lock
Definition: setup_common.h:79
ArgsManager m_args
Test-specific arguments and settings.
Definition: setup_common.h:98
BasicTestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
void SeedRandomForTest(SeedRand seed)
Seed the global RNG state and m_rng for testing and log the seed value.
Definition: setup_common.h:69
FastRandomContext m_rng
Definition: setup_common.h:67
util::SignalInterrupt m_interrupt
Definition: setup_common.h:64
fs::path m_path_root
Definition: setup_common.h:78
node::NodeContext m_node
Definition: setup_common.h:65
A mutable version of CTransaction.
Definition: transaction.h:378
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:105
bool m_block_tree_db_in_memory
Definition: setup_common.h:108
ChainTestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
node::CacheSizes m_cache_sizes
Definition: setup_common.h:106
std::function< void()> m_make_chainman
Definition: setup_common.h:109
void LoadVerifyActivateChainstate()
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:127
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:139
void mineBlocks(int num_blocks)
Mine a series of new blocks on the active chain.
std::vector< CTransactionRef > m_coinbase_txns
Definition: setup_common.h:245
CMutableTransaction CreateValidMempoolTransaction(const std::vector< CTransactionRef > &input_transactions, const std::vector< COutPoint > &inputs, int input_height, const std::vector< CKey > &input_signing_keys, const std::vector< CTxOut > &outputs, bool submit=true)
Create a transaction and, optionally, submit to the mempool.
std::pair< CMutableTransaction, CAmount > CreateValidTransaction(const std::vector< CTransactionRef > &input_transactions, const std::vector< COutPoint > &inputs, int input_height, const std::vector< CKey > &input_signing_keys, const std::vector< CTxOut > &outputs, const std::optional< CFeeRate > &feerate, const std::optional< uint32_t > &fee_output)
Create a transaction, optionally setting the fee based on the feerate.
std::vector< CTransactionRef > PopulateMempool(FastRandomContext &det_rand, size_t num_transactions, bool submit)
Create transactions spending from m_coinbase_txns.
void MockMempoolMinFee(const CFeeRate &target_feerate)
Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),...
CBlock CreateAndProcessBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, Chainstate *chainstate=nullptr)
Create a new block with just given transactions, coinbase paying to scriptPubKey, and try to add it t...
TestChain100Setup(const ChainType chain_type=ChainType::REGTEST, TestOpts={})
CBlock CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, Chainstate &chainstate)
Create a new block with just given transactions, coinbase paying to scriptPubKey.
bool coins_db_in_memory
Definition: setup_common.h:53
bool min_validation_cache
Definition: setup_common.h:57
bool block_tree_db_in_memory
Definition: setup_common.h:54
std::vector< const char * > extra_args
Definition: setup_common.h:52
bool setup_validation_interface
Definition: setup_common.h:56
bool setup_net
Definition: setup_common.h:55
Testing setup that configures a complete environment.
Definition: setup_common.h:120
TestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
NodeContext struct containing references to chain state and connection state.
Definition: context.h:56
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
Definition: random.cpp:19
SeedRand
Definition: random.h:15
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34