cosmos-sdk/server/init.go

402 lines
11 KiB
Go
Raw Normal View History

package server
import (
"encoding/json"
2018-03-28 08:03:17 -07:00
"fmt"
"io/ioutil"
"os"
2018-04-22 10:36:35 -07:00
"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/pkg/errors"
"github.com/spf13/cobra"
2018-04-23 17:05:58 -07:00
"github.com/spf13/pflag"
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-24 22:37:46 -07:00
// genesis piece structure for creating combined genesis
type GenesisTx struct {
NodeID string `json:"node_id"`
IP string `json:"ip"`
Validator tmtypes.GenesisValidator `json:"validator"`
AppGenTx json.RawMessage `json:"app_gen_tx"`
}
var (
flagOverwrite = "overwrite"
flagGenTxs = "gen-txs"
flagIP = "ip"
flagChainID = "chain-id"
2018-04-24 22:37:46 -07:00
)
2018-04-20 16:55:22 -07:00
// get cmd to initialize all files for tendermint and application
2018-04-24 22:37:46 -07:00
func GenTxCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
2018-04-22 10:36:35 -07:00
cmd := &cobra.Command{
2018-04-24 22:37:46 -07:00
Use: "gen-tx",
Short: "Create genesis transaction file (under [--home]/gentx-[nodeID].json)",
2018-04-23 08:47:39 -07:00
Args: cobra.NoArgs,
2018-04-24 22:37:46 -07:00
RunE: func(_ *cobra.Command, args []string) error {
2018-04-20 16:55:22 -07:00
config := ctx.Config
2018-04-24 22:37:46 -07:00
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return err
}
nodeID := string(nodeKey.ID())
pubKey := ReadOrCreatePrivValidator(config)
2018-04-20 16:55:22 -07:00
appGenTx, cliPrint, validator, err := appInit.AppGenTx(cdc, pubKey)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
2018-04-24 22:37:46 -07:00
ip := viper.GetString(flagIP)
if len(ip) == 0 {
ip, err = externalIP()
if err != nil {
return err
}
2018-04-21 21:57:53 -07:00
}
2018-04-24 22:37:46 -07:00
tx := GenesisTx{
NodeID: nodeID,
IP: ip,
Validator: validator,
AppGenTx: appGenTx,
}
bz, err := wire.MarshalJSONIndent(cdc, tx)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
2018-04-25 14:17:40 -07:00
genTxFile := json.RawMessage(bz)
2018-04-24 22:37:46 -07:00
name := fmt.Sprintf("gentx-%v.json", nodeID)
file := filepath.Join(viper.GetString("home"), name)
2018-04-25 14:17:40 -07:00
err = cmn.WriteFile(file, bz, 0644)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
2018-04-23 12:15:50 -07:00
2018-04-25 14:17:40 -07:00
// print out some key information
toPrint := struct {
AppMessage json.RawMessage `json:"app_message"`
GenTxFile json.RawMessage `json:"gen_tx_file"`
}{
cliPrint,
genTxFile,
}
out, err := wire.MarshalJSONIndent(cdc, toPrint)
2018-04-20 16:55:22 -07:00
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}
2018-04-24 16:46:39 -07:00
cmd.Flags().String(flagIP, "", "external facing IP to use if left blank IP will be retrieved from this machine")
cmd.Flags().AddFlagSet(appInit.FlagsAppGenTx)
2018-04-22 10:36:35 -07:00
return cmd
}
// get cmd to initialize all files for tendermint and application
2018-04-24 22:37:46 -07:00
func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
cmd := &cobra.Command{
Use: "init",
Short: "Initialize genesis config, priv-validator file, and p2p-node file",
Args: cobra.NoArgs,
RunE: func(_ *cobra.Command, _ []string) error {
2018-04-22 10:36:35 -07:00
config := ctx.Config
2018-04-24 22:37:46 -07:00
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
if err != nil {
return err
2018-04-22 10:36:35 -07:00
}
2018-04-24 22:37:46 -07:00
nodeID := string(nodeKey.ID())
pubKey := ReadOrCreatePrivValidator(config)
chainID := viper.GetString(flagChainID)
if chainID == "" {
chainID = cmn.Fmt("test-chain-%v", cmn.RandStr(6))
2018-04-22 10:36:35 -07:00
}
2018-04-23 08:47:39 -07:00
genFile := config.GenesisFile()
2018-04-24 22:37:46 -07:00
if !viper.GetBool(flagOverwrite) && cmn.FileExists(genFile) {
return fmt.Errorf("genesis.json file already exists: %v", genFile)
}
2018-04-22 10:36:35 -07:00
2018-04-24 22:37:46 -07:00
// process genesis transactions, or otherwise create one for defaults
var appMessage json.RawMessage
var appGenTxs []json.RawMessage
2018-04-24 22:37:46 -07:00
var validators []tmtypes.GenesisValidator
var persistentPeers string
2018-04-24 22:37:46 -07:00
genTxsDir := viper.GetString(flagGenTxs)
if genTxsDir != "" {
validators, appGenTxs, persistentPeers, err = processGenTxs(genTxsDir, cdc, appInit)
if err != nil {
return err
}
2018-04-24 22:37:46 -07:00
config.P2P.PersistentPeers = persistentPeers
configFilePath := filepath.Join(viper.GetString("home"), "config", "config.toml") //TODO this is annoying should be easier to get
cfg.WriteConfigFile(configFilePath, config)
} else {
appGenTx, am, validator, err := appInit.AppGenTx(cdc, pubKey)
appMessage = am
2018-04-24 22:37:46 -07:00
if err != nil {
return err
}
validators = []tmtypes.GenesisValidator{validator}
appGenTxs = []json.RawMessage{appGenTx}
2018-04-24 22:37:46 -07:00
}
appState, err := appInit.AppGenState(cdc, appGenTxs)
2018-04-24 22:37:46 -07:00
if err != nil {
return err
}
err = WriteGenesisFile(cdc, genFile, chainID, validators, appState)
if err != nil {
return err
}
// print out some key information
toPrint := struct {
ChainID string `json:"chain_id"`
NodeID string `json:"node_id"`
AppMessage json.RawMessage `json:"app_message"`
2018-04-24 22:37:46 -07:00
}{
chainID,
nodeID,
appMessage,
2018-04-24 22:37:46 -07:00
}
out, err := wire.MarshalJSONIndent(cdc, toPrint)
2018-04-23 18:32:43 -07:00
if err != nil {
return err
}
2018-04-24 22:37:46 -07:00
fmt.Println(string(out))
2018-04-22 10:36:35 -07:00
return nil
},
}
2018-04-24 22:37:46 -07:00
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the genesis.json file")
cmd.Flags().String(flagChainID, "", "genesis file chain-id, if left blank will be randomly created")
cmd.Flags().String(flagGenTxs, "", "directory containing the genesis transactions")
cmd.Flags().AddFlagSet(appInit.FlagsAppGenState)
cmd.Flags().AddFlagSet(appInit.FlagsAppGenTx) // need to add this flagset for when no GenTx's provided
cmd.AddCommand(GenTxCmd(ctx, cdc, appInit))
2018-04-24 22:37:46 -07:00
return cmd
}
2018-04-22 10:36:35 -07:00
// append a genesis-piece
2018-04-24 22:37:46 -07:00
func processGenTxs(genTxsDir string, cdc *wire.Codec, appInit AppInit) (
validators []tmtypes.GenesisValidator, appGenTxs []json.RawMessage, persistentPeers string, err error) {
2018-04-22 10:36:35 -07:00
// XXX sort the files by contents just incase people renamed their files
var fos []os.FileInfo
fos, err = ioutil.ReadDir(genTxsDir)
if err != nil {
return
}
2018-04-24 22:37:46 -07:00
for _, fo := range fos {
2018-04-25 14:17:40 -07:00
filename := path.Join(genTxsDir, fo.Name())
2018-04-24 22:37:46 -07:00
if !fo.IsDir() && (path.Ext(filename) != ".json") {
return
2018-04-23 08:47:39 -07:00
}
2018-04-22 10:36:35 -07:00
2018-04-24 22:37:46 -07:00
// get the genTx
var bz []byte
bz, err = ioutil.ReadFile(filename)
2018-04-23 08:47:39 -07:00
if err != nil {
return
2018-04-23 08:47:39 -07:00
}
2018-04-24 22:37:46 -07:00
var genTx GenesisTx
err = cdc.UnmarshalJSON(bz, &genTx)
2018-04-22 10:36:35 -07:00
if err != nil {
return
2018-04-22 10:36:35 -07:00
}
2018-04-24 22:37:46 -07:00
// combine some stuff
validators = append(validators, genTx.Validator)
appGenTxs = append(appGenTxs, genTx.AppGenTx)
2018-04-22 10:36:35 -07:00
2018-04-24 22:37:46 -07:00
// Add a persistent peer
2018-04-23 18:32:43 -07:00
comma := ","
2018-04-24 22:37:46 -07:00
if len(persistentPeers) == 0 {
2018-04-23 18:32:43 -07:00
comma = ""
}
persistentPeers += fmt.Sprintf("%s%s@%s:46656", comma, genTx.NodeID, genTx.IP)
2018-04-22 10:36:35 -07:00
}
2018-04-24 22:37:46 -07:00
return
2018-04-22 10:36:35 -07:00
}
//________________________________________________________________________________________
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-23 17:05:58 -07:00
// Core functionality passed from the application to the server init command
type AppInit struct {
2018-04-20 16:55:22 -07:00
2018-04-24 22:37:46 -07:00
// flags required for application init functions
FlagsAppGenState *pflag.FlagSet
FlagsAppGenTx *pflag.FlagSet
2018-04-23 17:05:58 -07:00
// AppGenState creates the core parameters initialization. It takes in a
2018-04-23 17:05:58 -07:00
// pubkey meant to represent the pubkey of the validator of this machine.
AppGenState func(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error)
2018-04-23 17:05:58 -07:00
2018-04-24 22:37:46 -07:00
// create the application genesis tx
AppGenTx func(cdc *wire.Codec, pk crypto.PubKey) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error)
2018-04-23 17:05:58 -07:00
}
//_____________________________________________________________________
2018-04-23 17:05:58 -07:00
// simple default application init
var DefaultAppInit = AppInit{
AppGenState: SimpleAppGenState,
AppGenTx: SimpleAppGenTx,
2018-04-23 17:05:58 -07:00
}
2018-04-22 10:36:35 -07:00
// simple genesis tx
type SimpleGenTx struct {
Addr sdk.Address `json:"addr"`
}
2018-04-20 16:55:22 -07:00
// create the genesis app state
func SimpleAppGenState(cdc *wire.Codec, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
2018-04-23 17:05:58 -07:00
if len(appGenTxs) != 1 {
err = errors.New("must provide a single genesis transaction")
2018-04-20 16:55:22 -07:00
return
}
2018-04-21 19:26:46 -07:00
var genTx SimpleGenTx
err = cdc.UnmarshalJSON(appGenTxs[0], &genTx)
if err != nil {
return
}
2018-04-20 16:55:22 -07:00
appState = json.RawMessage(fmt.Sprintf(`{
"accounts": [{
"address": "%s",
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}]
}`, genTx.Addr.String()))
return
}
// Generate a genesis transaction
func SimpleAppGenTx(cdc *wire.Codec, pk crypto.PubKey) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
var addr sdk.Address
var secret string
addr, secret, err = GenerateCoinKey()
if err != nil {
return
}
var bz []byte
simpleGenTx := SimpleGenTx{addr}
bz, err = cdc.MarshalJSON(simpleGenTx)
if err != nil {
return
}
appGenTx = json.RawMessage(bz)
mm := map[string]string{"secret": secret}
bz, err = cdc.MarshalJSON(mm)
if err != nil {
return
}
cliPrint = json.RawMessage(bz)
validator = tmtypes.GenesisValidator{
PubKey: pk,
Power: 10,
}
2018-04-20 16:55:22 -07:00
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
}