/
tm.go
118 lines (100 loc) · 3.15 KB
/
tm.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/bech32"
"github.com/tendermint/tendermint/p2p"
pvm "github.com/tendermint/tendermint/privval"
"github.com/bluele/hypermint/pkg/app"
)
const (
FlagJson = "json"
)
// showNodeIDCmd - ported from Tendermint, dump node ID to stdout
func showNodeIDCmd(ctx *app.Context) *cobra.Command {
return &cobra.Command{
Use: "show-node-id",
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
fmt.Println(nodeKey.ID())
return nil
},
}
}
// showValidator - ported from Tendermint, show this node's validator info
func showValidatorCmd(ctx *app.Context) *cobra.Command {
cmd := cobra.Command{
Use: "show-validator",
Short: "Show this node's tendermint validator info",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valPubKey := privValidator.Key.PubKey
if viper.GetBool(FlagJson) {
return printlnJSON(valPubKey)
}
pubkey, err := Bech32ifyConsPub(valPubKey)
if err != nil {
return err
}
fmt.Println(pubkey)
return nil
},
}
cmd.Flags().Bool(FlagJson, false, "get machine parseable output")
return &cmd
}
// showAddressCmd - show this node's validator address
func showAddressCmd(ctx *app.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "show-address",
Short: "Shows this node's tendermint validator address",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valAddr := privValidator.Key.Address
if viper.GetBool(FlagJson) {
return printlnJSON(valAddr)
}
fmt.Printf("0x%X", valAddr)
return nil
},
}
cmd.Flags().Bool(FlagJson, false, "get machine parseable output")
return cmd
}
func printlnJSON(v interface{}) error {
cdc := app.GetCodec()
marshalled, err := cdc.MarshalJSON(v)
if err != nil {
return err
}
fmt.Println(string(marshalled))
return nil
}
// UnsafeResetAllCmd - extension of the tendermint command, resets initialization
func UnsafeResetAllCmd(ctx *app.Context) *cobra.Command {
return &cobra.Command{
Use: "unsafe-reset-all",
Short: "Resets the blockchain database, removes address book files, and resets priv_validator.json to the genesis state",
RunE: func(cmd *cobra.Command, args []string) error {
cfg := ctx.Config
tcmd.ResetAll(cfg.DBDir(), cfg.P2P.AddrBookFile(), cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), ctx.Logger)
return nil
},
}
}
const Bech32PrefixConsPub = "cosmosvalconspub"
// Bech32ifyConsPub returns a Bech32 encoded string containing the
// Bech32PrefixConsPub prefixfor a given consensus node's PubKey.
func Bech32ifyConsPub(pub crypto.PubKey) (string, error) {
return bech32.ConvertAndEncode(Bech32PrefixConsPub, pub.Bytes())
}