Skip to content

Commit

Permalink
init: improve checks
Browse files Browse the repository at this point in the history
  • Loading branch information
gligneul committed May 29, 2024
1 parent 5809f52 commit 9552235
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions cmd/nitro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,6 @@ func databaseEmpty(path string) bool {
}

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?)"
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)
}
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())
}
}
if !config.Init.Force {
if readOnlyDb, err := stack.OpenDatabaseWithFreezerWithExtraOptions("l2chaindata", 0, 0, "", "l2chaindata/", true, persistentConfig.Pebble.ExtraOptions("l2chaindata")); err == nil {
if chainConfig := gethexec.TryReadStoredChainConfig(readOnlyDb); chainConfig != nil {
Expand Down Expand Up @@ -356,10 +342,21 @@ func openInitializeChainDb(ctx context.Context, stack *node.Node, config *NodeCo
}
readOnlyDb.Close()
}
} else 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())
}

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)
}
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())
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())
}

initFile, err := downloadInit(ctx, &config.Init)
Expand Down

0 comments on commit 9552235

Please sign in to comment.