github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/cmd/tendermint/commands/show_validator.go (about)

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/spf13/cobra"
     8  
     9  	"github.com/ari-anchor/sei-tendermint/config"
    10  	"github.com/ari-anchor/sei-tendermint/crypto"
    11  	tmjson "github.com/ari-anchor/sei-tendermint/libs/json"
    12  	"github.com/ari-anchor/sei-tendermint/libs/log"
    13  	tmnet "github.com/ari-anchor/sei-tendermint/libs/net"
    14  	tmos "github.com/ari-anchor/sei-tendermint/libs/os"
    15  	"github.com/ari-anchor/sei-tendermint/privval"
    16  	tmgrpc "github.com/ari-anchor/sei-tendermint/privval/grpc"
    17  )
    18  
    19  // MakeShowValidatorCommand constructs a command to show the validator info.
    20  func MakeShowValidatorCommand(conf *config.Config, logger log.Logger) *cobra.Command {
    21  	return &cobra.Command{
    22  		Use:   "show-validator",
    23  		Short: "Show this node's validator info",
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			var (
    26  				pubKey crypto.PubKey
    27  				err    error
    28  				bctx   = cmd.Context()
    29  			)
    30  			//TODO: remove once gRPC is the only supported protocol
    31  			protocol, _ := tmnet.ProtocolAndAddress(conf.PrivValidator.ListenAddr)
    32  			switch protocol {
    33  			case "grpc":
    34  				pvsc, err := tmgrpc.DialRemoteSigner(
    35  					bctx,
    36  					conf.PrivValidator,
    37  					conf.ChainID(),
    38  					logger,
    39  					conf.Instrumentation.Prometheus,
    40  				)
    41  				if err != nil {
    42  					return fmt.Errorf("can't connect to remote validator %w", err)
    43  				}
    44  
    45  				ctx, cancel := context.WithTimeout(bctx, ctxTimeout)
    46  				defer cancel()
    47  
    48  				pubKey, err = pvsc.GetPubKey(ctx)
    49  				if err != nil {
    50  					return fmt.Errorf("can't get pubkey: %w", err)
    51  				}
    52  			default:
    53  
    54  				keyFilePath := conf.PrivValidator.KeyFile()
    55  				if !tmos.FileExists(keyFilePath) {
    56  					return fmt.Errorf("private validator file %s does not exist", keyFilePath)
    57  				}
    58  
    59  				pv, err := privval.LoadFilePV(keyFilePath, conf.PrivValidator.StateFile())
    60  				if err != nil {
    61  					return err
    62  				}
    63  
    64  				ctx, cancel := context.WithTimeout(bctx, ctxTimeout)
    65  				defer cancel()
    66  
    67  				pubKey, err = pv.GetPubKey(ctx)
    68  				if err != nil {
    69  					return fmt.Errorf("can't get pubkey: %w", err)
    70  				}
    71  			}
    72  
    73  			bz, err := tmjson.Marshal(pubKey)
    74  			if err != nil {
    75  				return fmt.Errorf("failed to marshal private validator pubkey: %w", err)
    76  			}
    77  
    78  			fmt.Println(string(bz))
    79  			return nil
    80  		},
    81  	}
    82  
    83  }