mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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.
This commit is contained in:
parent
b432e36742
commit
0713548137
2 changed files with 23 additions and 7 deletions
|
@ -28,16 +28,25 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
|
|
||||||
|
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval)
|
||||||
|
{
|
||||||
|
int64_t min_time{pindexPrev->GetMedianTimePast() + 1};
|
||||||
|
// Height of block to be mined.
|
||||||
|
const int height{pindexPrev->nHeight + 1};
|
||||||
|
// Account for BIP94 timewarp rule on all networks. This makes future
|
||||||
|
// activation safer.
|
||||||
|
if (height % difficulty_adjustment_interval == 0) {
|
||||||
|
min_time = std::max<int64_t>(min_time, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
|
||||||
|
}
|
||||||
|
return min_time;
|
||||||
|
}
|
||||||
|
|
||||||
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
|
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
|
||||||
{
|
{
|
||||||
int64_t nOldTime = pblock->nTime;
|
int64_t nOldTime = pblock->nTime;
|
||||||
int64_t nNewTime{std::max<int64_t>(pindexPrev->GetMedianTimePast() + 1, TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
|
int64_t nNewTime{std::max<int64_t>(GetMinimumTime(pindexPrev, consensusParams.DifficultyAdjustmentInterval()),
|
||||||
|
TicksSinceEpoch<std::chrono::seconds>(NodeClock::now()))};
|
||||||
// Height of block to be mined.
|
|
||||||
const int height{pindexPrev->nHeight + 1};
|
|
||||||
if (height % consensusParams.DifficultyAdjustmentInterval() == 0) {
|
|
||||||
nNewTime = std::max<int64_t>(nNewTime, pindexPrev->GetBlockTime() - MAX_TIMEWARP);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nOldTime < nNewTime) {
|
if (nOldTime < nNewTime) {
|
||||||
pblock->nTime = nNewTime;
|
pblock->nTime = nNewTime;
|
||||||
|
|
|
@ -211,6 +211,13 @@ private:
|
||||||
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
|
void SortForBlock(const CTxMemPool::setEntries& package, std::vector<CTxMemPool::txiter>& sortedEntries);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the minimum time a miner should use in the next block. This always
|
||||||
|
* accounts for the BIP94 timewarp rule, so does not necessarily reflect the
|
||||||
|
* consensus limit.
|
||||||
|
*/
|
||||||
|
int64_t GetMinimumTime(const CBlockIndex* pindexPrev, const int64_t difficulty_adjustment_interval);
|
||||||
|
|
||||||
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
|
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);
|
||||||
|
|
||||||
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
|
/** Update an old GenerateCoinbaseCommitment from CreateNewBlock after the block txs have changed */
|
||||||
|
|
Loading…
Add table
Reference in a new issue