github.com/number571/tendermint@v0.34.11-gost/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 cfg "github.com/number571/tendermint/config" 11 tmos "github.com/number571/tendermint/libs/os" 12 tmrand "github.com/number571/tendermint/libs/rand" 13 tmtime "github.com/number571/tendermint/libs/time" 14 "github.com/number571/tendermint/privval" 15 "github.com/number571/tendermint/types" 16 17 "github.com/number571/tendermint/crypto/gost256" 18 "github.com/number571/tendermint/crypto/gost512" 19 ) 20 21 // InitFilesCmd initializes a fresh Tendermint Core instance. 22 var InitFilesCmd = &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: initFiles, 29 } 30 31 var ( 32 keyType string 33 ) 34 35 func init() { 36 InitFilesCmd.Flags().StringVar(&keyType, "key", types.ABCIPubKeyTypeGost512, 37 "Key type to generate privval file with. Options: '"+gost512.KeyType+"', '"+gost256.KeyType+"'") 38 } 39 40 func initFiles(cmd *cobra.Command, args []string) error { 41 if len(args) == 0 { 42 return errors.New("must specify a node type: tendermint init [validator|full|seed]") 43 } 44 config.Mode = args[0] 45 return initFilesWithConfig(config) 46 } 47 48 func initFilesWithConfig(config *cfg.Config) error { 49 var ( 50 pv *privval.FilePV 51 err error 52 ) 53 54 if config.Mode == cfg.ModeValidator { 55 // private validator 56 privValKeyFile := config.PrivValidator.KeyFile() 57 privValStateFile := config.PrivValidator.StateFile() 58 if tmos.FileExists(privValKeyFile) { 59 pv, err = privval.LoadFilePV(privValKeyFile, privValStateFile) 60 if err != nil { 61 return err 62 } 63 64 logger.Info("Found private validator", "keyFile", privValKeyFile, 65 "stateFile", privValStateFile) 66 } else { 67 pv, err = privval.GenFilePV(privValKeyFile, privValStateFile, keyType) 68 if err != nil { 69 return err 70 } 71 pv.Save() 72 logger.Info("Generated private validator", "keyFile", privValKeyFile, 73 "stateFile", privValStateFile) 74 } 75 } 76 77 nodeKeyFile := config.NodeKeyFile() 78 if tmos.FileExists(nodeKeyFile) { 79 logger.Info("Found node key", "path", nodeKeyFile) 80 } else { 81 if _, err := types.LoadOrGenNodeKey(nodeKeyFile); err != nil { 82 return err 83 } 84 logger.Info("Generated node key", "path", nodeKeyFile) 85 } 86 87 // genesis file 88 genFile := config.GenesisFile() 89 if tmos.FileExists(genFile) { 90 logger.Info("Found genesis file", "path", genFile) 91 } else { 92 93 genDoc := types.GenesisDoc{ 94 ChainID: fmt.Sprintf("test-chain-%v", tmrand.Str(6)), 95 GenesisTime: tmtime.Now(), 96 ConsensusParams: types.DefaultConsensusParams(), 97 } 98 if keyType == gost256.KeyType { 99 genDoc.ConsensusParams.Validator = types.ValidatorParams{ 100 PubKeyTypes: []string{types.ABCIPubKeyTypeGost256}, 101 } 102 } 103 104 ctx, cancel := context.WithTimeout(context.TODO(), ctxTimeout) 105 defer cancel() 106 107 // if this is a validator we add it to genesis 108 if pv != nil { 109 pubKey, err := pv.GetPubKey(ctx) 110 if err != nil { 111 return fmt.Errorf("can't get pubkey: %w", err) 112 } 113 genDoc.Validators = []types.GenesisValidator{{ 114 Address: pubKey.Address(), 115 PubKey: pubKey, 116 Power: 10, 117 }} 118 } 119 120 if err := genDoc.SaveAs(genFile); err != nil { 121 return err 122 } 123 logger.Info("Generated genesis file", "path", genFile) 124 } 125 126 // write config file 127 cfg.WriteConfigFile(config.RootDir, config) 128 logger.Info("Generated config", "mode", config.Mode) 129 130 return nil 131 }