From 94ddda6a1f18222f923a3ebc1bdb913a7b71efab Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 21 Feb 2018 11:38:48 +0100 Subject: [PATCH] Copy init code from tendermint so it runs properly --- examples/basecoin/cmd/basecoind/main.go | 10 ++-- examples/gaia/gaiad/main.go | 4 +- server/init.go | 66 ++++++++++++++++++++++--- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 170fb15e27..786c0f3f1e 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -49,8 +49,12 @@ func defaultOptions(args []string) (json.RawMessage, error) { } func main() { + // TODO: this should somehow be updated on cli flags? + // But we need to create the app first... hmmm..... + rootDir := os.ExpandEnv("$HOME/.basecoind") + logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main") - db, err := dbm.NewGoLevelDB("/tmp/basecoind", "data") + db, err := dbm.NewGoLevelDB("basecoin", rootDir) if err != nil { fmt.Println(err) os.Exit(1) @@ -58,13 +62,13 @@ func main() { bapp := app.NewBasecoinApp(logger, db) gaiadCmd.AddCommand( - server.InitCmd(defaultOptions), + server.InitCmd(defaultOptions, bapp.Logger), server.StartCmd(bapp, bapp.Logger), server.UnsafeResetAllCmd(bapp.Logger), version.VersionCmd, ) // prepare and add flags - executor := cli.PrepareBaseCmd(gaiadCmd, "GA", os.ExpandEnv("$HOME/.gaiad")) + executor := cli.PrepareBaseCmd(gaiadCmd, "BC", rootDir) executor.Execute() } diff --git a/examples/gaia/gaiad/main.go b/examples/gaia/gaiad/main.go index d0e1262a12..fb5c0b20f5 100644 --- a/examples/gaia/gaiad/main.go +++ b/examples/gaia/gaiad/main.go @@ -48,10 +48,10 @@ func defaultOptions(args []string) (json.RawMessage, error) { func main() { // TODO: set this to something real - var app *baseapp.BaseApp + app := new(baseapp.BaseApp) gaiadCmd.AddCommand( - server.InitCmd(defaultOptions), + server.InitCmd(defaultOptions, app.Logger), server.StartCmd(app, app.Logger), server.UnsafeResetAllCmd(app.Logger), version.VersionCmd, diff --git a/server/init.go b/server/init.go index 7372509384..7e6eaf78cc 100644 --- a/server/init.go +++ b/server/init.go @@ -10,9 +10,13 @@ import ( crypto "github.com/tendermint/go-crypto" "github.com/tendermint/go-crypto/keys" "github.com/tendermint/go-crypto/keys/words" - tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" - tmtypes "github.com/tendermint/tendermint/types" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" + "github.com/tendermint/tmlibs/log" + + tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" + cfg "github.com/tendermint/tendermint/config" + tmtypes "github.com/tendermint/tendermint/types" ) // InitCmd will initialize all files for tendermint, @@ -20,9 +24,10 @@ import ( // The application can pass in a function to generate // proper options. And may want to use GenerateCoinKey // to create default account(s). -func InitCmd(gen GenOptions) *cobra.Command { +func InitCmd(gen GenOptions, logger log.Logger) *cobra.Command { cmd := initCmd{ - gen: gen, + gen: gen, + logger: logger, } return &cobra.Command{ Use: "init", @@ -56,22 +61,31 @@ func GenerateCoinKey() (crypto.Address, string, error) { if err != nil { return nil, "", err } + + // debug + bz, err := json.Marshal(info.PubKey) + fmt.Printf("PubKey: %s\n", string(bz)) + addr := info.PubKey.Address() return addr, secret, nil } type initCmd struct { - gen GenOptions + gen GenOptions + logger log.Logger } func (c initCmd) run(cmd *cobra.Command, args []string) error { // Run the basic tendermint initialization, // set up a default genesis with no app_options - cfg, err := tcmd.ParseConfig() + config, err := tcmd.ParseConfig() + if err != nil { + return err + } + err = c.initTendermintFiles(config) if err != nil { return err } - tcmd.InitFilesCmd.Run(cmd, args) // no app_options, leave like tendermint if c.gen == nil { @@ -85,10 +99,46 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error { } // And add them to the genesis file - genFile := cfg.GenesisFile() + genFile := config.GenesisFile() return addGenesisOptions(genFile, options) } +// This was copied from tendermint/cmd/tendermint/commands/init.go +// so we could pass in the config and the logger. +func (c initCmd) initTendermintFiles(config *cfg.Config) error { + // private validator + privValFile := config.PrivValidatorFile() + var privValidator *tmtypes.PrivValidatorFS + if cmn.FileExists(privValFile) { + privValidator = tmtypes.LoadPrivValidatorFS(privValFile) + c.logger.Info("Found private validator", "path", privValFile) + } else { + privValidator = tmtypes.GenPrivValidatorFS(privValFile) + privValidator.Save() + c.logger.Info("Genetated private validator", "path", privValFile) + } + + // genesis file + genFile := config.GenesisFile() + if cmn.FileExists(genFile) { + c.logger.Info("Found genesis file", "path", genFile) + } else { + genDoc := tmtypes.GenesisDoc{ + ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)), + } + genDoc.Validators = []tmtypes.GenesisValidator{{ + PubKey: privValidator.GetPubKey(), + Power: 10, + }} + + if err := genDoc.SaveAs(genFile); err != nil { + return err + } + c.logger.Info("Genetated genesis file", "path", genFile) + } + return nil +} + func addGenesisOptions(filename string, options json.RawMessage) error { bz, err := ioutil.ReadFile(filename) if err != nil {