@@ -372,18 +372,44 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
372
372
// Stop here if we needed to mine more blocks.
373
373
assert(nblocks == sizeof(blockinfo)/sizeof(*blockinfo));
374
374
375
+ auto nTime = pblocktemplate->block.nTime;
376
+ delete pblocktemplate;
377
+
375
378
// Set the clock to be just ahead of the last "mined" block, to ensure we satisfy the
376
379
// future timestamp soft fork rule.
377
- auto curTime = GetTime();
378
- OffsetClock::SetGlobal();
379
- OffsetClock::Instance()->Set(std::chrono::seconds(-curTime + pblocktemplate->block.nTime));
380
+ FixedClock::SetGlobal();
381
+ FixedClock::Instance()->Set(std::chrono::seconds(nTime));
380
382
383
+ // Just to make sure we can still make simple blocks
384
+ BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
385
+ BOOST_CHECK_EQUAL(pblocktemplate->block.nTime, nTime);
386
+
387
+ auto mtp = chainActive.Tip()->GetMedianTimePast();
381
388
delete pblocktemplate;
389
+ auto minTime = mtp + 1;
390
+ auto maxTime = mtp + MAX_FUTURE_BLOCK_TIME_MTP;
382
391
383
- // Just to make sure we can still make simple blocks
392
+ // Set the clock to be too far ahead of the MTP, violating the future timestamp rule.
393
+ FixedClock::Instance()->Set(std::chrono::seconds(maxTime + 1));
394
+
395
+ // This should succeed, and nTime should be clamped to the maximum consensus-valid value.
396
+ BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
397
+ BOOST_CHECK_EQUAL(pblocktemplate->block.nTime, maxTime);
398
+ delete pblocktemplate;
399
+
400
+ // Set the clock to be equal to the median-time-past, violating the rule that it must
401
+ // be after the MTP.
402
+ FixedClock::Instance()->Set(std::chrono::seconds(minTime - 1));
403
+
404
+ // This should succeed, and nTime should be clamped to the minimum consensus-valid value.
384
405
BOOST_CHECK(pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey));
406
+ BOOST_CHECK_EQUAL(pblocktemplate->block.nTime, minTime);
385
407
delete pblocktemplate;
386
408
409
+ auto curTime = GetTime();
410
+ OffsetClock::SetGlobal();
411
+ OffsetClock::Instance()->Set(std::chrono::seconds(-curTime + nTime));
412
+
387
413
TestMemPoolEntryHelper entry;
388
414
entry.nFee = 0;
389
415
entry.nHeight = 0;
0 commit comments