Skip to content

Commit 63879f0

Browse files
committed
tests: pull ComputeBlockVersion test into its own function
The intent here is to allow checking ComputeBlockVersion behaviour with each deployment, rather than only testdummy on mainnet. This commit does the trivial refactoring component of that change.
1 parent de4d3ba commit 63879f0

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

src/test/versionbits_tests.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,15 @@ BOOST_AUTO_TEST_CASE(versionbits_test)
242242
}
243243
}
244244

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)
246247
{
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();
251250

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;
256254

257255
assert(nStartTime < nTimeout);
258256

@@ -267,40 +265,40 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
267265
// Before MedianTimePast of the chain has crossed nStartTime, the bit
268266
// should not be set.
269267
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);
272270

273271
// 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();
276274
// This works because VERSIONBITS_LAST_OLD_BLOCK_VERSION happens
277275
// 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);
279277
}
280278
// Now mine 5 more blocks at the start time -- MTP should not have passed yet, so
281279
// CBV should still not yet set the bit.
282280
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);
286284
}
287285

288286
// 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();
290288
// 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);
292290
// 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);
294292

295293
// Check that ComputeBlockVersion will set the bit until nTimeout
296294
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;
299297
// These blocks are all before nTimeout is reached.
300298
while (nTime < nTimeout && blocksToMine > 0) {
301299
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);
304302
blocksToMine--;
305303
nTime += 600;
306304
nHeight += 1;
@@ -309,14 +307,14 @@ BOOST_AUTO_TEST_CASE(versionbits_computeblockversion)
309307
nTime = nTimeout;
310308
// FAILED is only triggered at the end of a period, so CBV should be setting
311309
// 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++) {
313311
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);
315313
nHeight += 1;
316314
}
317315
// The next block should trigger no longer setting the bit.
318316
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);
320318

321319
// On a new chain:
322320
// 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)
325323

326324
// Mine one period worth of blocks, and check that the bit will be on for the
327325
// 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);
330328

331329
// 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();
333331
// After one period of setting the bit on each block, it should have locked in.
334332
// 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);
336334

337335
// Now check that we keep mining the block until the end of this period, and
338336
// 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);
343341

344342
// Finally, verify that after a soft fork has activated, CBV no longer uses
345343
// VERSIONBITS_LAST_OLD_BLOCK_VERSION.
346344
//BOOST_CHECK_EQUAL(ComputeBlockVersion(lastBlock, mainnetParams) & VERSIONBITS_TOP_MASK, VERSIONBITS_TOP_BITS);
347345
}
348346

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+
}
349353

350354
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)