Commit 421df894 authored by David Vorick's avatar David Vorick

change the siac progress estimator to be more accurate

parent 39c2c12d
Pipeline #40813928 failed with stages
in 2 minutes and 33 seconds
......@@ -7,6 +7,7 @@ import (
"github.com/spf13/cobra"
"gitlab.com/NebulousLabs/Sia/types"
"gitlab.com/NebulousLabs/Sia/node/api"
)
var (
......@@ -33,10 +34,10 @@ Target: %v
Difficulty: %v
`, yesNo(cg.Synced), cg.CurrentBlock, cg.Height, cg.Target, cg.Difficulty)
} else {
estimatedHeight := estimatedHeightAt(time.Now())
estimatedHeight := estimatedHeightAt(time.Now(), cg)
estimatedProgress := float64(cg.Height) / float64(estimatedHeight) * 100
if estimatedProgress > 100 {
estimatedProgress = 100
estimatedProgress = 99.9
}
fmt.Printf(`Synced: %v
Height: %v
......@@ -48,10 +49,8 @@ Progress (estimated): %.1f%%
// estimatedHeightAt returns the estimated block height for the given time.
// Block height is estimated by calculating the minutes since a known block in
// the past and dividing by 10 minutes (the block time).
func estimatedHeightAt(t time.Time) types.BlockHeight {
block100kTimestamp := time.Date(2017, time.April, 13, 23, 29, 49, 0, time.UTC)
blockTime := float64(9) // overestimate block time for better UX
diff := t.Sub(block100kTimestamp)
estimatedHeight := 100e3 + (diff.Minutes() / blockTime)
return types.BlockHeight(estimatedHeight + 0.5) // round to the nearest block
func estimatedHeightAt(t time.Time, cg api.ConsensusGET) types.BlockHeight {
gt := cg.GenesisTimestamp
bf := cg.BlockFrequency
return types.BlockHeight(types.Timestamp(t.Unix()) - gt) / bf
}
package main
import (
"testing"
"time"
"gitlab.com/NebulousLabs/Sia/types"
)
// TestEstimatedHeightAt tests that the expectedHeightAt function correctly
// estimates the blockheight (and rounds to the nearest block).
func TestEstimatedHeightAt(t *testing.T) {
tests := []struct {
t time.Time
expectedHeight types.BlockHeight
}{
// Test on the same block that is used to estimate the height
{
time.Date(2017, time.April, 13, 23, 29, 49, 0, time.UTC),
100e3,
},
// 4 minutes later
{
time.Date(2017, time.April, 13, 23, 33, 49, 0, time.UTC),
100e3,
},
// 5 minutes later
{
time.Date(2017, time.April, 13, 23, 34, 49, 0, time.UTC),
100e3 + 1,
},
// 15 minutes later
{
time.Date(2017, time.April, 13, 23, 44, 49, 0, time.UTC),
100e3 + 2,
},
// 1 day later
{
time.Date(2017, time.April, 14, 23, 29, 49, 0, time.UTC),
100e3 + 160,
},
}
for _, tt := range tests {
h := estimatedHeightAt(tt.t)
if h != tt.expectedHeight {
t.Errorf("expected an estimated height of %v, but got %v", tt.expectedHeight, h)
}
}
}
......@@ -38,7 +38,7 @@ func TestIntegrationConsensusGET(t *testing.T) {
if cg.BlockFrequency != types.BlockFrequency {
t.Error("constant mismatch")
}
if cg.SiafundCount != types.SiafundCount {
if cg.SiafundCount.Cmp(types.SiafundCount) != 0 {
t.Error("constant mismatch")
}
if cg.InitialCoinbase != types.InitialCoinbase {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment