Skip to content

Commit

Permalink
cmd/ethermint: use default Genesis when necessary
Browse files Browse the repository at this point in the history
Requires tendermint/tmlibs#35
Fixes cosmos#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
```
  • Loading branch information
odeke-em committed Aug 13, 2017
1 parent 763ea26 commit 2efc3fa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 46 additions & 16 deletions cmd/ethermint/init.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"os"
"path/filepath"

Expand All @@ -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)
}
Expand All @@ -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"
}
}
`,
}

0 comments on commit 2efc3fa

Please sign in to comment.