MCPcopy Create free account
hub / github.com/ElementsProject/elements / check_computeblockversion

Function check_computeblockversion

src/test/versionbits_tests.cpp:260–413  ·  view source on GitHub ↗

Check that ComputeBlockVersion will set the appropriate bit correctly */

Source from the content-addressed store, hash-verified

258
259/** Check that ComputeBlockVersion will set the appropriate bit correctly */
260static void check_computeblockversion(const Consensus::Params& params, Consensus::DeploymentPos dep)
261{
262 // This implicitly uses g_versionbitscache, so clear it every time
263 g_versionbitscache.Clear();
264
265 int64_t bit = params.vDeployments[dep].bit;
266 int64_t nStartTime = params.vDeployments[dep].nStartTime;
267 int64_t nTimeout = params.vDeployments[dep].nTimeout;
268 int min_activation_height = params.vDeployments[dep].min_activation_height;
269
270 // should not be any signalling for first block
271 BOOST_CHECK_EQUAL(g_versionbitscache.ComputeBlockVersion(nullptr, params), VERSIONBITS_TOP_BITS);
272
273 // always/never active deployments shouldn't need to be tested further
274 if (nStartTime == Consensus::BIP9Deployment::ALWAYS_ACTIVE ||
275 nStartTime == Consensus::BIP9Deployment::NEVER_ACTIVE)
276 {
277 BOOST_CHECK_EQUAL(min_activation_height, 0);
278 return;
279 }
280
281 BOOST_REQUIRE(nStartTime < nTimeout);
282 BOOST_REQUIRE(nStartTime >= 0);
283 BOOST_REQUIRE(nTimeout <= std::numeric_limits<uint32_t>::max() || nTimeout == Consensus::BIP9Deployment::NO_TIMEOUT);
284 BOOST_REQUIRE(0 <= bit && bit < 32);
285 // Make sure that no deployment tries to set an invalid bit.
286 BOOST_REQUIRE(((1 << bit) & VERSIONBITS_TOP_MASK) == 0);
287 BOOST_REQUIRE(min_activation_height >= 0);
288 // Check min_activation_height is on a retarget boundary
289 BOOST_REQUIRE_EQUAL(min_activation_height % params.nMinerConfirmationWindow, 0U);
290
291 const uint32_t bitmask{g_versionbitscache.Mask(params, dep)};
292 BOOST_CHECK_EQUAL(bitmask, uint32_t{1} << bit);
293
294 // In the first chain, test that the bit is set by CBV until it has failed.
295 // In the second chain, test the bit is set by CBV while STARTED and
296 // LOCKED-IN, and then no longer set while ACTIVE.
297 VersionBitsTester firstChain, secondChain;
298
299 int64_t nTime = nStartTime;
300
301 const CBlockIndex *lastBlock = nullptr;
302
303 // Before MedianTimePast of the chain has crossed nStartTime, the bit
304 // should not be set.
305 if (nTime == 0) {
306 // since CBlockIndex::nTime is uint32_t we can't represent any
307 // earlier time, so will transition from DEFINED to STARTED at the
308 // end of the first period by mining blocks at nTime == 0
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);
311 lastBlock = firstChain.Mine(params.nMinerConfirmationWindow, nTime, VERSIONBITS_LAST_OLD_BLOCK_VERSION).Tip();
312 BOOST_CHECK((g_versionbitscache.ComputeBlockVersion(lastBlock, params) & (1 << bit)) != 0);
313 // then we'll keep mining at nStartTime...
314 } else {
315 // use a time 1s earlier than start time to check we stay DEFINED
316 --nTime;
317

Callers 1

BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 4

ComputeBlockVersionMethod · 0.80
ClearMethod · 0.45
MaskMethod · 0.45
TipMethod · 0.45

Tested by

no test coverage detected