Skip to content

Commit

Permalink
test: add UT for resolveChainFreezerDir function
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Jan 15, 2024
1 parent fee8a25 commit a17fef4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 46 deletions.
4 changes: 2 additions & 2 deletions core/rawdb/accessors_trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func ParseStateScheme(provided string, disk ethdb.Database) (string, error) {
log.Info("State schema set to default", "scheme", "hash")
return HashScheme, nil
}
log.Info("State scheme set to already existing", "scheme", stored)
log.Info("State scheme set to already existing disk db", "scheme", stored)
return stored, nil // reuse scheme of persistent scheme
}
// If state scheme is specified, ensure it's compatible with
Expand All @@ -347,5 +347,5 @@ func ParseStateScheme(provided string, disk ethdb.Database) (string, error) {
log.Info("State scheme set by user", "scheme", provided)
return provided, nil
}
return "", fmt.Errorf("incompatible state scheme, stored: %s, provided: %s", stored, provided)
return "", fmt.Errorf("incompatible state scheme, stored: %s, user provided: %s", stored, provided)
}
4 changes: 4 additions & 0 deletions core/rawdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ func NewFreezerDb(db ethdb.KeyValueStore, frz, namespace string, readonly bool,

// resolveChainFreezerDir is a helper function which resolves the absolute path
// of chain freezer by considering backward compatibility.
//
// rules:
// 1. in path mode, block data is stored in chain dir and state data is in state dir.
// 2. in hash mode, block data is stored in chain dir or ancient dir(before big merge), no state dir.
func resolveChainFreezerDir(ancient string) string {
// Check if the chain freezer is already present in the specified
// sub folder, if not then two possibilities:
Expand Down
54 changes: 10 additions & 44 deletions core/rawdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func Test_resolveChainFreezerDir(t *testing.T) {
wantedResult string
}{
{
name: "run geth in pruned mode and chain dir is existent",
// chain dir is existent, so it should be returned.
name: "1",
fn: func(dir string) string {
path := fmt.Sprintf("%s%s", dir, mockChainFreezerPath)
if err := os.MkdirAll(path, 0700); err != nil {
Expand All @@ -47,7 +48,9 @@ func Test_resolveChainFreezerDir(t *testing.T) {
wantedResult: mockChainFreezerPath,
},
{
name: "run geth in path and pruned mode; chain is nonexistent and state is existent",
// chain dir is nonexistent and state dir is existent; so chain
// dir should be returned.
name: "2",
fn: func(dir string) string {
path := fmt.Sprintf("%s%s", dir, mockStateFreezerPath)
if err := os.MkdirAll(path, 0700); err != nil {
Expand All @@ -58,7 +61,9 @@ func Test_resolveChainFreezerDir(t *testing.T) {
wantedResult: mockChainFreezerPath,
},
{
name: "run geth in hash and pruned mode; ancient block data locates in ancient dir",
// both chain dir and state dir are nonexistent, if ancient dir is
// existent, so ancient dir should be returned.
name: "3",
fn: func(dir string) string {
path := fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
if err := os.MkdirAll(path, 0700); err != nil {
Expand All @@ -69,52 +74,13 @@ func Test_resolveChainFreezerDir(t *testing.T) {
wantedResult: mockAncientFreezerPath,
},
{
name: "run geth in pruned mode and there is no ancient dir",
// ancient dir is nonexistent, so chain dir should be returned.
name: "4",
fn: func(dir string) string {
return fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
},
wantedResult: mockChainFreezerPath,
},
{
name: "run geth in non-pruned mode; ancient is existent and state dir is nonexistent",
fn: func(dir string) string {
path := fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
if err := os.MkdirAll(path, 0700); err != nil {
t.Fatalf("Failed to mkdir all dirs, error: %v", err)
}
return fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
},
wantedResult: mockAncientFreezerPath,
},
// {
// name: "run geth in non-pruned mode and chain dir is existent",
// fn: func(dir string) string {
// path := fmt.Sprintf("%s%s", dir, mockChainFreezerPath)
// if err := os.MkdirAll(path, 0700); err != nil {
// t.Fatalf("Failed to mkdir all dirs, error: %v", err)
// }
// return fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
// },
// wantedResult: mockChainFreezerPath,
// },
// {
// name: "run geth in non-pruned mode, ancient and chain dir is nonexistent",
// fn: func(dir string) string {
// return fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
// },
// wantedResult: mockChainFreezerPath,
// },
// {
// name: "run geth in non-pruned mode; ancient dir is existent, chain and state dir is nonexistent",
// fn: func(dir string) string {
// path := fmt.Sprintf("%s%s", dir, mockStateFreezerPath)
// if err := os.MkdirAll(path, 0700); err != nil {
// t.Fatalf("Failed to mkdir all dirs, error: %v", err)
// }
// return fmt.Sprintf("%s%s", dir, mockAncientFreezerPath)
// },
// wantedResult: mockChainFreezerPath,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit a17fef4

Please sign in to comment.