github.com/evdatsion/aphelion-dpos-bft@v0.32.1/cmd/tendermint/commands/init.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/spf13/cobra"
     7  	cfg "github.com/evdatsion/aphelion-dpos-bft/config"
     8  	cmn "github.com/evdatsion/aphelion-dpos-bft/libs/common"
     9  	"github.com/evdatsion/aphelion-dpos-bft/p2p"
    10  	"github.com/evdatsion/aphelion-dpos-bft/privval"
    11  	"github.com/evdatsion/aphelion-dpos-bft/types"
    12  	tmtime "github.com/evdatsion/aphelion-dpos-bft/types/time"
    13  )
    14  
    15  // InitFilesCmd initialises a fresh Tendermint Core instance.
    16  var InitFilesCmd = &cobra.Command{
    17  	Use:   "init",
    18  	Short: "Initialize Tendermint",
    19  	RunE:  initFiles,
    20  }
    21  
    22  func initFiles(cmd *cobra.Command, args []string) error {
    23  	return initFilesWithConfig(config)
    24  }
    25  
    26  func initFilesWithConfig(config *cfg.Config) error {
    27  	// private validator
    28  	privValKeyFile := config.PrivValidatorKeyFile()
    29  	privValStateFile := config.PrivValidatorStateFile()
    30  	var pv *privval.FilePV
    31  	if cmn.FileExists(privValKeyFile) {
    32  		pv = privval.LoadFilePV(privValKeyFile, privValStateFile)
    33  		logger.Info("Found private validator", "keyFile", privValKeyFile,
    34  			"stateFile", privValStateFile)
    35  	} else {
    36  		pv = privval.GenFilePV(privValKeyFile, privValStateFile)
    37  		pv.Save()
    38  		logger.Info("Generated private validator", "keyFile", privValKeyFile,
    39  			"stateFile", privValStateFile)
    40  	}
    41  
    42  	nodeKeyFile := config.NodeKeyFile()
    43  	if cmn.FileExists(nodeKeyFile) {
    44  		logger.Info("Found node key", "path", nodeKeyFile)
    45  	} else {
    46  		if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil {
    47  			return err
    48  		}
    49  		logger.Info("Generated node key", "path", nodeKeyFile)
    50  	}
    51  
    52  	// genesis file
    53  	genFile := config.GenesisFile()
    54  	if cmn.FileExists(genFile) {
    55  		logger.Info("Found genesis file", "path", genFile)
    56  	} else {
    57  		genDoc := types.GenesisDoc{
    58  			ChainID:         fmt.Sprintf("test-chain-%v", cmn.RandStr(6)),
    59  			GenesisTime:     tmtime.Now(),
    60  			ConsensusParams: types.DefaultConsensusParams(),
    61  		}
    62  		key := pv.GetPubKey()
    63  		genDoc.Validators = []types.GenesisValidator{{
    64  			Address: key.Address(),
    65  			PubKey:  key,
    66  			Power:   10,
    67  		}}
    68  
    69  		if err := genDoc.SaveAs(genFile); err != nil {
    70  			return err
    71  		}
    72  		logger.Info("Generated genesis file", "path", genFile)
    73  	}
    74  
    75  	return nil
    76  }