From a17fef4f155b354d7d79924d30cb6cc8cb16cc3a Mon Sep 17 00:00:00 2001 From: VM Date: Mon, 15 Jan 2024 11:09:41 +0800 Subject: [PATCH] test: add UT for resolveChainFreezerDir function --- core/rawdb/accessors_trie.go | 4 +-- core/rawdb/database.go | 4 +++ core/rawdb/database_test.go | 54 +++++++----------------------------- 3 files changed, 16 insertions(+), 46 deletions(-) diff --git a/core/rawdb/accessors_trie.go b/core/rawdb/accessors_trie.go index cd90647346..5248fbecab 100644 --- a/core/rawdb/accessors_trie.go +++ b/core/rawdb/accessors_trie.go @@ -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 @@ -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) } diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 8a79ae9de5..ea300d5fd9 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -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: diff --git a/core/rawdb/database_test.go b/core/rawdb/database_test.go index bd5fa0ccc5..baec1663d3 100644 --- a/core/rawdb/database_test.go +++ b/core/rawdb/database_test.go @@ -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 { @@ -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 { @@ -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 { @@ -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) {