|
|
|
// Copyright (c) 2011-2016 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "test_bitcoin.h"
|
|
|
|
|
|
|
|
#include "chainparams.h"
|
|
|
|
#include "consensus/consensus.h"
|
|
|
|
#include "consensus/validation.h"
|
|
|
|
#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.
10 years ago
|
|
|
#include "key.h"
|
|
|
|
#include "validation.h"
|
|
|
|
#include "miner.h"
|
|
|
|
#include "net_processing.h"
|
|
|
|
#include "pubkey.h"
|
|
|
|
#include "random.h"
|
|
|
|
#include "txdb.h"
|
|
|
|
#include "txmempool.h"
|
|
|
|
#include "ui_interface.h"
|
|
|
|
#include "rpc/server.h"
|
|
|
|
#include "rpc/register.h"
|
|
|
|
#include "script/sigcache.h"
|
|
|
|
|
|
|
|
#include "test/testutil.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
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.
10 years ago
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
|
|
|
uint256 insecure_rand_seed = GetRandHash();
|
|
|
|
FastRandomContext insecure_rand_ctx(insecure_rand_seed);
|
|
|
|
|
|
|
|
extern bool fPrintToConsole;
|
|
|
|
extern void noui_connect();
|
|
|
|
|
|
|
|
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
|
|
|
{
|
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.
10 years ago
|
|
|
ECC_Start();
|
|
|
|
SetupEnvironment();
|
|
|
|
SetupNetworking();
|
|
|
|
InitSignatureCache();
|
|
|
|
fPrintToDebugLog = false; // don't want to write to debug.log file
|
|
|
|
fCheckBlockIndex = true;
|
|
|
|
SelectParams(chainName);
|
|
|
|
noui_connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
10 years ago
|
|
|
ECC_Stop();
|
|
|
|
g_connman.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
|
|
|
|
{
|
|
|
|
const CChainParams& chainparams = Params();
|
|
|
|
// 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);
|
|
|
|
ClearDatadirCache();
|
|
|
|
pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(insecure_randrange(100000)));
|
|
|
|
fs::create_directories(pathTemp);
|
|
|
|
ForceSetArg("-datadir", pathTemp.string());
|
|
|
|
mempool.setSanityCheck(1.0);
|
|
|
|
pblocktree = new CBlockTreeDB(1 << 20, true);
|
|
|
|
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
|
|
|
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
|
|
|
|
if (!InitBlockIndex(chainparams)) {
|
|
|
|
throw std::runtime_error("InitBlockIndex failed.");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
CValidationState state;
|
|
|
|
if (!ActivateBestChain(state, chainparams)) {
|
|
|
|
throw std::runtime_error("ActivateBestChain failed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nScriptCheckThreads = 3;
|
|
|
|
for (int i=0; i < nScriptCheckThreads-1; i++)
|
|
|
|
threadGroup.create_thread(&ThreadScriptCheck);
|
|
|
|
g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
|
|
|
|
connman = g_connman.get();
|
|
|
|
RegisterNodeSignals(GetNodeSignals());
|
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::~TestingSetup()
|
|
|
|
{
|
|
|
|
UnregisterNodeSignals(GetNodeSignals());
|
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
|
|
|
UnloadBlockIndex();
|
|
|
|
delete pcoinsTip;
|
|
|
|
delete pcoinsdbview;
|
|
|
|
delete pblocktree;
|
|
|
|
fs::remove_all(pathTemp);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
const CChainParams& chainparams = Params();
|
|
|
|
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).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(MakeTransactionRef(tx));
|
|
|
|
// IncrementExtraNonce creates a valid coinbase and merkleRoot
|
|
|
|
unsigned int extraNonce = 0;
|
|
|
|
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
|
|
|
|
|
|
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
|
|
|
|
|
|
|
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
|
|
|
|
ProcessNewBlock(chainparams, shared_pblock, true, NULL);
|
|
|
|
|
|
|
|
CBlock result = block;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) {
|
|
|
|
CTransaction txn(tx);
|
|
|
|
return FromTx(txn);
|
|
|
|
}
|
|
|
|
|
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransaction &txn) {
|
|
|
|
return CTxMemPoolEntry(MakeTransactionRef(txn), nFee, nTime, nHeight,
|
|
|
|
spendsCoinbase, sigOpCost, lp);
|
|
|
|
}
|