Skip to content

Commit 3001cc6

Browse files
author
MarcoFalke
committed
Merge #13555: Tests: parameterize adjustment period in versionbits_computeblockversion
2c448d6 parameterize hard coded numbers referring to miner conf window (Jordan Baczuk) Pull request description: Replace hard coded values (eg. 2016) with `mainnetParams.nMinerConfirmationWindow` where appropriate. This parameterizes hard coded values in the unit test that refer to the `Miner Confirmation Window`, which currently is `2016`. This includes values not exactly 2016 but which were derived from it. Also changed `int` to `uint32_t` where appropriate to avoid compiler warnings. This makes one source of truth, and also helps people who might be adjusting this value in testing so the unit tests don't break. ACKs for commit 2c448d: Tree-SHA512: 9262e0b89c1baf7857b49fe2221b2b00f948f61317b321c4871a9182a86d6f8aadeb59d6b133e8a213cc9b31b4a417888fb1ad31caef16ccbbab1de33c4b8459
2 parents 2d1583e + 2c448d6 commit 3001cc6

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/test/versionbits_tests.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,35 +271,35 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
271271
// Before MedianTimePast of the chain has crossed nStartTime, the bit
272272
// should not be set.
273273
CBlockIndex *lastBlock = nullptr;
274-
lastBlock = firstChain.Mine(2016, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
274+
lastBlock = firstChain.Mine(mainnetParams.nMinerConfirmationWindow, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
275275
BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit), 0);
276276

277-
// Mine 2011 more blocks at the old time, and check that CBV isn't setting the bit yet.
278-
for (int i=1; i<2012; i++) {
279-
lastBlock = firstChain.Mine(2016+i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
277+
// Mine more blocks (4 less than the adjustment period) at the old time, and check that CBV isn't setting the bit yet.
278+
for (uint32_t i = 1; i < mainnetParams.nMinerConfirmationWindow - 4; i++) {
279+
lastBlock = firstChain.Mine(mainnetParams.nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
280280
// This works because VERSIONBITS_LAST_OLD_BLOCK_VERSION happens
281281
// to be 4, and the bit we're testing happens to be bit 28.
282282
BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit), 0);
283283
}
284284
// Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
285285
// CBV should still not yet set the bit.
286286
nTime = nStartTime;
287-
for (int i=2012; i<=2016; i++) {
288-
lastBlock = firstChain.Mine(2016+i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
287+
for (uint32_t i = mainnetParams.nMinerConfirmationWindow - 4; i <= mainnetParams.nMinerConfirmationWindow; i++) {
288+
lastBlock = firstChain.Mine(mainnetParams.nMinerConfirmationWindow + i, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
289289
BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit), 0);
290290
}
291291

292292
// Advance to the next period and transition to STARTED,
293-
lastBlock = firstChain.Mine(6048, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
293+
lastBlock = firstChain.Mine(mainnetParams.nMinerConfirmationWindow * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
294294
// so ComputeBlockVersion should now set the bit,
295295
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit)) != 0);
296296
// and should also be using the VERSIONBITS_TOP_BITS.
297297
BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
298298

299299
// Check that ComputeBlockVersion will set the bit until nTimeout
300300
nTime += 600;
301-
int blocksToMine = 4032; // test blocks for up to 2 time periods
302-
int nHeight = 6048;
301+
uint32_t blocksToMine = mainnetParams.nMinerConfirmationWindow * 2; // test blocks for up to 2 time periods
302+
uint32_t nHeight = mainnetParams.nMinerConfirmationWindow * 3;
303303
// These blocks are all before nTimeout is reached.
304304
while (nTime < nTimeout && blocksToMine > 0) {
305305
lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
@@ -313,7 +313,7 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
313313
nTime = nTimeout;
314314
// FAILED is only triggered at the end of a period, so CBV should be setting
315315
// the bit until the period transition.
316-
for (int i=0; i<2015; i++) {
316+
for (uint32_t i = 0; i < mainnetParams.nMinerConfirmationWindow - 1; i++) {
317317
lastBlock = firstChain.Mine(nHeight+1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
318318
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit)) != 0);
319319
nHeight += 1;
@@ -329,20 +329,20 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
329329

330330
// Mine one period worth of blocks, and check that the bit will be on for the
331331
// next period.
332-
lastBlock = secondChain.Mine(2016, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
332+
lastBlock = secondChain.Mine(mainnetParams.nMinerConfirmationWindow, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
333333
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit)) != 0);
334334

335335
// Mine another period worth of blocks, signaling the new bit.
336-
lastBlock = secondChain.Mine(4032, nTime, VERSIONBITS_TOP_BITS | (1<<bit)).Tip();
336+
lastBlock = secondChain.Mine(mainnetParams.nMinerConfirmationWindow * 2, nTime, VERSIONBITS_TOP_BITS | (1<<bit)).Tip();
337337
// After one period of setting the bit on each block, it should have locked in.
338338
// We keep setting the bit for one more period though, until activation.
339339
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit)) != 0);
340340

341341
// Now check that we keep mining the block until the end of this period, and
342342
// then stop at the beginning of the next period.
343-
lastBlock = secondChain.Mine(6047, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
344-
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit)) != 0);
345-
lastBlock = secondChain.Mine(6048, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
343+
lastBlock = secondChain.Mine((mainnetParams.nMinerConfirmationWindow * 3) - 1, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
344+
BOOST_CHECK((ComputeBlockVersion(lastBlock, mainnetParams) & (1 << bit)) != 0);
345+
lastBlock = secondChain.Mine(mainnetParams.nMinerConfirmationWindow * 3, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
346346
BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & (1<<bit), 0);
347347

348348
// Finally, verify that after a soft fork has activated, CBV no longer uses

0 commit comments

Comments
 (0)