Skip to content

Commit

Permalink
cmd/geth: Add genesis flag (#9)
Browse files Browse the repository at this point in the history
### Description

add `--genesis` flag, ignore read genesis block from database

### Rationale

`SetEthConfig` adds process for handling Genesis JSON file

### Example

```sh
./geth --datadir=/data/dbsc --genesis=/data/genesis-dbsc-devnet.json
```

### Changes

Notable changes: 
* add `--genesis` flag
  • Loading branch information
0xcb9ff9 authored and DarianShawn committed Apr 19, 2023
1 parent d484cce commit 1f51681
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/geth/accountcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package main

import (
"fmt"
"io/ioutil"
"os"

"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -320,7 +320,7 @@ func importWallet(ctx *cli.Context) error {
if len(keyfile) == 0 {
utils.Fatalf("keyfile must be given as argument")
}
keyJSON, err := ioutil.ReadFile(keyfile)
keyJSON, err := os.ReadFile(keyfile)
if err != nil {
utils.Fatalf("Could not read wallet file: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var (
app = flags.NewApp(gitCommit, gitDate, "the go-ethereum command line interface")
// flags that configure the node
nodeFlags = []cli.Flag{
utils.GenesisFlag,
utils.IdentityFlag,
utils.UnlockedAccountFlag,
utils.PasswordFileFlag,
Expand Down
30 changes: 28 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package utils

import (
"bytes"
"crypto/ecdsa"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math"
"math/big"
"os"
Expand Down Expand Up @@ -111,6 +112,11 @@ func printHelp(out io.Writer, templ string, data interface{}) {
// are the same for all commands.

var (
// Genesis settings
GenesisFlag = cli.StringFlag{
Name: "genesis",
Usage: "Path to genesis JSON file, ignore genesis block from database",
}
// General settings
DataDirFlag = DirectoryFlag{
Name: "datadir",
Expand Down Expand Up @@ -1181,6 +1187,25 @@ func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error
return accs[index], nil
}

// setGenesis loads the genesis block from the given path
func setGenesis(ctx *cli.Context, cfg *ethconfig.Config) {
var genesisPath string
if ctx.GlobalIsSet(GenesisFlag.Name) {
genesisPath = ctx.GlobalString(GenesisFlag.Name)
}
if genesisPath != "" {
genesis := new(core.Genesis)
file, err := os.ReadFile(genesisPath)
if err != nil {
Fatalf("Failed to read genesis file: %v", err)
}
if err := json.NewDecoder(bytes.NewReader(file)).Decode(genesis); err != nil {
Fatalf("invalid genesis file: %v", err)
}
cfg.Genesis = genesis
}
}

// setEtherbase retrieves the etherbase either from the directly specified
// command line flags or from the keystore if CLI indexed.
func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *ethconfig.Config) {
Expand Down Expand Up @@ -1209,7 +1234,7 @@ func MakePasswordList(ctx *cli.Context) []string {
if path == "" {
return nil
}
text, err := ioutil.ReadFile(path)
text, err := os.ReadFile(path)
if err != nil {
Fatalf("Failed to read password file: %v", err)
}
Expand Down Expand Up @@ -1570,6 +1595,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
ks = keystores[0].(*keystore.KeyStore)
}
setGenesis(ctx, cfg)
setEtherbase(ctx, ks, cfg)
setGPO(ctx, &cfg.GPO, ctx.GlobalString(SyncModeFlag.Name) == "light")
setTxPool(ctx, &cfg.TxPool)
Expand Down

0 comments on commit 1f51681

Please sign in to comment.