cosmos-sdk/server/init.go

202 lines
5.2 KiB
Go
Raw Normal View History

package server
import (
"encoding/json"
2018-03-28 08:03:17 -07:00
"fmt"
"io/ioutil"
2018-04-05 03:24:53 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"
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"
cmn "github.com/tendermint/tmlibs/common"
)
// testnetInformation contains the info necessary
// to setup a testnet including this account and validator.
2018-03-28 08:03:17 -07:00
type testnetInformation struct {
Secret string `json:"secret"`
ChainID string `json:"chain_id"`
2018-03-28 08:03:17 -07:00
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).
func InitCmd(gen GenAppState, ctx *Context) *cobra.Command {
cmd := initCmd{
2018-03-17 17:42:18 -07:00
genAppState: gen,
context: ctx,
}
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
return &cobraCmd
}
2018-04-05 03:24:53 -07:00
// GenAppState takes the command line args, as well
// as an address and coin denomination.
// It returns a default app_state to be included in
// in the genesis file.
// This is application-specific
2018-04-05 03:24:53 -07:00
type GenAppState func(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error)
// DefaultGenAppState expects two args: an account address
// and a coin denomination, and gives lots of coins to that address.
func DefaultGenAppState(args []string, addr sdk.Address, coinDenom string) (json.RawMessage, error) {
opts := fmt.Sprintf(`{
"accounts": [{
"address": "%s",
"coins": [
{
"denom": "%s",
"amount": 9007199254740992
}
]
}]
}`, addr.String(), coinDenom)
return json.RawMessage(opts), nil
}
type initCmd struct {
2018-03-14 12:22:06 -07:00
genAppState GenAppState
context *Context
}
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
// Run the basic tendermint initialization,
// set up a default genesis with no app_options
config := c.context.Config
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
}
// generate secrete and address
addr, secret, err := GenerateCoinKey()
if err != nil {
return err
}
var DEFAULT_DENOM = "mycoin"
2018-03-14 12:22:06 -07:00
// Now, we want to add the custom app_state
2018-04-05 03:24:53 -07:00
appState, err := c.genAppState(args, addr, DEFAULT_DENOM)
if err != nil {
return err
}
2018-03-28 08:03:17 -07:00
testnetInfo.Secret = secret
testnetInfo.Account = addr.String()
2018-03-28 08:03:17 -07:00
// 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
}
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
2018-03-28 08:03:17 -07:00
}
fmt.Println(string(out))
2018-03-28 08:03:17 -07:00
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.context.Logger.Info("Found private validator", "path", privValFile)
} else {
privValidator = tmtypes.GenPrivValidatorFS(privValFile)
privValidator.Save()
c.context.Logger.Info("Generated private validator", "path", privValFile)
}
// genesis file
genFile := config.GenesisFile()
if cmn.FileExists(genFile) {
c.context.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.context.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
}
}
info.ChainID = loadedDoc.ChainID
2018-03-28 08:03:17 -07:00
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)
}