Skip to content

Commit 00fead9

Browse files
authored
cmd/utils: fix a startup issue on deleted chaindata but dangling ancients (#27989)
1 parent bce5c46 commit 00fead9

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

cmd/utils/flags.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2073,10 +2073,10 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.
20732073
// tryMakeReadOnlyDatabase try to open the chain database in read-only mode,
20742074
// or fallback to write mode if the database is not initialized.
20752075
func tryMakeReadOnlyDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
2076-
// If datadir doesn't exist we need to open db in write-mode
2077-
// so database engine can create files.
2076+
// If the database doesn't exist we need to open it in write-mode to allow
2077+
// the engine to create files.
20782078
readonly := true
2079-
if !common.FileExist(stack.ResolvePath("chaindata")) {
2079+
if rawdb.PreexistingDatabase(stack.ResolvePath("chaindata")) == "" {
20802080
readonly = false
20812081
}
20822082
return MakeChainDatabase(ctx, stack, readonly)

core/rawdb/database.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,10 @@ const (
326326
dbLeveldb = "leveldb"
327327
)
328328

329-
// hasPreexistingDb checks the given data directory whether a database is already
329+
// PreexistingDatabase checks the given data directory whether a database is already
330330
// instantiated at that location, and if so, returns the type of database (or the
331331
// empty string).
332-
func hasPreexistingDb(path string) string {
332+
func PreexistingDatabase(path string) string {
333333
if _, err := os.Stat(filepath.Join(path, "CURRENT")); err != nil {
334334
return "" // No pre-existing db
335335
}
@@ -367,7 +367,7 @@ func openKeyValueDatabase(o OpenOptions) (ethdb.Database, error) {
367367
}
368368
// Retrieve any pre-existing database's type and use that or the requested one
369369
// as long as there's no conflict between the two types
370-
existingDb := hasPreexistingDb(o.Directory)
370+
existingDb := PreexistingDatabase(o.Directory)
371371
if len(existingDb) != 0 && len(o.Type) != 0 && o.Type != existingDb {
372372
return nil, fmt.Errorf("db.engine choice was %v but found pre-existing %v database in specified data directory", o.Type, existingDb)
373373
}

0 commit comments

Comments
 (0)