Skip to content

Commit

Permalink
init: raise error when overwriting old database
Browse files Browse the repository at this point in the history
  • Loading branch information
gligneul committed May 29, 2024
1 parent f84bb54 commit 5809f52
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,14 @@ func dirExists(path string) bool {
return info.IsDir()
}

func databaseEmpty(path string) bool {
entries, err := os.ReadDir(path)
if err != nil {
return true
}
return len(entries) == 0 || (len(entries) == 1 && entries[0].Name() == "LOCK")
}

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 !dirExists(path.Join(stack.InstanceDir(), "l2chaindata")) {
const errorFmt = "database was not found in %s, but it was found in %s (have you placed the database in the wrong directory?)"
Expand All @@ -305,11 +313,7 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
if dirExists(path.Join(grandParentDir, "l2chaindata")) {
return nil, nil, fmt.Errorf(errorFmt, stack.InstanceDir(), grandParentDir)
}
entries, err := os.ReadDir(stack.InstanceDir())
if err != nil {
return nil, nil, fmt.Errorf("failed to open database directory: %w", err)
}
if len(entries) > 1 || (len(entries) == 1 && entries[0].Name() != "LOCK") {
if !databaseEmpty(stack.InstanceDir()) {
return nil, nil, fmt.Errorf("found unexpected files in database directory '%s' (have you set --persistent.chain and --persistent.global-config correctly? If so, delete the database directory and try again)", stack.InstanceDir())
}
}
Expand Down Expand Up @@ -354,6 +358,10 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
}
}

if !databaseEmpty(stack.InstanceDir()) {
return nil, nil, fmt.Errorf("trying to overwrite old database directory '%s' (delete the database directory and try again)", stack.InstanceDir())
}

initFile, err := downloadInit(ctx, &config.Init)
if err != nil {
return nil, nil, err
Expand Down

0 comments on commit 5809f52

Please sign in to comment.