cosmos-sdk/server/tm_cmds.go

159 lines
4.2 KiB
Go
Raw Normal View History

2018-04-05 03:31:33 -07:00
package server
// DONTCOVER
2018-04-05 03:31:33 -07:00
import (
"fmt"
"strings"
2018-04-05 03:31:33 -07:00
"github.com/spf13/cobra"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
"github.com/tendermint/tendermint/libs/cli"
2018-04-05 03:31:33 -07:00
"github.com/tendermint/tendermint/p2p"
2018-06-05 18:04:16 -07:00
pvm "github.com/tendermint/tendermint/privval"
2019-01-11 20:30:16 -08:00
tversion "github.com/tendermint/tendermint/version"
yaml "gopkg.in/yaml.v2"
2018-12-10 06:27:25 -08:00
"github.com/cosmos/cosmos-sdk/codec"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
2018-12-10 06:27:25 -08:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-04-05 03:31:33 -07:00
)
// ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout
func ShowNodeIDCmd() *cobra.Command {
2018-04-05 03:31:33 -07:00
return &cobra.Command{
Use: "show-node-id",
2018-04-05 03:31:33 -07:00
Short: "Show this node's ID",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
fmt.Println(nodeKey.ID())
return nil
},
2018-04-05 03:31:33 -07:00
}
}
// ShowValidator - ported from Tendermint, show this node's validator info
func ShowValidatorCmd() *cobra.Command {
cmd := cobra.Command{
Use: "show-validator",
2018-04-25 21:27:40 -07:00
Short: "Show this node's tendermint validator info",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valPubKey, err := privValidator.GetPubKey()
if err != nil {
return err
}
2018-04-05 03:31:33 -07:00
output, _ := cmd.Flags().GetString(cli.OutputFlag)
if strings.ToLower(output) == "json" {
return printlnJSON(valPubKey)
}
pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
if err != nil {
return err
}
2018-06-01 07:23:58 -07:00
fmt.Println(pubkey)
return nil
},
2018-04-05 03:31:33 -07:00
}
cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")
return &cmd
2018-04-05 03:31:33 -07:00
}
// ShowAddressCmd - show this node's validator address
func ShowAddressCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show-address",
Short: "Shows this node's tendermint validator consensus address",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
output, _ := cmd.Flags().GetString(cli.OutputFlag)
if strings.ToLower(output) == "json" {
return printlnJSON(valConsAddr)
}
fmt.Println(valConsAddr.String())
return nil
},
}
cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")
return cmd
}
2019-01-11 20:30:16 -08:00
// VersionCmd prints tendermint and ABCI version numbers.
func VersionCmd() *cobra.Command {
return &cobra.Command{
2019-01-11 20:30:16 -08:00
Use: "version",
Short: "Print tendermint libraries' version",
Long: `Print protocols' and libraries' version numbers
2019-01-11 20:30:16 -08:00
against which this app has been compiled.
`,
RunE: func(cmd *cobra.Command, args []string) error {
bs, err := yaml.Marshal(&struct {
Tendermint string
ABCI string
BlockProtocol uint64
P2PProtocol uint64
}{
Tendermint: tversion.Version,
ABCI: tversion.ABCIVersion,
BlockProtocol: tversion.BlockProtocol.Uint64(),
P2PProtocol: tversion.P2PProtocol.Uint64(),
})
if err != nil {
return err
}
2019-01-11 20:30:16 -08:00
fmt.Println(string(bs))
2019-01-11 20:30:16 -08:00
return nil
},
}
}
func printlnJSON(v interface{}) error {
cdc := codec.New()
cryptocodec.RegisterCrypto(cdc)
marshalled, err := cdc.MarshalJSON(v)
if err != nil {
return err
}
fmt.Println(string(marshalled))
return nil
}
2018-04-05 03:31:33 -07:00
// UnsafeResetAllCmd - extension of the tendermint command, resets initialization
func UnsafeResetAllCmd() *cobra.Command {
2018-04-05 03:31:33 -07:00
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 {
serverCtx := GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
tcmd.ResetAll(cfg.DBDir(), cfg.P2P.AddrBookFile(), cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), serverCtx.Logger)
return nil
},
2018-04-05 03:31:33 -07:00
}
}