Skip to content

Commit

Permalink
Check whether temp directory exists in config
Browse files Browse the repository at this point in the history
The default temp directory is changed to system default. This change
introduces a utility function `directoryExists` which return true if a
given path exists. This utility function checks existance of aida-db and
db temp directory. In the case, db temp directory doesn't exist, system
default temporary directory is used instead.
  • Loading branch information
wsodsong committed Nov 13, 2023
1 parent 17718f5 commit d1b8dec
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
28 changes: 24 additions & 4 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ func setAidaDbRepositoryUrl(chainId ChainID) error {
return nil
}

// directoryExists returns true if a directory exists
func directoryExists(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true

}

// SetBlockRange checks the validity of a block range and return the first and last block as numbers.
func SetBlockRange(firstArg string, lastArg string, chainId ChainID) (uint64, uint64, error) {
var err error = nil
Expand Down Expand Up @@ -522,34 +531,45 @@ func updateConfigBlockRange(args []string, cfg *Config, mode ArgumentMode, log l
}

// adjustMissingConfigValues fill the missing values in the config
func adjustMissingConfigValues(cfg *Config) {
func adjustMissingConfigValues(cfg *Config) error {
// set default db variant if not provided.
if cfg.DbImpl == "carmen" && cfg.DbVariant == "" {
cfg.DbVariant = "go-file"
}

// --continue-on-failure implicitly enables transaction state validation
cfg.ValidateTxState = cfg.Validate || cfg.ValidateTxState || cfg.ContinueOnFailure

cfg.ValidateWorldState = cfg.Validate || cfg.ValidateWorldState

if cfg.RandomSeed < 0 {
cfg.RandomSeed = int64(rand.Uint32())
}

if _, err := os.Stat(cfg.AidaDb); !os.IsNotExist(err) {
// if AidaDB path is given, redirect source path to AidaDB.
if found := directoryExists(cfg.AidaDb); found {
OverwriteDbPathsByAidaDb(cfg)
}

if _, err := os.Stat(cfg.DeletionDb); os.IsNotExist(err) {
// TODO: can be deleted as AidaDB is the default data source.
if found := directoryExists(cfg.DeletionDb); found {
cfg.HasDeletedAccounts = true
} else {
cfg.HasDeletedAccounts = false
}

// in-memory StateDB cannot be kept after run.
if cfg.KeepDb && strings.Contains(cfg.DbVariant, "memory") {
cfg.KeepDb = false
}
if cfg.First == 0 {
cfg.SkipPriming = true
}

// if path doesn't exist, use system temp directory.
if found := directoryExists(cfg.DbTmp); !found {
cfg.DbTmp = os.TempDir()
}
return nil
}

// OverwriteDbPathsByAidaDb overwrites the paths of the DBs by the AidaDb path
Expand Down
18 changes: 16 additions & 2 deletions utils/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,7 @@ func TestUtilsConfig_adjustMissingConfigValuesValidationOff(t *testing.T) {
func TestUtilsConfig_adjustMissingConfigValuesDeletionDb(t *testing.T) {
// prepare mock config
cfg := &Config{
HasDeletedAccounts: true,
DeletionDb: "./test.db",
DeletionDb: "./test.db",
}

adjustMissingConfigValues(cfg)
Expand All @@ -735,6 +734,21 @@ func TestUtilsConfig_adjustMissingConfigValuesKeepStateDb(t *testing.T) {
}
}

// TestUtilsConfig_adjustMissingConfigValuesWrongDbTmp tests if temporary db path doesn't exist, system temp location is used instead.
func TestUtilsConfig_adjustMissingConfigValuesWrongDbTmp(t *testing.T) {
// prepare mock config
cfg := &Config{
DbTmp: "./wrong_path",
}

adjustMissingConfigValues(cfg)

// checks
if cfg.DbTmp != os.TempDir() {
t.Fatalf("failed to adjust temporary database location; got: %v; expected: %v", cfg.DbTmp, os.TempDir())
}
}

// createFakeAidaDb creates fake empty aidaDB with metadata for testing purposes
func createFakeAidaDb(cfg *Config) error {
// fake metadata values
Expand Down
2 changes: 1 addition & 1 deletion utils/default_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func createConfigFromFlags(ctx *cli.Context) *Config {
DeleteSourceDbs: getFlagValue(ctx, DeleteSourceDbsFlag).(bool),
DiagnosticServer: getFlagValue(ctx, DiagnosticServerFlag).(int64),
CompactDb: getFlagValue(ctx, CompactDbFlag).(bool),
HasDeletedAccounts: true,
HasDeletedAccounts: false,
KeepDb: getFlagValue(ctx, KeepDbFlag).(bool),
KeysNumber: getFlagValue(ctx, KeysNumberFlag).(int64),
MaxNumTransactions: getFlagValue(ctx, MaxNumTransactionsFlag).(int),
Expand Down
4 changes: 3 additions & 1 deletion utils/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package utils

import "github.com/urfave/cli/v2"
import (
"github.com/urfave/cli/v2"
)

// Command line options for common flags in record and replay.
var (
Expand Down

0 comments on commit d1b8dec

Please sign in to comment.