@@ -84,13 +84,19 @@ class TxPriorityCompare
84
84
}
85
85
};
86
86
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)
88
88
{
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;
90
94
91
95
// Updating time can change work required on testnet:
92
96
if (consensusParams.fPowAllowMinDifficultyBlocks )
93
97
pblock->nBits = GetNextWorkRequired (pindexPrev, pblock, consensusParams);
98
+
99
+ return nNewTime - nOldTime;
94
100
}
95
101
96
102
CBlockTemplate* CreateNewBlock (const CScript& scriptPubKeyIn)
@@ -521,7 +527,9 @@ void static BitcoinMiner(const CChainParams& chainparams)
521
527
break ;
522
528
523
529
// 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.
525
533
if (chainparams.GetConsensus ().fPowAllowMinDifficultyBlocks )
526
534
{
527
535
// Changing pblock->nTime can change work required on testnet:
0 commit comments