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

     1  package commands
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/ari-anchor/sei-tendermint/config"
    11  	"github.com/ari-anchor/sei-tendermint/libs/log"
    12  	tmos "github.com/ari-anchor/sei-tendermint/libs/os"
    13  	tmrand "github.com/ari-anchor/sei-tendermint/libs/rand"
    14  	tmtime "github.com/ari-anchor/sei-tendermint/libs/time"
    15  	"github.com/ari-anchor/sei-tendermint/privval"
    16  	"github.com/ari-anchor/sei-tendermint/types"
    17  )
    18  
    19  // MakeInitFilesCommand returns the command to initialize a fresh Tendermint Core instance.
    20  func MakeInitFilesCommand(conf *config.Config, logger log.Logger) *cobra.Command {
    21  	var keyType string
    22  	cmd := &cobra.Command{
    23  		Use:       "init [full|validator|seed]",
    24  		Short:     "Initializes a Tendermint node",
    25  		ValidArgs: []string{"full", "validator", "seed"},
    26  		// We allow for zero args so we can throw a more informative error
    27  		Args: cobra.MaximumNArgs(1),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			if len(args) == 0 {
    30  				return errors.New("must specify a node type: tendermint init [validator|full|seed]")
    31  			}
    32  			conf.Mode = args[0]
    33  			return initFilesWithConfig(cmd.Context(), conf, logger, keyType)
    34  		},
    35  	}
    36  
    37  	cmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeEd25519,
    38  		"Key type to generate privval file with. Options: ed25519, secp256k1")
    39  
    40  	return cmd
    41  }
    42  
    43  func initFilesWithConfig(ctx context.Context, conf *config.Config, logger log.Logger, keyType string) error {
    44  	var (
    45  		pv  *privval.FilePV
    46  		err error
    47  	)
    48  
    49  	if conf.Mode == config.ModeValidator {
    50  		// private validator
    51  		privValKeyFile := conf.PrivValidator.KeyFile()
    52  		privValStateFile := conf.PrivValidator.StateFile()
    53  		if tmos.FileExists(privValKeyFile) {
    54  			pv, err = privval.LoadFilePV(privValKeyFile, privValStateFile)
    55  			if err != nil {
    56  				return err
    57  			}
    58  
    59  			logger.Info("Found private validator", "keyFile", privValKeyFile,
    60  				"stateFile", privValStateFile)
    61  		} else {
    62  			pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, keyType)
    63  			if err != nil {
    64  				return err
    65  			}
    66  			if err := pv.Save(); err != nil {
    67  				return err
    68  			}
    69  			logger.Info("Generated private validator", "keyFile", privValKeyFile,
    70  				"stateFile", privValStateFile)
    71  		}
    72  	}
    73  
    74  	nodeKeyFile := conf.NodeKeyFile()
    75  	if tmos.FileExists(nodeKeyFile) {
    76  		logger.Info("Found node key", "path", nodeKeyFile)
    77  	} else {
    78  		if _, err := types.LoadOrGenNodeKey(nodeKeyFile); err != nil {
    79  			return err
    80  		}
    81  		logger.Info("Generated node key", "path", nodeKeyFile)
    82  	}
    83  
    84  	// genesis file
    85  	genFile := conf.GenesisFile()
    86  	if tmos.FileExists(genFile) {
    87  		logger.Info("Found genesis file", "path", genFile)
    88  	} else {
    89  
    90  		genDoc := types.GenesisDoc{
    91  			ChainID:         fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
    92  			GenesisTime:     tmtime.Now(),
    93  			ConsensusParams: types.DefaultConsensusParams(),
    94  		}
    95  		if keyType == "secp256k1" {
    96  			genDoc.ConsensusParams.Validator = types.ValidatorParams{
    97  				PubKeyTypes: []string{types.ABCIPubKeyTypeSecp256k1},
    98  			}
    99  		}
   100  
   101  		ctx, cancel := context.WithTimeout(ctx, ctxTimeout)
   102  		defer cancel()
   103  
   104  		// if this is a validator we add it to genesis
   105  		if pv != nil {
   106  			pubKey, err := pv.GetPubKey(ctx)
   107  			if err != nil {
   108  				return fmt.Errorf("can't get pubkey: %w", err)
   109  			}
   110  			genDoc.Validators = []types.GenesisValidator{{
   111  				Address: pubKey.Address(),
   112  				PubKey:  pubKey,
   113  				Power:   10,
   114  			}}
   115  		}
   116  
   117  		if err := genDoc.SaveAs(genFile); err != nil {
   118  			return err
   119  		}
   120  		logger.Info("Generated genesis file", "path", genFile)
   121  	}
   122  
   123  	// write config file
   124  	if err := config.WriteConfigFile(conf.RootDir, conf); err != nil {
   125  		return err
   126  	}
   127  	logger.Info("Generated config", "mode", conf.Mode)
   128  
   129  	return nil
   130  }