Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NIT-2424] Improve error log when opening database #2353

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"sync"
Expand Down Expand Up @@ -316,6 +318,14 @@ func validateBlockChain(blockChain *core.BlockChain, chainConfig *params.ChainCo
return nil
}

func dirExists(path string) bool {
info, err := os.Stat(path)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}

func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeConfig, chainId *big.Int, cacheConfig *core.CacheConfig, persistentConfig *conf.PersistentConfig, l1Client arbutil.L1Interface, rollupAddrs chaininfo.RollupAddresses) (ethdb.Database, *core.BlockChain, error) {
if !config.Init.Force {
if readOnlyDb, err := stack.OpenDatabaseWithFreezerWithExtraOptions("l2chaindata", 0, 0, "", "l2chaindata/", true, persistentConfig.Pebble.ExtraOptions("l2chaindata")); err == nil {
Expand Down Expand Up @@ -389,6 +399,36 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
}
}

// Check if database was misplaced in parent dir
const errorFmt = "database was not found in %s, but it was found in %s (have you placed the database in the wrong directory?)"
parentDir := filepath.Dir(stack.InstanceDir())
if dirExists(path.Join(parentDir, "l2chaindata")) {
return nil, nil, fmt.Errorf(errorFmt, stack.InstanceDir(), parentDir)
}
grandParentDir := filepath.Dir(parentDir)
if dirExists(path.Join(grandParentDir, "l2chaindata")) {
return nil, nil, fmt.Errorf(errorFmt, stack.InstanceDir(), grandParentDir)
}

// Check if database directory is empty
entries, err := os.ReadDir(stack.InstanceDir())
if err != nil {
return nil, nil, fmt.Errorf("failed to open database dir %s: %w", stack.InstanceDir(), err)
}
unexpectedFiles := []string{}
for _, entry := range entries {
if entry.Name() != "LOCK" {
unexpectedFiles = append(unexpectedFiles, entry.Name())
}
}
if len(unexpectedFiles) > 0 {
if config.Init.Force {
return nil, nil, fmt.Errorf("trying to overwrite old database directory '%s' (delete the database directory and try again)", stack.InstanceDir())
}
firstThreeFilenames := strings.Join(unexpectedFiles[:min(len(unexpectedFiles), 3)], ", ")
return nil, nil, fmt.Errorf("found %d unexpected files in database directory, including: %s", len(unexpectedFiles), firstThreeFilenames)
}

if err := setLatestSnapshotUrl(ctx, &config.Init, config.Chain.Name); err != nil {
return nil, nil, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/nitro/nitro.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ func mainImpl() int {
deferFuncs = append(deferFuncs, func() { closeDb(arbDb, "arbDb") })
if err != nil {
log.Error("failed to open database", "err", err)
log.Error("database is corrupt; delete it and try again", "database-directory", stack.InstanceDir())
return 1
}

Expand Down
Loading