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:
2 files changed
+23
-7
No files matched your search
+16
-7
@@ -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 */
|
||||||
|
|||||||
Reference in new issue
Block a user