github.com/Finschia/finschia-sdk@v0.48.1/server/oc_cmds.go (about)

     1  package server
     2  
     3  // DONTCOVER
     4  
     5  import (
     6  	"fmt"
     7  
     8  	"github.com/spf13/cobra"
     9  	yaml "gopkg.in/yaml.v2"
    10  
    11  	cfg "github.com/Finschia/ostracon/config"
    12  	osjson "github.com/Finschia/ostracon/libs/json"
    13  	ostos "github.com/Finschia/ostracon/libs/os"
    14  	"github.com/Finschia/ostracon/node"
    15  	"github.com/Finschia/ostracon/p2p"
    16  	pvm "github.com/Finschia/ostracon/privval"
    17  	"github.com/Finschia/ostracon/types"
    18  	ostversion "github.com/Finschia/ostracon/version"
    19  
    20  	sdk "github.com/Finschia/finschia-sdk/types"
    21  )
    22  
    23  // ShowNodeIDCmd - ported from Ostracon, dump node ID to stdout
    24  func ShowNodeIDCmd() *cobra.Command {
    25  	return &cobra.Command{
    26  		Use:   "show-node-id",
    27  		Short: "Show this node's ID",
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			serverCtx := GetServerContextFromCmd(cmd)
    30  			cfg := serverCtx.Config
    31  
    32  			nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile())
    33  			if err != nil {
    34  				return err
    35  			}
    36  			fmt.Println(nodeKey.ID())
    37  			return nil
    38  		},
    39  	}
    40  }
    41  
    42  // ShowValidatorCmd - ported from Ostracon, show this node's validator info
    43  func ShowValidatorCmd() *cobra.Command {
    44  	cmd := cobra.Command{
    45  		Use:   "show-validator",
    46  		Short: "Show this node's ostracon validator info",
    47  		RunE: func(cmd *cobra.Command, args []string) error {
    48  			serverCtx := GetServerContextFromCmd(cmd)
    49  			cfg := serverCtx.Config
    50  			return showValidator(cmd, cfg)
    51  		},
    52  	}
    53  
    54  	return &cmd
    55  }
    56  
    57  func showValidator(cmd *cobra.Command, config *cfg.Config) error {
    58  	var pv types.PrivValidator
    59  	if config.PrivValidatorListenAddr != "" {
    60  		chainID, err := loadChainID(config)
    61  		if err != nil {
    62  			return err
    63  		}
    64  		serverCtx := GetServerContextFromCmd(cmd)
    65  		log := serverCtx.Logger
    66  		pv, err = node.CreateAndStartPrivValidatorSocketClient(config, chainID, log)
    67  		if err != nil {
    68  			return err
    69  		}
    70  	} else {
    71  		keyFilePath := config.PrivValidatorKeyFile()
    72  		if !ostos.FileExists(keyFilePath) {
    73  			return fmt.Errorf("private validator file %s does not exist", keyFilePath)
    74  		}
    75  		pv = pvm.LoadFilePV(keyFilePath, config.PrivValidatorStateFile())
    76  	}
    77  
    78  	pubKey, err := pv.GetPubKey()
    79  	if err != nil {
    80  		return fmt.Errorf("can't get pubkey: %w", err)
    81  	}
    82  
    83  	bz, err := osjson.Marshal(pubKey)
    84  	if err != nil {
    85  		return fmt.Errorf("failed to marshal private validator pubkey: %w", err)
    86  	}
    87  
    88  	fmt.Println(string(bz))
    89  	return nil
    90  }
    91  
    92  func loadChainID(config *cfg.Config) (string, error) {
    93  	stateDB, err := node.DefaultDBProvider(&node.DBContext{ID: "state", Config: config})
    94  	if err != nil {
    95  		return "", err
    96  	}
    97  	defer func() {
    98  		var _ = stateDB.Close()
    99  	}()
   100  	genesisDocProvider := node.DefaultGenesisDocProviderFunc(config)
   101  	_, genDoc, err := node.LoadStateFromDBOrGenesisDocProvider(stateDB, genesisDocProvider)
   102  	if err != nil {
   103  		return "", err
   104  	}
   105  	return genDoc.ChainID, nil
   106  }
   107  
   108  // ShowAddressCmd - show this node's validator address
   109  func ShowAddressCmd() *cobra.Command {
   110  	cmd := &cobra.Command{
   111  		Use:   "show-address",
   112  		Short: "Shows this node's ostracon validator consensus address",
   113  		RunE: func(cmd *cobra.Command, args []string) error {
   114  			serverCtx := GetServerContextFromCmd(cmd)
   115  			cfg := serverCtx.Config
   116  
   117  			privValidator := pvm.LoadFilePV(cfg.PrivValidatorKeyFile(), cfg.PrivValidatorStateFile())
   118  			valConsAddr := (sdk.ConsAddress)(privValidator.GetAddress())
   119  			fmt.Println(valConsAddr.String())
   120  			return nil
   121  		},
   122  	}
   123  
   124  	return cmd
   125  }
   126  
   127  // VersionCmd prints ostracon and ABCI version numbers.
   128  func VersionCmd() *cobra.Command {
   129  	return &cobra.Command{
   130  		Use:   "version",
   131  		Short: "Print ostracon libraries' version",
   132  		Long: `Print protocols' and libraries' version numbers
   133  against which this app has been compiled.
   134  `,
   135  		RunE: func(cmd *cobra.Command, args []string) error {
   136  			bs, err := yaml.Marshal(&struct {
   137  				Ostracon      string
   138  				ABCI          string
   139  				BlockProtocol uint64
   140  				P2PProtocol   uint64
   141  			}{
   142  				Ostracon:      ostversion.OCCoreSemVer,
   143  				ABCI:          ostversion.ABCIVersion,
   144  				BlockProtocol: ostversion.BlockProtocol,
   145  				P2PProtocol:   ostversion.P2PProtocol,
   146  			})
   147  			if err != nil {
   148  				return err
   149  			}
   150  
   151  			fmt.Println(string(bs))
   152  			return nil
   153  		},
   154  	}
   155  }