package commands import ( "fmt" "github.com/spf13/cobra" cfg "git.cw.tr/mukan-network/mukan-consensus/config" cmtos "git.cw.tr/mukan-network/mukan-consensus/libs/os" cmtrand "git.cw.tr/mukan-network/mukan-consensus/libs/rand" "git.cw.tr/mukan-network/mukan-consensus/p2p" "git.cw.tr/mukan-network/mukan-consensus/privval" "git.cw.tr/mukan-network/mukan-consensus/types" cmttime "git.cw.tr/mukan-network/mukan-consensus/types/time" ) // InitFilesCmd initializes a fresh CometBFT instance. var InitFilesCmd = &cobra.Command{ Use: "init", Short: "Initialize CometBFT", RunE: initFiles, } func initFiles(*cobra.Command, []string) error { return initFilesWithConfig(config) } func initFilesWithConfig(config *cfg.Config) error { // private validator privValKeyFile := config.PrivValidatorKeyFile() privValStateFile := config.PrivValidatorStateFile() var pv *privval.FilePV if cmtos.FileExists(privValKeyFile) { pv = privval.LoadFilePV(privValKeyFile, privValStateFile) logger.Info("Found private validator", "keyFile", privValKeyFile, "stateFile", privValStateFile) } else { pv = privval.GenFilePV(privValKeyFile, privValStateFile) pv.Save() logger.Info("Generated private validator", "keyFile", privValKeyFile, "stateFile", privValStateFile) } nodeKeyFile := config.NodeKeyFile() if cmtos.FileExists(nodeKeyFile) { logger.Info("Found node key", "path", nodeKeyFile) } else { if _, err := p2p.LoadOrGenNodeKey(nodeKeyFile); err != nil { return err } logger.Info("Generated node key", "path", nodeKeyFile) } // genesis file genFile := config.GenesisFile() if cmtos.FileExists(genFile) { logger.Info("Found genesis file", "path", genFile) } else { genDoc := types.GenesisDoc{ ChainID: fmt.Sprintf("test-chain-%v", cmtrand.Str(6)), GenesisTime: cmttime.Now(), ConsensusParams: types.DefaultConsensusParams(), } pubKey, err := pv.GetPubKey() if err != nil { return fmt.Errorf("can't get pubkey: %w", err) } genDoc.Validators = []types.GenesisValidator{{ Address: pubKey.Address(), PubKey: pubKey, Power: 10, }} if err := genDoc.SaveAs(genFile); err != nil { return err } logger.Info("Generated genesis file", "path", genFile) } return nil }