Bitcoin Core  27.99.0
P2P Digital Currency
setup_common.h
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 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 <primitives/transaction.h>
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>
19 #include <util/signalinterrupt.h>
20 #include <util/string.h>
21 #include <util/vector.h>
22 
23 #include <functional>
24 #include <type_traits>
25 #include <vector>
26 
27 class CFeeRate;
28 class Chainstate;
29 class FastRandomContext;
30 
32 extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
33 
35 extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
36 
38 extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
39 
40 // Enable BOOST_CHECK_EQUAL for enum class types
41 namespace std {
42 template <typename T>
43 std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
44 {
45  return stream << static_cast<typename std::underlying_type<T>::type>(e);
46 }
47 } // namespace std
48 
49 static constexpr CAmount CENT{1000000};
50 
56  node::NodeContext m_node; // keep as first member to be destructed last
57 
58  explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
60 
63  bool m_has_custom_datadir{false};
65 };
66 
75 
76  explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
78 
79  // Supplies a chainstate, if one is needed
81 };
82 
86  explicit TestingSetup(
87  const ChainType chainType = ChainType::MAIN,
88  const std::vector<const char*>& extra_args = {},
89  const bool coins_db_in_memory = true,
90  const bool block_tree_db_in_memory = true);
91 };
92 
94 struct RegTestingSetup : public TestingSetup {
97 };
98 
99 class CBlock;
100 struct CMutableTransaction;
101 class CScript;
102 
108  const ChainType chain_type = ChainType::REGTEST,
109  const std::vector<const char*>& extra_args = {},
110  const bool coins_db_in_memory = true,
111  const bool block_tree_db_in_memory = true);
112 
118  CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
119  const CScript& scriptPubKey,
120  Chainstate* chainstate = nullptr);
121 
127  const std::vector<CMutableTransaction>& txns,
128  const CScript& scriptPubKey,
129  Chainstate& chainstate);
130 
132  void mineBlocks(int num_blocks);
133 
147  std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
148  const std::vector<COutPoint>& inputs,
149  int input_height,
150  const std::vector<CKey>& input_signing_keys,
151  const std::vector<CTxOut>& outputs,
152  const std::optional<CFeeRate>& feerate,
153  const std::optional<uint32_t>& fee_output);
164  CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
165  const std::vector<COutPoint>& inputs,
166  int input_height,
167  const std::vector<CKey>& input_signing_keys,
168  const std::vector<CTxOut>& outputs,
169  bool submit = true);
170 
183  uint32_t input_vout,
184  int input_height,
185  CKey input_signing_key,
186  CScript output_destination,
187  CAmount output_amount = CAmount(1 * COIN),
188  bool submit = true);
189 
201  std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
202 
212  void MockMempoolMinFee(const CFeeRate& target_feerate);
213 
214  std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
215  CKey coinbaseKey; // private/public key needed to spend coinbase transactions
216 };
217 
222 template <class T = const BasicTestingSetup>
223 std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, const std::vector<const char*>& extra_args = {})
224 {
225  const std::vector<const char*> arguments = Cat(
226  {
227  "-nodebuglogfile",
228  "-nodebug",
229  },
230  extra_args);
231 
232  return std::make_unique<T>(chain_type, arguments);
233 }
234 
236 
237 // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
238 std::ostream& operator<<(std::ostream& os, const uint256& num);
239 
246 {
247 public:
248  explicit HasReason(const std::string& reason) : m_reason(reason) {}
249  bool operator()(const std::exception& e) const
250  {
251  return std::string(e.what()).find(m_reason) != std::string::npos;
252  };
253 
254 private:
255  const std::string m_reason;
256 };
257 
258 #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:33
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:490
Fast randomness source.
Definition: random.h:145
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:246
HasReason(const std::string &reason)
Definition: setup_common.h:248
const std::string m_reason
Definition: setup_common.h:252
bool operator()(const std::exception &e) const
Definition: setup_common.h:249
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
256-bit opaque blob.
Definition: uint256.h:106
Helper class that manages an interrupt flag, and allows a thread or signal to interrupt another threa...
std::ostream & operator<<(typename std::enable_if< std::is_enum< T >::value, std::ostream >::type &stream, const T &e)
Definition: setup_common.h:43
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
std::ostream & operator<<(std::ostream &os, const uint256 &num)
static constexpr CAmount CENT
Definition: setup_common.h:49
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:23
CBlock getBlock13b8a()
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, const std::vector< const char * > &extra_args={})
Make a test setup that has disk access to the debug.log file disabled.
Definition: setup_common.h:223
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:25
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: bench.cpp:27
Basic testing setup.
Definition: setup_common.h:54
fs::path m_path_lock
Definition: setup_common.h:62
ArgsManager m_args
Definition: setup_common.h:64
BasicTestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char * > &extra_args={})
util::SignalInterrupt m_interrupt
Definition: setup_common.h:55
fs::path m_path_root
Definition: setup_common.h:61
node::NodeContext m_node
Definition: setup_common.h:56
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:71
ChainTestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char * > &extra_args={})
bool m_block_tree_db_in_memory
Definition: setup_common.h:74
node::CacheSizes m_cache_sizes
Definition: setup_common.h:72
void LoadVerifyActivateChainstate()
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:94
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:106
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:214
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(const ChainType chain_type=ChainType::REGTEST, const std::vector< const char * > &extra_args={}, const bool coins_db_in_memory=true, const bool block_tree_db_in_memory=true)
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...
CBlock CreateBlock(const std::vector< CMutableTransaction > &txns, const CScript &scriptPubKey, Chainstate &chainstate)
Create a new block with just given transactions, coinbase paying to scriptPubKey.
Testing setup that configures a complete environment.
Definition: setup_common.h:85
TestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char * > &extra_args={}, const bool coins_db_in_memory=true, const bool block_tree_db_in_memory=true)
NodeContext struct containing references to chain state and connection state.
Definition: context.h:53
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34