@@ -257,18 +257,18 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
257257}
258258
259259/* * 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)
261261{
262- // This implicitly uses g_versionbitscache, so clear it every time
263- g_versionbitscache .Clear ();
262+ // Clear the cache everytime
263+ versionbitscache .Clear ();
264264
265265 int64_t bit = params.vDeployments [dep].bit ;
266266 int64_t nStartTime = params.vDeployments [dep].nStartTime ;
267267 int64_t nTimeout = params.vDeployments [dep].nTimeout ;
268268 int min_activation_height = params.vDeployments [dep].min_activation_height ;
269269
270270 // 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);
272272
273273 // always/never active deployments shouldn't need to be tested further
274274 if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE ||
@@ -288,7 +288,7 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
288288 // Check min_activation_height is on a retarget boundary
289289 BOOST_REQUIRE_EQUAL (min_activation_height % params.nMinerConfirmationWindow , 0U );
290290
291- const uint32_t bitmask{g_versionbitscache .Mask (params, dep)};
291+ const uint32_t bitmask{versionbitscache .Mask (params, dep)};
292292 BOOST_CHECK_EQUAL (bitmask, uint32_t {1 } << bit);
293293
294294 // 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
307307 // earlier time, so will transition from DEFINED to STARTED at the
308308 // end of the first period by mining blocks at nTime == 0
309309 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 );
311311 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 );
313313 // then we'll keep mining at nStartTime...
314314 } else {
315315 // use a time 1s earlier than start time to check we stay DEFINED
316316 --nTime;
317317
318318 // Start generating blocks before nStartTime
319319 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 );
321321
322322 // Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
323323 for (uint32_t i = 1 ; i < params.nMinerConfirmationWindow - 4 ; i++) {
324324 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 );
326326 }
327327 // Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
328328 // CBV should still not yet set the bit.
329329 nTime = nStartTime;
330330 for (uint32_t i = params.nMinerConfirmationWindow - 4 ; i <= params.nMinerConfirmationWindow ; i++) {
331331 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 );
333333 }
334334 // Next we will advance to the next period and transition to STARTED,
335335 }
336336
337337 lastBlock = firstChain.Mine (params.nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
338338 // 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 );
340340 // 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);
342342
343343 // Check that ComputeBlockVersion will set the bit until nTimeout
344344 nTime += 600 ;
@@ -347,8 +347,8 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
347347 // These blocks are all before nTimeout is reached.
348348 while (nTime < nTimeout && blocksToMine > 0 ) {
349349 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);
352352 blocksToMine--;
353353 nTime += 600 ;
354354 nHeight += 1 ;
@@ -362,20 +362,20 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
362362 // finish the last period before we start timing out
363363 while (nHeight % params.nMinerConfirmationWindow != 0 ) {
364364 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 );
366366 nHeight += 1 ;
367367 }
368368
369369 // FAILED is only triggered at the end of a period, so CBV should be setting
370370 // the bit until the period transition.
371371 for (uint32_t i = 0 ; i < params.nMinerConfirmationWindow - 1 ; i++) {
372372 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 );
374374 nHeight += 1 ;
375375 }
376376 // The next block should trigger no longer setting the bit.
377377 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 );
379379 }
380380
381381 // On a new chain:
@@ -386,30 +386,30 @@ static void check_computeblockversion(const Consensus::Params& params, Consensus
386386 // Mine one period worth of blocks, and check that the bit will be on for the
387387 // next period.
388388 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 );
390390
391391 // Mine another period worth of blocks, signaling the new bit.
392392 lastBlock = secondChain.Mine (params.nMinerConfirmationWindow * 2 , nTime, VERSIONBITS_TOP_BITS | (1 <<bit)).Tip ();
393393 // After one period of setting the bit on each block, it should have locked in.
394394 // 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 );
396396
397397 // Now check that we keep mining the block until the end of this period, and
398398 // then stop at the beginning of the next period.
399399 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 );
401401 lastBlock = secondChain.Mine (params.nMinerConfirmationWindow * 3 , nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
402402
403403 if (lastBlock->nHeight + 1 < min_activation_height) {
404404 // check signalling continues while min_activation_height is not reached
405405 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 );
407407 // then reach min_activation_height, which was already REQUIRE'd to start a new period
408408 lastBlock = secondChain.Mine (min_activation_height, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip ();
409409 }
410410
411411 // 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 );
413413}
414414
415415BOOST_AUTO_TEST_CASE (versionbits_computeblockversion)
@@ -429,7 +429,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
429429 const uint32_t dep_mask{g_versionbitscache.Mask (chainParams->GetConsensus (), dep)};
430430 BOOST_CHECK (!(chain_all_vbits & dep_mask));
431431 chain_all_vbits |= dep_mask;
432- check_computeblockversion (chainParams->GetConsensus (), dep);
432+ check_computeblockversion (g_versionbitscache, chainParams->GetConsensus (), dep);
433433 }
434434 }
435435
@@ -439,7 +439,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
439439 ArgsManager args;
440440 args.ForceSetArg (" -vbparams" , " testdummy:1199145601:1230767999" ); // January 1, 2008 - December 31, 2008
441441 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);
443443 }
444444
445445 {
@@ -449,7 +449,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
449449 ArgsManager args;
450450 args.ForceSetArg (" -vbparams" , " testdummy:1199145601:1230767999:403200" ); // January 1, 2008 - December 31, 2008, min act height 403200
451451 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);
453453 }
454454}
455455
0 commit comments