forked from NebulousLabs/Sia
/
consensuscmd.go
59 lines (53 loc) · 1.69 KB
/
consensuscmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"time"
"github.com/spf13/cobra"
"github.com/NebulousLabs/Sia/node/api"
"github.com/NebulousLabs/Sia/types"
)
var (
consensusCmd = &cobra.Command{
Use: "consensus",
Short: "Print the current state of consensus",
Long: "Print the current state of consensus such as current block, block height, and target.",
Run: wrap(consensuscmd),
}
)
// consensuscmd is the handler for the command `siac consensus`.
// Prints the current state of consensus.
func consensuscmd() {
var cg api.ConsensusGET
err := getAPI("/consensus", &cg)
if err != nil {
die("Could not get current consensus state:", err)
}
if cg.Synced {
fmt.Printf(`Synced: %v
Block: %v
Height: %v
Target: %v
Difficulty: %v
`, yesNo(cg.Synced), cg.CurrentBlock, cg.Height, cg.Target, cg.Difficulty)
} else {
estimatedHeight := estimatedHeightAt(time.Now())
estimatedProgress := float64(cg.Height) / float64(estimatedHeight) * 100
if estimatedProgress > 100 {
estimatedProgress = 100
}
fmt.Printf(`Synced: %v
Height: %v
Progress (estimated): %.1f%%
`, yesNo(cg.Synced), cg.Height, estimatedProgress)
}
}
// 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
}