Skip to content

Commit

Permalink
cmd/utils: fix a startup issue on deleted chaindata but dangling anci…
Browse files Browse the repository at this point in the history
…ents (#27989)
  • Loading branch information
karalabe authored Aug 23, 2023
1 parent bce5c46 commit 00fead9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2073,10 +2073,10 @@ func MakeChainDatabase(ctx *cli.Context, stack *node.Node, readonly bool) ethdb.
// tryMakeReadOnlyDatabase try to open the chain database in read-only mode,
// or fallback to write mode if the database is not initialized.
func tryMakeReadOnlyDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
// If datadir doesn't exist we need to open db in write-mode
// so database engine can create files.
// If the database doesn't exist we need to open it in write-mode to allow
// the engine to create files.
readonly := true
if !common.FileExist(stack.ResolvePath("chaindata")) {
if rawdb.PreexistingDatabase(stack.ResolvePath("chaindata")) == "" {
readonly = false
}
return MakeChainDatabase(ctx, stack, readonly)
Expand Down
6 changes: 3 additions & 3 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ const (
dbLeveldb = "leveldb"
)

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

0 comments on commit 00fead9

Please sign in to comment.