Bitcoin Core 31.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 <consensus/amount.h>
10#include <kernel/caches.h>
11#include <key.h>
12#include <node/caches.h>
13#include <node/context.h> // IWYU pragma: export
15#include <random.h>
16#include <test/util/random.h>
17#include <util/chaintype.h> // IWYU pragma: export
18#include <util/fs.h>
20#include <util/vector.h>
21
22#include <cstddef>
23#include <cstdint>
24#include <functional>
25#include <memory>
26#include <optional>
27#include <string>
28#include <utility>
29#include <vector>
30
31class CFeeRate;
32
34extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
35
37extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
38
39static constexpr CAmount CENT{1000000};
40
42void SetupCommonTestArgs(ArgsManager& argsman);
43
44struct TestOpts {
45 std::vector<const char*> extra_args{};
48 bool setup_net{true};
50 bool min_validation_cache{false}; // Equivalent of -maxsigcachebytes=0
51};
52
58 node::NodeContext m_node; // keep as first member to be destructed last
59
63 {
66 }
67
68 explicit BasicTestingSetup(ChainType chainType = ChainType::MAIN, TestOpts = {});
70
71 fs::path m_path_root;
72 fs::path m_path_lock;
92};
93
102 std::function<void()> m_make_chainman{};
103
104 explicit ChainTestingSetup(ChainType chainType = ChainType::MAIN, TestOpts = {});
106
107 // Supplies a chainstate, if one is needed
109};
110
114 explicit TestingSetup(
115 ChainType chainType = ChainType::MAIN,
116 TestOpts = {});
117};
118
123};
124
129};
130
131class CBlock;
132class CScript;
133
139 ChainType chain_type = ChainType::REGTEST,
140 TestOpts = {});
141
146 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
147 const CScript& scriptPubKey);
148
154 const std::vector<CMutableTransaction>& txns,
155 const CScript& scriptPubKey);
156
158 void mineBlocks(int num_blocks);
159
173 std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
174 const std::vector<COutPoint>& inputs,
175 int input_height,
176 const std::vector<CKey>& input_signing_keys,
177 const std::vector<CTxOut>& outputs,
178 const std::optional<CFeeRate>& feerate,
179 const std::optional<uint32_t>& fee_output);
190 CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
191 const std::vector<COutPoint>& inputs,
192 int input_height,
193 const std::vector<CKey>& input_signing_keys,
194 const std::vector<CTxOut>& outputs,
195 bool submit = true);
196
209 uint32_t input_vout,
210 int input_height,
211 CKey input_signing_key,
212 CScript output_destination,
213 CAmount output_amount = CAmount(1 * COIN),
214 bool submit = true);
215
227 std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
228
229 std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
230 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
231};
232
237template <class T = const BasicTestingSetup>
238std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
239{
240 opts.extra_args = Cat(
241 {
242 "-nodebuglogfile",
243 "-nodebug",
244 },
245 opts.extra_args);
246
247 return std::make_unique<T>(chain_type, opts);
248}
249
251
252#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:12
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:37
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Fast randomness source.
Definition: random.h:386
void Reseed(const uint256 &seed) noexcept
Reseed with explicit seed (only for testing).
Definition: random.cpp:634
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
Definition: transaction.h:403
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition: random.h:463
static constexpr CAmount CENT
Definition: setup_common.h:39
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:29
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:238
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: bench.cpp:40
Basic testing setup.
Definition: setup_common.h:56
fs::path m_path_lock
Definition: setup_common.h:72
ArgsManager m_args
Test-specific arguments and settings.
Definition: setup_common.h:91
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.
Definition: setup_common.h:62
FastRandomContext m_rng
Definition: setup_common.h:60
util::SignalInterrupt m_interrupt
Definition: setup_common.h:57
fs::path m_path_root
Definition: setup_common.h:71
node::NodeContext m_node
Definition: setup_common.h:58
A mutable version of CTransaction.
Definition: transaction.h:358
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:98
ChainTestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
kernel::CacheSizes m_kernel_cache_sizes
Definition: setup_common.h:99
bool m_block_tree_db_in_memory
Definition: setup_common.h:101
std::function< void()> m_make_chainman
Definition: setup_common.h:102
void LoadVerifyActivateChainstate()
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:120
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:137
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:229
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.
CBlock CreateAndProcessBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
Create a new block with just given transactions, coinbase paying to scriptPubKey, and try to add it t...
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 CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
Create a new block with just given transactions, coinbase paying to scriptPubKey.
bool coins_db_in_memory
Definition: setup_common.h:46
bool min_validation_cache
Definition: setup_common.h:50
bool block_tree_db_in_memory
Definition: setup_common.h:47
std::vector< const char * > extra_args
Definition: setup_common.h:45
bool setup_validation_interface
Definition: setup_common.h:49
bool setup_net
Definition: setup_common.h:48
Testing setup that configures a complete environment.
Definition: setup_common.h:113
TestingSetup(ChainType chainType=ChainType::MAIN, TestOpts={})
Identical to TestingSetup, but chain set to testnet4.
Definition: setup_common.h:126
kernel::CacheSizes kernel
Definition: caches.h:29
NodeContext struct containing references to chain state and connection state.
Definition: context.h:59
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