Skip to content

Commit cad0710

Browse files
authored
Merge pull request #14412 from karalabe/init-both-chains
cmd/geth, cmd/utils: init/removedb on light/full dbs simultaneously
2 parents 02fa3e3 + 181a330 commit cad0710

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

cmd/geth/chaincmd.go

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ participating.
5858
ArgsUsage: "<filename> (<filename 2> ... <filename N>) ",
5959
Category: "BLOCKCHAIN COMMANDS",
6060
Description: `
61-
The import command imports blocks from an RLP-encoded form. The form can be one file
62-
with several RLP-encoded blocks, or several files can be used.
63-
If only one file is used, import error will result in failure. If several files are used,
64-
processing will proceed even if an individual RLP-file import failure occurs.
61+
The import command imports blocks from an RLP-encoded form. The form can be one file
62+
with several RLP-encoded blocks, or several files can be used.
63+
If only one file is used, import error will result in failure. If several files are used,
64+
processing will proceed even if an individual RLP-file import failure occurs.
6565
`,
6666
}
6767
exportCommand = cli.Command{
@@ -103,30 +103,34 @@ Use "ethereum dump 0" to dump the genesis block.
103103
// initGenesis will initialise the given JSON format genesis file and writes it as
104104
// the zero'd block (i.e. genesis) or will fail hard if it can't succeed.
105105
func initGenesis(ctx *cli.Context) error {
106+
// Make sure we have a valid genesis JSON
106107
genesisPath := ctx.Args().First()
107108
if len(genesisPath) == 0 {
108-
utils.Fatalf("must supply path to genesis JSON file")
109+
utils.Fatalf("Must supply path to genesis JSON file")
109110
}
110-
111-
stack := makeFullNode(ctx)
112-
chaindb := utils.MakeChainDatabase(ctx, stack)
113-
114111
file, err := os.Open(genesisPath)
115112
if err != nil {
116-
utils.Fatalf("failed to read genesis file: %v", err)
113+
utils.Fatalf("Failed to read genesis file: %v", err)
117114
}
118115
defer file.Close()
119116

120117
genesis := new(core.Genesis)
121118
if err := json.NewDecoder(file).Decode(genesis); err != nil {
122119
utils.Fatalf("invalid genesis file: %v", err)
123120
}
124-
125-
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
126-
if err != nil {
127-
utils.Fatalf("failed to write genesis block: %v", err)
121+
// Open an initialise both full and light databases
122+
stack := makeFullNode(ctx)
123+
for _, name := range []string{"chaindata", "lightchaindata"} {
124+
chaindb, err := stack.OpenDatabase(name, 0, 0)
125+
if err != nil {
126+
utils.Fatalf("Failed to open database: %v", err)
127+
}
128+
_, hash, err := core.SetupGenesisBlock(chaindb, genesis)
129+
if err != nil {
130+
utils.Fatalf("Failed to write genesis block: %v", err)
131+
}
132+
log.Info("Successfully wrote genesis state", "database", name, "hash", hash)
128133
}
129-
log.Info("Successfully wrote genesis state", "hash", hash)
130134
return nil
131135
}
132136

@@ -245,24 +249,29 @@ func exportChain(ctx *cli.Context) error {
245249

246250
func removeDB(ctx *cli.Context) error {
247251
stack, _ := makeConfigNode(ctx)
248-
dbdir := stack.ResolvePath(utils.ChainDbName(ctx))
249-
if !common.FileExist(dbdir) {
250-
fmt.Println(dbdir, "does not exist")
251-
return nil
252-
}
253252

254-
fmt.Println(dbdir)
255-
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
256-
switch {
257-
case err != nil:
258-
utils.Fatalf("%v", err)
259-
case !confirm:
260-
fmt.Println("Operation aborted")
261-
default:
262-
fmt.Println("Removing...")
263-
start := time.Now()
264-
os.RemoveAll(dbdir)
265-
fmt.Printf("Removed in %v\n", time.Since(start))
253+
for _, name := range []string{"chaindata", "lightchaindata"} {
254+
// Ensure the database exists in the first place
255+
logger := log.New("database", name)
256+
257+
dbdir := stack.ResolvePath(name)
258+
if !common.FileExist(dbdir) {
259+
logger.Info("Database doesn't exist, skipping", "path", dbdir)
260+
continue
261+
}
262+
// Confirm removal and execute
263+
fmt.Println(dbdir)
264+
confirm, err := console.Stdin.PromptConfirm("Remove this database?")
265+
switch {
266+
case err != nil:
267+
utils.Fatalf("%v", err)
268+
case !confirm:
269+
logger.Warn("Database deletion aborted")
270+
default:
271+
start := time.Now()
272+
os.RemoveAll(dbdir)
273+
logger.Info("Database successfully deleted", "elapsed", common.PrettyDuration(time.Since(start)))
274+
}
266275
}
267276
return nil
268277
}

cmd/utils/flags.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -908,22 +908,16 @@ func SetupNetwork(ctx *cli.Context) {
908908
params.TargetGasLimit = new(big.Int).SetUint64(ctx.GlobalUint64(TargetGasLimitFlag.Name))
909909
}
910910

911-
func ChainDbName(ctx *cli.Context) string {
912-
if ctx.GlobalBool(LightModeFlag.Name) {
913-
return "lightchaindata"
914-
} else {
915-
return "chaindata"
916-
}
917-
}
918-
919911
// MakeChainDatabase open an LevelDB using the flags passed to the client and will hard crash if it fails.
920912
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
921913
var (
922914
cache = ctx.GlobalInt(CacheFlag.Name)
923915
handles = makeDatabaseHandles()
924-
name = ChainDbName(ctx)
925916
)
926-
917+
name := "chaindata"
918+
if ctx.GlobalBool(LightModeFlag.Name) {
919+
name = "lightchaindata"
920+
}
927921
chainDb, err := stack.OpenDatabase(name, cache, handles)
928922
if err != nil {
929923
Fatalf("Could not open database: %v", err)

0 commit comments

Comments
 (0)