cosmos-sdk/server/init.go

188 lines
4.7 KiB
Go
Raw Normal View History

package server
import (
"encoding/json"
2018-03-28 08:03:17 -07:00
"fmt"
"io/ioutil"
"github.com/spf13/cobra"
2018-03-28 08:03:17 -07:00
"github.com/spf13/viper"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tmlibs/log"
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
2018-03-28 08:03:17 -07:00
"github.com/tendermint/tendermint/p2p"
tmtypes "github.com/tendermint/tendermint/types"
)
2018-03-28 08:03:17 -07:00
const (
flagTestnet = "testnet"
)
type testnetInformation struct {
Secret string `json:"secret"`
Account string `json:"account"`
Validator tmtypes.GenesisValidator `json:"validator"`
NodeID p2p.ID `json:"node_id"`
}
// InitCmd will initialize all files for tendermint,
2018-03-14 12:22:06 -07:00
// along with proper app_state.
// 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
// to create default account(s).
2018-03-14 12:22:06 -07:00
func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command {
cmd := initCmd{
2018-03-17 17:42:18 -07:00
genAppState: gen,
logger: logger,
}
2018-03-28 08:03:17 -07:00
cobraCmd := cobra.Command{
Use: "init",
Short: "Initialize genesis files",
RunE: cmd.run,
}
2018-03-28 08:03:17 -07:00
cobraCmd.Flags().Bool(flagTestnet, false, "Output testnet information in JSON")
return &cobraCmd
}
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-03-28 08:03:17 -07:00
// Also must return generated seed and address
// This is application-specific
2018-03-28 08:03:17 -07:00
type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, error)
type initCmd struct {
2018-03-14 12:22:06 -07:00
genAppState GenAppState
logger log.Logger
}
func (c initCmd) run(cmd *cobra.Command, args []string) error {
2018-03-28 08:03:17 -07:00
// Store testnet information as we go
var testnetInfo testnetInformation
if viper.GetBool(flagTestnet) {
c.logger = log.NewFilter(c.logger, log.AllowError())
}
// Run the basic tendermint initialization,
// set up a default genesis with no app_options
config, err := tcmd.ParseConfig()
if err != nil {
return err
}
2018-03-28 08:03:17 -07:00
err = c.initTendermintFiles(config, &testnetInfo)
if err != nil {
return err
}
// no app_options, leave like tendermint
2018-03-14 12:22:06 -07:00
if c.genAppState == nil {
return nil
}
2018-03-14 12:22:06 -07:00
// Now, we want to add the custom app_state
2018-03-28 08:03:17 -07:00
appState, secret, address, err := c.genAppState(args)
if err != nil {
return err
}
2018-03-28 08:03:17 -07:00
testnetInfo.Secret = secret
testnetInfo.Account = address.String()
// And add them to the genesis file
genFile := config.GenesisFile()
2018-03-28 08:03:17 -07:00
if err := addGenesisState(genFile, appState); err != nil {
return err
}
if viper.GetBool(flagTestnet) {
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return err
}
testnetInfo.NodeID = nodeKey.ID()
out, err := json.MarshalIndent(testnetInfo, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
}
return nil
}
// This was copied from tendermint/cmd/tendermint/commands/init.go
// so we could pass in the config and the logger.
2018-03-28 08:03:17 -07:00
func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformation) 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)
}
// 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-03-28 08:03:17 -07:00
// reload the config file and find our validator info
loadedDoc, err := tmtypes.GenesisDocFromFile(genFile)
if err != nil {
return err
}
for _, validator := range loadedDoc.Validators {
if validator.PubKey == privValidator.GetPubKey() {
info.Validator = validator
}
}
return nil
}
// 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 {
bz, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
var doc GenesisDoc
err = json.Unmarshal(bz, &doc)
if err != nil {
return err
}
2018-03-14 12:22:06 -07:00
doc["app_state"] = appState
out, err := json.MarshalIndent(doc, "", " ")
if err != nil {
return err
}
return ioutil.WriteFile(filename, out, 0600)
}