Skip to content

Commit 92735e4

Browse files
committed
Merge #19775: test: Activate segwit in TestChain100Setup
fad84b7 test: Activate segwit in TestChain100Setup (MarcoFalke) fa11ff2 test: Pass empty tx pool to block assembler (MarcoFalke) fa96574 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 * bitcoin/bitcoin#19704 (comment) * ... ACKs for top commit: jnewbery: utACK fad84b7 Tree-SHA512: 64cf16a59656d49e022b603f3b06441ceae35a33a4253b4382bc8a89a56e08ad5412c8fa734d0fc7b58586f40ea6d57b348a3b4838bc6890a41ae2ec3902e378
2 parents 8d6224f + fad84b7 commit 92735e4

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

src/test/util/setup_common.cpp

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -196,58 +196,43 @@ TestingSetup::~TestingSetup()
196196

197197
TestChain100Setup::TestChain100Setup()
198198
{
199-
// CreateAndProcessBlock() does not support building SegWit blocks, so don't activate in these tests.
200-
// TODO: fix the code to support SegWit blocks.
201-
gArgs.ForceSetArg("-segwitheight", "432");
202-
// Need to recreate chainparams
203-
SelectParams(CBaseChainParams::REGTEST);
204-
205199
// Generate a 100-block chain:
206200
coinbaseKey.MakeNewKey(true);
207-
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
208-
for (int i = 0; i < COINBASE_MATURITY; i++)
209-
{
201+
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
202+
for (int i = 0; i < COINBASE_MATURITY; i++) {
210203
std::vector<CMutableTransaction> noTxns;
211204
CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
212205
m_coinbase_txns.push_back(b.vtx[0]);
213206
}
214207
}
215208

216-
// Create a new block with just given transactions, coinbase paying to
217-
// scriptPubKey, and try to add it to the current chain.
218209
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
219210
{
220211
const CChainParams& chainparams = Params();
221-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
222-
CBlock& block = pblocktemplate->block;
212+
CTxMemPool empty_pool;
213+
CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
223214

224-
// Replace mempool-selected txns with just coinbase plus passed-in txns:
225-
block.vtx.resize(1);
226-
for (const CMutableTransaction& tx : txns)
215+
Assert(block.vtx.size() == 1);
216+
for (const CMutableTransaction& tx : txns) {
227217
block.vtx.push_back(MakeTransactionRef(tx));
228-
// IncrementExtraNonce creates a valid coinbase and merkleRoot
229-
{
230-
LOCK(cs_main);
231-
unsigned int extraNonce = 0;
232-
IncrementExtraNonce(&block, ::ChainActive().Tip(), extraNonce);
233218
}
219+
RegenerateCommitments(block);
234220

235221
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
236222

237223
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
238224
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
239225

240-
CBlock result = block;
241-
return result;
226+
return block;
242227
}
243228

244229
TestChain100Setup::~TestChain100Setup()
245230
{
246231
gArgs.ForceSetArg("-segwitheight", "0");
247232
}
248233

249-
250-
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) {
234+
CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction& tx)
235+
{
251236
return FromTx(MakeTransactionRef(tx));
252237
}
253238

src/test/util/setup_common.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,15 +102,16 @@ class CBlock;
102102
struct CMutableTransaction;
103103
class CScript;
104104

105-
//
106-
// Testing fixture that pre-creates a
107-
// 100-block REGTEST-mode block chain
108-
//
105+
/**
106+
* Testing fixture that pre-creates a 100-block REGTEST-mode block chain
107+
*/
109108
struct TestChain100Setup : public RegTestingSetup {
110109
TestChain100Setup();
111110

112-
// Create a new block with just given transactions, coinbase paying to
113-
// scriptPubKey, and try to add it to the current chain.
111+
/**
112+
* Create a new block with just given transactions, coinbase paying to
113+
* scriptPubKey, and try to add it to the current chain.
114+
*/
114115
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
115116
const CScript& scriptPubKey);
116117

0 commit comments

Comments
 (0)