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 <key.h>
10 #include <node/caches.h>
11 #include <node/context.h> // IWYU pragma: export
12 #include <primitives/transaction.h>
13 #include <pubkey.h>
14 #include <stdexcept>
15 #include <util/chaintype.h> // IWYU pragma: export
16 #include <util/check.h>
17 #include <util/fs.h>
18 #include <util/string.h>
19 #include <util/vector.h>
20 
21 #include <functional>
22 #include <type_traits>
23 #include <vector>
24 
25 class CFeeRate;
26 class Chainstate;
27 class FastRandomContext;
28 
30 extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
31 
33 extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
34 
36 extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
37 
38 // Enable BOOST_CHECK_EQUAL for enum class types
39 namespace std {
40 template <typename T>
41 std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
42 {
43  return stream << static_cast<typename std::underlying_type<T>::type>(e);
44 }
45 } // namespace std
46 
47 static constexpr CAmount CENT{1000000};
48 
54  node::NodeContext m_node; // keep as first member to be destructed last
55 
56  explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
58 
61  bool m_has_custom_datadir{false};
63 };
64 
73 
74  explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
76 
77  // Supplies a chainstate, if one is needed
79 };
80 
84  explicit TestingSetup(
85  const ChainType chainType = ChainType::MAIN,
86  const std::vector<const char*>& extra_args = {},
87  const bool coins_db_in_memory = true,
88  const bool block_tree_db_in_memory = true);
89 };
90 
92 struct RegTestingSetup : public TestingSetup {
95 };
96 
97 class CBlock;
98 struct CMutableTransaction;
99 class CScript;
100 
106  const ChainType chain_type = ChainType::REGTEST,
107  const std::vector<const char*>& extra_args = {},
108  const bool coins_db_in_memory = true,
109  const bool block_tree_db_in_memory = true);
110 
116  CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
117  const CScript& scriptPubKey,
118  Chainstate* chainstate = nullptr);
119 
125  const std::vector<CMutableTransaction>& txns,
126  const CScript& scriptPubKey,
127  Chainstate& chainstate);
128 
130  void mineBlocks(int num_blocks);
131 
145  std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
146  const std::vector<COutPoint>& inputs,
147  int input_height,
148  const std::vector<CKey>& input_signing_keys,
149  const std::vector<CTxOut>& outputs,
150  const std::optional<CFeeRate>& feerate,
151  const std::optional<uint32_t>& fee_output);
162  CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
163  const std::vector<COutPoint>& inputs,
164  int input_height,
165  const std::vector<CKey>& input_signing_keys,
166  const std::vector<CTxOut>& outputs,
167  bool submit = true);
168 
181  uint32_t input_vout,
182  int input_height,
183  CKey input_signing_key,
184  CScript output_destination,
185  CAmount output_amount = CAmount(1 * COIN),
186  bool submit = true);
187 
199  std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
200 
210  void MockMempoolMinFee(const CFeeRate& target_feerate);
211 
212  std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
213  CKey coinbaseKey; // private/public key needed to spend coinbase transactions
214 };
215 
220 template <class T = const BasicTestingSetup>
221 std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, const std::vector<const char*>& extra_args = {})
222 {
223  const std::vector<const char*> arguments = Cat(
224  {
225  "-nodebuglogfile",
226  "-nodebug",
227  },
228  extra_args);
229 
230  return std::make_unique<T>(chain_type, arguments);
231 }
232 
234 
235 // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
236 std::ostream& operator<<(std::ostream& os, const uint256& num);
237 
244 {
245 public:
246  explicit HasReason(const std::string& reason) : m_reason(reason) {}
247  bool operator()(const std::exception& e) const
248  {
249  return std::string(e.what()).find(m_reason) != std::string::npos;
250  };
251 
252 private:
253  const std::string m_reason;
254 };
255 
256 #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:491
Fast randomness source.
Definition: random.h:145
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:244
HasReason(const std::string &reason)
Definition: setup_common.h:246
const std::string m_reason
Definition: setup_common.h:250
bool operator()(const std::exception &e) const
Definition: setup_common.h:247
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:41
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:47
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:22
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:221
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:24
const std::function< std::string()> G_TEST_GET_FULL_NAME
Retrieve the unit test name.
Definition: bench.cpp:26
Basic testing setup.
Definition: setup_common.h:52
fs::path m_path_lock
Definition: setup_common.h:60
ArgsManager m_args
Definition: setup_common.h:62
BasicTestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char * > &extra_args={})
util::SignalInterrupt m_interrupt
Definition: setup_common.h:53
fs::path m_path_root
Definition: setup_common.h:59
node::NodeContext m_node
Definition: setup_common.h:54
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:69
ChainTestingSetup(const ChainType chainType=ChainType::MAIN, const std::vector< const char * > &extra_args={})
bool m_block_tree_db_in_memory
Definition: setup_common.h:72
node::CacheSizes m_cache_sizes
Definition: setup_common.h:70
void LoadVerifyActivateChainstate()
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:92
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:104
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:212
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:83
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:49
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:34