Skip to content

Commit 6bfa260

Browse files
committed
testnet: Add timewarp attack prevention for Testnet4
1 parent 0100907 commit 6bfa260

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/validation.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4182,6 +4182,18 @@ static bool ContextualCheckBlockHeader(const CBlockHeader& block, BlockValidatio
41824182
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
41834183
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-too-old", "block's timestamp is too early");
41844184

4185+
// Testnet4 only: Check timestamp against prev for difficulty-adjustment
4186+
// blocks to prevent timewarp attacks (see https://github.com/bitcoin/bitcoin/pull/15482).
4187+
if (consensusParams.enforce_BIP94) {
4188+
// Check timestamp for the first block of each difficulty adjustment
4189+
// interval, except the genesis block.
4190+
if (nHeight % consensusParams.DifficultyAdjustmentInterval() == 0) {
4191+
if (block.GetBlockTime() < pindexPrev->GetBlockTime() - 60 * 60 * 2) {
4192+
return state.Invalid(BlockValidationResult::BLOCK_INVALID_HEADER, "time-timewarp-attack", "block's timestamp is too early on diff adjustment block");
4193+
}
4194+
}
4195+
}
4196+
41854197
// Check timestamp
41864198
if (block.Time() > NodeClock::now() + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) {
41874199
return state.Invalid(BlockValidationResult::BLOCK_TIME_FUTURE, "time-too-new", "block timestamp too far in the future");

0 commit comments

Comments
 (0)