diff --git a/app/app.go b/app/app.go index 0b8fb6d2e3..b783ea5c6c 100644 --- a/app/app.go +++ b/app/app.go @@ -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 diff --git a/app/genesis.go b/app/genesis.go index afa94ecab3..8f96aa8bc9 100644 --- a/app/genesis.go +++ b/app/genesis.go @@ -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, ...]") } diff --git a/cmd/commands/init.go b/cmd/commands/init.go index 9d44167e46..695220445c 100644 --- a/cmd/commands/init.go +++ b/cmd/commands/init.go @@ -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", diff --git a/cmd/commands/reset.go b/cmd/commands/reset.go index 693e6107ee..d9688fd859 100644 --- a/cmd/commands/reset.go +++ b/cmd/commands/reset.go @@ -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 } diff --git a/cmd/commands/start.go b/cmd/commands/start.go index f9867e86c6..97946384a3 100644 --- a/cmd/commands/start.go +++ b/cmd/commands/start.go @@ -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")) diff --git a/demo/clean.sh b/demo/clean.sh index e2d519337d..e39f090eec 100644 --- a/demo/clean.sh +++ b/demo/clean.sh @@ -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 diff --git a/demo/data/chain1/basecoin/genesis.json b/demo/data/chain1/basecoin/genesis.json deleted file mode 100644 index 588fc86f4f..0000000000 --- a/demo/data/chain1/basecoin/genesis.json +++ /dev/null @@ -1,15 +0,0 @@ -[ - "base/chainID", "test_chain_1", - "base/account", { - "pub_key": { - "type": "ed25519", - "data": "B3588BDC92015ED3CDB6F57A86379E8C79A7111063610B7E625487C76496F4DF" - }, - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - } -] diff --git a/demo/data/chain1/tendermint/config.toml b/demo/data/chain1/config.toml similarity index 100% rename from demo/data/chain1/tendermint/config.toml rename to demo/data/chain1/config.toml diff --git a/demo/data/chain1/genesis.json b/demo/data/chain1/genesis.json new file mode 100644 index 0000000000..4b9ab3079a --- /dev/null +++ b/demo/data/chain1/genesis.json @@ -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 + } + ] + } + ] + } +} diff --git a/demo/data/chain1/basecoin/key.json b/demo/data/chain1/key.json similarity index 100% rename from demo/data/chain1/basecoin/key.json rename to demo/data/chain1/key.json diff --git a/demo/data/chain1/tendermint/priv_validator.json b/demo/data/chain1/priv_validator.json similarity index 100% rename from demo/data/chain1/tendermint/priv_validator.json rename to demo/data/chain1/priv_validator.json diff --git a/demo/data/chain1/tendermint/genesis.json b/demo/data/chain1/tendermint/genesis.json deleted file mode 100644 index 91830dd23f..0000000000 --- a/demo/data/chain1/tendermint/genesis.json +++ /dev/null @@ -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" - ] - } - ] -} diff --git a/demo/data/chain2/basecoin/genesis.json b/demo/data/chain2/basecoin/genesis.json deleted file mode 100644 index 05df04be5a..0000000000 --- a/demo/data/chain2/basecoin/genesis.json +++ /dev/null @@ -1,15 +0,0 @@ -[ - "base/chainID", "test_chain_2", - "base/account", { - "pub_key": { - "type": "ed25519", - "data": "0628C8E6C2D50B15764B443394E06C6A64F3082CE966A2A8C1A55A4D63D0FC5D" - }, - "coins": [ - { - "denom": "mycoin", - "amount": 9007199254740992 - } - ] - } -] diff --git a/demo/data/chain2/tendermint/config.toml b/demo/data/chain2/config.toml similarity index 100% rename from demo/data/chain2/tendermint/config.toml rename to demo/data/chain2/config.toml diff --git a/demo/data/chain2/genesis.json b/demo/data/chain2/genesis.json new file mode 100644 index 0000000000..2079970346 --- /dev/null +++ b/demo/data/chain2/genesis.json @@ -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 + } + ] + } + ] + } +} diff --git a/demo/data/chain2/basecoin/key.json b/demo/data/chain2/key.json similarity index 100% rename from demo/data/chain2/basecoin/key.json rename to demo/data/chain2/key.json diff --git a/demo/data/chain2/tendermint/priv_validator.json b/demo/data/chain2/priv_validator.json similarity index 100% rename from demo/data/chain2/tendermint/priv_validator.json rename to demo/data/chain2/priv_validator.json diff --git a/demo/data/chain2/tendermint/genesis.json b/demo/data/chain2/tendermint/genesis.json deleted file mode 100644 index 6c9f17c952..0000000000 --- a/demo/data/chain2/tendermint/genesis.json +++ /dev/null @@ -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" - ] - } - ] -} diff --git a/demo/start.sh b/demo/start.sh index 412f23d8b4..bfc6872350 100644 --- a/demo/start.sh +++ b/demo/start.sh @@ -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"