2019-04-11 09:53:04 -04:00
|
|
|
// Copyright (c) 2015-2019 The Bitcoin Core developers
|
2015-01-05 17:40:24 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-04-11 09:44:10 -04:00
|
|
|
#ifndef BITCOIN_TEST_SETUP_COMMON_H
|
|
|
|
#define BITCOIN_TEST_SETUP_COMMON_H
|
2015-03-03 12:49:12 -03:00
|
|
|
|
2017-11-09 21:57:53 -03: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 12:49:12 -03:00
|
|
|
|
2018-04-02 15:31:40 -03:00
|
|
|
#include <memory>
|
2018-04-29 12:45:44 -03:00
|
|
|
#include <type_traits>
|
2018-04-02 15:31:40 -03:00
|
|
|
|
2015-03-03 12:49:12 -03:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2018-04-29 12:45:44 -03: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 15:19:07 -03: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 12:33:53 -03:00
|
|
|
extern FastRandomContext g_insecure_rand_ctx;
|
2017-05-23 18:09:30 -04:00
|
|
|
|
2019-02-01 19:06:32 -03:00
|
|
|
/**
|
|
|
|
* Flag to make GetRand in random.h return the same number
|
|
|
|
*/
|
|
|
|
extern bool g_mock_deterministic_tests;
|
|
|
|
|
2018-10-31 19:51:57 -03:00
|
|
|
static inline void SeedInsecureRand(bool deterministic = false)
|
2017-05-23 18:09:30 -04:00
|
|
|
{
|
2018-12-13 16:33:28 -03:00
|
|
|
g_insecure_rand_ctx = FastRandomContext(deterministic);
|
2017-05-23 18:09:30 -04:00
|
|
|
}
|
|
|
|
|
2018-12-13 16:33:28 -03: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 18:09:30 -04:00
|
|
|
|
2018-09-17 15:33:52 -03:00
|
|
|
static constexpr CAmount CENT{1000000};
|
|
|
|
|
2015-03-12 05:34:42 -03:00
|
|
|
/** Basic testing setup.
|
|
|
|
* This just configures logging and chain parameters.
|
|
|
|
*/
|
|
|
|
struct BasicTestingSetup {
|
2015-07-28 15:11:20 -03:00
|
|
|
ECCVerifyHandle globalVerifyHandle;
|
|
|
|
|
2017-08-01 06:22:41 -04:00
|
|
|
explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
2015-03-12 05:34:42 -03:00
|
|
|
~BasicTestingSetup();
|
2018-07-11 23:44:12 -04:00
|
|
|
|
|
|
|
fs::path SetDataDir(const std::string& name);
|
|
|
|
|
|
|
|
private:
|
|
|
|
const fs::path m_path_root;
|
2015-03-12 05:34:42 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Testing setup that configures a complete environment.
|
2016-04-18 09:54:57 -03:00
|
|
|
* Included are data directory, coins database, script check threads setup.
|
2015-03-12 05:34:42 -03:00
|
|
|
*/
|
2018-12-13 16:33:28 -03:00
|
|
|
struct TestingSetup : public BasicTestingSetup {
|
2015-03-03 12:49:12 -03:00
|
|
|
boost::thread_group threadGroup;
|
2017-01-19 18:49:22 -03:00
|
|
|
CScheduler scheduler;
|
2015-03-03 12:49:12 -03:00
|
|
|
|
2017-08-01 06:22:41 -04:00
|
|
|
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
|
2015-03-03 12:49:12 -03:00
|
|
|
~TestingSetup();
|
|
|
|
};
|
|
|
|
|
2015-03-03 11:59:32 -03:00
|
|
|
class CBlock;
|
|
|
|
struct CMutableTransaction;
|
|
|
|
class CScript;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Testing fixture that pre-creates a
|
|
|
|
// 100-block REGTEST-mode block chain
|
|
|
|
//
|
|
|
|
struct TestChain100Setup : public TestingSetup {
|
|
|
|
TestChain100Setup();
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
~TestChain100Setup();
|
|
|
|
|
2018-04-11 14:51:28 -03:00
|
|
|
std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
|
2015-03-03 11:59:32 -03:00
|
|
|
CKey coinbaseKey; // private/public key needed to spend coinbase transactions
|
|
|
|
};
|
|
|
|
|
2015-11-14 19:04:15 -03:00
|
|
|
class CTxMemPoolEntry;
|
|
|
|
|
|
|
|
struct TestMemPoolEntryHelper
|
|
|
|
{
|
|
|
|
// Default values
|
|
|
|
CAmount nFee;
|
|
|
|
int64_t nTime;
|
|
|
|
unsigned int nHeight;
|
2015-10-29 15:06:13 -03:00
|
|
|
bool spendsCoinbase;
|
2016-01-03 14:54:50 -03:00
|
|
|
unsigned int sigOpCost;
|
2015-12-04 17:01:22 -03:00
|
|
|
LockPoints lp;
|
|
|
|
|
2015-11-14 19:04:15 -03:00
|
|
|
TestMemPoolEntryHelper() :
|
2017-01-20 00:46:50 -03:00
|
|
|
nFee(0), nTime(0), nHeight(1),
|
2016-11-11 13:57:51 -03:00
|
|
|
spendsCoinbase(false), sigOpCost(4) { }
|
2017-09-11 21:43:48 -03:00
|
|
|
|
2018-04-11 14:51:28 -03:00
|
|
|
CTxMemPoolEntry FromTx(const CMutableTransaction& tx);
|
|
|
|
CTxMemPoolEntry FromTx(const CTransactionRef& tx);
|
2015-11-14 19:04:15 -03: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 15:06:13 -03:00
|
|
|
TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
|
2016-01-03 14:54:50 -03:00
|
|
|
TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
|
2015-11-14 19:04:15 -03:00
|
|
|
};
|
2017-09-11 21:43:48 -03:00
|
|
|
|
|
|
|
CBlock getBlock13b8a();
|
|
|
|
|
2018-04-18 09:01:48 -03: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 12:49:12 -03:00
|
|
|
#endif
|