2017-03-13 14:23:05 -07:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2017-06-20 18:35:22 -07:00
|
|
|
"fmt"
|
2017-03-13 14:23:05 -07:00
|
|
|
"io/ioutil"
|
2017-03-29 22:30:55 -07:00
|
|
|
"os"
|
2017-06-20 23:47:42 -07:00
|
|
|
"path"
|
2017-03-13 14:23:05 -07:00
|
|
|
|
2017-03-08 23:19:07 -08:00
|
|
|
"github.com/spf13/cobra"
|
2017-06-26 05:18:36 -07:00
|
|
|
|
|
|
|
tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
|
2017-03-13 14:23:05 -07:00
|
|
|
)
|
|
|
|
|
2017-03-08 23:19:07 -08:00
|
|
|
//commands
|
|
|
|
var (
|
|
|
|
InitCmd = &cobra.Command{
|
|
|
|
Use: "init",
|
|
|
|
Short: "Initialize a basecoin blockchain",
|
2017-04-15 09:07:27 -07:00
|
|
|
RunE: initCmd,
|
2017-03-08 23:19:07 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-06-20 23:47:42 -07:00
|
|
|
//flags
|
|
|
|
var (
|
|
|
|
chainIDFlag string
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flags := []Flag2Register{
|
|
|
|
{&chainIDFlag, "chain-id", "test_chain_id", "Chain ID"},
|
|
|
|
}
|
|
|
|
RegisterFlags(InitCmd, flags)
|
|
|
|
}
|
|
|
|
|
2017-04-03 01:44:30 -07:00
|
|
|
// returns 1 iff it set a file, otherwise 0 (so we can add them)
|
2017-04-15 09:07:27 -07:00
|
|
|
func setupFile(path, data string, perm os.FileMode) (int, error) {
|
2017-04-03 01:44:30 -07:00
|
|
|
_, err := os.Stat(path)
|
2017-04-17 23:50:23 -07:00
|
|
|
if !os.IsNotExist(err) { //note, os.IsExist(err) != !os.IsNotExist(err)
|
2017-04-15 09:07:27 -07:00
|
|
|
return 0, nil
|
2017-04-03 01:44:30 -07:00
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(path, []byte(data), perm)
|
|
|
|
if err != nil {
|
2017-04-15 09:07:27 -07:00
|
|
|
return 0, err
|
2017-04-03 01:44:30 -07:00
|
|
|
}
|
2017-04-15 09:07:27 -07:00
|
|
|
return 1, nil
|
2017-04-03 01:44:30 -07:00
|
|
|
}
|
|
|
|
|
2017-04-15 09:07:27 -07:00
|
|
|
func initCmd(cmd *cobra.Command, args []string) error {
|
2017-05-24 06:21:56 -07:00
|
|
|
// this will ensure that config.toml is there if not yet created, and create dir
|
2017-06-26 05:18:36 -07:00
|
|
|
cfg, err := tcmd.ParseConfig()
|
2017-05-24 06:21:56 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-13 14:23:05 -07:00
|
|
|
|
2017-06-20 18:35:22 -07:00
|
|
|
if len(args) != 1 {
|
|
|
|
return fmt.Errorf("`init` takes one argument, a basecoin account address. Generate one using `basecli keys new mykey`")
|
|
|
|
}
|
|
|
|
userAddr := args[0]
|
|
|
|
|
2017-03-13 14:23:05 -07:00
|
|
|
// initalize basecoin
|
2017-05-24 06:21:56 -07:00
|
|
|
genesisFile := cfg.GenesisFile()
|
|
|
|
privValFile := cfg.PrivValidatorFile()
|
2017-06-20 21:32:59 -07:00
|
|
|
keyFile := path.Join(cfg.RootDir, "key.json")
|
2017-03-13 14:23:05 -07:00
|
|
|
|
2017-06-20 23:47:42 -07:00
|
|
|
mod1, err := setupFile(genesisFile, GetGenesisJSON(chainIDFlag, userAddr), 0644)
|
2017-04-15 09:07:27 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mod2, err := setupFile(privValFile, PrivValJSON, 0400)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-20 23:47:42 -07:00
|
|
|
mod3, err := setupFile(keyFile, KeyJSON, 0400)
|
2017-06-20 21:32:59 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-03-29 22:30:55 -07:00
|
|
|
|
2017-06-20 21:32:59 -07:00
|
|
|
if (mod1 + mod2 + mod3) > 0 {
|
2017-06-20 18:35:22 -07:00
|
|
|
logger.Info("Initialized Basecoin", "genesis", genesisFile, "priv_validator", privValFile)
|
2017-03-29 22:30:55 -07:00
|
|
|
} else {
|
2017-05-01 07:03:54 -07:00
|
|
|
logger.Info("Already initialized", "priv_validator", privValFile)
|
2017-03-13 14:23:05 -07:00
|
|
|
}
|
2017-04-15 09:07:27 -07:00
|
|
|
|
|
|
|
return nil
|
2017-03-13 14:23:05 -07:00
|
|
|
}
|
|
|
|
|
2017-04-01 14:31:44 -07:00
|
|
|
var PrivValJSON = `{
|
2017-04-30 15:21:41 -07:00
|
|
|
"address": "7A956FADD20D3A5B2375042B2959F8AB172A058F",
|
|
|
|
"last_height": 0,
|
|
|
|
"last_round": 0,
|
|
|
|
"last_signature": null,
|
|
|
|
"last_signbytes": "",
|
|
|
|
"last_step": 0,
|
|
|
|
"priv_key": {
|
2017-04-27 12:14:40 -07:00
|
|
|
"type": "ed25519",
|
2017-04-30 15:21:41 -07:00
|
|
|
"data": "D07ABE82A8B15559A983B2DB5D4842B2B6E4D6AF58B080005662F424F17D68C17B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
|
|
|
|
},
|
2017-04-27 12:14:40 -07:00
|
|
|
"pub_key": {
|
|
|
|
"type": "ed25519",
|
2017-04-30 15:21:41 -07:00
|
|
|
"data": "7B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
|
|
|
|
}
|
2017-03-14 00:11:49 -07:00
|
|
|
}`
|
|
|
|
|
2017-06-20 18:35:22 -07:00
|
|
|
// GetGenesisJSON returns a new tendermint genesis with Basecoin app_options
|
|
|
|
// that grant a large amount of "mycoin" to a single address
|
|
|
|
// TODO: A better UX for generating genesis files
|
2017-06-20 23:47:42 -07:00
|
|
|
func GetGenesisJSON(chainID, addr string) string {
|
2017-06-20 18:35:22 -07:00
|
|
|
return fmt.Sprintf(`{
|
2017-03-14 00:11:49 -07:00
|
|
|
"app_hash": "",
|
2017-06-20 23:47:42 -07:00
|
|
|
"chain_id": "%s",
|
2017-03-14 00:11:49 -07:00
|
|
|
"genesis_time": "0001-01-01T00:00:00.000Z",
|
|
|
|
"validators": [
|
|
|
|
{
|
|
|
|
"amount": 10,
|
|
|
|
"name": "",
|
2017-04-27 12:14:40 -07:00
|
|
|
"pub_key": {
|
|
|
|
"type": "ed25519",
|
2017-04-30 15:21:41 -07:00
|
|
|
"data": "7B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
|
2017-04-27 12:14:40 -07:00
|
|
|
}
|
2017-03-14 00:11:49 -07:00
|
|
|
}
|
|
|
|
],
|
|
|
|
"app_options": {
|
|
|
|
"accounts": [{
|
2017-06-20 18:35:22 -07:00
|
|
|
"address": "%s",
|
2017-03-14 00:11:49 -07:00
|
|
|
"coins": [
|
|
|
|
{
|
|
|
|
"denom": "mycoin",
|
|
|
|
"amount": 9007199254740992
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}]
|
2017-03-13 14:23:05 -07:00
|
|
|
}
|
2017-06-20 23:47:42 -07:00
|
|
|
}`, chainID, addr)
|
2017-06-20 18:35:22 -07:00
|
|
|
}
|
2017-06-20 21:32:59 -07:00
|
|
|
|
|
|
|
// TODO: remove this once not needed for relay
|
|
|
|
var KeyJSON = `{
|
|
|
|
"address": "1B1BE55F969F54064628A63B9559E7C21C925165",
|
|
|
|
"priv_key": {
|
|
|
|
"type": "ed25519",
|
|
|
|
"data": "C70D6934B4F55F1B7BC33B56B9CA8A2061384AFC19E91E44B40C4BBA182953D1619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
|
|
|
|
},
|
|
|
|
"pub_key": {
|
|
|
|
"type": "ed25519",
|
|
|
|
"data": "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
|
|
|
|
}
|
|
|
|
}`
|