Merge #19775: test: Activate segwit in TestChain100Setup

fad84b7e14 test: Activate segwit in TestChain100Setup (MarcoFalke)
fa11ff2980 test: Pass empty tx pool to block assembler (MarcoFalke)
fa96574b0d test: Move doxygen comment to header (MarcoFalke)

Pull request description:

  This fixes not only a TODO in the code, but also prevents a never ending source of uninitialized reads. E.g.

  * #18376
  * https://github.com/bitcoin/bitcoin/pull/19704#issuecomment-678259092
  * ...

ACKs for top commit:
  jnewbery:
    utACK fad84b7e14

Tree-SHA512: 64cf16a59656d49e022b603f3b06441ceae35a33a4253b4382bc8a89a56e08ad5412c8fa734d0fc7b58586f40ea6d57b348a3b4838bc6890a41ae2ec3902e378
This commit is contained in:
fanquake 2020-08-26 12:42:34 +08:00
commit 92735e45ba
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
2 changed files with 17 additions and 31 deletions

View file

@ -196,49 +196,34 @@ TestingSetup::~TestingSetup()
TestChain100Setup::TestChain100Setup() TestChain100Setup::TestChain100Setup()
{ {
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
// TODO: fix the code to support SegWit blocks.
gArgs.ForceSetArg("-segwitheight", "432");
// Need to recreate chainparams
SelectParams(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 < COINBASE_MATURITY; i++) {
{
std::vector<CMutableTransaction> noTxns; std::vector<CMutableTransaction> noTxns;
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey); CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
m_coinbase_txns.push_back(b.vtx[0]); m_coinbase_txns.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) CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
{ {
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(scriptPubKey); CTxMemPool empty_pool;
CBlock& block = pblocktemplate->block; CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
// Replace mempool-selected txns with just coinbase plus passed-in txns: Assert(block.vtx.size() == 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));
// IncrementExtraNonce creates a valid coinbase and merkleRoot
{
LOCK(cs_main);
unsigned int extraNonce = 0;
IncrementExtraNonce(&block, ::ChainActive().Tip(), extraNonce);
} }
RegenerateCommitments(block);
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); std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr); Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
CBlock result = block; return block;
return result;
} }
TestChain100Setup::~TestChain100Setup() TestChain100Setup::~TestChain100Setup()
@ -246,8 +231,8 @@ TestChain100Setup::~TestChain100Setup()
gArgs.ForceSetArg("-segwitheight", "0"); gArgs.ForceSetArg("-segwitheight", "0");
} }
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx)
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) { {
return FromTx(MakeTransactionRef(tx)); return FromTx(MakeTransactionRef(tx));
} }

View file

@ -102,15 +102,16 @@ class CBlock;
struct CMutableTransaction; struct CMutableTransaction;
class CScript; class CScript;
// /**
// Testing fixture that pre-creates a * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
// 100-block REGTEST-mode block chain */
//
struct TestChain100Setup : public RegTestingSetup { struct TestChain100Setup : public RegTestingSetup {
TestChain100Setup(); TestChain100Setup();
// Create a new block with just given transactions, coinbase paying to /**
// scriptPubKey, and try to add it to the current chain. * Create a new block with just given transactions, coinbase paying to
* 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);