Files

177 lines
5.5 KiB
C++
Raw Permalink Normal View History

2017-07-23 12:37:49 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
* Copyright (c) 2015 The Bitcoin Core developers
2021-01-20 19:21:53 +01:00
* Copyright (C) 2017-2021 Tom Zander <tom@flowee.org>
2017-07-23 12:37:49 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2015-01-05 21:40:24 +01:00
2018-01-16 10:47:52 +00:00
#ifndef FLOWEE_TEST_TEST_MAIN_H
#define FLOWEE_TEST_TEST_MAIN_H
2017-07-23 12:37:49 +02:00
#include <chainparamsbase.h>
2023-11-24 18:16:32 +01:00
#include <primitives/PrivateKey.h>
2023-11-24 18:01:36 +01:00
#include <primitives/PublicKey.h>
2017-07-23 12:37:49 +02:00
#include <txmempool.h>
#include <Application.h>
2018-01-15 15:26:12 +00:00
#include <validation/Engine.h>
#include <primitives/Block.h>
2018-01-15 15:26:12 +00:00
#include <primitives/transaction.h>
#include <boost/filesystem.hpp>
2018-01-15 15:26:12 +00:00
/** Basic testing setup.
* This just configures logging and chain parameters.
*/
struct BasicTestingSetup {
ECCVerifyHandle globalVerifyHandle;
BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~BasicTestingSetup();
const char *currentTestName() {
return "test";
}
};
2018-01-15 15:26:12 +00:00
class MockBlockValidation : public Validation::Engine {
public:
MockBlockValidation();
~MockBlockValidation();
void initSingletons();
2021-11-02 10:18:24 +01:00
Block createBlock(CBlockIndex *parent, const CScript& scriptPubKey, const std::vector<CTransaction>& txns = std::vector<CTransaction>()) const;
2018-01-15 15:26:12 +00:00
/// short version of the above
2021-11-02 10:18:24 +01:00
Block createBlock(CBlockIndex *parent);
2018-01-15 15:26:12 +00:00
/**
* @brief appendGenesis creates the standard reg-test genesis and appends.
* This will only succeed if the current chain (Params()) is REGTEST
*/
void appendGenesis();
enum OutputType {
EmptyOutScript,
2018-11-13 15:03:04 +01:00
StandardOutScript,
FullOutScript // full p2pkh output script
2018-01-15 15:26:12 +00:00
};
/**
* @brief Append a list of blocks to the block-validator and wait for them to be validated.
* @param blocks the amount of blocks to add to the blockchain-tip.
* @param coinbaseKey [out] an empty key we will initialize and use as coinbase.
* @param out one of the OutputType members.
*/
2022-07-06 22:12:33 +02:00
std::vector<Block> appendChain(int blocks, PrivateKey &coinbaseKey, OutputType out = StandardOutScript);
2018-01-15 15:26:12 +00:00
2021-11-02 10:18:24 +01:00
inline std::vector<Block> appendChain(int blocks, OutputType out = StandardOutScript) {
2022-07-06 22:12:33 +02:00
PrivateKey key;
2018-01-15 15:26:12 +00:00
return appendChain(blocks, key, out);
}
/**
* @brief This creates a chain of blocks on top of a random index.
* @param parent the index that is to be extended
* @param blocks the amount of blocks to build.
* @return The full list of blocks.
* This method doesn't add the blocks, use appendChain() for that.
*/
2021-11-02 10:18:24 +01:00
std::vector<Block> createChain(CBlockIndex *parent, int blocks) const;
2018-01-15 15:26:12 +00:00
CTxMemPool mp;
};
/** Testing setup that configures a complete environment.
* Included are data directory, coins database, script check threads
* and wallet (if enabled) setup.
*/
struct TestingSetup: public BasicTestingSetup {
2018-01-15 15:26:12 +00:00
MockBlockValidation bv;
boost::filesystem::path pathTemp;
2017-07-23 12:37:49 +02:00
enum BlocksDb {
BlocksDbInMemory,
BlocksDbOnDisk
};
2018-01-15 15:26:12 +00:00
TestingSetup(const std::string& chainName = CBaseChainParams::REGTEST);
~TestingSetup();
};
2018-01-15 15:26:12 +00:00
struct MainnetTestingSetup : TestingSetup {
MainnetTestingSetup() : TestingSetup(CBaseChainParams::MAIN) {}
2015-03-03 09:59:32 -05:00
};
class CTxMemPoolEntry;
class CTxMemPool;
struct TestMemPoolEntryHelper
{
// Default values
2021-01-20 19:21:53 +01:00
int64_t nFee;
int64_t nTime;
double dPriority;
unsigned int nHeight;
bool hadNoDependencies;
2015-10-29 14:06:13 -04:00
bool spendsCoinbase;
2015-12-04 15:01:22 -05:00
LockPoints lp;
TestMemPoolEntryHelper() :
nFee(0), nTime(0), dPriority(0.0), nHeight(1),
2020-04-12 23:48:32 +02:00
hadNoDependencies(false), spendsCoinbase(false) { }
2015-10-26 11:08:46 -04:00
CTxMemPoolEntry FromTx(CMutableTransaction &tx, CTxMemPool *pool = NULL);
// Change the default value
2021-01-20 19:21:53 +01:00
TestMemPoolEntryHelper &Fee(int64_t _fee) { nFee = _fee; return *this; }
TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
TestMemPoolEntryHelper &Priority(double _priority) { dPriority = _priority; return *this; }
TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
TestMemPoolEntryHelper &HadNoDependencies(bool _hnd) { hadNoDependencies = _hnd; return *this; }
2015-10-29 14:06:13 -04:00
TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
};
2017-07-23 12:37:49 +02:00
class MockApplication : public Application
{
public:
MockApplication() = delete;
inline static void doInit() {
static_cast<MockApplication*>(Application::instance())->pub_init();
}
2018-01-15 15:26:12 +00:00
inline static void doStartThreads() {
static_cast<MockApplication*>(Application::instance())->pub_startThreads();
}
inline static void setValidationEngine(Validation::Engine *bv) {
static_cast<MockApplication*>(Application::instance())->replaceValidationEngine(bv);
}
protected:
inline void pub_init() {
2017-07-23 12:37:49 +02:00
init();
}
2018-01-15 15:26:12 +00:00
inline void pub_startThreads() {
startThreads();
}
inline void replaceValidationEngine(Validation::Engine *bv) {
m_validationEngine.release();
m_validationEngine.reset(bv);
}
2017-07-23 12:37:49 +02:00
};
#endif