2018-02-14 12:53:06 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-03-28 08:03:17 -07:00
|
|
|
"fmt"
|
2018-02-14 12:53:06 -08:00
|
|
|
"io/ioutil"
|
2018-04-22 10:36:35 -07:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2018-02-14 12:53:06 -08:00
|
|
|
|
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"
|
2018-02-14 12:53:06 -08:00
|
|
|
"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-02-14 12:53:06 -08:00
|
|
|
|
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"
|
2018-02-21 02:38:48 -08:00
|
|
|
cfg "github.com/tendermint/tendermint/config"
|
2018-03-28 08:03:17 -07:00
|
|
|
"github.com/tendermint/tendermint/p2p"
|
2018-02-14 12:53:06 -08:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-04-06 17:25:08 -07:00
|
|
|
pvm "github.com/tendermint/tendermint/types/priv_validator"
|
2018-04-04 04:39:13 -07:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2018-04-05 03:31:33 -07:00
|
|
|
dbm "github.com/tendermint/tmlibs/db"
|
2018-02-14 12:53:06 -08:00
|
|
|
)
|
|
|
|
|
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-23 17:05:58 -07:00
|
|
|
func InitCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
|
2018-04-23 12:15:50 -07:00
|
|
|
flagOverwrite, flagPieceFile := "overwrite", "piece-file"
|
2018-04-22 10:36:35 -07:00
|
|
|
cmd := &cobra.Command{
|
2018-02-14 12:53:06 -08:00
|
|
|
Use: "init",
|
2018-04-22 10:36:35 -07:00
|
|
|
Short: "Initialize genesis config, priv-validator file, and p2p-node file",
|
2018-04-23 08:47:39 -07:00
|
|
|
Args: cobra.NoArgs,
|
2018-04-22 10:36:35 -07:00
|
|
|
RunE: func(_ *cobra.Command, _ []string) error {
|
2018-04-20 16:55:22 -07:00
|
|
|
|
|
|
|
config := ctx.Config
|
|
|
|
pubkey := ReadOrCreatePrivValidator(config)
|
|
|
|
|
2018-04-23 17:05:58 -07:00
|
|
|
chainID, validators, appState, cliPrint, err := appInit.GenAppParams(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
|
|
|
|
}
|
2018-04-23 12:15:50 -07:00
|
|
|
nodeID := string(nodeKey.ID())
|
2018-04-20 16:55:22 -07:00
|
|
|
|
|
|
|
// print out some key information
|
2018-04-23 12:15:50 -07:00
|
|
|
|
2018-04-20 16:55:22 -07:00
|
|
|
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,
|
2018-04-23 12:15:50 -07:00
|
|
|
nodeID,
|
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))
|
2018-04-23 12:15:50 -07:00
|
|
|
|
|
|
|
// write the piece file is path specified
|
|
|
|
pieceFile := viper.GetString(flagPieceFile)
|
|
|
|
if len(pieceFile) > 0 {
|
|
|
|
//create the piece
|
|
|
|
ip, err := externalIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
piece := GenesisPiece{
|
|
|
|
ChainID: chainID,
|
|
|
|
NodeID: nodeID,
|
|
|
|
IP: ip,
|
|
|
|
AppState: appState,
|
|
|
|
Validators: validators,
|
|
|
|
}
|
|
|
|
bz, err := cdc.MarshalJSON(piece)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return cmn.WriteFile(pieceFile, bz, 0644)
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:55:22 -07:00
|
|
|
return nil
|
|
|
|
},
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
2018-04-23 17:05:58 -07:00
|
|
|
if appInit.AppendAppState != nil {
|
|
|
|
cmd.AddCommand(FromPiecesCmd(ctx, cdc, appInit))
|
2018-04-23 12:15:50 -07:00
|
|
|
cmd.Flags().StringP(flagPieceFile, "a", "", "create an append file for others to import")
|
2018-04-23 08:47:39 -07:00
|
|
|
}
|
2018-04-22 10:36:35 -07:00
|
|
|
cmd.Flags().BoolP(flagOverwrite, "o", false, "overwrite the config file")
|
2018-04-23 17:05:58 -07:00
|
|
|
cmd.Flags().AddFlagSet(appInit.Flags)
|
2018-04-22 10:36:35 -07:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// genesis piece structure for creating combined genesis
|
|
|
|
type GenesisPiece struct {
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
NodeID string `json:"node_id"`
|
2018-04-23 12:15:50 -07:00
|
|
|
IP string `json:"ip"`
|
2018-04-22 10:36:35 -07:00
|
|
|
AppState json.RawMessage `json:"app_state"`
|
|
|
|
Validators []tmtypes.GenesisValidator `json:"validators"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// get cmd to initialize all files for tendermint and application
|
2018-04-23 17:05:58 -07:00
|
|
|
func FromPiecesCmd(ctx *Context, cdc *wire.Codec, appInit AppInit) *cobra.Command {
|
2018-04-22 10:36:35 -07:00
|
|
|
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()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// remove genFile for creation
|
|
|
|
genFile := config.GenesisFile()
|
|
|
|
os.Remove(genFile)
|
2018-04-22 10:36:35 -07:00
|
|
|
|
|
|
|
// deterministically walk the directory for genesis-piece files to import
|
2018-04-23 17:05:58 -07:00
|
|
|
filepath.Walk(pieceDir, appendPiece(ctx, cdc, appInit, nodeKeyFile, genFile))
|
2018-04-22 10:36:35 -07:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
|
|
|
|
2018-04-22 10:36:35 -07:00
|
|
|
// append a genesis-piece
|
2018-04-23 17:05:58 -07:00
|
|
|
func appendPiece(ctx *Context, cdc *wire.Codec, appInit AppInit, nodeKeyFile, genFile string) filepath.WalkFunc {
|
2018-04-23 08:47:39 -07:00
|
|
|
return func(pieceFile string, _ os.FileInfo, err error) error {
|
2018-04-22 10:36:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-23 08:47:39 -07:00
|
|
|
if path.Ext(pieceFile) != "json" {
|
2018-04-22 10:36:35 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// get the piece file bytes
|
|
|
|
bz, err := ioutil.ReadFile(pieceFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-22 10:36:35 -07:00
|
|
|
|
|
|
|
// 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) {
|
2018-04-23 08:47:39 -07:00
|
|
|
return WriteGenesisFile(cdc, genFile, piece.ChainID, piece.Validators, piece.AppState)
|
|
|
|
}
|
|
|
|
|
|
|
|
// read in the genFile
|
|
|
|
bz, err = ioutil.ReadFile(genFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var genMap map[string]json.RawMessage
|
|
|
|
err = cdc.UnmarshalJSON(bz, &genMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-22 10:36:35 -07:00
|
|
|
}
|
2018-04-23 08:47:39 -07:00
|
|
|
appState := genMap["app_state"]
|
2018-04-22 10:36:35 -07:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// verify chain-ids are the same
|
|
|
|
if piece.ChainID != string(genMap["chain_id"]) {
|
|
|
|
return fmt.Errorf("piece chain id's are mismatched, %s != %s", piece.ChainID, genMap["chain_id"])
|
|
|
|
}
|
2018-04-22 10:36:35 -07:00
|
|
|
|
2018-04-23 08:47:39 -07:00
|
|
|
// combine the validator set
|
2018-04-22 10:36:35 -07:00
|
|
|
var validators []tmtypes.GenesisValidator
|
2018-04-23 08:47:39 -07:00
|
|
|
err = cdc.UnmarshalJSON(genMap["validators"], &validators)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
validators = append(validators, piece.Validators...)
|
2018-04-22 10:36:35 -07:00
|
|
|
|
|
|
|
// combine the app state
|
2018-04-23 17:05:58 -07:00
|
|
|
appState, err = appInit.AppendAppState(cdc, appState, piece.AppState)
|
2018-04-22 10:36:35 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the appended genesis file
|
2018-04-23 08:47:39 -07:00
|
|
|
return WriteGenesisFile(cdc, genFile, piece.ChainID, validators, appState)
|
2018-04-22 10:36:35 -07:00
|
|
|
|
2018-04-23 12:15:50 -07:00
|
|
|
// Add a persistent peer if the config (if it's not me)
|
|
|
|
myIP, err := externalIP()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if myIP == piece.IP {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ctx.Config.P2P.PersistentPeers += fmt.Sprintf(",%s@%s", piece.NodeID, piece.IP)
|
|
|
|
configFilePath := filepath.Join(viper.GetString("home"), "config", "config.toml") //TODO this is annoying should be easier to get
|
|
|
|
cfg.WriteConfigFile(configFilePath, ctx.Config)
|
2018-04-22 10:36:35 -07:00
|
|
|
|
|
|
|
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 {
|
2018-02-21 02:38:48 -08:00
|
|
|
// 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
|
2018-02-21 02:38:48 -08:00
|
|
|
if cmn.FileExists(privValFile) {
|
2018-04-06 17:25:08 -07:00
|
|
|
privValidator = pvm.LoadFilePV(privValFile)
|
2018-02-21 02:38:48 -08:00
|
|
|
} else {
|
2018-04-06 17:25:08 -07:00
|
|
|
privValidator = pvm.GenFilePV(privValFile)
|
2018-02-21 02:38:48 -08:00
|
|
|
privValidator.Save()
|
|
|
|
}
|
2018-04-20 16:55:22 -07:00
|
|
|
return privValidator.GetPubKey()
|
|
|
|
}
|
2018-02-21 02:38:48 -08:00
|
|
|
|
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)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-21 19:26:46 -07:00
|
|
|
out, err := AppendJSON(cdc, bz, "app_state", appState)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-20 16:55:22 -07:00
|
|
|
return ioutil.WriteFile(genesisConfigPath, out, 0600)
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
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-23 17:05:58 -07:00
|
|
|
// flags required for GenAppParams
|
|
|
|
Flags *pflag.FlagSet
|
|
|
|
|
|
|
|
// GenAppParams creates the core parameters initialization. It takes in a
|
|
|
|
// pubkey meant to represent the pubkey of the validator of this machine.
|
|
|
|
GenAppParams func(*wire.Codec, crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState, cliPrint json.RawMessage, err error)
|
|
|
|
|
|
|
|
// append appState1 with appState2
|
|
|
|
AppendAppState func(cdc *wire.Codec, appState1, appState2 json.RawMessage) (appState json.RawMessage, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// simple default application init
|
|
|
|
var DefaultAppInit = AppInit{
|
|
|
|
GenAppParams: SimpleGenAppParams,
|
|
|
|
}
|
2018-04-22 10:36:35 -07:00
|
|
|
|
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
|
2018-04-23 17:05:58 -07:00
|
|
|
|
2018-04-20 16:55:22 -07:00
|
|
|
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
|
|
|
|
}
|