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
|
|
|
"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-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 = "ip"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2018-04-24 22:37:46 -07:00
|
|
|
appGenTx, toPrint, validator, err := appInit.GenAppTx(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-24 22:37:46 -07:00
|
|
|
name := fmt.Sprintf("gentx-%v.json", nodeID)
|
|
|
|
file := filepath.Join(viper.GetString("home"), name)
|
2018-04-20 16:55:22 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-04-24 22:37:46 -07:00
|
|
|
err = cmn.WriteFile(file, bz, 0644)
|
2018-04-20 16:55:22 -07:00
|
|
|
}
|
2018-04-23 12:15:50 -07:00
|
|
|
|
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-02-14 12:53:06 -08:00
|
|
|
}
|
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")
|
2018-04-24 22:37:46 -07:00
|
|
|
cmd.Flags().AddFlagSet(appInit.FlagsAppTx)
|
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 appGenTxs, cliPrints []json.RawMessage
|
|
|
|
var validators []tmtypes.GenesisValidator
|
|
|
|
var persistentPeers string
|
|
|
|
genTxsDir := viper.GetString(flagGenTxs)
|
|
|
|
if genTxsDir != "" {
|
|
|
|
validators, persistentPeers, appGenTxs, cliPrints = processGenTxs(genTxsDir, cdc, appInit)
|
|
|
|
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 {
|
|
|
|
appTx, cliPrint, validator, err := appInit.GenAppTx(cdc, pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
validators = []tmtypes.GenesisValidator{validator}
|
|
|
|
appGenTxs = []json.RawMessage{appTx}
|
|
|
|
cliPrints = []json.RawMessage{cliPrint}
|
|
|
|
}
|
|
|
|
|
|
|
|
appState, err := appInit.GenAppParams(cdc, appGenTxs)
|
|
|
|
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_messages"`
|
|
|
|
}{
|
|
|
|
chainID,
|
|
|
|
nodeID,
|
|
|
|
cliPrints,
|
|
|
|
}
|
|
|
|
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, "", "designated chain-id for the genesis")
|
|
|
|
cmd.Flags().AddFlagSet(appInit.FlagsAppParams)
|
|
|
|
return cmd
|
2018-02-14 12:53:06 -08:00
|
|
|
}
|
|
|
|
|
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, cliPrints []json.RawMessage, persistentPeers string, err error) {
|
2018-04-22 10:36:35 -07:00
|
|
|
|
2018-04-24 22:37:46 -07:00
|
|
|
fos, err := ioutil.ReadDir(genTxsDir)
|
|
|
|
for _, fo := range fos {
|
|
|
|
filename := fo.Name()
|
|
|
|
if !fo.IsDir() && (path.Ext(filename) != ".json") {
|
|
|
|
return nil
|
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
|
|
|
|
bz, err := ioutil.ReadFile(filename)
|
2018-04-23 08:47:39 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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 err
|
|
|
|
}
|
|
|
|
|
2018-04-24 22:37:46 -07:00
|
|
|
// combine some stuff
|
|
|
|
validators = append(validators, genTx.Validator)
|
|
|
|
appGenTxs = append(appGenTxs, genTx.AppGenTxs)
|
|
|
|
cliPrints = append(cliPrints, genTx.CliPrints)
|
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 = ""
|
|
|
|
}
|
2018-04-24 22:37:46 -07:00
|
|
|
persistentPeers += fmt.Sprintf("%s%s@%s:46656", comma, piece.NodeID, piece.IP)
|
2018-04-22 10:36:35 -07:00
|
|
|
}
|
2018-04-24 22:37:46 -07:00
|
|
|
|
|
|
|
return nil
|
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 {
|
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-24 22:37:46 -07:00
|
|
|
// flags required for application init functions
|
|
|
|
FlagsAppParams *pflag.FlagSet
|
|
|
|
FlagsAppTx *pflag.FlagSet
|
2018-04-23 17:05:58 -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-24 22:37:46 -07:00
|
|
|
GenAppParams 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
|
|
|
|
GenAppTx func(cdc *wire.Codec, pk crypto.PubKey) (
|
|
|
|
appTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error)
|
2018-04-23 17:05:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-24 22:37:46 -07:00
|
|
|
func SimpleGenAppParams(cdc *wire.Codec, pubKey crypto.PubKey, _ json.RawMessage) (
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|