2014-03-18 10:11:00 +01:00
|
|
|
// Copyright (c) 2011-2013 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.
|
|
|
|
|
2011-08-02 17:34:23 +02:00
|
|
|
#define BOOST_TEST_MODULE Bitcoin Test Suite
|
2013-04-13 07:13:08 +02:00
|
|
|
|
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"
|
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"
|
2011-10-12 01:50:06 +02:00
|
|
|
#include "main.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "miner.h"
|
|
|
|
#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"
|
|
|
|
#include "ui_interface.h"
|
2012-11-28 21:58:41 +01:00
|
|
|
#include "util.h"
|
2013-11-29 16:04:29 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2015-02-03 21:09:47 +01:00
|
|
|
#include "wallet/db.h"
|
|
|
|
#include "wallet/wallet.h"
|
2013-11-29 16:04:29 +01:00
|
|
|
#endif
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
#include <boost/thread.hpp>
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
|
2012-01-05 03:40:52 +01:00
|
|
|
CWallet* pwalletMain;
|
|
|
|
|
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
|
|
|
{
|
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();
|
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();
|
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
|
|
|
{
|
2013-11-29 16:04:29 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2012-05-22 21:51:13 +02:00
|
|
|
bitdb.MakeMock();
|
2013-11-29 16:04:29 +01:00
|
|
|
#endif
|
2015-03-03 16:49:12 +01:00
|
|
|
ClearDatadirCache();
|
2012-11-28 21:58:41 +01:00
|
|
|
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
|
|
|
|
boost::filesystem::create_directories(pathTemp);
|
|
|
|
mapArgs["-datadir"] = pathTemp.string();
|
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);
|
2013-01-30 21:43:36 +01:00
|
|
|
InitBlockIndex();
|
2013-11-29 16:04:29 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2012-05-22 21:51:13 +02:00
|
|
|
bool fFirstRun;
|
|
|
|
pwalletMain = new CWallet("wallet.dat");
|
|
|
|
pwalletMain->LoadWallet(fFirstRun);
|
2014-10-20 03:17:01 +02:00
|
|
|
RegisterValidationInterface(pwalletMain);
|
2013-11-29 16:04:29 +01:00
|
|
|
#endif
|
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);
|
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();
|
2013-11-29 16:04:29 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2015-03-03 16:49:12 +01:00
|
|
|
UnregisterValidationInterface(pwalletMain);
|
2012-01-05 03:40:52 +01:00
|
|
|
delete pwalletMain;
|
|
|
|
pwalletMain = NULL;
|
2013-11-29 16:04:29 +01:00
|
|
|
#endif
|
2015-03-03 16:49:12 +01:00
|
|
|
UnloadBlockIndex();
|
2012-09-03 15:26:57 +02:00
|
|
|
delete pcoinsTip;
|
|
|
|
delete pcoinsdbview;
|
|
|
|
delete pblocktree;
|
2013-11-29 16:04:29 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2012-05-22 21:51:13 +02:00
|
|
|
bitdb.Flush(true);
|
2015-03-03 16:49:12 +01:00
|
|
|
bitdb.Reset();
|
2013-11-29 16:04:29 +01:00
|
|
|
#endif
|
2012-11-28 21:58:41 +01:00
|
|
|
boost::filesystem::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);
|
|
|
|
coinbaseTxns.push_back(b.vtx[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
CBlockTemplate *pblocktemplate = CreateNewBlock(scriptPubKey);
|
|
|
|
CBlock& block = pblocktemplate->block;
|
|
|
|
|
|
|
|
// Replace mempool-selected txns with just coinbase plus passed-in txns:
|
|
|
|
block.vtx.resize(1);
|
|
|
|
BOOST_FOREACH(const CMutableTransaction& tx, txns)
|
|
|
|
block.vtx.push_back(tx);
|
|
|
|
// IncrementExtraNonce creates a valid coinbase and merkleRoot
|
|
|
|
unsigned int extraNonce = 0;
|
|
|
|
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
|
|
|
|
|
|
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params(CBaseChainParams::REGTEST).GetConsensus())) ++block.nNonce;
|
|
|
|
|
|
|
|
CValidationState state;
|
|
|
|
ProcessNewBlock(state, NULL, &block, true, NULL);
|
|
|
|
|
|
|
|
CBlock result = block;
|
|
|
|
delete pblocktemplate;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-07-31 20:00:38 +02:00
|
|
|
void Shutdown(void* parg)
|
|
|
|
{
|
2011-09-27 20:16:07 +02:00
|
|
|
exit(0);
|
2011-07-31 20:00:38 +02:00
|
|
|
}
|
2012-06-14 09:41:11 +02:00
|
|
|
|
|
|
|
void StartShutdown()
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2014-03-25 07:53:21 +01:00
|
|
|
bool ShutdownRequested()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|