From 2efc3fa5ce4115e07947a1df58bc1d4641bbddfa Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sat, 12 Aug 2017 18:11:19 -0600 Subject: [PATCH] cmd/ethermint: use default Genesis when necessary Requires https://github.com/tendermint/tmlibs/pull/35 Fixes https://github.com/tendermint/ethermint/issues/244 Act like `tendermint init` so that if we don't have a genesis-path, and also move to $HOME/.ethermint/keystore, the files that are currently contained in $GOPATH/src/github.com/tendermint/ethermint/setup/keystore Therefore now `ethermint init` should be one step e.g ```shell $ ethermint --datadir ~/.ethermint init ``` instead of the formerly tedious ```shell $ cd $GOPATH/src/github.com/tendermint/ethermint $ ethermint --datadir ~/.ethermint init setup/genesis.json $ cp -r setup/keystore ~/.ethermint ``` --- README.md | 11 ++++---- cmd/ethermint/init.go | 62 ++++++++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 0bbd69bb64..5818e438ed 100644 --- a/README.md +++ b/README.md @@ -103,13 +103,14 @@ these instructions. ```bash tendermint init --home ~/.ethermint/tendermint -cd $GOPATH/src/github.com/tendermint/ethermint - -ethermint --datadir ~/.ethermint init setup/genesis.json +ethermint --datadir ~/.ethermint init +``` -cp -r setup/keystore ~/.ethermint +* Note: +You can optionally copy a keystore to the Ethereum folder that you used in the steps above i.e `~/.ethermint` e.g +```bash +cp -r keystore ~/.ethermint ``` -In the last step we copy the private key from the initialisation folder into the actual ethereum folder. ### Running diff --git a/cmd/ethermint/init.go b/cmd/ethermint/init.go index 519588cdaa..8b7474206e 100644 --- a/cmd/ethermint/init.go +++ b/cmd/ethermint/init.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "os" "path/filepath" @@ -13,29 +12,19 @@ import ( "github.com/ethereum/go-ethereum/log" emtUtils "github.com/tendermint/ethermint/cmd/utils" + "github.com/tendermint/tmlibs/common" ) // nolint: vetshadow func initCmd(ctx *cli.Context) error { - - // ethereum genesis.json genesisPath := ctx.Args().First() - if len(genesisPath) == 0 { - ethUtils.Fatalf("must supply path to genesis JSON file") - } - - file, err := os.Open(genesisPath) + genesis, err := common.ParseGenesisOrDefault(genesisPath) if err != nil { - ethUtils.Fatalf("Failed to read genesis file: %v", err) + ethUtils.Fatalf("genesisJSON err: %v", err) } - defer file.Close() // nolint: errcheck - genesis := new(core.Genesis) - if err := json.NewDecoder(file).Decode(genesis); err != nil { - ethUtils.Fatalf("invalid genesis file: %v", err) - } - - chainDb, err := ethdb.NewLDBDatabase(filepath.Join(emtUtils.MakeDataDir(ctx), "ethermint/chaindata"), 0, 0) + ethermintDataDir := emtUtils.MakeDataDir(ctx) + chainDb, err := ethdb.NewLDBDatabase(filepath.Join(ethermintDataDir, "ethermint/chaindata"), 0, 0) if err != nil { ethUtils.Fatalf("could not open database: %v", err) } @@ -46,5 +35,46 @@ func initCmd(ctx *cli.Context) error { } log.Info("successfully wrote genesis block and/or chain rule set", "hash", hash) + + // As per https://github.com/tendermint/ethermint/issues/244#issuecomment-322024199 + // Let's implicitly add in the respective keystore files + // to avoid manually doing this step: + // $ cp -r $GOPATH/src/github.com/tendermint/ethermint/setup/keystore $(DATADIR) + keystoreDir := filepath.Join(ethermintDataDir, "keystore") + if err := os.MkdirAll(keystoreDir, 0777); err != nil { + ethUtils.Fatalf("mkdirAll keyStoreDir: %v", err) + } + + for filename, content := range keystoreFilesMap { + storeFileName := filepath.Join(keystoreDir, filename) + f, err := os.Create(storeFileName) + if err != nil { + log.Error("create %q err: %v", storeFileName, err) + continue + } + if _, err := f.Write([]byte(content)); err != nil { + log.Error("write content %q err: %v", storeFileName, err) + } + f.Close() + } + return nil } + +var keystoreFilesMap = map[string]string{ + // https://github.com/tendermint/ethermint/blob/edc95f9d47ba1fb7c8161182533b5f5d5c5d619b/setup/keystore/UTC--2016-10-21T22-30-03.071787745Z--7eff122b94897ea5b0e2a9abf47b86337fafebdc + // OR + // $GOPATH/src/github.com/ethermint/setup/keystore/UTC--2016-10-21T22-30-03.071787745Z--7eff122b94897ea5b0e2a9abf47b86337fafebdc + "UTC--2016-10-21T22-30-03.071787745Z--7eff122b94897ea5b0e2a9abf47b86337fafebdc": ` +{ + "address":"7eff122b94897ea5b0e2a9abf47b86337fafebdc", + "id":"f86a62b4-0621-4616-99af-c4b7f38fcc48","version":3, + "crypto":{ + "cipher":"aes-128-ctr","ciphertext":"19de8a919e2f4cbdde2b7352ebd0be8ead2c87db35fc8e4c9acaf74aaaa57dad", + "cipherparams":{"iv":"ba2bd370d6c9d5845e92fbc6f951c792"}, + "kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"c7cc2380a96adc9eb31d20bd8d8a7827199e8b16889582c0b9089da6a9f58e84"}, + "mac":"ff2c0caf051ca15d8c43b6f321ec10bd99bd654ddcf12dd1a28f730cc3c13730" + } +} +`, +}