2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2011-2016 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01: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.
|
|
|
|
|
2015-03-03 16:49:12 +01:00
|
|
|
#include "test_bitcoin.h"
|
|
|
|
|
2015-07-05 14:17:46 +02:00
|
|
|
#include "chainparams.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "consensus/consensus.h"
|
|
|
|
#include "consensus/validation.h"
|
2017-07-14 08:26:04 +02:00
|
|
|
#include "crypto/sha256.h"
|
2017-03-01 16:54:22 +01:00
|
|
|
#include "fs.h"
|
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
|
|
|
#include "key.h"
|
2016-12-02 01:06:41 +01:00
|
|
|
#include "validation.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "miner.h"
|
2016-12-02 00:45:50 +01:00
|
|
|
#include "net_processing.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "pubkey.h"
|
2014-06-26 14:41:53 +02:00
|
|
|
#include "random.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "txdb.h"
|
2015-11-14 23:04:15 +01:00
|
|
|
#include "txmempool.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "ui_interface.h"
|
2016-03-29 19:43:02 +02:00
|
|
|
#include "rpc/server.h"
|
|
|
|
#include "rpc/register.h"
|
2016-10-05 22:58:47 +02:00
|
|
|
#include "script/sigcache.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2016-03-11 16:04:05 +01:00
|
|
|
#include "test/testutil.h"
|
|
|
|
|
2016-06-18 19:38:28 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2017-02-16 02:45:22 +01:00
|
|
|
uint256 insecure_rand_seed = GetRandHash();
|
|
|
|
FastRandomContext insecure_rand_ctx(insecure_rand_seed);
|
2016-04-16 20:47:18 +02:00
|
|
|
|
2011-10-03 22:14:13 +02:00
|
|
|
extern bool fPrintToConsole;
|
2012-05-19 09:35:26 +02:00
|
|
|
extern void noui_connect();
|
|
|
|
|
2015-06-30 21:39:49 +02:00
|
|
|
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
2015-03-03 16:49:12 +01:00
|
|
|
{
|
2017-07-14 08:26:04 +02:00
|
|
|
SHA256AutoDetect();
|
2017-05-10 00:13:00 +02:00
|
|
|
RandomInit();
|
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
|
|
|
ECC_Start();
|
2015-03-25 12:09:17 +01:00
|
|
|
SetupEnvironment();
|
2015-11-01 11:43:55 +01:00
|
|
|
SetupNetworking();
|
2016-10-05 22:58:47 +02:00
|
|
|
InitSignatureCache();
|
Cache full script execution results in addition to signatures
This adds a new CuckooCache in validation, caching whether all of a
transaction's scripts were valid with a given set of script flags.
Unlike previous attempts at caching an entire transaction's
validity, which have nearly universally introduced consensus
failures, this only caches the validity of a transaction's
scriptSigs. As these are pure functions of the transaction and
data it commits to, this should be much safer.
This is somewhat duplicative with the sigcache, as entries in the
new cache will also have several entries in the sigcache. However,
the sigcache is kept both as ATMP relies on it and because it
prevents malleability-based DoS attacks on the new higher-level
cache. Instead, the -sigcachesize option is re-used - cutting the
sigcache size in half and using the newly freed memory for the
script execution cache.
Transactions which match the script execution cache never even have
entries in the script check thread's workqueue created.
Note that the cache is indexed only on the script execution flags
and the transaction's witness hash. While this is sufficient to
make the CScriptCheck() calls pure functions, this introduces
dependancies on the mempool calculating things such as the
PrecomputedTransactionData object, filling the CCoinsViewCache, etc
in the exact same way as ConnectBlock. I belive this is a reasonable
assumption, but should be noted carefully.
In a rather naive benchmark (reindex-chainstate up to block 284k
with cuckoocache always returning true for contains(),
-assumevalid=0 and a very large dbcache), this connected blocks
~1.7x faster.
2017-04-11 22:46:39 +02:00
|
|
|
InitScriptExecutionCache();
|
2013-12-14 13:51:11 +01:00
|
|
|
fPrintToDebugLog = false; // don't want to write to debug.log file
|
2015-03-13 17:25:34 +01:00
|
|
|
fCheckBlockIndex = true;
|
2015-06-30 21:39:49 +02:00
|
|
|
SelectParams(chainName);
|
2015-03-03 15:59:32 +01:00
|
|
|
noui_connect();
|
2015-03-12 09:34:42 +01:00
|
|
|
}
|
2015-03-03 15:59:32 +01:00
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BasicTestingSetup::~BasicTestingSetup()
|
|
|
|
{
|
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 23:28:26 +02:00
|
|
|
ECC_Stop();
|
2016-04-16 20:47:18 +02:00
|
|
|
g_connman.reset();
|
2015-03-12 09:34:42 +01:00
|
|
|
}
|
|
|
|
|
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();
|
2016-03-29 19:43:02 +02: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.
|
2016-04-16 20:47:18 +02:00
|
|
|
|
2016-03-29 19:43:02 +02:00
|
|
|
RegisterAllCoreRPCCommands(tableRPC);
|
2015-03-03 16:49:12 +01:00
|
|
|
ClearDatadirCache();
|
2017-06-07 21:03:17 +02:00
|
|
|
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::create_directories(pathTemp);
|
2017-08-01 21:17:40 +02:00
|
|
|
gArgs.ForceSetArg("-datadir", pathTemp.string());
|
2017-01-19 22:49:22 +01:00
|
|
|
|
|
|
|
// Note that because we don't bother running a scheduler thread here,
|
|
|
|
// callbacks via CValidationInterface are unreliable, but that's OK,
|
|
|
|
// our unit tests aren't testing multiple parts of the code at once.
|
|
|
|
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
|
|
|
|
|
2016-06-18 19:15:03 +02:00
|
|
|
mempool.setSanityCheck(1.0);
|
2012-11-10 00:09:57 +01:00
|
|
|
pblocktree = new CBlockTreeDB(1 << 20, true);
|
|
|
|
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
2014-09-24 03:19:04 +02:00
|
|
|
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
|
2017-07-07 01:57:20 +02:00
|
|
|
if (!LoadGenesisBlock(chainparams)) {
|
2017-08-01 23:02:10 +02:00
|
|
|
throw std::runtime_error("LoadGenesisBlock failed.");
|
2017-03-10 21:47:41 +01:00
|
|
|
}
|
2016-07-22 15:57:25 +02:00
|
|
|
{
|
|
|
|
CValidationState state;
|
2017-03-10 21:47:41 +01:00
|
|
|
if (!ActivateBestChain(state, chainparams)) {
|
|
|
|
throw std::runtime_error("ActivateBestChain failed.");
|
|
|
|
}
|
2016-07-22 15:57:25 +02:00
|
|
|
}
|
2012-12-01 23:04:14 +01:00
|
|
|
nScriptCheckThreads = 3;
|
|
|
|
for (int i=0; i < nScriptCheckThreads-1; i++)
|
2013-03-07 04:31:26 +01:00
|
|
|
threadGroup.create_thread(&ThreadScriptCheck);
|
2016-09-09 12:48:10 +02:00
|
|
|
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
|
2016-04-16 20:47:18 +02:00
|
|
|
connman = g_connman.get();
|
2013-11-18 01:25:17 +01:00
|
|
|
RegisterNodeSignals(GetNodeSignals());
|
2015-03-03 16:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::~TestingSetup()
|
|
|
|
{
|
|
|
|
UnregisterNodeSignals(GetNodeSignals());
|
2013-03-07 04:31:26 +01:00
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
2017-06-28 01:07:52 +02:00
|
|
|
GetMainSignals().FlushBackgroundCallbacks();
|
2017-01-19 22:49:22 +01:00
|
|
|
GetMainSignals().UnregisterBackgroundSignalScheduler();
|
2015-03-03 16:49:12 +01:00
|
|
|
UnloadBlockIndex();
|
2012-09-03 15:26:57 +02:00
|
|
|
delete pcoinsTip;
|
|
|
|
delete pcoinsdbview;
|
|
|
|
delete pblocktree;
|
2017-03-01 17:05:50 +01:00
|
|
|
fs::remove_all(pathTemp);
|
2015-03-03 16:49:12 +01:00
|
|
|
}
|
2011-10-03 22:14:13 +02:00
|
|
|
|
2015-03-03 15:59:32 +01:00
|
|
|
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
|
|
|
|
{
|
|
|
|
// 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);
|
2016-11-11 02:26:00 +01:00
|
|
|
coinbaseTxns.push_back(*b.vtx[0]);
|
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
|
|
|
|
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
|
|
|
|
{
|
2015-04-20 00:17:11 +02:00
|
|
|
const CChainParams& chainparams = Params();
|
2016-06-18 19:38:28 +02:00
|
|
|
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
|
2015-03-03 15:59:32 +01: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-11 02:34:17 +01:00
|
|
|
block.vtx.push_back(MakeTransactionRef(tx));
|
2015-03-03 15:59:32 +01:00
|
|
|
// IncrementExtraNonce creates a valid coinbase and merkleRoot
|
|
|
|
unsigned int extraNonce = 0;
|
|
|
|
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
|
|
|
|
|
2015-04-20 00:17:11 +02:00
|
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
2015-03-03 15:59:32 +01:00
|
|
|
|
2016-12-04 09:17:30 +01: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 15:59:32 +01:00
|
|
|
|
|
|
|
CBlock result = block;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-14 23:04:15 +01:00
|
|
|
|
2017-01-20 04:46:50 +01:00
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) {
|
2015-11-13 16:05:21 +01:00
|
|
|
CTransaction txn(tx);
|
2017-01-20 04:46:50 +01:00
|
|
|
return FromTx(txn);
|
2016-04-26 02:04:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-20 04:46:50 +01:00
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransaction &txn) {
|
2017-01-20 15:24:35 +01:00
|
|
|
return CTxMemPoolEntry(MakeTransactionRef(txn), nFee, nTime, nHeight,
|
|
|
|
spendsCoinbase, sigOpCost, lp);
|
2015-11-14 23:04:15 +01:00
|
|
|
}
|