2016-01-27 12:05:25 +01:00
|
|
|
// Copyright (c) 2015 The Bitcoin Core developers
|
2021-04-20 21:33:02 +02:00
|
|
|
// Copyright (c) 2014-2020 The Dash Core developers
|
2016-01-27 12:05:25 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-10-25 17:58:37 +02:00
|
|
|
#ifndef BITCOIN_TEST_SETUP_COMMON_H
|
|
|
|
#define BITCOIN_TEST_SETUP_COMMON_H
|
2015-03-03 16:49:12 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <chainparamsbase.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <key.h>
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <random.h>
|
|
|
|
#include <scheduler.h>
|
|
|
|
#include <txdb.h>
|
|
|
|
#include <txmempool.h>
|
2015-03-03 16:49:12 +01:00
|
|
|
|
2018-04-02 20:31:40 +02:00
|
|
|
#include <memory>
|
2018-12-12 06:00:20 +01:00
|
|
|
#include <type_traits>
|
2018-04-02 20:31:40 +02:00
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2018-12-12 06:00:20 +01: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-18 19:40:37 +01: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.
|
|
|
|
*/
|
|
|
|
extern FastRandomContext g_insecure_rand_ctx;
|
2017-06-07 23:59:41 +02:00
|
|
|
|
2019-02-04 12:11:34 +01:00
|
|
|
/**
|
|
|
|
* Flag to make GetRand in random.h return the same number
|
|
|
|
*/
|
|
|
|
extern bool g_mock_deterministic_tests;
|
|
|
|
|
2018-12-13 13:43:12 +01:00
|
|
|
static inline void SeedInsecureRand(bool deterministic = false)
|
2017-06-07 23:59:41 +02:00
|
|
|
{
|
2018-12-13 22:44:26 +01:00
|
|
|
g_insecure_rand_ctx = FastRandomContext(deterministic);
|
2017-06-07 23:59:41 +02:00
|
|
|
}
|
|
|
|
|
2018-12-13 22:44:26 +01: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-06-07 23:59:41 +02:00
|
|
|
|
2018-09-27 18:01:47 +02:00
|
|
|
static constexpr CAmount CENT{1000000};
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
/** Basic testing setup.
|
2019-06-25 12:13:08 +02: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;
|
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
2015-03-12 09:34:42 +01:00
|
|
|
~BasicTestingSetup();
|
2018-07-12 13:46:43 +02:00
|
|
|
private:
|
|
|
|
const fs::path m_path_root;
|
2015-03-12 09:34:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Testing setup that configures a complete environment.
|
2019-06-25 12:13:08 +02:00
|
|
|
* Included are coins database, script check threads setup.
|
2015-03-12 09:34:42 +01:00
|
|
|
*/
|
2018-12-13 22:44:26 +01:00
|
|
|
struct TestingSetup : public BasicTestingSetup {
|
2015-03-03 16:49:12 +01:00
|
|
|
boost::thread_group threadGroup;
|
2017-07-11 09:30:36 +02:00
|
|
|
CScheduler scheduler;
|
2015-03-03 16:49:12 +01:00
|
|
|
|
2017-08-17 22:59:56 +02:00
|
|
|
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
2015-03-03 16:49:12 +01:00
|
|
|
~TestingSetup();
|
|
|
|
};
|
|
|
|
|
2015-03-03 15:59:32 +01:00
|
|
|
class CBlock;
|
|
|
|
struct CMutableTransaction;
|
|
|
|
class CScript;
|
|
|
|
|
2018-07-18 12:06:56 +02:00
|
|
|
struct TestChainSetup : public TestingSetup
|
|
|
|
{
|
|
|
|
TestChainSetup(int blockCount);
|
|
|
|
~TestChainSetup();
|
2015-03-03 15:59:32 +01:00
|
|
|
|
|
|
|
// Create a new block with just given transactions, coinbase paying to
|
|
|
|
// scriptPubKey, and try to add it to the current chain.
|
|
|
|
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
|
|
|
const CScript& scriptPubKey);
|
2018-07-18 12:06:56 +02:00
|
|
|
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
|
|
|
const CKey& scriptKey);
|
|
|
|
CBlock CreateBlock(const std::vector<CMutableTransaction>& txns,
|
|
|
|
const CScript& scriptPubKey);
|
|
|
|
CBlock CreateBlock(const std::vector<CMutableTransaction>& txns,
|
|
|
|
const CKey& scriptKey);
|
2015-03-03 15:59:32 +01:00
|
|
|
|
2018-04-17 19:25:34 +02:00
|
|
|
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
|
2015-03-03 15:59:32 +01:00
|
|
|
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
|
|
|
};
|
|
|
|
|
2018-07-18 12:06:56 +02:00
|
|
|
//
|
|
|
|
// Testing fixture that pre-creates a
|
|
|
|
// 100-block REGTEST-mode block chain
|
|
|
|
//
|
|
|
|
struct TestChain100Setup : public TestChainSetup {
|
|
|
|
TestChain100Setup() : TestChainSetup(100) {}
|
|
|
|
};
|
|
|
|
|
2018-08-22 12:39:41 +02:00
|
|
|
struct TestChainDIP3Setup : public TestChainSetup
|
|
|
|
{
|
|
|
|
TestChainDIP3Setup() : TestChainSetup(431) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestChainDIP3BeforeActivationSetup : public TestChainSetup
|
|
|
|
{
|
|
|
|
TestChainDIP3BeforeActivationSetup() : TestChainSetup(430) {}
|
|
|
|
};
|
|
|
|
|
2015-11-14 23:04:15 +01:00
|
|
|
class CTxMemPoolEntry;
|
|
|
|
|
|
|
|
struct TestMemPoolEntryHelper
|
|
|
|
{
|
|
|
|
// Default values
|
|
|
|
CAmount nFee;
|
|
|
|
int64_t nTime;
|
|
|
|
unsigned int nHeight;
|
2015-10-29 19:06:13 +01:00
|
|
|
bool spendsCoinbase;
|
2015-10-26 16:08:46 +01:00
|
|
|
unsigned int sigOpCount;
|
2015-12-04 21:01:22 +01:00
|
|
|
LockPoints lp;
|
|
|
|
|
2015-11-14 23:04:15 +01:00
|
|
|
TestMemPoolEntryHelper() :
|
2019-03-14 15:44:42 +01:00
|
|
|
nFee(0), nTime(0), nHeight(1),
|
2021-11-08 19:43:24 +01:00
|
|
|
spendsCoinbase(false), sigOpCount(1) { }
|
2017-10-03 14:26:05 +02:00
|
|
|
|
2018-04-17 19:25:34 +02:00
|
|
|
CTxMemPoolEntry FromTx(const CMutableTransaction& tx);
|
|
|
|
CTxMemPoolEntry FromTx(const CTransactionRef& tx);
|
2015-11-14 23:04:15 +01: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 19:06:13 +01:00
|
|
|
TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
|
2015-10-26 16:08:46 +01:00
|
|
|
TestMemPoolEntryHelper &SigOps(unsigned int _sigops) { sigOpCount = _sigops; return *this; }
|
2015-11-14 23:04:15 +01:00
|
|
|
};
|
2017-10-03 14:26:05 +02:00
|
|
|
|
|
|
|
CBlock getBlock13b8a();
|
|
|
|
|
2021-05-25 15:43:13 +02:00
|
|
|
// BOOST_CHECK_EXCEPTION predicates to check the specific validation error
|
|
|
|
class HasReason {
|
|
|
|
public:
|
2018-08-27 19:32:59 +02:00
|
|
|
explicit HasReason(const std::string& reason) : m_reason(reason) {}
|
2021-05-25 15:43:13 +02:00
|
|
|
bool operator() (const std::runtime_error& e) const {
|
|
|
|
return std::string(e.what()).find(m_reason) != std::string::npos;
|
|
|
|
};
|
|
|
|
private:
|
|
|
|
const std::string m_reason;
|
|
|
|
};
|
|
|
|
|
2021-04-14 02:15:23 +02: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);
|
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
#endif
|