From 27d24610ab9a5ba3d08fd6c2b65d3997f30d3733 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 28 Mar 2018 17:03:17 +0200 Subject: [PATCH 1/6] basecoind init --testnet (closes #718) --- examples/basecoin/cmd/basecoind/main.go | 10 ++-- mock/app.go | 5 +- mock/app_test.go | 2 +- server/init.go | 67 ++++++++++++++++++++++--- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 6a4f45956f..6ecf37c94f 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -10,6 +10,7 @@ import ( abci "github.com/tendermint/abci/types" "github.com/tendermint/tmlibs/cli" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -28,14 +29,11 @@ var ( // defaultOptions sets up the app_options for the // default genesis file -func defaultOptions(args []string) (json.RawMessage, error) { +func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { addr, secret, err := server.GenerateCoinKey() if err != nil { - return nil, err + return nil, "", nil, err } - fmt.Println("Secret phrase to access coins:") - fmt.Println(secret) - opts := fmt.Sprintf(`{ "accounts": [{ "address": "%s", @@ -47,7 +45,7 @@ func defaultOptions(args []string) (json.RawMessage, error) { ] }] }`, addr) - return json.RawMessage(opts), nil + return json.RawMessage(opts), secret, addr, nil } func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { diff --git a/mock/app.go b/mock/app.go index c7c6432902..20863dd993 100644 --- a/mock/app.go +++ b/mock/app.go @@ -6,6 +6,7 @@ import ( "path/filepath" abci "github.com/tendermint/abci/types" + cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -106,7 +107,7 @@ func InitChainer(key sdk.StoreKey) func(sdk.Context, abci.RequestInitChain) abci // GenInitOptions can be passed into InitCmd, // returns a static string of a few key-values that can be parsed // by InitChainer -func GenInitOptions(args []string) (json.RawMessage, error) { +func GenInitOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { opts := []byte(`{ "values": [ { @@ -119,5 +120,5 @@ func GenInitOptions(args []string) (json.RawMessage, error) { } ] }`) - return opts, nil + return opts, "", nil, nil } diff --git a/mock/app_test.go b/mock/app_test.go index 103530e501..47db93e1c5 100644 --- a/mock/app_test.go +++ b/mock/app_test.go @@ -21,7 +21,7 @@ func TestInitApp(t *testing.T) { require.NoError(t, err) // initialize it future-way - opts, err := GenInitOptions(nil) + opts, _, _, err := GenInitOptions(nil) require.NoError(t, err) req := abci.RequestInitChain{AppStateBytes: opts} app.InitChain(req) diff --git a/server/init.go b/server/init.go index 12e330dbc8..19eedda76a 100644 --- a/server/init.go +++ b/server/init.go @@ -2,18 +2,32 @@ package server import ( "encoding/json" + "fmt" "io/ioutil" "github.com/spf13/cobra" + "github.com/spf13/viper" cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" tcmd "github.com/tendermint/tendermint/cmd/tendermint/commands" cfg "github.com/tendermint/tendermint/config" + "github.com/tendermint/tendermint/p2p" tmtypes "github.com/tendermint/tendermint/types" ) +const ( + flagTestnet = "testnet" +) + +type testnetInformation struct { + Secret string `json:"secret"` + Account string `json:"account"` + Validator tmtypes.GenesisValidator `json:"validator"` + NodeID p2p.ID `json:"node_id"` +} + // InitCmd will initialize all files for tendermint, // along with proper app_state. // The application can pass in a function to generate @@ -24,17 +38,20 @@ func InitCmd(gen GenAppState, logger log.Logger) *cobra.Command { genAppState: gen, logger: logger, } - return &cobra.Command{ + cobraCmd := cobra.Command{ Use: "init", Short: "Initialize genesis files", RunE: cmd.run, } + cobraCmd.Flags().Bool(flagTestnet, false, "Output testnet information in JSON") + return &cobraCmd } // GenAppState can parse command-line and flag to // generate default app_state for the genesis file. +// Also must return generated seed and address // This is application-specific -type GenAppState func(args []string) (json.RawMessage, error) +type GenAppState func(args []string) (json.RawMessage, string, cmn.HexBytes, error) type initCmd struct { genAppState GenAppState @@ -42,13 +59,20 @@ type initCmd struct { } func (c initCmd) run(cmd *cobra.Command, args []string) error { + // Store testnet information as we go + var testnetInfo testnetInformation + + if viper.GetBool(flagTestnet) { + c.logger = log.NewFilter(c.logger, log.AllowError()) + } + // Run the basic tendermint initialization, // set up a default genesis with no app_options config, err := tcmd.ParseConfig() if err != nil { return err } - err = c.initTendermintFiles(config) + err = c.initTendermintFiles(config, &testnetInfo) if err != nil { return err } @@ -59,19 +83,38 @@ func (c initCmd) run(cmd *cobra.Command, args []string) error { } // Now, we want to add the custom app_state - appState, err := c.genAppState(args) + appState, secret, address, err := c.genAppState(args) if err != nil { return err } + testnetInfo.Secret = secret + testnetInfo.Account = address.String() + // And add them to the genesis file genFile := config.GenesisFile() - return addGenesisState(genFile, appState) + if err := addGenesisState(genFile, appState); err != nil { + return err + } + + if viper.GetBool(flagTestnet) { + nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile()) + if err != nil { + return err + } + testnetInfo.NodeID = nodeKey.ID() + out, err := json.MarshalIndent(testnetInfo, "", " ") + if err != nil { + return err + } + fmt.Println(string(out)) + } + return nil } // This was copied from tendermint/cmd/tendermint/commands/init.go // so we could pass in the config and the logger. -func (c initCmd) initTendermintFiles(config *cfg.Config) error { +func (c initCmd) initTendermintFiles(config *cfg.Config, info *testnetInformation) error { // private validator privValFile := config.PrivValidatorFile() var privValidator *tmtypes.PrivValidatorFS @@ -102,6 +145,18 @@ func (c initCmd) initTendermintFiles(config *cfg.Config) error { } c.logger.Info("Generated genesis file", "path", genFile) } + + // reload the config file and find our validator info + loadedDoc, err := tmtypes.GenesisDocFromFile(genFile) + if err != nil { + return err + } + for _, validator := range loadedDoc.Validators { + if validator.PubKey == privValidator.GetPubKey() { + info.Validator = validator + } + } + return nil } From bb66b852ef4354377533e67f6b21af76afe0071f Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 28 Mar 2018 17:52:18 +0200 Subject: [PATCH 2/6] Move keybase DB to ~/.basecoind/data (closes #644) --- Makefile | 2 -- client/keys/utils.go | 5 +++-- client/lcd/lcd_test.go | 2 ++ examples/kvstore/main.go | 7 ++++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 3a53f19446..e9565084d6 100644 --- a/Makefile +++ b/Makefile @@ -78,9 +78,7 @@ test_unit: test_cover: @rm -rf examples/basecoin/vendor/ - @rm -rf client/lcd/keys.db ~/.tendermint_test @bash tests/test_cover.sh - @rm -rf client/lcd/keys.db ~/.tendermint_test benchmark: @go test -bench=. $(PACKAGES) diff --git a/client/keys/utils.go b/client/keys/utils.go index c6239002ca..eed7abc6cc 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -2,6 +2,7 @@ package keys import ( "fmt" + "path/filepath" "github.com/spf13/viper" @@ -31,8 +32,8 @@ type KeyOutput struct { // GetKeyBase initializes a keybase based on the configuration func GetKeyBase() (keys.Keybase, error) { if keybase == nil { - rootDir := viper.GetString(cli.HomeFlag) - db, err := dbm.NewGoLevelDB(KeyDBName, rootDir) + rootDir := filepath.Join(viper.GetString(cli.HomeFlag), ".basecoind") + db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "data")) if err != nil { return nil, err } diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 1e7104dae2..1e956d768c 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -25,6 +25,7 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" tmrpc "github.com/tendermint/tendermint/rpc/lib/server" tmtypes "github.com/tendermint/tendermint/types" + "github.com/tendermint/tmlibs/cli" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -294,6 +295,7 @@ func TestTxs(t *testing.T) { // strt TM and the LCD in process, listening on their respective sockets func startTMAndLCD() (*nm.Node, net.Listener, error) { + viper.Set(cli.HomeFlag, os.ExpandEnv("$HOME")) kb, err := keys.GetKeyBase() // dbm.NewMemDB()) // :( if err != nil { return nil, nil, err diff --git a/examples/kvstore/main.go b/examples/kvstore/main.go index 9bcabb306a..31e4def8e3 100644 --- a/examples/kvstore/main.go +++ b/examples/kvstore/main.go @@ -3,8 +3,12 @@ package main import ( "fmt" "os" + "path/filepath" + + "github.com/spf13/viper" "github.com/tendermint/abci/server" + "github.com/tendermint/tmlibs/cli" cmn "github.com/tendermint/tmlibs/common" dbm "github.com/tendermint/tmlibs/db" "github.com/tendermint/tmlibs/log" @@ -17,7 +21,8 @@ func main() { logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "main") - db, err := dbm.NewGoLevelDB("basecoind", "data") + rootDir := viper.GetString(cli.HomeFlag) + db, err := dbm.NewGoLevelDB("basecoind", filepath.Join(rootDir, "data")) if err != nil { fmt.Println(err) os.Exit(1) From 44de18e8da3f674c7e4b547b360aba7ea9cb2811 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Wed, 28 Mar 2018 18:00:51 +0200 Subject: [PATCH 3/6] Use temporary dir for lcd keybase tests (closes #646) --- client/lcd/lcd_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index 1e956d768c..c0245ba86c 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -295,7 +295,7 @@ func TestTxs(t *testing.T) { // strt TM and the LCD in process, listening on their respective sockets func startTMAndLCD() (*nm.Node, net.Listener, error) { - viper.Set(cli.HomeFlag, os.ExpandEnv("$HOME")) + viper.Set(cli.HomeFlag, os.ExpandEnv("/tmp/$HOME")) kb, err := keys.GetKeyBase() // dbm.NewMemDB()) // :( if err != nil { return nil, nil, err From 0f9bd93bb1335126f8f7987a1f6ef0b185244ea1 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 29 Mar 2018 12:10:13 +0200 Subject: [PATCH 4/6] Switch a few directories (#644 #646) --- client/keys/utils.go | 2 +- client/lcd/lcd_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/client/keys/utils.go b/client/keys/utils.go index eed7abc6cc..ebebf5d263 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -32,7 +32,7 @@ type KeyOutput struct { // GetKeyBase initializes a keybase based on the configuration func GetKeyBase() (keys.Keybase, error) { if keybase == nil { - rootDir := filepath.Join(viper.GetString(cli.HomeFlag), ".basecoind") + rootDir := filepath.Join(viper.GetString(cli.HomeFlag), ".tlc") db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "data")) if err != nil { return nil, err diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index c0245ba86c..9bed2d3700 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -295,7 +295,7 @@ func TestTxs(t *testing.T) { // strt TM and the LCD in process, listening on their respective sockets func startTMAndLCD() (*nm.Node, net.Listener, error) { - viper.Set(cli.HomeFlag, os.ExpandEnv("/tmp/$HOME")) + viper.Set(cli.HomeFlag, os.TempDir()) kb, err := keys.GetKeyBase() // dbm.NewMemDB()) // :( if err != nil { return nil, nil, err From fd4e2c53edd4414685cf3d7db7e073ff980a25f9 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 29 Mar 2018 12:16:08 +0200 Subject: [PATCH 5/6] Update gaid for GenAppState change --- examples/gaia/gaiad/main.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/gaia/gaiad/main.go b/examples/gaia/gaiad/main.go index 0c4c49eec7..70a44d8cbc 100644 --- a/examples/gaia/gaiad/main.go +++ b/examples/gaia/gaiad/main.go @@ -9,6 +9,7 @@ import ( abci "github.com/tendermint/abci/types" "github.com/tendermint/tmlibs/cli" + cmn "github.com/tendermint/tmlibs/common" "github.com/tendermint/tmlibs/log" "github.com/cosmos/cosmos-sdk/baseapp" @@ -26,10 +27,10 @@ var ( // defaultOptions sets up the app_options for the // default genesis file -func defaultOptions(args []string) (json.RawMessage, error) { +func defaultOptions(args []string) (json.RawMessage, string, cmn.HexBytes, error) { addr, secret, err := server.GenerateCoinKey() if err != nil { - return nil, err + return nil, "", nil, err } fmt.Println("Secret phrase to access coins:") fmt.Println(secret) @@ -45,7 +46,7 @@ func defaultOptions(args []string) (json.RawMessage, error) { ] }] }`, addr) - return json.RawMessage(opts), nil + return json.RawMessage(opts), secret, addr, nil } func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { From e13b1f2ac9ade542527c1d46121648d9a44e2509 Mon Sep 17 00:00:00 2001 From: Christopher Goes Date: Thu, 29 Mar 2018 18:23:11 +0200 Subject: [PATCH 6/6] Change key DB to ~/.basecli/keys/keys.db --- client/keys/utils.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/keys/utils.go b/client/keys/utils.go index ebebf5d263..d1b3d3f657 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -32,8 +32,8 @@ type KeyOutput struct { // GetKeyBase initializes a keybase based on the configuration func GetKeyBase() (keys.Keybase, error) { if keybase == nil { - rootDir := filepath.Join(viper.GetString(cli.HomeFlag), ".tlc") - db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "data")) + rootDir := viper.GetString(cli.HomeFlag) + db, err := dbm.NewGoLevelDB(KeyDBName, filepath.Join(rootDir, "keys")) if err != nil { return nil, err }