@@ -84,13 +84,19 @@ class TxPriorityCompare
8484 }
8585};
8686
87- void UpdateTime (CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
87+ int64_t UpdateTime (CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
8888{
89- pblock->nTime = std::max (pindexPrev->GetMedianTimePast ()+1 , GetAdjustedTime ());
89+ int64_t nOldTime = pblock->nTime ;
90+ int64_t nNewTime = std::max (pindexPrev->GetMedianTimePast ()+1 , GetAdjustedTime ());
91+
92+ if (nOldTime < nNewTime)
93+ pblock->nTime = nNewTime;
9094
9195 // Updating time can change work required on testnet:
9296 if (consensusParams.fPowAllowMinDifficultyBlocks )
9397 pblock->nBits = GetNextWorkRequired (pindexPrev, pblock, consensusParams);
98+
99+ return nNewTime - nOldTime;
94100}
95101
96102CBlockTemplate* CreateNewBlock (const CScript& scriptPubKeyIn)
@@ -521,7 +527,9 @@ void static BitcoinMiner(const CChainParams& chainparams)
521527 break ;
522528
523529 // Update nTime every few seconds
524- UpdateTime (pblock, chainparams.GetConsensus (), pindexPrev);
530+ if (UpdateTime (pblock, chainparams.GetConsensus (), pindexPrev) < 0 )
531+ break ; // Recreate the block if the clock has run backwards,
532+ // so that we can use the correct time.
525533 if (chainparams.GetConsensus ().fPowAllowMinDifficultyBlocks )
526534 {
527535 // Changing pblock->nTime can change work required on testnet:
0 commit comments