Changeset View
Changeset View
Standalone View
Standalone View
src/test/test_bitcoin.cpp
Show All 14 Lines | |||||
#include "miner.h" | #include "miner.h" | ||||
#include "net_processing.h" | #include "net_processing.h" | ||||
#include "pubkey.h" | #include "pubkey.h" | ||||
#include "random.h" | #include "random.h" | ||||
#include "rpc/register.h" | #include "rpc/register.h" | ||||
#include "rpc/server.h" | #include "rpc/server.h" | ||||
#include "script/scriptcache.h" | #include "script/scriptcache.h" | ||||
#include "script/sigcache.h" | #include "script/sigcache.h" | ||||
#include "script/standard.h" | |||||
#include "txdb.h" | #include "txdb.h" | ||||
#include "txmempool.h" | #include "txmempool.h" | ||||
#include "ui_interface.h" | #include "ui_interface.h" | ||||
#include "validation.h" | #include "validation.h" | ||||
#include "test/testutil.h" | #include "test/testutil.h" | ||||
#include <atomic> | #include <atomic> | ||||
▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
TestChain100Setup::TestChain100Setup() | TestChain100Setup::TestChain100Setup() | ||||
: TestingSetup(CBaseChainParams::REGTEST) { | : TestingSetup(CBaseChainParams::REGTEST) { | ||||
// Generate a 100-block chain: | // Generate a 100-block chain: | ||||
coinbaseKey.MakeNewKey(true); | coinbaseKey.MakeNewKey(true); | ||||
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) | CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) | ||||
<< OP_CHECKSIG; | << OP_CHECKSIG; | ||||
for (int i = 0; i < COINBASE_MATURITY; i++) { | |||||
// Create a p2pkh coinbase | |||||
std::vector<CMutableTransaction> noTxns; | std::vector<CMutableTransaction> noTxns; | ||||
CBlock b1 = CreateAndProcessBlock(noTxns, scriptPubKey); | |||||
coinbaseTxns.push_back(*b1.vtx[0]); | |||||
// Create a p2sh coinbase that can fail NULLDUMMY (Don't put the null in | |||||
// the txn). This is for testing consensus vs non-standard rules. | |||||
CKey dummyKey; | |||||
dummyKey.MakeNewKey(true); | |||||
CScript scriptPubKeyNullDummy = | |||||
CScript() << OP_1 << ToByteVector(coinbaseKey.GetPubKey()) | |||||
<< ToByteVector(dummyKey.GetPubKey()) << OP_2 | |||||
<< OP_CHECKMULTISIG; | |||||
CScript p2sh_scriptPubKeyNullDummy = | |||||
GetScriptForDestination(CScriptID(scriptPubKeyNullDummy)); | |||||
CBlock b2 = CreateAndProcessBlock(noTxns, p2sh_scriptPubKeyNullDummy); | |||||
coinbaseTxns.push_back(*b2.vtx[0]); | |||||
deadalnix: I don't think this really warrant to modify the test framework. | |||||
// Mature the coinbases | |||||
for (int i = 0; i < COINBASE_MATURITY; i++) { | |||||
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey); | CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey); | ||||
coinbaseTxns.push_back(*b.vtx[0]); | coinbaseTxns.push_back(*b.vtx[0]); | ||||
} | } | ||||
} | } | ||||
// | // | ||||
// Create a new block with just given transactions, coinbase paying to | // Create a new block with just given transactions, coinbase paying to | ||||
// scriptPubKey, and try to add it to the current chain. | // scriptPubKey, and try to add it to the current chain. | ||||
// | // | ||||
CBlock TestChain100Setup::CreateAndProcessBlock( | CBlock TestChain100Setup::CreateAndProcessBlock( | ||||
const std::vector<CMutableTransaction> &txns, const CScript &scriptPubKey) { | const std::vector<CMutableTransaction> &txns, const CScript &scriptPubKey) { | ||||
const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
std::unique_ptr<CBlockTemplate> pblocktemplate = | std::unique_ptr<CBlockTemplate> pblocktemplate = | ||||
BlockAssembler(config).CreateNewBlock(scriptPubKey); | BlockAssembler(config).CreateNewBlock(scriptPubKey); | ||||
CBlock &block = pblocktemplate->block; | CBlock &block = pblocktemplate->block; | ||||
// Replace mempool-selected txns with just coinbase plus passed-in txns: | // Replace mempool-selected txns with just coinbase plus passed-in txns: | ||||
block.vtx.resize(1); | block.vtx.resize(1); | ||||
for (const CMutableTransaction &tx : txns) { | for (const CMutableTransaction &tx : txns) { | ||||
block.vtx.push_back(MakeTransactionRef(tx)); | block.vtx.push_back(MakeTransactionRef(tx)); | ||||
} | } | ||||
// Order transactions by canonical order | |||||
std::sort(std::begin(block.vtx) + 1, std::end(block.vtx), | |||||
[](const std::shared_ptr<const CTransaction> &txa, | |||||
const std::shared_ptr<const CTransaction> &txb) -> bool { | |||||
return txa->GetId() < txb->GetId(); | |||||
}); | |||||
// IncrementExtraNonce creates a valid coinbase and merkleRoot | // IncrementExtraNonce creates a valid coinbase and merkleRoot | ||||
unsigned int extraNonce = 0; | unsigned int extraNonce = 0; | ||||
IncrementExtraNonce(config, &block, chainActive.Tip(), extraNonce); | IncrementExtraNonce(config, &block, chainActive.Tip(), extraNonce); | ||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, config)) { | while (!CheckProofOfWork(block.GetHash(), block.nBits, config)) { | ||||
++block.nNonce; | ++block.nNonce; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 95 Lines • Show Last 20 Lines |
I don't think this really warrant to modify the test framework.