@@ -257,18 +257,18 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
257
257
}
258
258
259
259
/* * Check that ComputeBlockVersion will set the appropriate bit correctly */
260
- static void check_computeblockversion (const Consensus::Params& params, Consensus::DeploymentPos dep)
260
+ static void check_computeblockversion (VersionBitsCache& versionbitscache, const Consensus::Params& params, Consensus::DeploymentPos dep)
261
261
{
262
- // This implicitly uses g_versionbitscache, so clear it every time
263
- g_versionbitscache .Clear ();
262
+ // Clear the cache everytime
263
+ versionbitscache .Clear ();
264
264
265
265
int64_t bit = params.vDeployments [dep].bit ;
266
266
int64_t nStartTime = params.vDeployments [dep].nStartTime ;
267
267
int64_t nTimeout = params.vDeployments [dep].nTimeout ;
268
268
int min_activation_height = params.vDeployments [dep].min_activation_height ;
269
269
270
270
// should not be any signalling for first block
271
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (nullptr , params), VERSIONBITS_TOP_BITS);
271
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (nullptr , params), VERSIONBITS_TOP_BITS);
272
272
273
273
// always/never active deployments shouldn't need to be tested further
274
274
if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE ||
@@ -288,7 +288,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
288
288
// Check min_activation_height is on a retarget boundary
289
289
BOOST_REQUIRE_EQUAL (min_activation_height % params.nMinerConfirmationWindow , 0U );
290
290
291
- const uint32_t bitmask{g_versionbitscache .Mask (params, dep)};
291
+ const uint32_t bitmask{versionbitscache .Mask (params, dep)};
292
292
BOOST_CHECK_EQUAL (bitmask, uint32_t {1 } << bit);
293
293
294
294
// In the first chain, test that the bit is set by CBV until it has failed.
@@ -307,38 +307,38 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
307
307
// earlier time, so will transition from DEFINED to STARTED at the
308
308
// end of the first period by mining blocks at nTime == 0
309
309
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow - 1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
310
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
310
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
311
311
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
312
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
312
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
313
313
// then we'll keep mining at nStartTime...
314
314
} else {
315
315
// use a time 1s earlier than start time to check we stay DEFINED
316
316
--nTime;
317
317
318
318
// Start generating blocks before nStartTime
319
319
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
320
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
320
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
321
321
322
322
// Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
323
323
for (uint32_t i = 1 ; i < params.nMinerConfirmationWindow - 4 ; i++) {
324
324
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
325
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
325
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
326
326
}
327
327
// Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
328
328
// CBV should still not yet set the bit.
329
329
nTime = nStartTime;
330
330
for (uint32_t i = params.nMinerConfirmationWindow - 4 ; i <= params.nMinerConfirmationWindow ; i++) {
331
331
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
332
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
332
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
333
333
}
334
334
// Next we will advance to the next period and transition to STARTED,
335
335
}
336
336
337
337
lastBlock = firstChain.Mine (params.nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
338
338
// so ComputeBlockVersion should now set the bit,
339
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
339
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
340
340
// and should also be using the VERSIONBITS_TOP_BITS.
341
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
341
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
342
342
343
343
// Check that ComputeBlockVersion will set the bit until nTimeout
344
344
nTime += 600 ;
@@ -347,8 +347,8 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
347
347
// These blocks are all before nTimeout is reached.
348
348
while (nTime < nTimeout && blocksToMine > 0 ) {
349
349
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
350
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
351
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
350
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
351
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
352
352
blocksToMine--;
353
353
nTime += 600 ;
354
354
nHeight += 1 ;
@@ -362,20 +362,20 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
362
362
// finish the last period before we start timing out
363
363
while (nHeight % params.nMinerConfirmationWindow != 0 ) {
364
364
lastBlock = firstChain.Mine (nHeight+1 , nTime - 1 , VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
365
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
365
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
366
366
nHeight += 1 ;
367
367
}
368
368
369
369
// FAILED is only triggered at the end of a period, so CBV should be setting
370
370
// the bit until the period transition.
371
371
for (uint32_t i = 0 ; i < params.nMinerConfirmationWindow - 1 ; i++) {
372
372
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
373
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
373
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
374
374
nHeight += 1 ;
375
375
}
376
376
// The next block should trigger no longer setting the bit.
377
377
lastBlock = firstChain.Mine (nHeight+1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
378
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
378
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
379
379
}
380
380
381
381
// On a new chain:
@@ -386,30 +386,30 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
386
386
// Mine one period worth of blocks, and check that the bit will be on for the
387
387
// next period.
388
388
lastBlock = secondChain.Mine (params.nMinerConfirmationWindow , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
389
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
389
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
390
390
391
391
// Mine another period worth of blocks, signaling the new bit.
392
392
lastBlock = secondChain.Mine (params.nMinerConfirmationWindow * 2 , nTime, VERSIONBITS_TOP_BITS | (1 <<bit)).Tip ();
393
393
// After one period of setting the bit on each block, it should have locked in.
394
394
// We keep setting the bit for one more period though, until activation.
395
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
395
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
396
396
397
397
// Now check that we keep mining the block until the end of this period, and
398
398
// then stop at the beginning of the next period.
399
399
lastBlock = secondChain.Mine ((params.nMinerConfirmationWindow * 3 ) - 1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
400
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
400
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
401
401
lastBlock = secondChain.Mine (params.nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
402
402
403
403
if (lastBlock->nHeight + 1 < min_activation_height) {
404
404
// check signalling continues while min_activation_height is not reached
405
405
lastBlock = secondChain.Mine (min_activation_height - 1 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
406
- BOOST_CHECK ((g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
406
+ BOOST_CHECK ((versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit)) != 0 );
407
407
// then reach min_activation_height, which was already REQUIRE'd to start a new period
408
408
lastBlock = secondChain.Mine (min_activation_height, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
409
409
}
410
410
411
411
// Check that we don't signal after activation
412
- BOOST_CHECK_EQUAL (g_versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
412
+ BOOST_CHECK_EQUAL (versionbitscache .ComputeBlockVersion (lastBlock, params) & (1 << bit), 0 );
413
413
}
414
414
415
415
BOOST_AUTO_TEST_CASE (versionbits_computeblockversion)
@@ -429,7 +429,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
429
429
const uint32_t dep_mask{g_versionbitscache.Mask (chainParams->GetConsensus (), dep)};
430
430
BOOST_CHECK (!(chain_all_vbits & dep_mask));
431
431
chain_all_vbits |= dep_mask;
432
- check_computeblockversion (chainParams->GetConsensus (), dep);
432
+ check_computeblockversion (g_versionbitscache, chainParams->GetConsensus (), dep);
433
433
}
434
434
}
435
435
@@ -439,7 +439,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
439
439
ArgsManager args;
440
440
args.ForceSetArg (" -vbparams" , " testdummy:1199145601:1230767999" ); // January 1, 2008 - December 31, 2008
441
441
const auto chainParams = CreateChainParams (args, CBaseChainParams::REGTEST);
442
- check_computeblockversion (chainParams->GetConsensus (), Consensus::DEPLOYMENT_TESTDUMMY);
442
+ check_computeblockversion (g_versionbitscache, chainParams->GetConsensus (), Consensus::DEPLOYMENT_TESTDUMMY);
443
443
}
444
444
445
445
{
@@ -449,7 +449,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
449
449
ArgsManager args;
450
450
args.ForceSetArg (" -vbparams" , " testdummy:1199145601:1230767999:403200" ); // January 1, 2008 - December 31, 2008, min act height 403200
451
451
const auto chainParams = CreateChainParams (args, CBaseChainParams::REGTEST);
452
- check_computeblockversion (chainParams->GetConsensus (), Consensus::DEPLOYMENT_TESTDUMMY);
452
+ check_computeblockversion (g_versionbitscache, chainParams->GetConsensus (), Consensus::DEPLOYMENT_TESTDUMMY);
453
453
}
454
454
}
455
455
0 commit comments