github.com/number571/tendermint@v0.34.11-gost/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/number571/tendermint/crypto"
    10  	tmjson "github.com/number571/tendermint/libs/json"
    11  	tmnet "github.com/number571/tendermint/libs/net"
    12  	tmos "github.com/number571/tendermint/libs/os"
    13  	"github.com/number571/tendermint/privval"
    14  	tmgrpc "github.com/number571/tendermint/privval/grpc"
    15  )
    16  
    17  // ShowValidatorCmd adds capabilities for showing the validator info.
    18  var ShowValidatorCmd = &cobra.Command{
    19  	Use:     "show-validator",
    20  	Aliases: []string{"show_validator"},
    21  	Short:   "Show this node's validator info",
    22  	RunE:    showValidator,
    23  	PreRun:  deprecateSnakeCase,
    24  }
    25  
    26  func showValidator(cmd *cobra.Command, args []string) error {
    27  	var (
    28  		pubKey crypto.PubKey
    29  		err    error
    30  	)
    31  
    32  	//TODO: remove once gRPC is the only supported protocol
    33  	protocol, _ := tmnet.ProtocolAndAddress(config.PrivValidator.ListenAddr)
    34  	switch protocol {
    35  	case "grpc":
    36  		pvsc, err := tmgrpc.DialRemoteSigner(
    37  			config.PrivValidator,
    38  			config.ChainID(),
    39  			logger,
    40  			config.Instrumentation.Prometheus,
    41  		)
    42  		if err != nil {
    43  			return fmt.Errorf("can't connect to remote validator %w", err)
    44  		}
    45  
    46  		ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout)
    47  		defer cancel()
    48  
    49  		pubKey, err = pvsc.GetPubKey(ctx)
    50  		if err != nil {
    51  			return fmt.Errorf("can't get pubkey: %w", err)
    52  		}
    53  	default:
    54  
    55  		keyFilePath := config.PrivValidator.KeyFile()
    56  		if !tmos.FileExists(keyFilePath) {
    57  			return fmt.Errorf("private validator file %s does not exist", keyFilePath)
    58  		}
    59  
    60  		pv, err := privval.LoadFilePV(keyFilePath, config.PrivValidator.StateFile())
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout)
    66  		defer cancel()
    67  
    68  		pubKey, err = pv.GetPubKey(ctx)
    69  		if err != nil {
    70  			return fmt.Errorf("can't get pubkey: %w", err)
    71  		}
    72  	}
    73  
    74  	bz, err := tmjson.Marshal(pubKey)
    75  	if err != nil {
    76  		return fmt.Errorf("failed to marshal private validator pubkey: %w", err)
    77  	}
    78  
    79  	fmt.Println(string(bz))
    80  	return nil
    81  }