Bitcoin Core 31.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/caches.h>
10#include <kernel/context.h>
11#include <key.h>
12#include <node/caches.h>
13#include <node/context.h> // IWYU pragma: export
14#include <optional>
15#include <ostream>
17#include <pubkey.h>
18#include <stdexcept>
19#include <test/util/random.h>
20#include <util/chaintype.h> // IWYU pragma: export
21#include <util/check.h>
22#include <util/fs.h>
24#include <util/string.h>
25#include <util/vector.h>
26
27#include <functional>
28#include <type_traits>
29#include <vector>
30
31class arith_uint256;
32class CFeeRate;
33class Chainstate;
35class uint160;
36class uint256;
37
39extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
40
42extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
43
45extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
46
47static constexpr CAmount CENT{1000000};
48
50void SetupCommonTestArgs(ArgsManager& argsman);
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
66 node::NodeContext m_node; // keep as first member to be destructed last
67
71 {
73 m_rng.Reseed(GetRandHash());
74 }
75
76 explicit BasicTestingSetup(ChainType chainType = ChainType::MAIN, TestOpts = {});
78
79 fs::path m_path_root;
80 fs::path m_path_lock;
100};
101
110 std::function<void()> m_make_chainman{};
111
112 explicit ChainTestingSetup(ChainType chainType = ChainType::MAIN, TestOpts = {});
114
115 // Supplies a chainstate, if one is needed
117};
118
122 explicit TestingSetup(
123 ChainType chainType = ChainType::MAIN,
124 TestOpts = {});
125};
126
132
138
139class CBlock;
141class CScript;
142
148 ChainType chain_type = ChainType::REGTEST,
149 TestOpts = {});
150
156 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
157 const CScript& scriptPubKey,
158 Chainstate* chainstate = nullptr);
159
165 const std::vector<CMutableTransaction>& txns,
166 const CScript& scriptPubKey,
167 Chainstate& chainstate);
168
170 void mineBlocks(int num_blocks);
171
185 std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
186 const std::vector<COutPoint>& inputs,
187 int input_height,
188 const std::vector<CKey>& input_signing_keys,
189 const std::vector<CTxOut>& outputs,
190 const std::optional<CFeeRate>& feerate,
191 const std::optional<uint32_t>& fee_output);
202 CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
203 const std::vector<COutPoint>& inputs,
204 int input_height,
205 const std::vector<CKey>& input_signing_keys,
206 const std::vector<CTxOut>& outputs,
207 bool submit = true);
208
221 uint32_t input_vout,
222 int input_height,
223 CKey input_signing_key,
224 CScript output_destination,
225 CAmount output_amount = CAmount(1 * COIN),
226 bool submit = true);
227
239 std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
240
241 std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
242 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
243};
244
249template <class T = const BasicTestingSetup>
250std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
251{
252 opts.extra_args = Cat(
253 {
254 "-nodebuglogfile",
255 "-nodebug",
256 },
257 opts.extra_args);
258
259 return std::make_unique<T>(chain_type, opts);
260}
261
263
264#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
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:31
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition bench.cpp:42
ChainType
Definition chaintype.h:11
Definition block.h:74
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition feerate.h:32
An encapsulated private key.
Definition key.h:36
Serialized script, used inside transaction inputs and outputs.
Definition script.h:405
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition validation.h:551
Fast randomness source.
Definition random.h:386
256-bit unsigned big integer.
160-bit opaque blob.
Definition uint256.h:183
256-bit opaque blob.
Definition uint256.h:195
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition caches.cpp:57
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition random.h:463
static constexpr CAmount CENT
void SetupCommonTestArgs(ArgsManager &argsman)
Register common test args.
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.
ArgsManager m_args
Test-specific arguments and settings.
BasicTestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
void SeedRandomForTest(SeedRand seed)
Seed the global RNG state and m_rng for testing and log the seed value.
FastRandomContext m_rng
util::SignalInterrupt m_interrupt
node::NodeContext m_node
A mutable version of CTransaction.
ChainTestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
kernel::CacheSizes m_kernel_cache_sizes
std::function< void()> m_make_chainman
void LoadVerifyActivateChainstate()
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.
TestChain100Setup(ChainType chain_type=ChainType::REGTEST, TestOpts={})
std::vector< CTransactionRef > PopulateMempool(FastRandomContext &det_rand, size_t num_transactions, bool submit)
Create transactions spending from m_coinbase_txns.
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...
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
TestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
kernel::CacheSizes kernel
Definition caches.h:29
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