Skip to content

Commit

Permalink
Merge pull request #6177
Browse files Browse the repository at this point in the history
ef8dfe4 Prevent block.nTime from decreasing (Mark Friedenbach)
  • Loading branch information
laanwj committed Aug 6, 2015
2 parents 149f96c + ef8dfe4 commit 2f746c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,19 @@ class TxPriorityCompare
}
};

void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
{
pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
int64_t nOldTime = pblock->nTime;
int64_t nNewTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());

if (nOldTime < nNewTime)
pblock->nTime = nNewTime;

// Updating time can change work required on testnet:
if (consensusParams.fPowAllowMinDifficultyBlocks)
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, consensusParams);

return nNewTime - nOldTime;
}

CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
Expand Down Expand Up @@ -521,7 +527,9 @@ void static BitcoinMiner(const CChainParams& chainparams)
break;

// Update nTime every few seconds
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
if (UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev) < 0)
break; // Recreate the block if the clock has run backwards,
// so that we can use the correct time.
if (chainparams.GetConsensus().fPowAllowMinDifficultyBlocks)
{
// Changing pblock->nTime can change work required on testnet:
Expand Down
2 changes: 1 addition & 1 deletion src/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainpar
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
/** Modify the extranonce in a block */
void IncrementExtraNonce(CBlock* pblock, CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
void UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

#endif // BITCOIN_MINER_H

0 comments on commit 2f746c6

Please sign in to comment.