Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
14#include <pubkey.h>
15#include <stdexcept>
16#include <util/chaintype.h> // IWYU pragma: export
17#include <util/check.h>
18#include <util/fs.h>
20#include <util/string.h>
21#include <util/vector.h>
22
23#include <functional>
24#include <type_traits>
25#include <vector>
26
27class arith_uint256;
28class CFeeRate;
29class Chainstate;
31
33extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
34
36extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
37
39extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
40
41// Enable BOOST_CHECK_EQUAL for enum class types
42namespace std {
43template <typename T>
44std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
45{
46 return stream << static_cast<typename std::underlying_type<T>::type>(e);
47}
48} // namespace std
49
50static constexpr CAmount CENT{1000000};
51
52struct TestOpts {
53 std::vector<const char*> extra_args{};
56 bool setup_net{true};
58 bool min_validation_cache{false}; // Equivalent of -maxsigcachebytes=0
59};
60
76
85 std::function<void()> m_make_chainman{};
86
87 explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {});
89
90 // Supplies a chainstate, if one is needed
92};
93
97 explicit TestingSetup(
98 const ChainType chainType = ChainType::MAIN,
99 TestOpts = {});
100};
101
107
108class CBlock;
110class CScript;
111
117 const ChainType chain_type = ChainType::REGTEST,
118 TestOpts = {});
119
125 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
126 const CScript& scriptPubKey,
127 Chainstate* chainstate = nullptr);
128
134 const std::vector<CMutableTransaction>& txns,
135 const CScript& scriptPubKey,
136 Chainstate& chainstate);
137
139 void mineBlocks(int num_blocks);
140
154 std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
155 const std::vector<COutPoint>& inputs,
156 int input_height,
157 const std::vector<CKey>& input_signing_keys,
158 const std::vector<CTxOut>& outputs,
159 const std::optional<CFeeRate>& feerate,
160 const std::optional<uint32_t>& fee_output);
171 CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
172 const std::vector<COutPoint>& inputs,
173 int input_height,
174 const std::vector<CKey>& input_signing_keys,
175 const std::vector<CTxOut>& outputs,
176 bool submit = true);
177
190 uint32_t input_vout,
191 int input_height,
192 CKey input_signing_key,
193 CScript output_destination,
194 CAmount output_amount = CAmount(1 * COIN),
195 bool submit = true);
196
208 std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
209
219 void MockMempoolMinFee(const CFeeRate& target_feerate);
220
221 std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
222 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
223};
224
229template <class T = const BasicTestingSetup>
230std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
231{
232 opts.extra_args = Cat(
233 {
234 "-nodebuglogfile",
235 "-nodebug",
236 },
237 opts.extra_args);
238
239 return std::make_unique<T>(chain_type, opts);
240}
241
243
244// Make types usable in BOOST_CHECK_*
245std::ostream& operator<<(std::ostream& os, const arith_uint256& num);
246std::ostream& operator<<(std::ostream& os, const uint160& num);
247std::ostream& operator<<(std::ostream& os, const uint256& num);
248
255{
256public:
257 explicit HasReason(const std::string& reason) : m_reason(reason) {}
258 bool operator()(const std::exception& e) const
259 {
260 return std::string(e.what()).find(m_reason) != std::string::npos;
261 };
262
263private:
264 const std::string m_reason;
265};
266
267#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:414
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:513
Fast randomness source.
Definition random.h:377
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
HasReason(const std::string &reason)
const std::string m_reason
bool operator()(const std::exception &e) const
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:166
256-bit opaque blob.
Definition uint256.h:178
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
#define T(expected, seed, data)
std::ostream & operator<<(typename std::enable_if< std::is_enum< T >::value, std::ostream >::type &stream, const T &e)
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr CAmount CENT
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition bench.cpp:23
const std::function< std::vector< const char * >()> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition bench.cpp:25
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.
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:27
Basic testing setup.
ArgsManager m_args
BasicTestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
util::SignalInterrupt m_interrupt
node::NodeContext m_node
A mutable version of CTransaction.
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
bool m_block_tree_db_in_memory
ChainTestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
node::CacheSizes m_cache_sizes
std::function< void()> m_make_chainman
void LoadVerifyActivateChainstate()
Identical to TestingSetup, but chain set to regtest.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
void mineBlocks(int num_blocks)
Mine a series of new blocks on the active chain.
std::vector< CTransactionRef > m_coinbase_txns
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
bool min_validation_cache
bool block_tree_db_in_memory
std::vector< const char * > extra_args
bool setup_validation_interface
bool setup_net
Testing setup that configures a complete environment.
TestingSetup(const ChainType chainType=ChainType::MAIN, TestOpts={})
NodeContext struct containing references to chain state and connection state.
Definition context.h:55
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition vector.h:34