Skip to content

Commit

Permalink
validation: Use MAX_TIMEWARP constant as testnet4 timewarp defense delta
Browse files Browse the repository at this point in the history
The value is equal to MAX_FUTURE_BLOCK_TIME.
  • Loading branch information
fjahr committed Aug 9, 2024
1 parent 4b2fad5 commit 92c1d7d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ const std::vector<std::string> CHECKLEVEL_DOC {
* */
static constexpr int PRUNE_LOCK_BUFFER{10};

/**
* Maximum number of seconds that the timestamp of the first
* block of a difficulty adjustment period is allowed to
* be earlier than the last block of the previous period (BIP94).
*/
static constexpr int64_t MAX_TIMEWARP{MAX_FUTURE_BLOCK_TIME};

/**
* If the timestamp of the last block in a difficulty period is set
* MAX_FUTURE_BLOCK_TIME seconds in the future, an honest miner should
* be able to mine the first block using the current time. This is not
* a consensus rule, but changing MAX_TIMEWARP should be done with caution.
*/
static_assert(MAX_FUTURE_BLOCK_TIME <= MAX_TIMEWARP);

GlobalMutex g_best_block_mutex;
std::condition_variable g_best_block_cv;
uint256 g_best_block;
Expand Down Expand Up @@ -4188,7 +4203,7 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
// Check timestamp for the first block of each difficulty adjustment
// interval, except the genesis block.
if (nHeight % consensusParams.DifficultyAdjustmentInterval() == 0) {
if (block.GetBlockTime() < pindexPrev->GetBlockTime() - 60 * 60 * 2) {
if (block.GetBlockTime() < pindexPrev->GetBlockTime() - MAX_TIMEWARP) {
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-timewarp-attack", "block's timestamp is too early on diff adjustment block");
}
}
Expand Down

0 comments on commit 92c1d7d

Please sign in to comment.