2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2015-2020 The Bitcoin Core developers
|
2015-01-05 21:40:24 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-11-05 15:18:59 -05:00
|
|
|
#ifndef BITCOIN_TEST_UTIL_SETUP_COMMON_H
|
|
|
|
#define BITCOIN_TEST_UTIL_SETUP_COMMON_H
|
2015-03-03 07:49:12 -08:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <chainparamsbase.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <key.h>
|
2021-04-18 11:37:06 +02:00
|
|
|
#include <util/system.h>
|
2019-09-17 18:28:03 -04:00
|
|
|
#include <node/context.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <pubkey.h>
|
|
|
|
#include <random.h>
|
2020-09-29 12:13:10 +02:00
|
|
|
#include <stdexcept>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <txmempool.h>
|
2020-06-08 08:47:10 -04:00
|
|
|
#include <util/check.h>
|
2020-02-12 23:01:45 -05:00
|
|
|
#include <util/string.h>
|
2021-01-25 12:22:03 +01:00
|
|
|
#include <util/vector.h>
|
2015-03-03 07:49:12 -08:00
|
|
|
|
2018-04-29 15:45:44 +00:00
|
|
|
#include <type_traits>
|
2021-01-27 15:04:34 +08:00
|
|
|
#include <vector>
|
2015-03-03 07:49:12 -08:00
|
|
|
|
2019-09-27 11:53:34 -04:00
|
|
|
/** This is connected to the logger. Can be used to redirect logs to any other log */
|
|
|
|
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
|
|
|
|
|
2018-04-29 15:45:44 +00:00
|
|
|
// Enable BOOST_CHECK_EQUAL for enum class types
|
|
|
|
template <typename T>
|
|
|
|
std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
|
|
|
|
{
|
|
|
|
return stream << static_cast<typename std::underlying_type<T>::type>(e);
|
|
|
|
}
|
|
|
|
|
2018-12-17 13:19:07 -05:00
|
|
|
/**
|
|
|
|
* This global and the helpers that use it are not thread-safe.
|
|
|
|
*
|
|
|
|
* If thread-safety is needed, the global could be made thread_local (given
|
|
|
|
* that thread_local is supported on all architectures we support) or a
|
|
|
|
* per-thread instance could be used in the multi-threaded test.
|
|
|
|
*/
|
2018-12-17 10:33:53 -05:00
|
|
|
extern FastRandomContext g_insecure_rand_ctx;
|
2017-05-23 15:09:30 -07:00
|
|
|
|
2019-02-01 17:06:32 -05:00
|
|
|
/**
|
|
|
|
* Flag to make GetRand in random.h return the same number
|
|
|
|
*/
|
|
|
|
extern bool g_mock_deterministic_tests;
|
|
|
|
|
2019-09-26 09:14:19 -04:00
|
|
|
enum class SeedRand {
|
|
|
|
ZEROS, //!< Seed with a compile time constant of zeros
|
|
|
|
SEED, //!< Call the Seed() helper
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Seed the given random ctx or use the seed passed in via an environment var */
|
|
|
|
void Seed(FastRandomContext& ctx);
|
|
|
|
|
|
|
|
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
|
2017-05-23 15:09:30 -07:00
|
|
|
{
|
2019-09-26 09:14:19 -04:00
|
|
|
if (seed == SeedRand::ZEROS) {
|
|
|
|
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
|
|
|
|
} else {
|
|
|
|
Seed(g_insecure_rand_ctx);
|
|
|
|
}
|
2017-05-23 15:09:30 -07:00
|
|
|
}
|
|
|
|
|
2018-12-13 14:33:28 -05:00
|
|
|
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
|
|
|
|
static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
|
|
|
|
static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
|
|
|
|
static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
|
|
|
|
static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
|
2017-05-23 15:09:30 -07:00
|
|
|
|
2018-09-17 14:33:52 -04:00
|
|
|
static constexpr CAmount CENT{1000000};
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
/** Basic testing setup.
|
2019-06-19 17:52:35 -04:00
|
|
|
* This just configures logging, data dir and chain parameters.
|
2015-03-12 09:34:42 +01:00
|
|
|
*/
|
|
|
|
struct BasicTestingSetup {
|
2015-07-28 20:11:20 +02:00
|
|
|
ECCVerifyHandle globalVerifyHandle;
|
2020-04-08 19:47:56 -04:00
|
|
|
NodeContext m_node;
|
2015-07-28 20:11:20 +02:00
|
|
|
|
2020-04-08 20:02:20 -04:00
|
|
|
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
|
2015-03-12 09:34:42 +01:00
|
|
|
~BasicTestingSetup();
|
2020-04-08 19:47:56 -04:00
|
|
|
|
2018-07-11 23:44:12 -04:00
|
|
|
const fs::path m_path_root;
|
2021-04-18 11:37:06 +02:00
|
|
|
ArgsManager m_args;
|
2015-03-12 09:34:42 +01:00
|
|
|
};
|
|
|
|
|
test: Add new ChainTestingSetup and use it
Previously, the validation_chainstatemanager_tests test suite
instantiated its own duplicate ChainstateManager on which tests were
performed.
This wasn't a problem for the specific actions performed in
that suite. However, the existence of this duplicate ChainstateManager
and the fact that many of our validation static functions reach for
g_chainman, ::Chain(state|)Active means we may end up acting on two
different CChainStates should we write more extensive tests in the
future.
This change adds a new ChainTestingSetup which performs all
initialization previously done by TestingSetup except:
1. RPC command registration
2. ChainState initialization
3. Genesis Activation
4. {Ban,Conn,Peer}Man initialization
Means that we will no longer need to initialize a duplicate
ChainstateManger in order to test the initialization codepaths of
CChainState and ChainstateManager.
Lastly, this change has the additional benefit of allowing for
review-only assertions meant to show correctness to work in future work
de-globalizing g_chainman.
In the test chainstatemanager_rebalance_caches, an additional
LoadGenesisBlock call is added as MaybeReblanaceCaches eventually calls
FlushBlockFile, which tries to access vinfoBlockFile[nLastBlockFile],
which is out of bounds when LoadGenesisBlock hasn't been called yet.
-----
Note for the future:
The class con/destructor inheritance structure we have for these
TestingSetup classes is probably not the most suitable abstraction. In
particular, for both TestingSetup and ChainTestingSetup, we need to stop
the scheduler first before anything else. Otherwise classes depending on
the scheduler may be referenced by the scheduler after said classes are
freed. This means that there's no clear parallel between our teardown
code and C++'s destructuring order for class hierarchies.
Future work should strive to coalesce (as much as possible) test and
non-test init codepaths and perhaps structure it in a more fail-proof
way.
2020-10-13 16:55:20 -04:00
|
|
|
/** Testing setup that performs all steps up until right before
|
|
|
|
* ChainstateManager gets initialized. Meant for testing ChainstateManager
|
|
|
|
* initialization behaviour.
|
2015-03-12 09:34:42 +01:00
|
|
|
*/
|
test: Add new ChainTestingSetup and use it
Previously, the validation_chainstatemanager_tests test suite
instantiated its own duplicate ChainstateManager on which tests were
performed.
This wasn't a problem for the specific actions performed in
that suite. However, the existence of this duplicate ChainstateManager
and the fact that many of our validation static functions reach for
g_chainman, ::Chain(state|)Active means we may end up acting on two
different CChainStates should we write more extensive tests in the
future.
This change adds a new ChainTestingSetup which performs all
initialization previously done by TestingSetup except:
1. RPC command registration
2. ChainState initialization
3. Genesis Activation
4. {Ban,Conn,Peer}Man initialization
Means that we will no longer need to initialize a duplicate
ChainstateManger in order to test the initialization codepaths of
CChainState and ChainstateManager.
Lastly, this change has the additional benefit of allowing for
review-only assertions meant to show correctness to work in future work
de-globalizing g_chainman.
In the test chainstatemanager_rebalance_caches, an additional
LoadGenesisBlock call is added as MaybeReblanaceCaches eventually calls
FlushBlockFile, which tries to access vinfoBlockFile[nLastBlockFile],
which is out of bounds when LoadGenesisBlock hasn't been called yet.
-----
Note for the future:
The class con/destructor inheritance structure we have for these
TestingSetup classes is probably not the most suitable abstraction. In
particular, for both TestingSetup and ChainTestingSetup, we need to stop
the scheduler first before anything else. Otherwise classes depending on
the scheduler may be referenced by the scheduler after said classes are
freed. This means that there's no clear parallel between our teardown
code and C++'s destructuring order for class hierarchies.
Future work should strive to coalesce (as much as possible) test and
non-test init codepaths and perhaps structure it in a more fail-proof
way.
2020-10-13 16:55:20 -04:00
|
|
|
struct ChainTestingSetup : public BasicTestingSetup {
|
2015-03-03 07:49:12 -08:00
|
|
|
|
test: Add new ChainTestingSetup and use it
Previously, the validation_chainstatemanager_tests test suite
instantiated its own duplicate ChainstateManager on which tests were
performed.
This wasn't a problem for the specific actions performed in
that suite. However, the existence of this duplicate ChainstateManager
and the fact that many of our validation static functions reach for
g_chainman, ::Chain(state|)Active means we may end up acting on two
different CChainStates should we write more extensive tests in the
future.
This change adds a new ChainTestingSetup which performs all
initialization previously done by TestingSetup except:
1. RPC command registration
2. ChainState initialization
3. Genesis Activation
4. {Ban,Conn,Peer}Man initialization
Means that we will no longer need to initialize a duplicate
ChainstateManger in order to test the initialization codepaths of
CChainState and ChainstateManager.
Lastly, this change has the additional benefit of allowing for
review-only assertions meant to show correctness to work in future work
de-globalizing g_chainman.
In the test chainstatemanager_rebalance_caches, an additional
LoadGenesisBlock call is added as MaybeReblanaceCaches eventually calls
FlushBlockFile, which tries to access vinfoBlockFile[nLastBlockFile],
which is out of bounds when LoadGenesisBlock hasn't been called yet.
-----
Note for the future:
The class con/destructor inheritance structure we have for these
TestingSetup classes is probably not the most suitable abstraction. In
particular, for both TestingSetup and ChainTestingSetup, we need to stop
the scheduler first before anything else. Otherwise classes depending on
the scheduler may be referenced by the scheduler after said classes are
freed. This means that there's no clear parallel between our teardown
code and C++'s destructuring order for class hierarchies.
Future work should strive to coalesce (as much as possible) test and
non-test init codepaths and perhaps structure it in a more fail-proof
way.
2020-10-13 16:55:20 -04:00
|
|
|
explicit ChainTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
|
|
|
|
~ChainTestingSetup();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Testing setup that configures a complete environment.
|
|
|
|
*/
|
|
|
|
struct TestingSetup : public ChainTestingSetup {
|
2020-04-08 20:02:20 -04:00
|
|
|
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
|
2015-03-03 07:49:12 -08:00
|
|
|
};
|
|
|
|
|
2019-09-24 11:23:19 -04:00
|
|
|
/** Identical to TestingSetup, but chain set to regtest */
|
|
|
|
struct RegTestingSetup : public TestingSetup {
|
|
|
|
RegTestingSetup()
|
|
|
|
: TestingSetup{CBaseChainParams::REGTEST} {}
|
|
|
|
};
|
|
|
|
|
2015-03-03 09:59:32 -05:00
|
|
|
class CBlock;
|
|
|
|
struct CMutableTransaction;
|
|
|
|
class CScript;
|
|
|
|
|
2020-08-21 15:37:38 +02:00
|
|
|
/**
|
|
|
|
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
|
|
|
|
*/
|
2019-09-24 11:23:19 -04:00
|
|
|
struct TestChain100Setup : public RegTestingSetup {
|
2021-04-04 10:50:12 +02:00
|
|
|
TestChain100Setup();
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2020-08-21 15:37:38 +02:00
|
|
|
/**
|
|
|
|
* Create a new block with just given transactions, coinbase paying to
|
|
|
|
* scriptPubKey, and try to add it to the current chain.
|
|
|
|
*/
|
2015-03-03 09:59:32 -05:00
|
|
|
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
|
|
|
const CScript& scriptPubKey);
|
|
|
|
|
2020-08-31 12:39:39 -04:00
|
|
|
//! Mine a series of new blocks on the active chain.
|
|
|
|
void mineBlocks(int num_blocks);
|
|
|
|
|
2021-01-25 11:51:38 -08:00
|
|
|
/**
|
|
|
|
* Create a transaction and submit to the mempool.
|
|
|
|
*
|
|
|
|
* @param input_transaction The transaction to spend
|
|
|
|
* @param input_vout The vout to spend from the input_transaction
|
|
|
|
* @param input_height The height of the block that included the input_transaction
|
|
|
|
* @param input_signing_key The key to spend the input_transaction
|
|
|
|
* @param output_destination Where to send the output
|
|
|
|
* @param output_amount How much to send
|
|
|
|
*/
|
|
|
|
CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
|
|
|
|
int input_vout,
|
|
|
|
int input_height,
|
|
|
|
CKey input_signing_key,
|
|
|
|
CScript output_destination,
|
|
|
|
CAmount output_amount = CAmount(1 * COIN));
|
|
|
|
|
2015-03-03 09:59:32 -05:00
|
|
|
~TestChain100Setup();
|
|
|
|
|
2018-04-11 13:51:28 -04:00
|
|
|
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
|
2015-03-03 09:59:32 -05:00
|
|
|
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
|
|
|
};
|
|
|
|
|
2021-01-25 12:22:03 +01:00
|
|
|
/**
|
|
|
|
* Make a test setup that has disk access to the debug.log file disabled. Can
|
|
|
|
* be used in "hot loops", for example fuzzing or benchmarking.
|
|
|
|
*/
|
|
|
|
template <class T = const BasicTestingSetup>
|
|
|
|
std::unique_ptr<T> MakeNoLogFileContext(const std::string& chain_name = CBaseChainParams::REGTEST, const std::vector<const char*>& extra_args = {})
|
|
|
|
{
|
|
|
|
const std::vector<const char*> arguments = Cat(
|
|
|
|
{
|
|
|
|
"-nodebuglogfile",
|
|
|
|
"-nodebug",
|
|
|
|
},
|
|
|
|
extra_args);
|
|
|
|
|
|
|
|
return std::make_unique<T>(chain_name, arguments);
|
|
|
|
}
|
|
|
|
|
2015-11-14 17:04:15 -05:00
|
|
|
class CTxMemPoolEntry;
|
|
|
|
|
|
|
|
struct TestMemPoolEntryHelper
|
|
|
|
{
|
|
|
|
// Default values
|
|
|
|
CAmount nFee;
|
|
|
|
int64_t nTime;
|
|
|
|
unsigned int nHeight;
|
2015-10-29 14:06:13 -04:00
|
|
|
bool spendsCoinbase;
|
2016-01-03 18:54:50 +01:00
|
|
|
unsigned int sigOpCost;
|
2015-12-04 15:01:22 -05:00
|
|
|
LockPoints lp;
|
|
|
|
|
2015-11-14 17:04:15 -05:00
|
|
|
TestMemPoolEntryHelper() :
|
2017-01-19 22:46:50 -05:00
|
|
|
nFee(0), nTime(0), nHeight(1),
|
2016-11-11 11:57:51 -05:00
|
|
|
spendsCoinbase(false), sigOpCost(4) { }
|
2017-09-11 17:43:48 -07:00
|
|
|
|
2020-12-06 15:51:22 +00:00
|
|
|
CTxMemPoolEntry FromTx(const CMutableTransaction& tx) const;
|
|
|
|
CTxMemPoolEntry FromTx(const CTransactionRef& tx) const;
|
2015-11-14 17:04:15 -05:00
|
|
|
|
|
|
|
// Change the default value
|
|
|
|
TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
|
|
|
|
TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
|
|
|
|
TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
|
2015-10-29 14:06:13 -04:00
|
|
|
TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
|
2016-01-03 18:54:50 +01:00
|
|
|
TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
|
2015-11-14 17:04:15 -05:00
|
|
|
};
|
2017-09-11 17:43:48 -07:00
|
|
|
|
|
|
|
CBlock getBlock13b8a();
|
|
|
|
|
2018-04-18 08:01:48 -04:00
|
|
|
// define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
|
|
|
|
std::ostream& operator<<(std::ostream& os, const uint256& num);
|
|
|
|
|
2020-08-31 13:16:13 +02:00
|
|
|
/**
|
|
|
|
* BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
|
|
|
|
* Use as
|
|
|
|
* BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
|
|
|
|
*/
|
2020-09-29 12:13:10 +02:00
|
|
|
class HasReason
|
|
|
|
{
|
2020-08-31 13:16:13 +02:00
|
|
|
public:
|
|
|
|
explicit HasReason(const std::string& reason) : m_reason(reason) {}
|
2020-09-29 12:13:10 +02:00
|
|
|
bool operator()(const std::exception& e) const
|
|
|
|
{
|
2020-08-31 13:16:13 +02:00
|
|
|
return std::string(e.what()).find(m_reason) != std::string::npos;
|
|
|
|
};
|
2020-09-29 12:13:10 +02:00
|
|
|
|
2020-08-31 13:16:13 +02:00
|
|
|
private:
|
|
|
|
const std::string m_reason;
|
|
|
|
};
|
|
|
|
|
2015-03-03 07:49:12 -08:00
|
|
|
#endif
|