/
status.go
100 lines (88 loc) · 3.16 KB
/
status.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package cli
import (
"context"
"strconv"
"time"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/bytes"
tmcli "github.com/tendermint/tendermint/libs/cli"
"github.com/tendermint/tendermint/p2p"
)
// ValidatorInfo is info about the node's validator, same as Tendermint,
// except that we use our own PubKey.
type validatorInfo struct {
Address bytes.HexBytes
PubKey cryptotypes.PubKey
VotingPower int64
}
type syncInfo struct {
LatestBlockHash bytes.HexBytes `json:"latest_block_hash"`
LatestAppHash bytes.HexBytes `json:"latest_app_hash"`
LatestBlockHeight string `json:"latest_block_height"`
LatestBlockTime time.Time `json:"latest_block_time"`
EarliestBlockHash bytes.HexBytes `json:"earliest_block_hash"`
EarliestAppHash bytes.HexBytes `json:"earliest_app_hash"`
EarliestBlockHeight string `json:"earliest_block_height"`
EarliestBlockTime time.Time `json:"earliest_block_time"`
CatchingUp bool `json:"catching_up"`
}
// ResultStatus is node's info, same as Tendermint, except that we use our own
// PubKey.
type resultStatus struct {
NodeInfo p2p.DefaultNodeInfo
SyncInfo syncInfo
ValidatorInfo validatorInfo
}
// StatusCommand returns the command to return the status of the network.
func StatusCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "status",
Short: "Query remote node for status",
RunE: func(cmd *cobra.Command, _ []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
node, err := clientCtx.GetNode()
if err != nil {
return err
}
status, err := node.Status(context.Background())
if err != nil {
return err
}
// `status` has TM pubkeys, we need to convert them to our pubkeys.
pk, err := cryptocodec.FromTmPubKeyInterface(status.ValidatorInfo.PubKey)
if err != nil {
return err
}
statusWithPk := resultStatus{
NodeInfo: status.NodeInfo,
SyncInfo: syncInfo{
LatestBlockHash: status.SyncInfo.LatestBlockHash,
LatestAppHash: status.SyncInfo.LatestAppHash,
LatestBlockHeight: strconv.FormatInt(status.SyncInfo.LatestBlockHeight, 10),
LatestBlockTime: status.SyncInfo.LatestBlockTime,
EarliestBlockHash: status.SyncInfo.EarliestBlockHash,
EarliestAppHash: status.SyncInfo.EarliestAppHash,
EarliestBlockHeight: strconv.FormatInt(status.SyncInfo.EarliestBlockHeight, 10),
EarliestBlockTime: status.SyncInfo.EarliestBlockTime,
CatchingUp: status.SyncInfo.CatchingUp,
},
ValidatorInfo: validatorInfo{
Address: status.ValidatorInfo.Address,
PubKey: pk,
VotingPower: status.ValidatorInfo.VotingPower,
},
}
return PrintOutput(clientCtx, statusWithPk)
},
}
cmd.Flags().StringP(tmcli.OutputFlag, "o", "json", "Output format (text|json)")
cmd.Flags().String(flags.FlagNode, "tcp://localhost:26657", "<host>:<port> to Tendermint RPC interface for this chain")
return cmd
}