2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2015-03-19 15:15:08 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2015-04-05 23:56:58 +02:00
|
|
|
#define BOOST_TEST_MODULE Dash Test Suite
|
2015-03-19 15:15:08 +01:00
|
|
|
|
2016-03-03 20:20:32 +01:00
|
|
|
#include "test_dash.h"
|
2015-03-03 16:49:12 +01:00
|
|
|
|
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"
|
2017-08-09 02:19:06 +02:00
|
|
|
#include "validation.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "miner.h"
|
2017-08-09 02:19:06 +02:00
|
|
|
#include "net_processing.h"
|
2015-03-03 15:59:32 +01:00
|
|
|
#include "pubkey.h"
|
2015-04-05 00:58:10 +02:00
|
|
|
#include "random.h"
|
2015-03-19 15:15:08 +01:00
|
|
|
#include "txdb.h"
|
2015-11-14 23:04:15 +01:00
|
|
|
#include "txmempool.h"
|
2015-03-19 15:15:08 +01:00
|
|
|
#include "ui_interface.h"
|
2016-03-31 10:55:06 +02:00
|
|
|
#include "rpc/server.h"
|
|
|
|
#include "rpc/register.h"
|
2015-03-19 15:15:08 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
2015-02-03 21:09:47 +01:00
|
|
|
#include "wallet/db.h"
|
|
|
|
#include "wallet/wallet.h"
|
2015-03-19 15:15:08 +01:00
|
|
|
#endif
|
|
|
|
|
2016-03-14 11:18:02 +01:00
|
|
|
#include "test/testutil.h"
|
|
|
|
|
2015-03-19 15:15:08 +01:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2015-04-05 00:58:10 +02:00
|
|
|
#include <boost/thread.hpp>
|
2015-03-19 15:15:08 +01:00
|
|
|
|
2015-04-16 16:20:01 +02:00
|
|
|
CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
|
2015-03-19 15:15:08 +01:00
|
|
|
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
std::unique_ptr<CConnman> g_connman;
|
|
|
|
|
2015-03-19 15:15:08 +01:00
|
|
|
extern bool fPrintToConsole;
|
|
|
|
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();
|
2015-03-19 15:15:08 +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-19 15:15:08 +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();
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +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-31 10:55:06 +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.
|
|
|
|
RegisterAllCoreRPCCommands(tableRPC);
|
2015-03-19 15:15:08 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
bitdb.MakeMock();
|
2016-03-31 10:55:06 +02:00
|
|
|
RegisterWalletRPCCommands(tableRPC);
|
2015-03-19 15:15:08 +01:00
|
|
|
#endif
|
2015-03-03 16:49:12 +01:00
|
|
|
ClearDatadirCache();
|
2015-03-19 15:15:08 +01:00
|
|
|
pathTemp = GetTempPath() / strprintf("test_dash_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
|
|
|
|
boost::filesystem::create_directories(pathTemp);
|
|
|
|
mapArgs["-datadir"] = pathTemp.string();
|
|
|
|
pblocktree = new CBlockTreeDB(1 << 20, true);
|
|
|
|
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
|
2015-04-05 00:58:10 +02:00
|
|
|
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
|
2015-04-17 14:40:24 +02:00
|
|
|
InitBlockIndex(chainparams);
|
2015-03-19 15:15:08 +01:00
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
bool fFirstRun;
|
|
|
|
pwalletMain = new CWallet("wallet.dat");
|
|
|
|
pwalletMain->LoadWallet(fFirstRun);
|
2015-04-05 00:58:10 +02:00
|
|
|
RegisterValidationInterface(pwalletMain);
|
2015-03-19 15:15:08 +01:00
|
|
|
#endif
|
|
|
|
nScriptCheckThreads = 3;
|
|
|
|
for (int i=0; i < nScriptCheckThreads-1; i++)
|
|
|
|
threadGroup.create_thread(&ThreadScriptCheck);
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
g_connman = std::unique_ptr<CConnman>(new CConnman());
|
|
|
|
connman = g_connman.get();
|
2015-03-19 15:15:08 +01:00
|
|
|
RegisterNodeSignals(GetNodeSignals());
|
2015-03-03 16:49:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TestingSetup::~TestingSetup()
|
|
|
|
{
|
|
|
|
UnregisterNodeSignals(GetNodeSignals());
|
2015-03-19 15:15:08 +01:00
|
|
|
threadGroup.interrupt_all();
|
|
|
|
threadGroup.join_all();
|
|
|
|
#ifdef ENABLE_WALLET
|
2015-03-03 16:49:12 +01:00
|
|
|
UnregisterValidationInterface(pwalletMain);
|
2015-03-19 15:15:08 +01:00
|
|
|
delete pwalletMain;
|
|
|
|
pwalletMain = NULL;
|
|
|
|
#endif
|
2015-03-03 16:49:12 +01:00
|
|
|
UnloadBlockIndex();
|
2015-03-19 15:15:08 +01:00
|
|
|
delete pcoinsTip;
|
|
|
|
delete pcoinsdbview;
|
|
|
|
delete pblocktree;
|
|
|
|
#ifdef ENABLE_WALLET
|
|
|
|
bitdb.Flush(true);
|
2015-03-03 16:49:12 +01:00
|
|
|
bitdb.Reset();
|
2015-03-19 15:15:08 +01:00
|
|
|
#endif
|
|
|
|
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]);
|
2015-03-19 15:15:08 +01:00
|
|
|
}
|
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();
|
2015-04-17 14:19:52 +02:00
|
|
|
CBlockTemplate *pblocktemplate = CreateNewBlock(chainparams, 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);
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
2017-08-02 20:35:04 +02:00
|
|
|
ProcessNewBlock(chainparams, &block, true, NULL, NULL);
|
2015-03-19 15:15:08 +01:00
|
|
|
|
2015-03-03 15:59:32 +01:00
|
|
|
CBlock result = block;
|
|
|
|
delete pblocktemplate;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestChain100Setup::~TestChain100Setup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-11-14 23:04:15 +01:00
|
|
|
|
|
|
|
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(CMutableTransaction &tx, CTxMemPool *pool) {
|
2015-11-13 16:05:21 +01: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
|
|
|
|
CAmount inChainValue = hasNoDependencies ? txn.GetValueOut() : 0;
|
|
|
|
|
|
|
|
return CTxMemPoolEntry(txn, nFee, nTime, dPriority, nHeight,
|
2015-12-04 21:01:22 +01:00
|
|
|
hasNoDependencies, inChainValue, spendsCoinbase, sigOpCount, lp);
|
2015-11-14 23:04:15 +01:00
|
|
|
}
|
2015-03-19 15:15:08 +01:00
|
|
|
|
|
|
|
void Shutdown(void* parg)
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartShutdown()
|
|
|
|
{
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ShutdownRequested()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|