Refactor TestChain100Setup to allow different test chain setups

Allows testing of features which depend on BIP9 deployments
This commit is contained in:
Alexander Block 2018-07-18 12:06:56 +02:00
parent ad31dbbd73
commit 9674be8f96
2 changed files with 47 additions and 16 deletions

View File

@ -100,12 +100,12 @@ TestingSetup::~TestingSetup()
boost::filesystem::remove_all(pathTemp); boost::filesystem::remove_all(pathTemp);
} }
TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST) TestChainSetup::TestChainSetup(int blockCount) : 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()) << OP_CHECKSIG; CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
for (int i = 0; i < COINBASE_MATURITY; i++) for (int i = 0; i < blockCount; i++)
{ {
std::vector<CMutableTransaction> noTxns; std::vector<CMutableTransaction> noTxns;
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey); CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
@ -118,7 +118,25 @@ TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
// scriptPubKey, and try to add it to the current chain. // scriptPubKey, and try to add it to the current chain.
// //
CBlock CBlock
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey) TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
{
const CChainParams& chainparams = Params();
auto block = CreateBlock(txns, scriptPubKey);
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
ProcessNewBlock(chainparams, shared_pblock, true, NULL);
CBlock result = block;
return result;
}
CBlock TestChainSetup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey)
{
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
return CreateAndProcessBlock(txns, scriptPubKey);
}
CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey); std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
@ -134,14 +152,17 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce; 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; CBlock result = block;
return result; return result;
} }
TestChain100Setup::~TestChain100Setup() CBlock TestChainSetup::CreateBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey)
{
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
return CreateBlock(txns, scriptPubKey);
}
TestChainSetup::~TestChainSetup()
{ {
} }

View File

@ -43,24 +43,34 @@ class CBlock;
struct CMutableTransaction; struct CMutableTransaction;
class CScript; class CScript;
// struct TestChainSetup : public TestingSetup
// Testing fixture that pre-creates a {
// 100-block REGTEST-mode block chain TestChainSetup(int blockCount);
// ~TestChainSetup();
struct TestChain100Setup : public TestingSetup {
TestChain100Setup();
// 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 CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey); const CScript& scriptPubKey);
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
~TestChain100Setup(); const CKey& scriptKey);
CBlock CreateBlock(const std::vector<CMutableTransaction>& txns,
const CScript& scriptPubKey);
CBlock CreateBlock(const std::vector<CMutableTransaction>& txns,
const CKey& scriptKey);
std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
CKey coinbaseKey; // private/public key needed to spend coinbase transactions CKey coinbaseKey; // private/public key needed to spend coinbase transactions
}; };
//
// Testing fixture that pre-creates a
// 100-block REGTEST-mode block chain
//
struct TestChain100Setup : public TestChainSetup {
TestChain100Setup() : TestChainSetup(100) {}
};
class CTxMemPoolEntry; class CTxMemPoolEntry;
class CTxMemPool; class CTxMemPool;