Skip to content

Commit

Permalink
Copy init code from tendermint so it runs properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanfrey authored and rigelrozanski committed Mar 1, 2018
1 parent 4e91a0d commit 94ddda6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
10 changes: 7 additions & 3 deletions examples/basecoin/cmd/basecoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,26 @@ 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)
}
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()
}
4 changes: 2 additions & 2 deletions examples/gaia/gaiad/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
64 changes: 57 additions & 7 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@ import (
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/go-crypto/keys"
"github.com/tendermint/go-crypto/keys/words"
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"
dbm "github.com/tendermint/tmlibs/db"
)

// InitCmd will initialize all files for tendermint,
// along with proper app_options.
// 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",
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit 94ddda6

Please sign in to comment.