Skip to content

Commit 0713548

Browse files
committed
refactor: add GetMinimumTime() helper
Before bip94 there was an assumption that the minimum permitted timestamp is GetMedianTimePast() + 1. This commit splits a helper function out of UpdateTime() to obtain the minimum time in a way that takes the timewarp attack rule into account.
1 parent b432e36 commit 0713548

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/node/miner.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,25 @@
2828
#include <utility>
2929

3030
namespace node {
31-
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
32-
{
33-
int64_t nOldTime = pblock->nTime;
34-
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
3531

32+
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
33+
{
34+
int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
3635
// Height of block to be mined.
3736
const int height{pindexPrev->nHeight + 1};
38-
if (height % consensusParams.DifficultyAdjustmentInterval() == 0) {
39-
nNewTime = std::max<int64_t>(nNewTime, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
37+
// Account for BIP94 timewarp rule on all networks. This makes future
38+
// activation safer.
39+
if (height % difficulty_adjustment_interval == 0) {
40+
min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
4041
}
42+
return min_time;
43+
}
44+
45+
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
46+
{
47+
int64_t nOldTime = pblock->nTime;
48+
int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
49+
TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
4150

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

src/node/miner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ class BlockAssembler
211211
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
212212
};
213213

214+
/**
215+
* Get the minimum time a miner should use in the next block. This always
216+
* accounts for the BIP94 timewarp rule, so does not necessarily reflect the
217+
* consensus limit.
218+
*/
219+
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval);
220+
214221
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
215222

216223
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */

0 commit comments

Comments
 (0)