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-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-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-20 16:55:22 -07:00
|
|
|
// get cmd to initialize all files for tendermint and application
|
|
|
|
func InitCmd(gen GenAppParams, ctx *Context) *cobra.Command {
|
2018-03-28 08:03:17 -07:00
|
|
|
cobraCmd := cobra.Command{
|
2018-02-14 12:53:06 -08:00
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize genesis files",
|
2018-04-20 16:55:22 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
|
|
|
config := ctx.Config
|
|
|
|
pubkey := ReadOrCreatePrivValidator(config)
|
|
|
|
|
|
|
|
chainID, validators, appState, err := gen(pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = CreateGenesisFile(config, chainID, validators, appState)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// print out some key information
|
|
|
|
toPrint := struct {
|
|
|
|
ChainID string `json:"chain_id"`
|
|
|
|
NodeID string `json:"node_id"`
|
|
|
|
}{
|
|
|
|
chainID,
|
|
|
|
string(nodeKey.ID()),
|
|
|
|
}
|
|
|
|
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-03-28 08:03:17 -07:00
|
|
|
return &cobraCmd
|
2018-02-14 12:53:06 -08: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
|
|
|
|
func CreateGenesisFile(tmConfig *cfg.Config, chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage) error {
|
|
|
|
genFile := tmConfig.GenesisFile()
|
2018-02-21 02:38:48 -08:00
|
|
|
if cmn.FileExists(genFile) {
|
2018-04-20 16:55:22 -07:00
|
|
|
return fmt.Errorf("genesis config file already exists: %v", genFile)
|
2018-02-21 02:38:48 -08:00
|
|
|
}
|
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-20 16:55:22 -07:00
|
|
|
if err := genDoc.SaveAs(genFile); err != nil {
|
2018-04-20 14:42:56 -07:00
|
|
|
return err
|
|
|
|
}
|
2018-04-20 16:55:22 -07:00
|
|
|
return addAppStateToGenesis(genFile, appState)
|
2018-04-05 03:31:33 -07:00
|
|
|
}
|
|
|
|
|
2018-04-20 16:55:22 -07:00
|
|
|
// Add one line to the genesis file
|
|
|
|
func addAppStateToGenesis(genesisConfigPath string, appState json.RawMessage) error {
|
|
|
|
bz, err := ioutil.ReadFile(genesisConfigPath)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-04-20 16:55:22 -07:00
|
|
|
var doc map[string]json.RawMessage
|
2018-04-09 10:32:19 -07:00
|
|
|
err = cdc.UnmarshalJSON(bz, &doc)
|
2018-02-14 12:53:06 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-14 12:22:06 -07:00
|
|
|
doc["app_state"] = appState
|
2018-04-09 10:32:19 -07:00
|
|
|
out, err := wire.MarshalJSONIndent(cdc, doc)
|
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-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.
|
|
|
|
type GenAppParams func(crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error)
|
|
|
|
|
|
|
|
// Create one account with a whole bunch of mycoin in it
|
|
|
|
func SimpleGenAppState(pubKey crypto.PubKey) (chainID string, validators []tmtypes.GenesisValidator, appState json.RawMessage, err error) {
|
|
|
|
|
|
|
|
var addr sdk.Address
|
|
|
|
var secret string
|
|
|
|
addr, secret, err = GenerateCoinKey()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fmt.Printf("secret recovery key:\n%s\n", secret)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|