diff --git a/cmd/nitro/init.go b/cmd/nitro/init.go index 0f98a01bec..0d10cb3294 100644 --- a/cmd/nitro/init.go +++ b/cmd/nitro/init.go @@ -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?)" @@ -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()) } } @@ -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