consolidate genesis files

This commit is contained in:
Ethan Buchman 2017-03-14 03:11:49 -04:00
parent 4a6b7131e4
commit 03ea00f1e6
19 changed files with 180 additions and 141 deletions

View File

@ -65,7 +65,7 @@ func (app *Basecoin) SetOption(key string, value string) string {
} else {
// Set option on basecoin
switch key {
case "chainID":
case "chain_id":
app.state.SetChainID(value)
return "Success"
case "account":
@ -75,7 +75,8 @@ func (app *Basecoin) SetOption(key string, value string) string {
return "Error decoding acc message: " + err.Error()
}
app.state.SetAccount(acc.PubKey.Address(), &acc)
log.Info("SetAccount", "addr", acc.PubKey.Address(), "acc", acc)
log.Notice("SetAccount", "addr", acc.PubKey.Address(), "acc", acc)
return "Success"
}
return "Unrecognized option key " + key

View File

@ -2,18 +2,32 @@ package app
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
tmtypes "github.com/tendermint/tendermint/types"
)
func (app *Basecoin) LoadGenesis(path string) error {
kvz, err := loadGenesis(path)
tmDoc, appDoc, err := loadGenesis(path)
if err != nil {
return err
}
for _, kv := range kvz {
app.SetOption(kv.Key, kv.Value)
fmt.Println("TMGendoc", tmDoc)
fmt.Println("AppGendoc", appDoc)
app.SetOption("base/chain_id", appDoc.ChainID)
for _, acc := range appDoc.Accounts {
accBytes, err := json.Marshal(acc)
if err != nil {
return err
}
r := app.SetOption("base/account", string(accBytes))
// TODO: SetOption returns an error
log.Notice("SetOption", "result", r)
}
return nil
}
@ -23,16 +37,37 @@ type keyValue struct {
Value string `json:"value"`
}
func loadGenesis(filePath string) (kvz []keyValue, err error) {
kvz_ := []json.RawMessage{}
// includes tendermint (in the json, we ignore here)
type FullGenesisDoc struct {
AppOptions *GenesisDoc `json:"app_options"`
}
type GenesisDoc struct {
ChainID string `json:"chain_id"`
Accounts []types.Account `json:"accounts"`
}
func loadGenesis(filePath string) (*tmtypes.GenesisDoc, *GenesisDoc, error) {
bytes, err := cmn.ReadFile(filePath)
if err != nil {
return nil, errors.Wrap(err, "loading genesis file")
return nil, nil, errors.Wrap(err, "loading genesis file")
}
err = json.Unmarshal(bytes, &kvz_)
tmGenesis := new(tmtypes.GenesisDoc)
appGenesis := new(FullGenesisDoc)
// the tendermint genesis is go-wire
err = wire.ReadJSONBytes(bytes, tmGenesis)
// the basecoin genesis go-data :)
err = json.Unmarshal(bytes, appGenesis)
if err != nil {
return nil, errors.Wrap(err, "parsing genesis file")
return nil, nil, errors.Wrap(err, "unmarshaling genesis file")
}
return tmGenesis, appGenesis.AppOptions, nil
}
func parseGenesisList(kvz_ []json.RawMessage) (kvz []keyValue, err error) {
if len(kvz_)%2 != 0 {
return nil, errors.New("genesis cannot have an odd number of items. Format = [key1, value1, key2, value2, ...]")
}

View File

@ -2,14 +2,11 @@ package commands
import (
"io/ioutil"
"os"
"path"
"github.com/urfave/cli"
cmn "github.com/tendermint/go-common"
tmcfg "github.com/tendermint/tendermint/config/tendermint"
types "github.com/tendermint/tendermint/types"
)
var InitCmd = cli.Command{
@ -25,44 +22,22 @@ var InitCmd = cli.Command{
}
func cmdInit(c *cli.Context) error {
basecoinDir := BasecoinRoot("")
tmDir := path.Join(basecoinDir, "tendermint")
rootDir := BasecoinRoot("")
// initalize tendermint
tmConfig := tmcfg.GetConfig(tmDir)
privValFile := tmConfig.GetString("priv_validator_file")
if _, err := os.Stat(privValFile); os.IsNotExist(err) {
privValidator := types.GenPrivValidator()
privValidator.SetFile(privValFile)
privValidator.Save()
genFile := tmConfig.GetString("genesis_file")
if _, err := os.Stat(genFile); os.IsNotExist(err) {
genDoc := types.GenesisDoc{
ChainID: cmn.Fmt("test-chain-%v", cmn.RandStr(6)),
}
genDoc.Validators = []types.GenesisValidator{types.GenesisValidator{
PubKey: privValidator.PubKey,
Amount: 10,
}}
genDoc.SaveAs(genFile)
}
log.Notice("Initialized Tendermint", "genesis", tmConfig.GetString("genesis_file"), "priv_validator", tmConfig.GetString("priv_validator_file"))
} else {
log.Notice("Already initialized Tendermint", "priv_validator", tmConfig.GetString("priv_validator_file"))
}
cmn.EnsureDir(rootDir, 0777)
// initalize basecoin
genesisFile := path.Join(basecoinDir, "genesis.json")
key1File := path.Join(basecoinDir, "key.json")
key2File := path.Join(basecoinDir, "key2.json")
genesisFile := path.Join(rootDir, "genesis.json")
privValFile := path.Join(rootDir, "priv_validator.json")
key1File := path.Join(rootDir, "key.json")
key2File := path.Join(rootDir, "key2.json")
if err := ioutil.WriteFile(genesisFile, []byte(genesisJSON), 0644); err != nil {
return err
}
if err := ioutil.WriteFile(privValFile, []byte(privValJSON), 0400); err != nil {
return err
}
if err := ioutil.WriteFile(key1File, []byte(key1JSON), 0400); err != nil {
return err
}
@ -75,21 +50,53 @@ func cmdInit(c *cli.Context) error {
return nil
}
const genesisJSON = `[
"base/chainID", "test_chain_id",
"base/account", {
"pub_key": {
"type": "ed25519",
"data": "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
const privValJSON = `{
"address": "7A956FADD20D3A5B2375042B2959F8AB172A058F",
"last_height": 0,
"last_round": 0,
"last_signature": null,
"last_signbytes": "",
"last_step": 0,
"priv_key": [
1,
"D07ABE82A8B15559A983B2DB5D4842B2B6E4D6AF58B080005662F424F17D68C17B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
],
"pub_key": [
1,
"7B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
]
}`
const genesisJSON = `{
"app_hash": "",
"chain_id": "test-chain-Ppk1h3",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"7B90EA87E7DC0C7145C8C48C08992BE271C7234134343E8A8E8008E617DE7B30"
]
}
],
"app_options": {
"chain_id": "test_chain_id",
"accounts": [{
"pub_key": {
"type": "ed25519",
"data": "619D3678599971ED29C7529DDD4DA537B97129893598A17C82E3AC9A8BA95279"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}]
}
]`
}`
const key1JSON = `{
"address": "1B1BE55F969F54064628A63B9559E7C21C925165",

View File

@ -21,7 +21,7 @@ var UnsafeResetAllCmd = cli.Command{
func cmdUnsafeResetAll(c *cli.Context) error {
basecoinDir := BasecoinRoot("")
tmDir := path.Join(basecoinDir, "tendermint")
tmDir := path.Join(basecoinDir)
tmConfig := tmcfg.GetConfig(tmDir)
// Get and Reset PrivValidator
@ -41,12 +41,7 @@ func cmdUnsafeResetAll(c *cli.Context) error {
// Remove all tendermint data
tmDataDir := tmConfig.GetString("db_dir")
os.RemoveAll(tmDataDir)
log.Notice("Removed Tendermint data", "dir", tmDataDir)
// Remove all basecoin data
basecoinDataDir := path.Join(basecoinDir, "merkleeyes.db")
os.RemoveAll(basecoinDataDir)
log.Notice("Removed Basecoin data", "dir", basecoinDataDir)
log.Notice("Removed all data", "dir", tmDataDir)
return nil
}

View File

@ -56,7 +56,7 @@ func cmdStart(c *cli.Context) error {
// Connect to MerkleEyes
var eyesCli *eyes.Client
if c.String("eyes") == "local" {
eyesCli = eyes.NewLocalClient(path.Join(basecoinDir, "merkleeyes.db"), EyesCacheSize)
eyesCli = eyes.NewLocalClient(path.Join(basecoinDir, "data", "merkleeyes.db"), EyesCacheSize)
} else {
var err error
eyesCli, err = eyes.NewClient(c.String("eyes"))
@ -117,7 +117,7 @@ func startBasecoinABCI(c *cli.Context, basecoinApp *app.Basecoin) error {
func startTendermint(dir string, basecoinApp *app.Basecoin) {
// Get configuration
tmConfig := tmcfg.GetConfig(path.Join(dir, "tendermint"))
tmConfig := tmcfg.GetConfig(dir)
// logger.SetLogLevel("notice") //config.GetString("log_level"))

View File

@ -1,13 +1,10 @@
#! /bin/bash
killall -9 basecoin tendermint
TMROOT=./data/chain1/tendermint tendermint unsafe_reset_all
TMROOT=./data/chain2/tendermint tendermint unsafe_reset_all
rm -rf ./data/chain1/basecoin/merkleeyes.db
rm -rf ./data/chain2/basecoin/merkleeyes.db
TMROOT=./data/chain1 tendermint unsafe_reset_all
TMROOT=./data/chain2 tendermint unsafe_reset_all
rm ./*.log
rm ./data/chain1/tendermint/*.bak
rm ./data/chain2/tendermint/*.bak
rm ./data/chain1/*.bak
rm ./data/chain2/*.bak

View File

@ -1,15 +0,0 @@
[
"base/chainID", "test_chain_1",
"base/account", {
"pub_key": {
"type": "ed25519",
"data": "B3588BDC92015ED3CDB6F57A86379E8C79A7111063610B7E625487C76496F4DF"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}
]

View File

@ -0,0 +1,32 @@
{
"app_hash": "",
"chain_id": "test_chain_1",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"D6EBB92440CF375054AA59BCF0C99D596DEEDFFB2543CAE1BA1908B72CF9676A"
]
}
],
"app_options": {
"chain_id": "test_chain_1",
"accounts": [
{
"pub_key": {
"type": "ed25519",
"data": "B3588BDC92015ED3CDB6F57A86379E8C79A7111063610B7E625487C76496F4DF"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}
]
}
}

View File

@ -1,15 +0,0 @@
{
"app_hash": "",
"chain_id": "test_chain_1",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"D6EBB92440CF375054AA59BCF0C99D596DEEDFFB2543CAE1BA1908B72CF9676A"
]
}
]
}

View File

@ -1,15 +0,0 @@
[
"base/chainID", "test_chain_2",
"base/account", {
"pub_key": {
"type": "ed25519",
"data": "0628C8E6C2D50B15764B443394E06C6A64F3082CE966A2A8C1A55A4D63D0FC5D"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}
]

View File

@ -0,0 +1,32 @@
{
"app_hash": "",
"chain_id": "test_chain_2",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"9A76DDE4CA4EE660C073D288DBE4F8A128F23857881A95F18167682D47E7058F"
]
}
],
"app_options": {
"chain_id": "test_chain_2",
"accounts": [
{
"pub_key": {
"type": "ed25519",
"data": "0628C8E6C2D50B15764B443394E06C6A64F3082CE966A2A8C1A55A4D63D0FC5D"
},
"coins": [
{
"denom": "mycoin",
"amount": 9007199254740992
}
]
}
]
}
}

View File

@ -1,15 +0,0 @@
{
"app_hash": "",
"chain_id": "test_chain_2",
"genesis_time": "0001-01-01T00:00:00.000Z",
"validators": [
{
"amount": 10,
"name": "",
"pub_key": [
1,
"9A76DDE4CA4EE660C073D288DBE4F8A128F23857881A95F18167682D47E7058F"
]
}
]
}

View File

@ -62,13 +62,13 @@ function waitForBlock() {
# make basecoin root vars
export BCHOME="."
BCHOME1="./data/chain1/basecoin"
BCHOME2="./data/chain2/basecoin"
BCHOME1="./data/chain1"
BCHOME2="./data/chain2"
# grab the chain ids
CHAIN_ID1=$(cat $BCHOME1/genesis.json | jq .[1])
CHAIN_ID1=$(cat $BCHOME1/genesis.json | jq .chain_id)
CHAIN_ID1=$(removeQuotes $CHAIN_ID1)
CHAIN_ID2=$(cat $BCHOME2/genesis.json | jq .[1])
CHAIN_ID2=$(cat $BCHOME2/genesis.json | jq .chain_id)
CHAIN_ID2=$(removeQuotes $CHAIN_ID2)
echo "CHAIN_ID1: $CHAIN_ID1"
echo "CHAIN_ID2: $CHAIN_ID2"
@ -82,11 +82,11 @@ echo ""
echo "... starting chains"
echo ""
# start the first node
TMROOT=./data/chain1/tendermint tendermint node --skip_upnp --log_level=info &> $LOG_DIR/chain1_tendermint.log &
TMROOT=./data/chain1 tendermint node --skip_upnp --log_level=info &> $LOG_DIR/chain1_tendermint.log &
BCHOME=$BCHOME1 basecoin start --without-tendermint &> $LOG_DIR/chain1_basecoin.log &
# start the second node
TMROOT=./data/chain2/tendermint tendermint node --skip_upnp --log_level=info --node_laddr tcp://localhost:36656 --rpc_laddr tcp://localhost:36657 --proxy_app tcp://localhost:36658 &> $LOG_DIR/chain2_tendermint.log &
TMROOT=./data/chain2 tendermint node --skip_upnp --log_level=info --node_laddr tcp://localhost:36656 --rpc_laddr tcp://localhost:36657 --proxy_app tcp://localhost:36658 &> $LOG_DIR/chain2_tendermint.log &
BCHOME=$BCHOME2 basecoin start --address tcp://localhost:36658 --without-tendermint &> $LOG_DIR/chain2_basecoin.log &
echo ""
@ -103,7 +103,7 @@ sleep 3
echo "... registering chain1 on chain2"
echo ""
# register chain1 on chain2
basecoin tx ibc --amount 10mycoin $CHAIN_FLAGS2 register --chain_id $CHAIN_ID1 --genesis ./data/chain1/tendermint/genesis.json
basecoin tx ibc --amount 10mycoin $CHAIN_FLAGS2 register --chain_id $CHAIN_ID1 --genesis ./data/chain1/genesis.json
echo ""
echo "... creating egress packet on chain1"