Skip to content

Commit 2f746c6

Browse files
committed
Merge pull request #6177
ef8dfe4 Prevent block.nTime from decreasing (Mark Friedenbach)
2 parents 149f96c + ef8dfe4 commit 2f746c6

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/miner.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

96102
CBlockTemplate* 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:

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainpar
3030
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
3131
/** Modify the extranonce in a block */
3232
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
33-
void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
33+
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
3434

3535
#endif // BITCOIN_MINER_H

0 commit comments

Comments
 (0)