Files

247 lines
7.6 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2015 The Bitcoin Core developers
2021-01-20 19:21:53 +01:00
* Copyright (C) 2017-2021 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01: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/>.
*/
2014-03-18 10:11:00 +01:00
#include "test_bitcoin.h"
2018-01-15 15:26:12 +00:00
#include <chain.h>
#include <chainparams.h>
2020-04-01 22:51:47 +02:00
#include <merkle.h>
2018-01-15 15:26:12 +00:00
#include <main.h>
2023-11-24 18:16:32 +01:00
#include <primitives/PrivateKey.h>
2018-01-15 15:26:12 +00:00
#include <net.h>
#include <random.h>
2018-02-12 21:19:28 +01:00
#include <UiInterface.h>
2018-06-23 20:37:27 +02:00
#include <interfaces/validationinterface.h>
#include <utxo/UnspentOutputDatabase.h>
2018-08-10 10:33:34 +03:00
#include <script/sigcache.h>
2019-08-24 13:20:37 +02:00
#include <server/serverutil.h>
2026-05-14 13:13:40 +02:00
#include <utiltime.h>
2018-11-13 23:07:30 +01:00
#include <BlocksDB.h>
2022-07-06 22:50:53 +02:00
#include <BitcoinVersion.h>
2013-04-13 00:13:08 -05:00
2018-02-13 15:08:41 +01:00
#include <boost/test/included/unit_test.hpp>
2013-04-13 00:13:08 -05:00
2018-02-12 21:19:28 +01:00
CClientUIInterface uiInterface; // Declared but not defined in UiInterface.h
UnspentOutputDatabase *g_utxo = nullptr;
extern void noui_connect();
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
{
Log::Manager::instance()->loadDefaultTestSetup(std::bind(&BasicTestingSetup::currentTestName, this));
2018-01-15 15:26:12 +00:00
ECC_Start();
SetupEnvironment();
SetupNetworking();
2018-08-10 10:33:34 +03:00
InitSignatureCache();
2018-01-15 15:26:12 +00:00
mapArgs["-checkblockindex"] = "1";
SelectParams(chainName);
noui_connect();
MockApplication::doStartThreads();
MockApplication::doInit();
}
2015-03-03 09:59:32 -05:00
BasicTestingSetup::~BasicTestingSetup()
{
2018-01-15 15:26:12 +00:00
ECC_Stop();
Application::quit(0);
}
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
{
2018-01-15 15:26:12 +00:00
ClearDatadirCache();
2019-11-13 11:46:09 +01:00
pathTemp = boost::filesystem::temp_directory_path() / strprintf("test_flowee_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
2018-01-15 15:26:12 +00:00
boost::filesystem::create_directories(pathTemp / "regtest/blocks/index");
boost::filesystem::create_directories(pathTemp / "blocks/index");
mapArgs["-datadir"] = pathTemp.string();
Blocks::DB::createTestInstance(1<<20);
UnspentOutputDatabase::setSmallLimits();
2025-02-08 19:05:26 +01:00
g_utxo = new UnspentOutputDatabase(Application::instance()->ioContext(), GetDataDir(true) / "unspent");
2018-01-15 15:26:12 +00:00
bv.initSingletons();
bv.appendGenesis();
MockApplication::setValidationEngine(&bv);
RegisterNodeSignals(GetNodeSignals());
}
TestingSetup::~TestingSetup()
{
2018-01-15 15:26:12 +00:00
MockApplication::setValidationEngine(nullptr);
bv.shutdown();
Blocks::Index::unload();
UnregisterNodeSignals(GetNodeSignals());
2018-03-25 13:55:48 +02:00
ValidationNotifier().removeAll();
2018-01-15 15:26:12 +00:00
UnloadBlockIndex();
delete g_utxo;
2018-01-15 15:26:12 +00:00
boost::filesystem::remove_all(pathTemp);
}
2018-01-15 15:26:12 +00:00
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(CMutableTransaction &tx, CTxMemPool *pool)
2015-03-03 09:59:32 -05:00
{
2015-11-13 10:05:21 -05:00
CTransaction txn(tx);
bool hasNoDependencies = pool ? pool->HasNoInputsOf(tx) : hadNoDependencies;
// Hack to assume either its completely dependent on other mempool txs or not at all
2021-01-20 19:21:53 +01:00
int64_t inChainValue = hasNoDependencies ? txn.GetValueOut() : 0;
2015-11-13 10:05:21 -05:00
return CTxMemPoolEntry(txn, nFee, nTime, dPriority, nHeight,
2020-04-12 23:48:32 +02:00
hasNoDependencies, inChainValue, spendsCoinbase, lp);
}
2011-07-31 20:00:38 +02:00
void Shutdown(void* parg)
{
2018-01-15 15:26:12 +00:00
exit(0);
2011-07-31 20:00:38 +02:00
}
void StartShutdown()
{
2018-01-15 15:26:12 +00:00
exit(0);
}
2014-03-25 07:53:21 +01:00
bool ShutdownRequested()
{
2018-01-15 15:26:12 +00:00
return false;
}
////////////////////////////////////
MockBlockValidation::MockBlockValidation()
{
}
void MockBlockValidation::initSingletons()
{
// set all the stuff that has been created in the Fixture (TestingSetup::TestingSetup())
mp.setUtxo(g_utxo);
2018-01-15 15:26:12 +00:00
setMempool(&mp);
chainActive.SetTip(nullptr);
setBlockchain(&chainActive);
}
MockBlockValidation::~MockBlockValidation()
{
g_utxo = 0;
2018-01-15 15:26:12 +00:00
}
2021-11-02 10:18:24 +01:00
Block MockBlockValidation::createBlock(CBlockIndex *parent, const CScript& scriptPubKey, const std::vector<CTransaction>& txns) const
2018-01-15 15:26:12 +00:00
{
CMutableTransaction coinbase;
coinbase.vin.resize(1);
coinbase.vout.resize(1);
coinbase.vin[0].scriptSig = CScript() << (parent->nHeight + 1) << OP_0;
coinbase.vout[0].nValue = 50 * COIN;
coinbase.vout[0].scriptPubKey = scriptPubKey;
2018-11-08 15:17:12 +01:00
// Make sure the coinbase is big enough. (since 20181115 HF we require a min 100bytes tx size)
const uint32_t coinbaseSize = ::GetSerializeSize(coinbase, SER_NETWORK, PROTOCOL_VERSION);
if (coinbaseSize < 100)
coinbase.vin[0].scriptSig << std::vector<uint8_t>(100 - coinbaseSize - 1);
2018-01-15 15:26:12 +00:00
2021-11-02 09:36:09 +01:00
MutableBlock block;
2018-01-15 15:26:12 +00:00
block.vtx.push_back(coinbase);
block.nVersion = 4;
block.hashPrevBlock = *parent->phashBlock;
block.nTime = parent->nTime + 2;
block.nNonce = 0;
block.nBits = CalculateNextWorkRequired(parent, block.blockHeader(), Params().GetConsensus());
2018-01-25 23:40:50 +00:00
2018-01-15 15:26:12 +00:00
for (const CTransaction &tx : txns) {
block.vtx.push_back(tx);
}
block.hashMerkleRoot = BlockMerkleRoot(block);
const bool mine = Params().NetworkIDString() == "regtest";
do {
++block.nNonce;
2021-11-02 09:28:35 +01:00
} while (mine && !CheckProofOfWork(block.createHash(), block.nBits, Params().GetConsensus()));
2018-01-15 15:26:12 +00:00
2021-11-02 10:18:24 +01:00
return Block::fromOldBlock(block);
2018-01-15 15:26:12 +00:00
}
2021-11-02 10:18:24 +01:00
Block MockBlockValidation::createBlock(CBlockIndex *parent)
2018-01-15 15:26:12 +00:00
{
2022-07-06 22:12:33 +02:00
PrivateKey coinbaseKey;
2022-05-11 13:46:15 +02:00
coinbaseKey.makeNewKey();
2018-01-15 15:26:12 +00:00
CScript scriptPubKey;
2022-05-11 13:46:15 +02:00
scriptPubKey << ToByteVector(coinbaseKey.getPubKey()) << OP_CHECKSIG;
2018-01-15 15:26:12 +00:00
return createBlock(parent, scriptPubKey);
}
void MockBlockValidation::appendGenesis()
{
addBlock(Params().GenesisBlock(), Validation::SaveGoodToDisk);
2018-01-15 15:26:12 +00:00
waitValidationFinished();
}
2022-07-06 22:12:33 +02:00
std::vector<Block> MockBlockValidation::appendChain(int blocks, PrivateKey &coinbaseKey, OutputType out)
2018-01-15 15:26:12 +00:00
{
2021-11-02 10:18:24 +01:00
std::vector<Block> answer;
2018-01-15 15:26:12 +00:00
answer.reserve(blocks);
2022-05-11 13:46:15 +02:00
coinbaseKey.makeNewKey();
2018-01-15 15:26:12 +00:00
CScript scriptPubKey;
2018-11-13 15:03:04 +01:00
if (out == StandardOutScript) {
2022-05-11 13:46:15 +02:00
scriptPubKey << ToByteVector(coinbaseKey.getPubKey()) << OP_CHECKSIG;
2018-11-13 15:03:04 +01:00
} else if (out == FullOutScript) {
2022-05-11 13:46:15 +02:00
scriptPubKey << OP_DUP << OP_HASH160 << ToByteVector(coinbaseKey.getPubKey().getKeyId())
2018-11-13 15:03:04 +01:00
<< OP_EQUALVERIFY << OP_CHECKSIG;
}
2018-01-15 15:26:12 +00:00
waitValidationFinished();
const bool allowFullChecks = Params().NetworkIDString() == "regtest";
for (int i = 0; i < blocks; i++)
{
CBlockIndex *tip = blockchain()->Tip();
assert(tip);
auto block = createBlock(tip, scriptPubKey);
answer.push_back(block);
2018-09-19 21:39:13 +02:00
auto future = addBlock(block, Validation::SaveGoodToDisk, nullptr);
2018-01-15 15:26:12 +00:00
future.setCheckPoW(allowFullChecks);
future.setCheckMerkleRoot(allowFullChecks);
future.start();
future.waitUntilFinished();
}
return answer;
}
2021-11-02 10:18:24 +01:00
std::vector<Block> MockBlockValidation::createChain(CBlockIndex *parent, int blocks) const
2018-01-15 15:26:12 +00:00
{
2022-07-06 22:12:33 +02:00
PrivateKey coinbaseKey;
2022-05-11 13:46:15 +02:00
coinbaseKey.makeNewKey();
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.getPubKey()) << OP_CHECKSIG;
2018-01-15 15:26:12 +00:00
CBlockIndex dummy;
dummy.nTime = parent->nTime;
dummy.phashBlock = parent->phashBlock;
uint256 dummySha;
2018-01-25 23:40:50 +00:00
uint32_t bits = parent->nBits;
2018-01-15 15:26:12 +00:00
2021-11-02 10:18:24 +01:00
std::vector<Block> answer;
2018-01-15 15:26:12 +00:00
for (int i = 0; i < blocks; ++i) {
dummy.nHeight = parent->nHeight + i;
dummy.nTime += 10;
2018-01-25 23:40:50 +00:00
dummy.nBits = bits;
2021-11-02 10:18:24 +01:00
Block block = createBlock(&dummy, scriptPubKey);
2018-01-25 23:40:50 +00:00
bits = block.bits();
2018-01-15 15:26:12 +00:00
answer.push_back(block);
dummySha = block.createHash();
dummy.phashBlock = &dummySha;
}
return answer;
2014-03-25 07:53:21 +01:00
}