Skip to content

Commit e301401

Browse files
committed
test: add IsActiveAfter tests for versionbits
1 parent 60950f7 commit e301401

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/test/versionbits_tests.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
255255
}
256256

257257
struct BlockVersionTest : BasicTestingSetup {
258-
/** Check that ComputeBlockVersion will set the appropriate bit correctly */
258+
/** Check that ComputeBlockVersion will set the appropriate bit correctly
259+
* Also checks IsActiveAfter() behaviour */
259260
void check_computeblockversion(VersionBitsCache& versionbitscache, const Consensus::Params& params, Consensus::DeploymentPos dep)
260261
{
261262
// Clear the cache every time
@@ -279,6 +280,11 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
279280
if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE ||
280281
nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE)
281282
{
283+
if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE) {
284+
BOOST_CHECK(versionbitscache.IsActiveAfter(nullptr, params, dep));
285+
} else {
286+
BOOST_CHECK(!versionbitscache.IsActiveAfter(nullptr, params, dep));
287+
}
282288
BOOST_CHECK_EQUAL(min_activation_height, 0);
283289
BOOST_CHECK_EQUAL(nTimeout, Consensus::BIP9Deployment::NO_TIMEOUT);
284290
return;
@@ -311,8 +317,10 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
311317
// end of the first period by mining blocks at nTime == 0
312318
lastBlock = firstChain.Mine(period - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
313319
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
320+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
314321
lastBlock = firstChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
315322
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
323+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
316324
// then we'll keep mining at nStartTime...
317325
} else {
318326
// use a time 1s earlier than start time to check we stay DEFINED
@@ -321,18 +329,21 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
321329
// Start generating blocks before nStartTime
322330
lastBlock = firstChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
323331
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
332+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
324333

325334
// Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
326335
for (uint32_t i = 1; i < period - 4; i++) {
327336
lastBlock = firstChain.Mine(period + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
328337
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
338+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
329339
}
330340
// Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
331341
// CBV should still not yet set the bit.
332342
nTime = nStartTime;
333343
for (uint32_t i = period - 4; i <= period; i++) {
334344
lastBlock = firstChain.Mine(period + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
335345
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
346+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
336347
}
337348
// Next we will advance to the next period and transition to STARTED,
338349
}
@@ -342,6 +353,7 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
342353
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
343354
// and should also be using the VERSIONBITS_TOP_BITS.
344355
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
356+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
345357

346358
// Check that ComputeBlockVersion will set the bit until nTimeout
347359
nTime += 600;
@@ -352,6 +364,7 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
352364
lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
353365
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
354366
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
367+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
355368
blocksToMine--;
356369
nTime += 600;
357370
nHeight += 1;
@@ -366,6 +379,7 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
366379
while (nHeight % period != 0) {
367380
lastBlock = firstChain.Mine(nHeight+1, nTime - 1, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
368381
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
382+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
369383
nHeight += 1;
370384
}
371385

@@ -374,11 +388,13 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
374388
for (uint32_t i = 0; i < period - 1; i++) {
375389
lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
376390
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
391+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
377392
nHeight += 1;
378393
}
379394
// The next block should trigger no longer setting the bit.
380395
lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
381396
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
397+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
382398
}
383399

384400
// On a new chain:
@@ -390,29 +406,34 @@ void check_computeblockversion(VersionBitsCache& versionbitscache, const Consens
390406
// next period.
391407
lastBlock = secondChain.Mine(period, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
392408
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
409+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
393410

394411
// Mine another period worth of blocks, signaling the new bit.
395412
lastBlock = secondChain.Mine(period * 2, nTime, VERSIONBITS_TOP_BITS | (1<<bit)).Tip();
396413
// After one period of setting the bit on each block, it should have locked in.
397414
// We keep setting the bit for one more period though, until activation.
398415
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
416+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
399417

400418
// Now check that we keep mining the block until the end of this period, and
401419
// then stop at the beginning of the next period.
402420
lastBlock = secondChain.Mine((period * 3) - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
403421
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
422+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
404423
lastBlock = secondChain.Mine(period * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
405424

406425
if (lastBlock->nHeight + 1 < min_activation_height) {
407426
// check signalling continues while min_activation_height is not reached
408427
lastBlock = secondChain.Mine(min_activation_height - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
409428
BOOST_CHECK((versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
429+
BOOST_CHECK(!versionbitscache.IsActiveAfter(lastBlock, params, dep));
410430
// then reach min_activation_height, which was already REQUIRE'd to start a new period
411431
lastBlock = secondChain.Mine(min_activation_height, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
412432
}
413433

414434
// Check that we don't signal after activation
415435
BOOST_CHECK_EQUAL(versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit), 0);
436+
BOOST_CHECK(versionbitscache.IsActiveAfter(lastBlock, params, dep));
416437
}
417438
}; // struct BlockVersionTest
418439

0 commit comments

Comments
 (0)