Copy init code from tendermint so it runs properly

This commit is contained in:
Ethan Frey 2018-02-21 11:38:48 +01:00 committed by rigelrozanski
parent 4e91a0db89
commit 94ddda6a1f
3 changed files with 67 additions and 13 deletions

View File

@ -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()
}

View File

@ -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,

View File

@ -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 {