cosmos-sdk/server/init.go

280 lines
7.9 KiB
Go
Raw Normal View History

package server
import (
"encoding/json"
2018-03-28 08:03:17 -07:00
"fmt"
"io/ioutil"
2018-04-22 10:36:35 -07:00
"os"
"path"
"path/filepath"
2018-04-05 03:24:53 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
2018-04-09 10:32:19 -07:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/spf13/cobra"
2018-04-21 21:57:53 -07:00
"github.com/spf13/viper"
2018-04-20 16:55:22 -07:00
crypto "github.com/tendermint/go-crypto"
2018-04-05 03:31:33 -07:00
"github.com/tendermint/go-crypto/keys"
"github.com/tendermint/go-crypto/keys/words"
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-04-06 17:25:08 -07:00
pvm "github.com/tendermint/tendermint/types/priv_validator"
cmn "github.com/tendermint/tmlibs/common"
2018-04-05 03:31:33 -07:00
dbm "github.com/tendermint/tmlibs/db"
)
2018-04-22 10:36:35 -07:00
// TODO flag to retrieve genesis file / config file from a URL?
2018-04-20 16:55:22 -07:00
// get cmd to initialize all files for tendermint and application
2018-04-22 10:36:35 -07:00
func InitCmd(ctx *Context, cdc *wire.Codec, gen GenAppParams, appendState AppendAppState) *cobra.Command {
flagOverwrite, flagAppendFile := "overwrite", "piece-file"
cmd := &cobra.Command{
Use: "init",
2018-04-22 10:36:35 -07:00
Short: "Initialize genesis config, priv-validator file, and p2p-node file",
RunE: func(_ *cobra.Command, _ []string) error {
2018-04-20 16:55:22 -07:00
config := ctx.Config
pubkey := ReadOrCreatePrivValidator(config)
2018-04-21 21:57:53 -07:00
chainID, validators, appState, cliPrint, err := gen(cdc, pubkey)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
2018-04-21 21:57:53 -07:00
genFile := config.GenesisFile()
if !viper.GetBool(flagOverwrite) && cmn.FileExists(genFile) {
return fmt.Errorf("genesis config file already exists: %v", genFile)
}
err = WriteGenesisFile(cdc, genFile, chainID, validators, appState)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return err
}
// print out some key information
toPrint := struct {
2018-04-21 21:57:53 -07:00
ChainID string `json:"chain_id"`
NodeID string `json:"node_id"`
AppMessage json.RawMessage `json:"app_message"`
2018-04-20 16:55:22 -07:00
}{
chainID,
string(nodeKey.ID()),
2018-04-21 21:57:53 -07:00
cliPrint,
2018-04-20 16:55:22 -07:00
}
out, err := wire.MarshalJSONIndent(cdc, toPrint)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}
2018-04-21 21:57:53 -07:00
2018-04-22 10:36:35 -07:00
cmd.AddCommand(FromPiecesCmd(ctx, cdc, appendState))
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the config file")
cmd.Flags().BoolP(flagAppendFile, "a", false, "create an append file for others to import")
return cmd
}
// genesis piece structure for creating combined genesis
type GenesisPiece struct {
ChainID string `json:"chain_id"`
NodeID string `json:"node_id"`
AppState json.RawMessage `json:"app_state"`
Validators []tmtypes.GenesisValidator `json:"validators"`
}
// get cmd to initialize all files for tendermint and application
func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appendState AppendAppState) *cobra.Command {
return &cobra.Command{
Use: "from-pieces [directory]",
Short: "Create genesis from directory of genesis pieces",
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
pieceDir := args[0]
// ensure that the privVal and nodeKey file already exist
config := ctx.Config
privValFile := config.PrivValidatorFile()
nodeKeyFile := config.NodeKeyFile()
genFile := config.GenesisFile()
if !cmn.FileExists(privValFile) {
return fmt.Errorf("privVal file must already exist, please initialize with init cmd: %v", privValFile)
}
if !cmn.FileExists(nodeKeyFile) {
return fmt.Errorf("nodeKey file must already exist, please initialize with init cmd: %v", nodeKeyFile)
}
cmn.FileExists(genFile)
// XXX remove the existing gen config file
// deterministically walk the directory for genesis-piece files to import
filepath.Walk(pieceDir, appendPiece(cdc, appendState, nodeKeyFile, genFile))
return nil
},
}
}
2018-04-22 10:36:35 -07:00
// append a genesis-piece
func appendPiece(cdc *wire.Codec, appendState AppendAppState, nodeKeyFile, genFile string) filepath.WalkFunc {
return func(filePath string, _ os.FileInfo, err error) error {
if err != nil {
return err
}
if path.Ext(filePath) != "json" {
return nil
}
// XXX get the file bytes
var bz []byte
// get the piece
var piece GenesisPiece
err = cdc.UnmarshalJSON(bz, &piece)
if err != nil {
return err
}
// if the first file, create the genesis from scratch with piece inputs
if !cmn.FileExists(genFile) {
return WriteGenesisFile(cdc, genFile, piece.chainID, piece.validators, piece.appState)
}
// XXX read in the genFile
// XXX verify chain-ids are the same
// XXX combine the validator set
var validators []tmtypes.GenesisValidator
// combine the app state
appState, err := appendState(appState, piece.AppState)
if err != nil {
return err
}
// write the appended genesis file
return WriteGenesisFile(cdc, genFile, piece.chainID, validators, appState)
// XXX read in nodeKey and combine new nodeID file
return nil
}
}
//________________________________________________________________________________________
2018-04-20 16:55:22 -07:00
// read of create the private key file for this config
func ReadOrCreatePrivValidator(tmConfig *cfg.Config) crypto.PubKey {
// private validator
2018-04-20 16:55:22 -07:00
privValFile := tmConfig.PrivValidatorFile()
2018-04-06 17:25:08 -07:00
var privValidator *pvm.FilePV
if cmn.FileExists(privValFile) {
2018-04-06 17:25:08 -07:00
privValidator = pvm.LoadFilePV(privValFile)
} else {
2018-04-06 17:25:08 -07:00
privValidator = pvm.GenFilePV(privValFile)
privValidator.Save()
}
2018-04-20 16:55:22 -07:00
return privValidator.GetPubKey()
}
2018-04-20 16:55:22 -07:00
// create the genesis file
2018-04-21 21:57:53 -07:00
func WriteGenesisFile(cdc *wire.Codec, genesisFile, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error {
2018-04-20 16:55:22 -07:00
genDoc := tmtypes.GenesisDoc{
ChainID: chainID,
Validators: validators,
2018-03-28 08:03:17 -07:00
}
2018-04-20 16:55:22 -07:00
if err := genDoc.ValidateAndComplete(); err != nil {
return err
2018-03-28 08:03:17 -07:00
}
2018-04-21 21:57:53 -07:00
if err := genDoc.SaveAs(genesisFile); err != nil {
2018-04-20 14:42:56 -07:00
return err
}
2018-04-21 21:57:53 -07:00
return addAppStateToGenesis(cdc, genesisFile, appState)
2018-04-05 03:31:33 -07:00
}
2018-04-20 16:55:22 -07:00
// Add one line to the genesis file
2018-04-21 19:26:46 -07:00
func addAppStateToGenesis(cdc *wire.Codec, genesisConfigPath string, appState json.RawMessage) error {
2018-04-20 16:55:22 -07:00
bz, err := ioutil.ReadFile(genesisConfigPath)
if err != nil {
return err
}
2018-04-21 19:26:46 -07:00
out, err := AppendJSON(cdc, bz, "app_state", appState)
if err != nil {
return err
}
2018-04-20 16:55:22 -07:00
return ioutil.WriteFile(genesisConfigPath, out, 0600)
}
2018-04-05 03:31:33 -07:00
2018-04-20 14:42:56 -07:00
//_____________________________________________________________________
2018-04-05 03:31:33 -07:00
2018-04-20 16:55:22 -07:00
// GenAppParams creates the core parameters initialization. It takes in a
// pubkey meant to represent the pubkey of the validator of this machine.
2018-04-21 22:32:47 -07:00
type GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error)
2018-04-20 16:55:22 -07:00
2018-04-22 10:36:35 -07:00
// append appState1 with appState2
type AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMesssage) (appState json.RawMessage, err error)
2018-04-20 16:55:22 -07:00
// Create one account with a whole bunch of mycoin in it
2018-04-21 21:57:53 -07:00
func SimpleGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error) {
2018-04-20 16:55:22 -07:00
var addr sdk.Address
var secret string
addr, secret, err = GenerateCoinKey()
if err != nil {
return
}
2018-04-21 19:26:46 -07:00
mm := map[string]string{"secret": secret}
bz, err := cdc.MarshalJSON(mm)
2018-04-21 21:57:53 -07:00
cliPrint = json.RawMessage(bz)
2018-04-20 16:55:22 -07:00
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
validators = []tmtypes.GenesisValidator{{
PubKey: pubKey,
Power: 10,
}}
appState = json.RawMessage(fmt.Sprintf(`{
"accounts": [{
"address": "%s",
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}]
}`, addr.String()))
return
}
2018-04-20 14:42:56 -07:00
// GenerateCoinKey returns the address of a public key, along with the secret
// phrase to recover the private key.
2018-04-05 03:31:33 -07:00
func GenerateCoinKey() (sdk.Address, string, error) {
2018-04-20 16:55:22 -07:00
2018-04-05 03:31:33 -07:00
// construct an in-memory key store
codec, err := words.LoadCodec("english")
if err != nil {
return nil, "", err
}
keybase := keys.New(
dbm.NewMemDB(),
codec,
)
// generate a private key, with recovery phrase
info, secret, err := keybase.Create("name", "pass", keys.AlgoEd25519)
if err != nil {
return nil, "", err
}
addr := info.PubKey.Address()
return addr, secret, nil
}