@@ -697,7 +697,7 @@ func TestAPI(t *testing.T) {
697697 }
698698}
699699
700- func TestDifficultyMetricsOffByOneRegression (t * testing.T ) {
700+ func TestDifficultyMetrics (t * testing.T ) {
701701 network , genesisBlock := ctestutil .Network ()
702702 pk := types .GeneratePrivateKey ()
703703 addr := types .StandardUnlockHash (pk .PublicKey ())
@@ -746,7 +746,12 @@ func TestDifficultyMetricsOffByOneRegression(t *testing.T) {
746746
747747 client := api .NewClient ("http://" + listenAddr + "/api" , testPassword )
748748
749- resp , err := client .DifficultyMetrics (150 , 450 )
749+ const (
750+ startHeight = 150
751+ endHeight = 450
752+ )
753+
754+ resp , err := client .DifficultyMetrics (startHeight , endHeight )
750755 if err != nil {
751756 t .Fatal (err )
752757 }
@@ -757,10 +762,22 @@ func TestDifficultyMetricsOffByOneRegression(t *testing.T) {
757762 testutil .Equal (t , "drifts length" , 150 , len (resp .Drifts ))
758763
759764 // Verify response starts at height 150
760- index , _ := cm .BestIndex (150 )
765+ index , _ := cm .BestIndex (startHeight )
761766 cs , _ := cm .State (index .ID )
762767 testutil .Equal (t , "first difficulty is for height 150" , cs .Difficulty , resp .Difficulties [0 ])
763768
764769 // Verify first block time is calculated from height 148 to 150
765770 testutil .Equal (t , "first block time" , cs .PrevTimestamps [0 ].Sub (cs .PrevTimestamps [2 ])/ 2 , resp .BlockTimes [0 ])
771+
772+ expectedDrifts := make ([]time.Duration , len (resp .Drifts ))
773+ step := resp .BlocksPerStep
774+ for i := range expectedDrifts {
775+ height := startHeight + uint64 (i )* step
776+ index , _ := cm .BestIndex (height )
777+ cs , _ := cm .State (index .ID )
778+ timestamp := cs .PrevTimestamps [0 ]
779+ expected := network .HardforkOak .GenesisTimestamp .Add (time .Duration (height ) * network .BlockInterval )
780+ expectedDrifts [i ] = timestamp .Sub (expected )
781+ }
782+ testutil .Equal (t , "drifts" , expectedDrifts , resp .Drifts )
766783}
0 commit comments