From 9f8f0fd7476e39f96999e399f7b23d4143c9183a Mon Sep 17 00:00:00 2001 From: Svarog Date: Mon, 21 Dec 2020 11:30:43 +0200 Subject: [PATCH] Added safeguard against running TestDifficulty with a fresh genesis block (#1251) --- .../blockvalidator/block_header_in_isolation.go | 1 - .../difficultymanager/difficultymanager_test.go | 11 +++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/domain/consensus/processes/blockvalidator/block_header_in_isolation.go b/domain/consensus/processes/blockvalidator/block_header_in_isolation.go index 01622825d..4578aef10 100644 --- a/domain/consensus/processes/blockvalidator/block_header_in_isolation.go +++ b/domain/consensus/processes/blockvalidator/block_header_in_isolation.go @@ -47,7 +47,6 @@ func (v *blockValidator) checkParentsLimit(header *externalapi.DomainBlockHeader } func (v *blockValidator) checkBlockTimestampInIsolation(header *externalapi.DomainBlockHeader) error { - blockTimestamp := header.TimeInMilliseconds now := mstime.Now().UnixMilliseconds() maxCurrentTime := now + int64(v.timestampDeviationTolerance)*v.targetTimePerBlock.Milliseconds() diff --git a/domain/consensus/processes/difficultymanager/difficultymanager_test.go b/domain/consensus/processes/difficultymanager/difficultymanager_test.go index cc66953a8..eef999e2a 100644 --- a/domain/consensus/processes/difficultymanager/difficultymanager_test.go +++ b/domain/consensus/processes/difficultymanager/difficultymanager_test.go @@ -2,6 +2,9 @@ package difficultymanager_test import ( "testing" + "time" + + "github.com/kaspanet/kaspad/util/mstime" "github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing" @@ -18,6 +21,14 @@ func TestDifficulty(t *testing.T) { if params.DisableDifficultyAdjustment { return } + // This test generates 3066 blocks above genesis with at least 1 second between each block, amounting to + // a bit less then an hour of timestamps. + // To prevent rejected blocks due to timestamps in the future, the following safeguard makes sure + // the genesis block is at least 1 hour in the past. + if params.GenesisBlock.Header.TimeInMilliseconds > mstime.ToMSTime(time.Now().Add(-time.Hour)).UnixMilliseconds() { + t.Fatalf("TestDifficulty requires the GenesisBlock to be at least 1 hour old to pass") + } + params.K = 1 params.DifficultyAdjustmentWindowSize = 264