github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/server/tm_cmds.go (about)

     1  package server
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/spf13/cobra"
     9  	"github.com/spf13/viper"
    10  	yaml "gopkg.in/yaml.v2"
    11  
    12  	tcmd "github.com/fibonacci-chain/fbc/libs/tendermint/cmd/tendermint/commands"
    13  	"github.com/fibonacci-chain/fbc/libs/tendermint/libs/cli"
    14  	"github.com/fibonacci-chain/fbc/libs/tendermint/p2p"
    15  	pvm "github.com/fibonacci-chain/fbc/libs/tendermint/privval"
    16  	tversion "github.com/fibonacci-chain/fbc/libs/tendermint/version"
    17  
    18  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    19  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    20  )
    21  
    22  // ShowNodeIDCmd - ported from Tendermint, dump node ID to stdout
    23  func ShowNodeIDCmd(ctx *Context) *cobra.Command {
    24  	return &cobra.Command{
    25  		Use:   "show-node-id",
    26  		Short: "Show this node's ID",
    27  		RunE: func(cmd *cobra.Command, args []string) error {
    28  			cfg := ctx.Config
    29  			nodeKey, err := p2p.LoadOrGenNodeKey(cfg.NodeKeyFile())
    30  			if err != nil {
    31  				return err
    32  			}
    33  			fmt.Println(nodeKey.ID())
    34  			return nil
    35  		},
    36  	}
    37  }
    38  
    39  // ShowValidator - ported from Tendermint, show this node's validator info
    40  func ShowValidatorCmd(ctx *Context) *cobra.Command {
    41  	cmd := cobra.Command{
    42  		Use:   "show-validator",
    43  		Short: "Show this node's tendermint validator info",
    44  		RunE: func(cmd *cobra.Command, args []string) error {
    45  			cfg := ctx.Config
    46  			privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
    47  
    48  			valPubKey, err := privValidator.GetPubKey()
    49  			if err != nil {
    50  				return err
    51  			}
    52  
    53  			if viper.GetString(cli.OutputFlag) == "json" {
    54  				return printlnJSON(valPubKey)
    55  			}
    56  
    57  			pubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, valPubKey)
    58  			if err != nil {
    59  				return err
    60  			}
    61  
    62  			fmt.Println(pubkey)
    63  			return nil
    64  		},
    65  	}
    66  
    67  	cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")
    68  	return &cmd
    69  }
    70  
    71  // ShowAddressCmd - show this node's validator address
    72  func ShowAddressCmd(ctx *Context) *cobra.Command {
    73  	cmd := &cobra.Command{
    74  		Use:   "show-address",
    75  		Short: "Shows this node's tendermint validator consensus address",
    76  		RunE: func(cmd *cobra.Command, args []string) error {
    77  			cfg := ctx.Config
    78  			privValidator := pvm.LoadOrGenFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
    79  			valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
    80  
    81  			if viper.GetString(cli.OutputFlag) == "json" {
    82  				return printlnJSON(valConsAddr)
    83  			}
    84  
    85  			fmt.Println(valConsAddr.String())
    86  			return nil
    87  		},
    88  	}
    89  
    90  	cmd.Flags().StringP(cli.OutputFlag, "o", "text", "Output format (text|json)")
    91  	return cmd
    92  }
    93  
    94  // VersionCmd prints tendermint and ABCI version numbers.
    95  func VersionCmd(ctx *Context) *cobra.Command {
    96  	cmd := &cobra.Command{
    97  		Use:   "version",
    98  		Short: "Print tendermint libraries' version",
    99  		Long: `Print protocols' and libraries' version numbers
   100  against which this app has been compiled.
   101  `,
   102  		RunE: func(cmd *cobra.Command, args []string) error {
   103  
   104  			bs, err := yaml.Marshal(&struct {
   105  				Tendermint    string
   106  				ABCI          string
   107  				BlockProtocol uint64
   108  				P2PProtocol   uint64
   109  			}{
   110  				Tendermint:    tversion.Version,
   111  				ABCI:          tversion.ABCIVersion,
   112  				BlockProtocol: tversion.BlockProtocol.Uint64(),
   113  				P2PProtocol:   tversion.P2PProtocol.Uint64(),
   114  			})
   115  			if err != nil {
   116  				return err
   117  			}
   118  
   119  			fmt.Println(string(bs))
   120  			return nil
   121  		},
   122  	}
   123  	return cmd
   124  }
   125  
   126  func printlnJSON(v interface{}) error {
   127  	cdc := codec.New()
   128  	codec.RegisterCrypto(cdc)
   129  	marshalled, err := cdc.MarshalJSON(v)
   130  	if err != nil {
   131  		return err
   132  	}
   133  	fmt.Println(string(marshalled))
   134  	return nil
   135  }
   136  
   137  // UnsafeResetAllCmd - extension of the tendermint command, resets initialization
   138  func UnsafeResetAllCmd(ctx *Context) *cobra.Command {
   139  	return &cobra.Command{
   140  		Use:   "unsafe-reset-all",
   141  		Short: "Resets the blockchain database, removes address book files, and resets priv_validator.json to the genesis state",
   142  		RunE: func(cmd *cobra.Command, args []string) error {
   143  			cfg := ctx.Config
   144  			tcmd.ResetAll(cfg.DBDir(), cfg.P2P.AddrBookFile(), cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile(), ctx.Logger)
   145  			return nil
   146  		},
   147  	}
   148  }