mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 15:09:59 +01:00
Unit test doublespends in new blocks
As suggested by Greg Maxwell-- unit test to make sure a block with a double-spend in it doesn't pass validation if half of the double-spend is already in the memory pool (so full-blown transaction validation is skipped) when the block is received.
This commit is contained in:
committed by
Pieter Wuille
parent
17b11428c1
commit
517e6dd256
@@ -7,8 +7,12 @@
|
||||
#include "test_bitcoin.h"
|
||||
|
||||
#include "chainparams.h"
|
||||
#include "consensus/consensus.h"
|
||||
#include "consensus/validation.h"
|
||||
#include "key.h"
|
||||
#include "main.h"
|
||||
#include "miner.h"
|
||||
#include "pubkey.h"
|
||||
#include "random.h"
|
||||
#include "txdb.h"
|
||||
#include "ui_interface.h"
|
||||
@@ -28,20 +32,22 @@ CWallet* pwalletMain;
|
||||
extern bool fPrintToConsole;
|
||||
extern void noui_connect();
|
||||
|
||||
BasicTestingSetup::BasicTestingSetup()
|
||||
BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network)
|
||||
{
|
||||
ECC_Start();
|
||||
SetupEnvironment();
|
||||
fPrintToDebugLog = false; // don't want to write to debug.log file
|
||||
fCheckBlockIndex = true;
|
||||
SelectParams(CBaseChainParams::MAIN);
|
||||
SelectParams(network);
|
||||
noui_connect();
|
||||
}
|
||||
|
||||
BasicTestingSetup::~BasicTestingSetup()
|
||||
{
|
||||
ECC_Stop();
|
||||
}
|
||||
|
||||
TestingSetup::TestingSetup()
|
||||
TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network)
|
||||
{
|
||||
#ifdef ENABLE_WALLET
|
||||
bitdb.MakeMock();
|
||||
@@ -87,6 +93,51 @@ TestingSetup::~TestingSetup()
|
||||
boost::filesystem::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)
|
||||
{
|
||||
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()
|
||||
{
|
||||
}
|
||||
|
||||
void Shutdown(void* parg)
|
||||
{
|
||||
exit(0);
|
||||
|
||||
Reference in New Issue
Block a user