Bitcoin Core  24.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 <chainparamsbase.h>
9 #include <fs.h>
10 #include <key.h>
11 #include <node/caches.h>
12 #include <node/context.h>
13 #include <primitives/transaction.h>
14 #include <pubkey.h>
15 #include <random.h>
16 #include <stdexcept>
17 #include <util/check.h>
18 #include <util/string.h>
19 #include <util/system.h>
20 #include <util/vector.h>
21 
22 #include <functional>
23 #include <type_traits>
24 #include <vector>
25 
26 class Chainstate;
27 
29 extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
30 
32 extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
33 
34 // Enable BOOST_CHECK_EQUAL for enum class types
35 namespace std {
36 template <typename T>
37 std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
38 {
39  return stream << static_cast<typename std::underlying_type<T>::type>(e);
40 }
41 } // namespace std
42 
51 
55 extern bool g_mock_deterministic_tests;
56 
57 enum class SeedRand {
58  ZEROS,
59  SEED,
60 };
61 
63 void Seed(FastRandomContext& ctx);
64 
65 static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
66 {
67  if (seed == SeedRand::ZEROS) {
68  g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
69  } else {
71  }
72 }
73 
74 static constexpr CAmount CENT{1000000};
75 
80  node::NodeContext m_node; // keep as first member to be destructed last
81 
82  explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
84 
87 };
88 
95 
96  explicit ChainTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
98 };
99 
105 
107 
108  explicit TestingSetup(
109  const std::string& chainName = CBaseChainParams::MAIN,
110  const std::vector<const char*>& extra_args = {},
111  const bool coins_db_in_memory = true,
112  const bool block_tree_db_in_memory = true);
113 };
114 
116 struct RegTestingSetup : public TestingSetup {
118  : TestingSetup{CBaseChainParams::REGTEST} {}
119 };
120 
121 class CBlock;
122 struct CMutableTransaction;
123 class CScript;
124 
130  const std::string& chain_name = CBaseChainParams::REGTEST,
131  const std::vector<const char*>& extra_args = {},
132  const bool coins_db_in_memory = true,
133  const bool block_tree_db_in_memory = true);
134 
140  CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
141  const CScript& scriptPubKey,
142  Chainstate* chainstate = nullptr);
143 
149  const std::vector<CMutableTransaction>& txns,
150  const CScript& scriptPubKey,
151  Chainstate& chainstate);
152 
154  void mineBlocks(int num_blocks);
155 
168  int input_vout,
169  int input_height,
170  CKey input_signing_key,
171  CScript output_destination,
172  CAmount output_amount = CAmount(1 * COIN),
173  bool submit = true);
174 
186  std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
187 
188  std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
189  CKey coinbaseKey; // private/public key needed to spend coinbase transactions
190 };
191 
196 template <class T = const BasicTestingSetup>
197 std::unique_ptr<T> MakeNoLogFileContext(const std::string& chain_name = CBaseChainParams::REGTEST, const std::vector<const char*>& extra_args = {})
198 {
199  const std::vector<const char*> arguments = Cat(
200  {
201  "-nodebuglogfile",
202  "-nodebug",
203  },
204  extra_args);
205 
206  return std::make_unique<T>(chain_name, arguments);
207 }
208 
210 
211 // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
212 std::ostream& operator<<(std::ostream& os, const uint256& num);
213 
220 {
221 public:
222  explicit HasReason(const std::string& reason) : m_reason(reason) {}
223  bool operator()(const std::exception& e) const
224  {
225  return std::string(e.what()).find(m_reason) != std::string::npos;
226  };
227 
228 private:
229  const std::string m_reason;
230 };
231 
232 #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
CBaseChainParams defines the base parameters (shared between bitcoin-cli and bitcoind) of a given ins...
static const std::string REGTEST
static const std::string MAIN
Chain name strings.
Definition: block.h:69
An encapsulated private key.
Definition: key.h:27
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:455
Fast randomness source.
Definition: random.h:144
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:220
HasReason(const std::string &reason)
Definition: setup_common.h:222
const std::string m_reason
Definition: setup_common.h:226
bool operator()(const std::exception &e) const
Definition: setup_common.h:223
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
256-bit opaque blob.
Definition: uint256.h:105
std::ostream & operator<<(typename std::enable_if< std::is_enum< T >::value, std::ostream >::type &stream, const T &e)
Definition: setup_common.h:37
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:421
std::ostream & operator<<(std::ostream &os, const uint256 &num)
SeedRand
Definition: setup_common.h:57
@ ZEROS
Seed with a compile time constant of zeros.
@ SEED
Call the Seed() helper.
bool g_mock_deterministic_tests
Flag to make GetRand in random.h return the same number.
Definition: random.cpp:584
std::unique_ptr< T > MakeNoLogFileContext(const std::string &chain_name=CBaseChainParams::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:197
static constexpr CAmount CENT
Definition: setup_common.h:74
const std::function< void(const std::string &)> G_TEST_LOG_FUN
This is connected to the logger.
Definition: bench.cpp:22
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: setup_common.h:65
void Seed(FastRandomContext &ctx)
Seed the given random ctx or use the seed passed in via an environment var.
CBlock getBlock13b8a()
const std::function< std::vector< const char * >)> G_TEST_COMMAND_LINE_ARGUMENTS
Retrieve the command line arguments.
Definition: bench.cpp:24
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
Basic testing setup.
Definition: setup_common.h:79
ArgsManager m_args
Definition: setup_common.h:86
BasicTestingSetup(const std::string &chainName=CBaseChainParams::MAIN, const std::vector< const char * > &extra_args={})
const fs::path m_path_root
Definition: setup_common.h:85
node::NodeContext m_node
Definition: setup_common.h:80
A mutable version of CTransaction.
Definition: transaction.h:380
Testing setup that performs all steps up until right before ChainstateManager gets initialized.
Definition: setup_common.h:93
ChainTestingSetup(const std::string &chainName=CBaseChainParams::MAIN, const std::vector< const char * > &extra_args={})
node::CacheSizes m_cache_sizes
Definition: setup_common.h:94
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:116
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:128
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:188
CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction, int input_vout, int input_height, CKey input_signing_key, CScript output_destination, CAmount output_amount=CAmount(1 *COIN), bool submit=true)
Create a transaction and submit to the mempool.
TestChain100Setup(const std::string &chain_name=CBaseChainParams::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.
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:102
bool m_block_tree_db_in_memory
Definition: setup_common.h:104
bool m_coins_db_in_memory
Definition: setup_common.h:103
void LoadVerifyActivateChainstate()
TestingSetup(const std::string &chainName=CBaseChainParams::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:43
V Cat(V v1, V &&v2)
Concatenate two vectors, moving elements.
Definition: vector.h:32