Skip to content

Commit 9c7913d

Browse files
authored
Added init sub-command (#2626)
1 parent 9f62fe7 commit 9c7913d

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

turbo/cli/make_app.go

+104
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@
22
package cli
33

44
import (
5+
"encoding/json"
6+
"os"
7+
8+
"github.com/ledgerwatch/erigon-lib/kv"
9+
"github.com/ledgerwatch/erigon/cmd/utils"
10+
"github.com/ledgerwatch/erigon/core"
511
"github.com/ledgerwatch/erigon/internal/debug"
612
"github.com/ledgerwatch/erigon/internal/flags"
13+
"github.com/ledgerwatch/erigon/node"
14+
"github.com/ledgerwatch/erigon/params"
715

16+
"github.com/ledgerwatch/log/v3"
817
"github.com/urfave/cli"
918
)
1019

@@ -24,5 +33,100 @@ func MakeApp(action func(*cli.Context), cliFlags []cli.Flag) *cli.App {
2433
debug.Exit()
2534
return nil
2635
}
36+
app.Commands = []cli.Command{initCommand}
2737
return app
2838
}
39+
40+
var initCommand = cli.Command{
41+
Action: MigrateFlags(initGenesis),
42+
Name: "init",
43+
Usage: "Bootstrap and initialize a new genesis block",
44+
ArgsUsage: "<genesisPath>",
45+
Flags: []cli.Flag{
46+
utils.DataDirFlag,
47+
},
48+
Category: "BLOCKCHAIN COMMANDS",
49+
Description: `
50+
The init command initializes a new genesis block and definition for the network.
51+
This is a destructive action and changes the network in which you will be
52+
participating.
53+
54+
It expects the genesis file as argument.`,
55+
}
56+
57+
// initGenesis will initialise the given JSON format genesis file and writes it as
58+
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
59+
func initGenesis(ctx *cli.Context) error {
60+
// Make sure we have a valid genesis JSON
61+
genesisPath := ctx.Args().First()
62+
if len(genesisPath) == 0 {
63+
utils.Fatalf("Must supply path to genesis JSON file")
64+
}
65+
66+
file, err := os.Open(genesisPath)
67+
if err != nil {
68+
utils.Fatalf("Failed to read genesis file: %v", err)
69+
}
70+
defer file.Close()
71+
72+
genesis := new(core.Genesis)
73+
if err := json.NewDecoder(file).Decode(genesis); err != nil {
74+
utils.Fatalf("invalid genesis file: %v", err)
75+
}
76+
77+
// Open and initialise both full and light databases
78+
stack := MakeConfigNodeDefault(ctx)
79+
defer stack.Close()
80+
81+
chaindb, err := node.OpenDatabase(stack.Config(), log.New(ctx), kv.ChainDB)
82+
if err != nil {
83+
utils.Fatalf("Failed to open database: %v", err)
84+
}
85+
_, hash, err := core.CommitGenesisBlock(chaindb, genesis)
86+
if err != nil {
87+
utils.Fatalf("Failed to write genesis block: %v", err)
88+
}
89+
chaindb.Close()
90+
log.Info("Successfully wrote genesis state", "hash", hash.Hash())
91+
return nil
92+
}
93+
94+
func MigrateFlags(action func(ctx *cli.Context) error) func(*cli.Context) error {
95+
return func(ctx *cli.Context) error {
96+
for _, name := range ctx.FlagNames() {
97+
if ctx.IsSet(name) {
98+
ctx.GlobalSet(name, ctx.String(name))
99+
}
100+
}
101+
return action(ctx)
102+
}
103+
}
104+
105+
func NewNodeConfig(ctx *cli.Context) *node.Config {
106+
nodeConfig := node.DefaultConfig
107+
// see simiar changes in `cmd/geth/config.go#defaultNodeConfig`
108+
if commit := params.GitCommit; commit != "" {
109+
nodeConfig.Version = params.VersionWithCommit(commit, "")
110+
} else {
111+
nodeConfig.Version = params.Version
112+
}
113+
nodeConfig.IPCPath = "erigon.ipc" // force-disable IPC endpoint
114+
nodeConfig.Name = "erigon"
115+
if ctx.GlobalIsSet(utils.DataDirFlag.Name) {
116+
nodeConfig.DataDir = ctx.GlobalString(utils.DataDirFlag.Name)
117+
}
118+
return &nodeConfig
119+
}
120+
121+
func MakeConfigNodeDefault(ctx *cli.Context) *node.Node {
122+
return makeConfigNode(NewNodeConfig(ctx))
123+
}
124+
125+
func makeConfigNode(config *node.Config) *node.Node {
126+
stack, err := node.New(config)
127+
if err != nil {
128+
utils.Fatalf("Failed to create Erigon node: %v", err)
129+
}
130+
131+
return stack
132+
}

turbo/node/node.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,15 @@ func NewNodeConfig() *node.Config {
115115
} else {
116116
nodeConfig.Version = params.Version
117117
}
118-
nodeConfig.IPCPath = "" // force-disable IPC endpoint
118+
nodeConfig.IPCPath = "erigon.ipc" // force-disable IPC endpoint
119119
nodeConfig.Name = "erigon"
120120
return &nodeConfig
121121
}
122122

123+
func MakeConfigNodeDefault() *node.Node {
124+
return makeConfigNode(NewNodeConfig())
125+
}
126+
123127
func makeConfigNode(config *node.Config) *node.Node {
124128
stack, err := node.New(config)
125129
if err != nil {

0 commit comments

Comments
 (0)