github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/cmd/spc/consensuscmd.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "time" 6 7 "github.com/spf13/cobra" 8 9 "SiaPrime/types" 10 ) 11 12 var ( 13 consensusCmd = &cobra.Command{ 14 Use: "consensus", 15 Short: "Print the current state of consensus", 16 Long: "Print the current state of consensus such as current block, block height, and target.", 17 Run: wrap(consensuscmd), 18 } 19 ) 20 21 // consensuscmd is the handler for the command `siac consensus`. 22 // Prints the current state of consensus. 23 func consensuscmd() { 24 cg, err := httpClient.ConsensusGet() 25 if err != nil { 26 die("Could not get current consensus state:", err) 27 } 28 if cg.Synced { 29 fmt.Printf(`Synced: %v 30 Block: %v 31 Height: %v 32 Target: %v 33 Difficulty: %v 34 `, yesNo(cg.Synced), cg.CurrentBlock, cg.Height, cg.Target, cg.Difficulty) 35 } else { 36 estimatedHeight := estimatedHeightAt(time.Now()) 37 estimatedProgress := float64(cg.Height) / float64(estimatedHeight) * 100 38 if estimatedProgress > 100 { 39 estimatedProgress = 100 40 } 41 fmt.Printf(`Synced: %v 42 Height: %v 43 Progress (estimated): %.1f%% 44 `, yesNo(cg.Synced), cg.Height, estimatedProgress) 45 } 46 } 47 48 // estimatedHeightAt returns the estimated block height for the given time. 49 // Block height is estimated by calculating the minutes since a known block in 50 // the past and dividing by 10 minutes (the block time). 51 func estimatedHeightAt(t time.Time) types.BlockHeight { 52 block5kTimestamp := time.Unix(1543958696, 0) 53 blockTime := float64(9) // overestimate block time for better UX 54 diff := t.Sub(block5kTimestamp) 55 estimatedHeight := 5000 + (diff.Minutes() / blockTime) 56 return types.BlockHeight(estimatedHeight + 0.5) // round to the nearest block 57 }