github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/cmd/siac/consensuscmd.go (about)

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