Skip to content

Commit b5e0143

Browse files
authored
Merge pull request #279 from SiaFoundation/metrics
fix height calculation in drift metrics
2 parents f64e9ef + 3b4ffd5 commit b5e0143

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

api/api_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

persist/sqlite/metrics.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (s *Store) DifficultyMetrics(start, end, step uint64, n *consensus.Network)
328328
}
329329
result.Drifts = make([]time.Duration, len(timestamps))
330330
for i := range result.Drifts {
331-
height := start + uint64(i)*step
331+
height := queryStart + uint64(i)*step
332332
expected := n.HardforkOak.GenesisTimestamp.Add(time.Duration(height) * n.BlockInterval)
333333
result.Drifts[i] = timestamps[i].Sub(expected)
334334
}

0 commit comments

Comments
 (0)