@@ -242,17 +242,15 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
242
242
}
243
243
}
244
244
245
- BOOST_AUTO_TEST_CASE (versionbits_computeblockversion)
245
+ /* * Check that ComputeBlockVersion will set the appropriate bit correctly */
246
+ static void check_computeblockversion (const Consensus::Params& params, Consensus::DeploymentPos dep)
246
247
{
247
- // Check that ComputeBlockVersion will set the appropriate bit correctly
248
- // on mainnet.
249
- const auto chainParams = CreateChainParams (*m_node.args , CBaseChainParams::MAIN);
250
- const Consensus::Params &mainnetParams = chainParams->GetConsensus ();
248
+ // This implicitly uses versionbitscache, so clear it every time
249
+ versionbitscache.Clear ();
251
250
252
- // Use the TESTDUMMY deployment for testing purposes.
253
- int64_t bit = mainnetParams.vDeployments [Consensus::DEPLOYMENT_TESTDUMMY].bit ;
254
- int64_t nStartTime = mainnetParams.vDeployments [Consensus::DEPLOYMENT_TESTDUMMY].nStartTime ;
255
- int64_t nTimeout = mainnetParams.vDeployments [Consensus::DEPLOYMENT_TESTDUMMY].nTimeout ;
251
+ int64_t bit = params.vDeployments [dep].bit ;
252
+ int64_t nStartTime = params.vDeployments [dep].nStartTime ;
253
+ int64_t nTimeout = params.vDeployments [dep].nTimeout ;
256
254
257
255
assert (nStartTime < nTimeout);
258
256
@@ -267,40 +265,40 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
267
265
// Before MedianTimePast of the chain has crossed nStartTime, the bit
268
266
// should not be set.
269
267
CBlockIndex *lastBlock = nullptr ;
270
- lastBlock = firstChain.Mine (mainnetParams .nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
271
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit), 0 );
268
+ lastBlock = firstChain.Mine (params .nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
269
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & (1 <<bit), 0 );
272
270
273
271
// Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
274
- for (uint32_t i = 1 ; i < mainnetParams .nMinerConfirmationWindow - 4 ; i++) {
275
- lastBlock = firstChain.Mine (mainnetParams .nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
272
+ for (uint32_t i = 1 ; i < params .nMinerConfirmationWindow - 4 ; i++) {
273
+ lastBlock = firstChain.Mine (params .nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
276
274
// This works because VERSIONBITS_LAST_OLD_BLOCK_VERSION happens
277
275
// to be 4, and the bit we're testing happens to be bit 28.
278
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit), 0 );
276
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & (1 <<bit), 0 );
279
277
}
280
278
// Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
281
279
// CBV should still not yet set the bit.
282
280
nTime = nStartTime;
283
- for (uint32_t i = mainnetParams .nMinerConfirmationWindow - 4 ; i <= mainnetParams .nMinerConfirmationWindow ; i++) {
284
- lastBlock = firstChain.Mine (mainnetParams .nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
285
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit), 0 );
281
+ for (uint32_t i = params .nMinerConfirmationWindow - 4 ; i <= params .nMinerConfirmationWindow ; i++) {
282
+ lastBlock = firstChain.Mine (params .nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
283
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & (1 <<bit), 0 );
286
284
}
287
285
288
286
// Advance to the next period and transition to STARTED,
289
- lastBlock = firstChain.Mine (mainnetParams .nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
287
+ lastBlock = firstChain.Mine (params .nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
290
288
// so ComputeBlockVersion should now set the bit,
291
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit)) != 0 );
289
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 <<bit)) != 0 );
292
290
// and should also be using the VERSIONBITS_TOP_BITS.
293
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
291
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
294
292
295
293
// Check that ComputeBlockVersion will set the bit until nTimeout
296
294
nTime += 600 ;
297
- uint32_t blocksToMine = mainnetParams .nMinerConfirmationWindow * 2 ; // test blocks for up to 2 time periods
298
- uint32_t nHeight = mainnetParams .nMinerConfirmationWindow * 3 ;
295
+ uint32_t blocksToMine = params .nMinerConfirmationWindow * 2 ; // test blocks for up to 2 time periods
296
+ uint32_t nHeight = params .nMinerConfirmationWindow * 3 ;
299
297
// These blocks are all before nTimeout is reached.
300
298
while (nTime < nTimeout && blocksToMine > 0 ) {
301
299
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
302
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit)) != 0 );
303
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
300
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 <<bit)) != 0 );
301
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
304
302
blocksToMine--;
305
303
nTime += 600 ;
306
304
nHeight += 1 ;
@@ -309,14 +307,14 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
309
307
nTime = nTimeout;
310
308
// FAILED is only triggered at the end of a period, so CBV should be setting
311
309
// the bit until the period transition.
312
- for (uint32_t i = 0 ; i < mainnetParams .nMinerConfirmationWindow - 1 ; i++) {
310
+ for (uint32_t i = 0 ; i < params .nMinerConfirmationWindow - 1 ; i++) {
313
311
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
314
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit)) != 0 );
312
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 <<bit)) != 0 );
315
313
nHeight += 1 ;
316
314
}
317
315
// The next block should trigger no longer setting the bit.
318
316
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
319
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit), 0 );
317
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & (1 <<bit), 0 );
320
318
321
319
// On a new chain:
322
320
// verify that the bit will be set after lock-in, and then stop being set
@@ -325,26 +323,32 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
325
323
326
324
// Mine one period worth of blocks, and check that the bit will be on for the
327
325
// next period.
328
- lastBlock = secondChain.Mine (mainnetParams .nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
329
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit)) != 0 );
326
+ lastBlock = secondChain.Mine (params .nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
327
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 <<bit)) != 0 );
330
328
331
329
// Mine another period worth of blocks, signaling the new bit.
332
- lastBlock = secondChain.Mine (mainnetParams .nMinerConfirmationWindow * 2 , nTime, VERSIONBITS_TOP_BITS | (1 <<bit)).Tip ();
330
+ lastBlock = secondChain.Mine (params .nMinerConfirmationWindow * 2 , nTime, VERSIONBITS_TOP_BITS | (1 <<bit)).Tip ();
333
331
// After one period of setting the bit on each block, it should have locked in.
334
332
// We keep setting the bit for one more period though, until activation.
335
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit)) != 0 );
333
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 <<bit)) != 0 );
336
334
337
335
// Now check that we keep mining the block until the end of this period, and
338
336
// then stop at the beginning of the next period.
339
- lastBlock = secondChain.Mine ((mainnetParams .nMinerConfirmationWindow * 3 ) - 1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
340
- BOOST_CHECK ((ComputeBlockVersion (lastBlock, mainnetParams ) & (1 << bit)) != 0 );
341
- lastBlock = secondChain.Mine (mainnetParams .nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
342
- BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, mainnetParams ) & (1 <<bit), 0 );
337
+ lastBlock = secondChain.Mine ((params .nMinerConfirmationWindow * 3 ) - 1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
338
+ BOOST_CHECK ((ComputeBlockVersion (lastBlock, params ) & (1 << bit)) != 0 );
339
+ lastBlock = secondChain.Mine (params .nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
340
+ BOOST_CHECK_EQUAL (ComputeBlockVersion (lastBlock, params ) & (1 <<bit), 0 );
343
341
344
342
// Finally, verify that after a soft fork has activated, CBV no longer uses
345
343
// VERSIONBITS_LAST_OLD_BLOCK_VERSION.
346
344
// BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
347
345
}
348
346
347
+ BOOST_AUTO_TEST_CASE (versionbits_computeblockversion)
348
+ {
349
+ // Use the TESTDUMMY deployment for testing purposes.
350
+ const auto chainParams = CreateChainParams (*m_node.args , CBaseChainParams::MAIN);
351
+ check_computeblockversion (chainParams->GetConsensus (), Consensus::DEPLOYMENT_TESTDUMMY);
352
+ }
349
353
350
354
BOOST_AUTO_TEST_SUITE_END ()
0 commit comments