forked from cosmos/ethermint-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/ethermint: use default Genesis when necessary
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
Showing
8 changed files
with
209 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io/ioutil" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/ethereum/go-ethereum/core" | ||
) | ||
|
||
// defaultGenesisBlob is the JSON representation of the default | ||
// genesis file in $GOPATH/src/github.com/tendermint/ethermint/setup/genesis.json | ||
var defaultGenesisBlob = []byte(` | ||
{ | ||
"config": { | ||
"chainId": 15, | ||
"homesteadBlock": 0, | ||
"eip155Block": 0, | ||
"eip158Block": 0 | ||
}, | ||
"nonce": "0xdeadbeefdeadbeef", | ||
"timestamp": "0x00", | ||
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"difficulty": "0x40", | ||
"gasLimit": "0x8000000", | ||
"alloc": { | ||
"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc": { "balance": "10000000000000000000000000000000000" }, | ||
"0xc6713982649D9284ff56c32655a9ECcCDA78422A": { "balance": "10000000000000000000000000000000000" } | ||
} | ||
}`) | ||
|
||
var blankGenesis = new(core.Genesis) | ||
|
||
var errBlankGenesis = errors.New("could not parse a valid/non-blank Genesis") | ||
|
||
// ParseGenesisOrDefault tries to read the content from provided | ||
// genesisPath. If the path is empty or doesn't exist, it will | ||
// use defaultGenesisBytes as the fallback genesis source. Otherwise, | ||
// it will open that path and if it encounters an error that doesn't | ||
// satisfy os.IsNotExist, it returns that error. | ||
func ParseGenesisOrDefault(genesisPath string) (*core.Genesis, error) { | ||
var genesisBlob = defaultGenesisBlob[:] | ||
if len(genesisPath) > 0 { | ||
blob, err := ioutil.ReadFile(genesisPath) | ||
if err != nil && !os.IsNotExist(err) { | ||
return nil, err | ||
} | ||
if len(blob) >= 2 { // Expecting atleast "{}" | ||
genesisBlob = blob | ||
} | ||
} | ||
|
||
genesis := new(core.Genesis) | ||
if err := json.Unmarshal(genesisBlob, genesis); err != nil { | ||
return nil, err | ||
} | ||
|
||
if reflect.DeepEqual(blankGenesis, genesis) { | ||
return nil, errBlankGenesis | ||
} | ||
|
||
return genesis, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"math/big" | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
ethCommon "github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core" | ||
) | ||
|
||
var defaultGenesis *core.Genesis = func() *core.Genesis { | ||
g := new(core.Genesis) | ||
if err := json.Unmarshal(defaultGenesisBlob, g); err != nil { | ||
log.Fatalf("parsing defaultGenesis: %v", err) | ||
} | ||
return g | ||
}() | ||
|
||
func bigString(s string) *big.Int { | ||
b, _ := big.NewInt(0).SetString(s, 10) | ||
return b | ||
} | ||
|
||
var genesis1 = &core.Genesis{ | ||
Difficulty: big.NewInt(0x40), | ||
GasLimit: 0x8000000, | ||
Alloc: core.GenesisAlloc{ | ||
ethCommon.HexToAddress("0x7eff122b94897ea5b0e2a9abf47b86337fafebdc"): { | ||
Balance: bigString("10000000000000000000000000000000000"), | ||
}, | ||
ethCommon.HexToAddress("0xc6713982649D9284ff56c32655a9ECcCDA78422A"): { | ||
Balance: bigString("10000000000000000000000000000000000"), | ||
}, | ||
}, | ||
} | ||
|
||
func TestParseGenesisOrDefault(t *testing.T) { | ||
tests := [...]struct { | ||
path string | ||
want *core.Genesis | ||
wantErr bool | ||
}{ | ||
0: {path: "", want: defaultGenesis}, | ||
1: {want: defaultGenesis}, | ||
2: {path: fmt.Sprintf("non-existent-%d", rand.Int()), want: defaultGenesis}, | ||
3: {path: "./testdata/blank-genesis.json", want: defaultGenesis}, | ||
4: {path: "./testdata/genesis1.json", want: genesis1}, | ||
5: {path: "./testdata/non-genesis.json", wantErr: true}, | ||
} | ||
|
||
for i, tt := range tests { | ||
gen, err := ParseGenesisOrDefault(tt.path) | ||
if tt.wantErr { | ||
assert.NotNil(t, err, "#%d: cannot be nil", i) | ||
continue | ||
} | ||
|
||
if err != nil { | ||
t.Errorf("#%d: path=%q unexpected error: %v", i, tt.path, err) | ||
continue | ||
} | ||
|
||
assert.NotEqual(t, blankGenesis, gen, true, "#%d: expecting a non-blank", i) | ||
assert.Equal(t, gen, tt.want, "#%d: expected them to be the same", i) | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"difficulty": "0x40", | ||
"gasLimit": "0x8000000", | ||
"alloc": { | ||
"0x7eff122b94897ea5b0e2a9abf47b86337fafebdc": { "balance": "10000000000000000000000000000000000" }, | ||
"0xc6713982649D9284ff56c32655a9ECcCDA78422A": { "balance": "10000000000000000000000000000000000" } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"tendermint": true, | ||
"introduction": "https://tendermint.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters