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