From 1cf443a63b6eab8277ac8acaaf1a6e4ea489eb94 Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Sun, 15 Mar 2020 18:08:03 +0200 Subject: [PATCH] [NOD-841] Fix tests to not be dependent on block rate (#661) * [NOD-841] Fix TestDifficulty * [NOD-841] Fix TestProcessDelayedBlocks * [NOD-841] Fix TestCheckBlockSanity * [NOD-841] Fix TestProcessDelayedBlocks * [NOD-841] Shorten long lines --- blockdag/difficulty_test.go | 19 +++++++++++++------ blockdag/process_test.go | 9 ++++++--- blockdag/validate_test.go | 6 ++++-- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/blockdag/difficulty_test.go b/blockdag/difficulty_test.go index 3a0aee5e2..de24aeb18 100644 --- a/blockdag/difficulty_test.go +++ b/blockdag/difficulty_test.go @@ -94,7 +94,8 @@ func TestDifficulty(t *testing.T) { addNode := func(parents blockSet, blockTime time.Time) *blockNode { bluestParent := parents.bluest() if blockTime == zeroTime { - blockTime = time.Unix(bluestParent.timestamp+1, 0) + blockTime = time.Unix(bluestParent.timestamp, 0) + blockTime = blockTime.Add(params.TargetTimePerBlock) } block, err := PrepareBlockForTest(dag, parents.hashes(), nil) if err != nil { @@ -119,7 +120,8 @@ func TestDifficulty(t *testing.T) { for i := uint64(0); i < dag.difficultyAdjustmentWindowSize; i++ { tip = addNode(blockSetFromSlice(tip), zeroTime) if tip.bits != dag.genesis.bits { - t.Fatalf("As long as the bluest parent's blue score is less then the difficulty adjustment window size, the difficulty should be the same as genesis'") + t.Fatalf("As long as the bluest parent's blue score is less then the difficulty adjustment " + + "window size, the difficulty should be the same as genesis'") } } for i := uint64(0); i < dag.difficultyAdjustmentWindowSize+100; i++ { @@ -140,7 +142,8 @@ func TestDifficulty(t *testing.T) { } tip = addNode(blockSetFromSlice(tip), zeroTime) if compareBits(tip.bits, nodeInThePast.bits) >= 0 { - t.Fatalf("tip.bits should be smaller than nodeInThePast.bits because nodeInThePast increased the block rate, so the difficulty should increase as well") + t.Fatalf("tip.bits should be smaller than nodeInThePast.bits because nodeInThePast increased the " + + "block rate, so the difficulty should increase as well") } expectedBits := uint32(0x207f83df) if tip.bits != expectedBits { @@ -167,7 +170,9 @@ func TestDifficulty(t *testing.T) { sameBitsCount = 0 } } - slowNode := addNode(blockSetFromSlice(tip), time.Unix(tip.timestamp+2, 0)) + slowBlockTime := time.Unix(tip.timestamp, 0) + slowBlockTime = slowBlockTime.Add(params.TargetTimePerBlock + time.Second) + slowNode := addNode(blockSetFromSlice(tip), slowBlockTime) if slowNode.bits != tip.bits { t.Fatalf("The difficulty should only change when slowNode is in the past of a block bluest parent") } @@ -180,7 +185,8 @@ func TestDifficulty(t *testing.T) { } tip = addNode(blockSetFromSlice(tip), zeroTime) if compareBits(tip.bits, slowNode.bits) <= 0 { - t.Fatalf("tip.bits should be smaller than slowNode.bits because slowNode decreased the block rate, so the difficulty should decrease as well") + t.Fatalf("tip.bits should be smaller than slowNode.bits because slowNode decreased the block" + + " rate, so the difficulty should decrease as well") } splitNode := addNode(blockSetFromSlice(tip), zeroTime) @@ -197,7 +203,8 @@ func TestDifficulty(t *testing.T) { tipWithRedPast := addNode(blockSetFromSlice(redChainTip, blueTip), zeroTime) tipWithoutRedPast := addNode(blockSetFromSlice(blueTip), zeroTime) if tipWithoutRedPast.bits != tipWithRedPast.bits { - t.Fatalf("tipWithoutRedPast.bits should be the same as tipWithRedPast.bits because red blocks shouldn't affect the difficulty") + t.Fatalf("tipWithoutRedPast.bits should be the same as tipWithRedPast.bits because red blocks" + + " shouldn't affect the difficulty") } } diff --git a/blockdag/process_test.go b/blockdag/process_test.go index 857e289fc..234c97828 100644 --- a/blockdag/process_test.go +++ b/blockdag/process_test.go @@ -95,7 +95,7 @@ func TestProcessDelayedBlocks(t *testing.T) { t.Fatalf("error in PrepareBlockForTest: %s", err) } - blockDelay := time.Duration(dag1.dagParams.TimestampDeviationTolerance+5) * time.Second + blockDelay := time.Duration(dag1.dagParams.TimestampDeviationTolerance*uint64(dag1.targetTimePerBlock)+5) * time.Second delayedBlock.Header.Timestamp = initialTime.Add(blockDelay) isOrphan, isDelayed, err := dag1.ProcessBlock(util.NewBlock(delayedBlock), BFNoPoWCheck) @@ -194,10 +194,13 @@ func TestProcessDelayedBlocks(t *testing.T) { } // We advance the clock to the point where delayedBlock timestamp is valid. - secondsUntilDelayedBlockIsValid := delayedBlock.Header.Timestamp.Unix() - int64(dag2.TimestampDeviationTolerance) - dag2.Now().Unix() + 1 + deviationTolerance := int64(dag2.TimestampDeviationTolerance) * dag2.targetTimePerBlock + secondsUntilDelayedBlockIsValid := delayedBlock.Header.Timestamp.Unix() - deviationTolerance - dag2.Now().Unix() + 1 dag2.timeSource = newFakeTimeSource(initialTime.Add(time.Duration(secondsUntilDelayedBlockIsValid) * time.Second)) - blockAfterDelay, err := PrepareBlockForTest(dag2, []*daghash.Hash{dag2.dagParams.GenesisBlock.BlockHash()}, nil) + blockAfterDelay, err := PrepareBlockForTest(dag2, + []*daghash.Hash{dag2.dagParams.GenesisBlock.BlockHash()}, + nil) if err != nil { t.Fatalf("error in PrepareBlockForTest: %s", err) } diff --git a/blockdag/validate_test.go b/blockdag/validate_test.go index a29e3acd9..4f7f0999a 100644 --- a/blockdag/validate_test.go +++ b/blockdag/validate_test.go @@ -192,7 +192,8 @@ func TestCheckBlockSanity(t *testing.T) { if !errors.As(err, &ruleErr) { t.Errorf("CheckBlockSanity: wrong error returned, expect RuleError, got %T", err) } else if ruleErr.ErrorCode != ErrTransactionsNotSorted { - t.Errorf("CheckBlockSanity: wrong error returned, expect ErrTransactionsNotSorted, got %v, err %s", ruleErr.ErrorCode, err) + t.Errorf("CheckBlockSanity: wrong error returned, expect ErrTransactionsNotSorted, got"+ + " %v, err %s", ruleErr.ErrorCode, err) } if delay != 0 { t.Errorf("CheckBlockSanity: unexpected return %s delay", delay) @@ -493,7 +494,8 @@ func TestCheckBlockSanity(t *testing.T) { blockInTheFuture := Block100000 expectedDelay := 10 * time.Second - blockInTheFuture.Header.Timestamp = dag.timeSource.Now().Add(time.Duration(dag.TimestampDeviationTolerance)*time.Second + expectedDelay) + deviationTolerance := time.Duration(dag.TimestampDeviationTolerance*uint64(dag.targetTimePerBlock)) * time.Second + blockInTheFuture.Header.Timestamp = dag.Now().Add(deviationTolerance + expectedDelay) delay, err = dag.checkBlockSanity(util.NewBlock(&blockInTheFuture), BFNoPoWCheck) if err != nil { t.Errorf("CheckBlockSanity: %v", err)