2019-04-11 09:53:04 -04:00
|
|
|
// Copyright (c) 2011-2019 The Bitcoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2019-04-11 09:44:10 -04:00
|
|
|
#include <test/setup_common.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
|
2017-10-05 16:40:43 -04:00
|
|
|
#include <banman.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <consensus/consensus.h>
|
2018-06-16 22:38:13 +02:00
|
|
|
#include <consensus/params.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <consensus/validation.h>
|
|
|
|
#include <crypto/sha256.h>
|
2019-06-20 09:34:45 -04:00
|
|
|
#include <init.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <miner.h>
|
2019-06-24 17:22:28 +02:00
|
|
|
#include <net.h>
|
2018-12-13 14:33:28 -05:00
|
|
|
#include <noui.h>
|
2018-05-13 23:39:53 -07:00
|
|
|
#include <pow.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <rpc/register.h>
|
2018-08-13 16:13:29 -04:00
|
|
|
#include <rpc/server.h>
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <script/sigcache.h>
|
2018-08-13 16:13:29 -04:00
|
|
|
#include <streams.h>
|
2019-06-24 17:22:28 +02:00
|
|
|
#include <txdb.h>
|
|
|
|
#include <util/memory.h>
|
|
|
|
#include <util/strencodings.h>
|
|
|
|
#include <util/time.h>
|
2019-06-17 10:56:52 +03:00
|
|
|
#include <util/translation.h>
|
2019-04-02 17:03:37 -04:00
|
|
|
#include <util/validation.h>
|
2018-08-13 16:13:29 -04:00
|
|
|
#include <validation.h>
|
2019-06-24 17:22:28 +02:00
|
|
|
#include <validationinterface.h>
|
2018-08-13 16:13:29 -04:00
|
|
|
|
2019-06-17 10:56:52 +03:00
|
|
|
#include <functional>
|
2018-08-06 15:40:20 -04:00
|
|
|
#include "claimtrie.h"
|
|
|
|
|
2019-06-17 10:56:52 +03:00
|
|
|
|
2018-08-13 16:13:29 -04:00
|
|
|
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2018-12-17 10:33:53 -05:00
|
|
|
FastRandomContext g_insecure_rand_ctx;
|
|
|
|
|
2018-04-18 08:01:48 -04:00
|
|
|
std::ostream& operator<<(std::ostream& os, const uint256& num)
|
|
|
|
{
|
|
|
|
os << num.ToString();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:40:20 -04:00
|
|
|
std::ostream& operator<<(std::ostream& os, const uint160& num)
|
|
|
|
{
|
|
|
|
os << num.ToString();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const COutPoint& point)
|
|
|
|
{
|
|
|
|
os << point.ToString();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const CClaimTrieNode& node)
|
|
|
|
{
|
|
|
|
os << node.hash.ToString();
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const CClaimValue& claim)
|
|
|
|
{
|
|
|
|
os << "claim(" << claim.outPoint.ToString()
|
|
|
|
<< ", " << claim.claimId.ToString()
|
|
|
|
<< ", " << claim.nAmount
|
|
|
|
<< ", " << claim.nEffectiveAmount
|
|
|
|
<< ", " << claim.nHeight
|
|
|
|
<< ", " << claim.nValidAtHeight << ')';
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:39:49 +02:00
|
|
|
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
2019-04-10 14:57:19 -04:00
|
|
|
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
|
2015-03-03 07:49:12 -08:00
|
|
|
{
|
2019-06-19 17:52:35 -04:00
|
|
|
fs::create_directories(m_path_root);
|
|
|
|
gArgs.ForceSetArg("-datadir", m_path_root.string());
|
|
|
|
ClearDatadirCache();
|
2019-06-20 09:34:45 -04:00
|
|
|
SelectParams(chainName);
|
|
|
|
gArgs.ForceSetArg("-printtoconsole", "0");
|
|
|
|
InitLogging();
|
|
|
|
LogInstance().StartLogging();
|
2018-07-11 23:44:12 -04:00
|
|
|
SHA256AutoDetect();
|
|
|
|
ECC_Start();
|
|
|
|
SetupEnvironment();
|
|
|
|
SetupNetworking();
|
|
|
|
InitSignatureCache();
|
|
|
|
InitScriptExecutionCache();
|
|
|
|
fCheckBlockIndex = true;
|
2019-03-29 09:55:21 +01:00
|
|
|
static bool noui_connected = false;
|
|
|
|
if (!noui_connected) {
|
|
|
|
noui_connect();
|
|
|
|
noui_connected = true;
|
|
|
|
}
|
2015-03-12 09:34:42 +01:00
|
|
|
}
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BasicTestingSetup::~BasicTestingSetup()
|
|
|
|
{
|
2019-06-20 09:34:45 -04:00
|
|
|
LogInstance().DisconnectTestLogger();
|
2018-07-11 23:44:12 -04:00
|
|
|
fs::remove_all(m_path_root);
|
|
|
|
ECC_Stop();
|
|
|
|
}
|
|
|
|
|
2015-06-30 21:39:49 +02:00
|
|
|
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
|
2015-03-12 09:34:42 +01:00
|
|
|
{
|
2015-04-17 14:40:24 +02:00
|
|
|
const CChainParams& chainparams = Params();
|
2019-02-28 14:51:11 -05:00
|
|
|
// Ideally we'd move all the RPC tests to the functional testing framework
|
|
|
|
// instead of unit tests, but for now we need these here.
|
|
|
|
RegisterAllCoreRPCCommands(tableRPC);
|
|
|
|
|
|
|
|
// We have to run a scheduler thread to prevent ActivateBestChain
|
|
|
|
// from blocking due to queue overrun.
|
|
|
|
threadGroup.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler));
|
|
|
|
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
|
|
|
|
|
|
|
|
mempool.setSanityCheck(1.0);
|
|
|
|
pblocktree.reset(new CBlockTreeDB(1 << 20, true));
|
2019-07-24 13:23:48 -04:00
|
|
|
g_chainstate = MakeUnique<CChainState>();
|
|
|
|
::ChainstateActive().InitCoinsDB(
|
|
|
|
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
|
2018-08-06 15:40:20 -04:00
|
|
|
pclaimTrie = new CClaimTrie(true, false, 1);
|
2019-07-24 13:23:48 -04:00
|
|
|
assert(!::ChainstateActive().CanFlushToDisk());
|
|
|
|
::ChainstateActive().InitCoinsCache();
|
|
|
|
assert(::ChainstateActive().CanFlushToDisk());
|
2019-02-28 14:51:11 -05:00
|
|
|
if (!LoadGenesisBlock(chainparams)) {
|
|
|
|
throw std::runtime_error("LoadGenesisBlock failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
if (!ActivateBestChain(state, chainparams)) {
|
|
|
|
throw std::runtime_error(strprintf("ActivateBestChain failed. (%s)", FormatStateMessage(state)));
|
|
|
|
}
|
|
|
|
|
|
|
|
nScriptCheckThreads = 3;
|
|
|
|
for (int i = 0; i < nScriptCheckThreads - 1; i++)
|
2018-06-13 14:50:59 -04:00
|
|
|
threadGroup.create_thread([i]() { return ThreadScriptCheck(i); });
|
2019-02-28 14:51:11 -05:00
|
|
|
|
|
|
|
g_banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
|
|
|
|
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
|
2015-03-03 07:49:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::~TestingSetup()
|
|
|
|
{
|
2018-12-13 14:33:28 -05:00
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
|
|
|
GetMainSignals().FlushBackgroundCallbacks();
|
|
|
|
GetMainSignals().UnregisterBackgroundSignalScheduler();
|
|
|
|
g_connman.reset();
|
2017-10-05 13:10:58 -04:00
|
|
|
g_banman.reset();
|
2018-12-13 14:33:28 -05:00
|
|
|
UnloadBlockIndex();
|
2019-07-24 13:23:48 -04:00
|
|
|
g_chainstate.reset();
|
2018-12-13 14:33:28 -05:00
|
|
|
pblocktree.reset();
|
2018-08-06 15:40:20 -04:00
|
|
|
delete pclaimTrie;
|
2015-03-03 07:49:12 -08:00
|
|
|
}
|
2011-10-03 16:14:13 -04:00
|
|
|
|
2015-03-03 09:59:32 -05:00
|
|
|
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
|
|
|
|
{
|
2019-04-10 15:58:11 -04:00
|
|
|
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
|
|
|
|
// TODO: fix the code to support SegWit blocks.
|
2019-05-20 14:59:07 -04:00
|
|
|
gArgs.ForceSetArg("-segwitheight", "432");
|
2019-04-10 15:58:11 -04:00
|
|
|
SelectParams(CBaseChainParams::REGTEST);
|
|
|
|
|
2015-03-03 09:59:32 -05:00
|
|
|
// Generate a 100-block chain:
|
|
|
|
coinbaseKey.MakeNewKey(true);
|
|
|
|
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
|
|
|
|
for (int i = 0; i < COINBASE_MATURITY; i++)
|
|
|
|
{
|
|
|
|
std::vector<CMutableTransaction> noTxns;
|
|
|
|
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
|
2018-04-11 13:51:28 -04:00
|
|
|
m_coinbase_txns.push_back(b.vtx[0]);
|
2015-03-03 09:59:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create a new block with just given transactions, coinbase paying to
|
|
|
|
// scriptPubKey, and try to add it to the current chain.
|
|
|
|
//
|
|
|
|
CBlock
|
|
|
|
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
|
|
|
|
{
|
2015-04-20 00:17:11 +02:00
|
|
|
const CChainParams& chainparams = Params();
|
2018-08-06 15:40:20 -04:00
|
|
|
BlockAssembler::Options options;
|
|
|
|
options.nBlockMaxWeight = DEFAULT_BLOCK_MAX_WEIGHT;
|
|
|
|
options.blockMinFeeRate = CFeeRate(0);
|
|
|
|
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams, options).CreateNewBlock(scriptPubKey);
|
2015-03-03 09:59:32 -05:00
|
|
|
CBlock& block = pblocktemplate->block;
|
|
|
|
|
|
|
|
// Replace mempool-selected txns with just coinbase plus passed-in txns:
|
|
|
|
block.vtx.resize(1);
|
2017-06-02 03:18:57 +02:00
|
|
|
for (const CMutableTransaction& tx : txns)
|
2016-11-10 17:34:17 -08:00
|
|
|
block.vtx.push_back(MakeTransactionRef(tx));
|
2015-03-03 09:59:32 -05:00
|
|
|
// IncrementExtraNonce creates a valid coinbase and merkleRoot
|
2017-11-06 23:08:55 +01:00
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2018-03-25 22:49:33 +02:00
|
|
|
unsigned int extraNonce = 0;
|
2019-03-27 11:14:25 -04:00
|
|
|
IncrementExtraNonce(&block, ::ChainActive().Tip(), extraNonce);
|
2017-11-06 23:08:55 +01:00
|
|
|
}
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2018-08-06 15:40:20 -04:00
|
|
|
while (!CheckProofOfWork(block.GetPoWHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
2015-03-03 09:59:32 -05:00
|
|
|
|
2016-12-04 00:17:30 -08:00
|
|
|
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
|
2017-08-07 07:36:37 +02:00
|
|
|
ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
2015-03-03 09:59:32 -05:00
|
|
|
|
|
|
|
CBlock result = block;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-08-06 15:40:20 -04:00
|
|
|
RegTestingSetup::RegTestingSetup() : TestingSetup(CBaseChainParams::REGTEST)
|
|
|
|
{
|
|
|
|
minRelayTxFee = CFeeRate(0);
|
|
|
|
minFeePerNameClaimChar = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegTestingSetup::~RegTestingSetup()
|
|
|
|
{
|
|
|
|
minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
|
|
|
|
}
|
2015-11-14 17:04:15 -05:00
|
|
|
|
2017-01-19 22:46:50 -05:00
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) {
|
2018-04-11 13:51:28 -04:00
|
|
|
return FromTx(MakeTransactionRef(tx));
|
2016-04-25 17:04:13 -07:00
|
|
|
}
|
|
|
|
|
2018-04-11 13:51:28 -04:00
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransactionRef& tx)
|
|
|
|
{
|
|
|
|
return CTxMemPoolEntry(tx, nFee, nTime, nHeight,
|
2017-01-20 09:24:35 -05:00
|
|
|
spendsCoinbase, sigOpCost, lp);
|
2015-11-14 17:04:15 -05:00
|
|
|
}
|
2017-09-11 17:43:48 -07:00
|
|
|
|
|
|
|
/**
|
2018-08-06 15:40:20 -04:00
|
|
|
* WARNING: never use real bitcoin block in lbry cause our block header is bigger
|
|
|
|
* @returns a block
|
2017-09-11 17:43:48 -07:00
|
|
|
*/
|
2018-08-06 15:40:20 -04:00
|
|
|
CBlock getTestBlock()
|
2017-09-11 17:43:48 -07:00
|
|
|
{
|
2018-08-06 15:40:20 -04:00
|
|
|
static CBlock block;
|
|
|
|
if (block.IsNull()) {
|
|
|
|
block.nVersion = 5;
|
|
|
|
block.hashPrevBlock = uint256S("e8fc54d1e6581fceaaf64cc8afeedb9c4ba1eb2349eaf638f1fc41e331869dee");
|
|
|
|
block.hashMerkleRoot = uint256S("25bdc368dd4e66dd75ba42140b3d26412aaf9a11a0a7ef4b6b20d5a299644c11");
|
|
|
|
block.hashClaimTrie = uint256S("7bae80d60b09031265f8a9f6282e4b9653764fadfac2c8b4c1f416c972b58814");
|
|
|
|
block.nTime = 1545050539;
|
|
|
|
block.nBits = 0x1a02cb1b;
|
|
|
|
block.nNonce = 3189423909;
|
|
|
|
block.vtx.resize(4);
|
|
|
|
uint256 prevHash = block.hashPrevBlock;
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
CMutableTransaction tx;
|
|
|
|
tx.nVersion = 5;
|
|
|
|
tx.nLockTime = 0;
|
|
|
|
tx.vin.resize(1);
|
|
|
|
tx.vout.resize(1);
|
|
|
|
tx.vin[0].prevout.hash = prevHash;
|
|
|
|
tx.vin[0].prevout.n = 0;
|
|
|
|
tx.vin[0].scriptSig = CScript();
|
|
|
|
tx.vin[0].nSequence = std::numeric_limits<unsigned int>::max();
|
|
|
|
tx.vout[0].scriptPubKey = CScript();
|
|
|
|
tx.vout[0].nValue = 0;
|
|
|
|
prevHash = tx.GetHash();
|
|
|
|
block.vtx[i] = MakeTransactionRef(std::move(tx));
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 17:43:48 -07:00
|
|
|
return block;
|
|
|
|
}
|