github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/cmd/tendermint/commands/init.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  
     8  	cfg "github.com/lazyledger/lazyledger-core/config"
     9  	"github.com/lazyledger/lazyledger-core/ipfs"
    10  	tmos "github.com/lazyledger/lazyledger-core/libs/os"
    11  	tmrand "github.com/lazyledger/lazyledger-core/libs/rand"
    12  	"github.com/lazyledger/lazyledger-core/p2p"
    13  	"github.com/lazyledger/lazyledger-core/privval"
    14  	tmproto "github.com/lazyledger/lazyledger-core/proto/tendermint/types"
    15  	"github.com/lazyledger/lazyledger-core/types"
    16  	tmtime "github.com/lazyledger/lazyledger-core/types/time"
    17  )
    18  
    19  // InitFilesCmd initialises a fresh Tendermint Core instance.
    20  var InitFilesCmd = &cobra.Command{
    21  	Use:   "init",
    22  	Short: "Initialize Tendermint",
    23  	RunE:  initFiles,
    24  }
    25  
    26  var (
    27  	keyType string
    28  )
    29  
    30  func init() {
    31  	InitFilesCmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeEd25519,
    32  		"Key type to generate privval file with. Options: ed25519, secp256k1")
    33  }
    34  
    35  func initFiles(cmd *cobra.Command, args []string) error {
    36  	return initFilesWithConfig(config)
    37  }
    38  
    39  func initFilesWithConfig(config *cfg.Config) error {
    40  	// private validator
    41  	privValKeyFile := config.PrivValidatorKeyFile()
    42  	privValStateFile := config.PrivValidatorStateFile()
    43  	var (
    44  		pv  *privval.FilePV
    45  		err error
    46  	)
    47  	if tmos.FileExists(privValKeyFile) {
    48  		pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
    49  		logger.Info("Found private validator", "keyFile", privValKeyFile,
    50  			"stateFile", privValStateFile)
    51  	} else {
    52  		pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, keyType)
    53  		if err != nil {
    54  			return err
    55  		}
    56  		pv.Save()
    57  		logger.Info("Generated private validator", "keyFile", privValKeyFile,
    58  			"stateFile", privValStateFile)
    59  	}
    60  
    61  	nodeKeyFile := config.NodeKeyFile()
    62  	if tmos.FileExists(nodeKeyFile) {
    63  		logger.Info("Found node key", "path", nodeKeyFile)
    64  	} else {
    65  		if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
    66  			return err
    67  		}
    68  		logger.Info("Generated node key", "path", nodeKeyFile)
    69  	}
    70  
    71  	// genesis file
    72  	genFile := config.GenesisFile()
    73  	if tmos.FileExists(genFile) {
    74  		logger.Info("Found genesis file", "path", genFile)
    75  	} else {
    76  		genDoc := types.GenesisDoc{
    77  			ChainID:         fmt.Sprintf("test-chain-%v", tmrand.Str(6)),
    78  			GenesisTime:     tmtime.Now(),
    79  			ConsensusParams: types.DefaultConsensusParams(),
    80  		}
    81  		if keyType == "secp256k1" {
    82  			genDoc.ConsensusParams.Validator = tmproto.ValidatorParams{
    83  				PubKeyTypes: []string{types.ABCIPubKeyTypeSecp256k1},
    84  			}
    85  		}
    86  		pubKey, err := pv.GetPubKey()
    87  		if err != nil {
    88  			return fmt.Errorf("can't get pubkey: %w", err)
    89  		}
    90  		genDoc.Validators = []types.GenesisValidator{{
    91  			Address: pubKey.Address(),
    92  			PubKey:  pubKey,
    93  			Power:   10,
    94  		}}
    95  
    96  		if err := genDoc.SaveAs(genFile); err != nil {
    97  			return err
    98  		}
    99  		logger.Info("Generated genesis file", "path", genFile)
   100  	}
   101  
   102  	// TODO(ismail): add counter part in ResetAllCmd
   103  	return ipfs.InitRepo(config.IPFS.Path(), logger)
   104  }