Bitcoin Core 32.99.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 <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/net.h>
17#include <test/util/random.h>
18#include <test/util/time.h>
19#include <util/chaintype.h> // IWYU pragma: export
20#include <util/fs.h>
22#include <util/vector.h>
23
24#include <cstddef>
25#include <cstdint>
26#include <functional>
27#include <memory>
28#include <optional>
29#include <string>
30#include <utility>
31#include <vector>
32
33class CFeeRate;
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
41inline constexpr CAmount CENT{1'000'000};
42
45
46struct TestOpts {
47 std::vector<const char*> extra_args{};
50 bool setup_net{true};
52 bool min_validation_cache{false}; // Equivalent of -maxsigcachebytes=0
53};
54
95
116
120 explicit TestingSetup(
122 TestOpts = {});
123};
124
130
136
137class CBlock;
138class CScript;
139
146 TestOpts = {});
147
151 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
152 const CScript& scriptPubKey,
153 bool sync = true);
154
160 const std::vector<CMutableTransaction>& txns,
161 const CScript& scriptPubKey);
162
165 void mineBlocks(int num_blocks);
166
180 std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
181 const std::vector<COutPoint>& inputs,
182 int input_height,
183 const std::vector<CKey>& input_signing_keys,
184 const std::vector<CTxOut>& outputs,
185 const std::optional<CFeeRate>& feerate,
186 const std::optional<uint32_t>& fee_output);
197 CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
198 const std::vector<COutPoint>& inputs,
199 int input_height,
200 const std::vector<CKey>& input_signing_keys,
201 const std::vector<CTxOut>& outputs,
202 bool submit = true);
203
217 int input_height,
221 bool submit = true);
222
234 std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
235
236 FakeNodeClock m_clock{std::chrono::seconds{1598887952}}; // 2020-08-31, arbitrary
237 std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
238 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
239};
240
245template <class T = const BasicTestingSetup>
247{
248 opts.extra_args = Cat(
249 {
250 "-nodebuglogfile",
251 "-nodebug",
252 },
253 opts.extra_args);
254
255 return std::make_unique<T>(chain_type, opts);
256}
257
259{
260public:
261 explicit SocketTestingSetup();
263
269 template <typename T = DynSock>
270 std::shared_ptr<typename T::Pipes> ConnectClient(std::span<const std::byte> data)
271 {
272 auto connected_socket_pipes(std::make_shared<typename T::Pipes>());
273 connected_socket_pipes->recv.PushBytes(data.data(), data.size());
274 m_accepted_sockets.Push(std::make_unique<T>(connected_socket_pipes));
276 }
277
278private:
281
284};
285
287
288#endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition amount.h:12
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:46
Serialized script, used inside transaction inputs and outputs.
Definition script.h:406
A basic thread-safe queue, used for queuing sockets to be returned by Accept().
Definition net.h:303
void Push(S s) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition net.h:307
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
Fast randomness source.
Definition random.h:386
void Reseed(const uint256 &seed) noexcept
Reseed with explicit seed (only for testing).
Definition random.cpp:634
decltype(CreateSock) m_create_sock_orig
Save the original value of CreateSock here and restore it when the test ends.
DynSock::Queue m_accepted_sockets
Queue of connected sockets returned by listening socket (represents network interface)
std::shared_ptr< typename T::Pipes > ConnectClient(std::span< const std::byte > data)
Connect to the socket with a mock client and send pre-loaded data.
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes)
Definition caches.cpp:57
std::function< std::unique_ptr< Sock >(int, int, int)> CreateSock
Socket factory.
Definition netbase.cpp:577
std::shared_ptr< const CTransaction > CTransactionRef
uint256 GetRandHash() noexcept
Generate a random uint256.
Definition random.h:463
constexpr CAmount CENT
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:28
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.
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition bench.cpp:39
Basic testing setup.
ArgsManager m_args
Test-specific arguments and settings.
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.
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
kernel::CacheSizes m_kernel_cache_sizes
std::function< void()> m_make_chainman
void LoadVerifyActivateChainstate()
void CreateBlockTemplateManager()
Create the block template manager.
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.
CBlock CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey)
Create a new block with just given transactions, coinbase paying to scriptPubKey.
CBlock CreateAndProcessBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, bool sync=true)
Create a new block with just the given transactions, coinbase paying to scriptPubKey,...
FakeNodeClock m_clock
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.
Identical to TestingSetup, but chain set to testnet4.
kernel::CacheSizes kernel
Definition caches.h:29
NodeContext struct containing references to chain state and connection state.
Definition context.h:57
SeedRandomStateForTest(SeedRand::ZEROS)
SeedRand
Definition random.h:15
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition vector.h:34