2018-02-14 12:53:06 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
2018-02-21 02:38:48 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
|
|
|
"github.com/tendermint/tmlibs/log"
|
|
|
|
|
2018-02-14 12:53:06 -08:00
|
|
|
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
2018-02-21 02:38:48 -08:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2018-02-14 12:53:06 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InitCmd will initialize all files for tendermint,
|
2018-03-14 12:22:06 -07:00
|
|
|
// along with proper app_state.
|
2018-02-14 12:53:06 -08:00
|
|
|
// The application can pass in a function to generate
|
2018-03-14 12:22:06 -07:00
|
|
|
// proper state. And may want to use GenerateCoinKey
|
2018-02-14 12:53:06 -08:00
|
|
|
// to create default account(s).
|
2018-03-14 12:22:06 -07:00
|
|
|
func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command {
|
2018-02-14 12:53:06 -08:00
|
|
|
cmd := initCmd{
|
2018-02-21 02:38:48 -08:00
|
|
|
gen: gen,
|
|
|
|
logger: logger,
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize genesis files",
|
|
|
|
RunE: cmd.run,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-14 12:22:06 -07:00
|
|
|
// GenAppState can parse command-line and flag to
|
|
|
|
// generate default app_state for the genesis file.
|
2018-02-14 12:53:06 -08:00
|
|
|
// This is application-specific
|
2018-03-14 12:22:06 -07:00
|
|
|
type GenAppState func(args []string) (json.RawMessage, error)
|
2018-02-14 12:53:06 -08:00
|
|
|
|
|
|
|
type initCmd struct {
|
2018-03-14 12:22:06 -07:00
|
|
|
genAppState GenAppState
|
|
|
|
logger log.Logger
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c initCmd) run(cmd *cobra.Command, args []string) error {
|
|
|
|
// Run the basic tendermint initialization,
|
|
|
|
// set up a default genesis with no app_options
|
2018-02-21 02:38:48 -08:00
|
|
|
config, err := tcmd.ParseConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = c.initTendermintFiles(config)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// no app_options, leave like tendermint
|
2018-03-14 12:22:06 -07:00
|
|
|
if c.genAppState == nil {
|
2018-02-14 12:53:06 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-14 12:22:06 -07:00
|
|
|
// Now, we want to add the custom app_state
|
|
|
|
appState, err := c.genAppState(args)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// And add them to the genesis file
|
2018-02-21 02:38:48 -08:00
|
|
|
genFile := config.GenesisFile()
|
2018-03-14 12:22:06 -07:00
|
|
|
return addGenesisState(genFile, appState)
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
|
|
|
|
2018-02-21 02:38:48 -08:00
|
|
|
// 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()
|
2018-02-21 07:41:56 -08:00
|
|
|
c.logger.Info("Generated private validator", "path", privValFile)
|
2018-02-21 02:38:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2018-02-21 07:41:56 -08:00
|
|
|
c.logger.Info("Generated genesis file", "path", genFile)
|
2018-02-21 02:38:48 -08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-21 03:49:56 -08:00
|
|
|
// GenesisDoc involves some tendermint-specific structures we don't
|
|
|
|
// want to parse, so we just grab it into a raw object format,
|
|
|
|
// so we can add one line.
|
|
|
|
type GenesisDoc map[string]json.RawMessage
|
|
|
|
|
2018-03-14 12:22:06 -07:00
|
|
|
func addGenesisState(filename string, appState json.RawMessage) error {
|
2018-02-14 12:53:06 -08:00
|
|
|
bz, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-02-21 03:49:56 -08:00
|
|
|
var doc GenesisDoc
|
2018-02-14 12:53:06 -08:00
|
|
|
err = json.Unmarshal(bz, &doc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-14 12:22:06 -07:00
|
|
|
doc["app_state"] = appState
|
2018-02-14 12:53:06 -08:00
|
|
|
out, err := json.MarshalIndent(doc, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(filename, out, 0600)
|
|
|
|
}
|