core/rawdb, node: fix era path resolution

This commit is contained in:
Gary Rong 2025-04-18 17:17:42 +08:00 committed by lightclient
parent d65c46526d
commit 6a5e0b40cd
No known key found for this signature in database
GPG key ID: 657913021EF45A6A
5 changed files with 30 additions and 22 deletions

View file

@ -114,6 +114,7 @@ var (
EraFlag = &flags.DirectoryFlag{ EraFlag = &flags.DirectoryFlag{
Name: "datadir.era", Name: "datadir.era",
Usage: "Root directory for era1 history (default = inside ancient/chain)", Usage: "Root directory for era1 history (default = inside ancient/chain)",
Category: flags.EthCategory,
} }
MinFreeDiskSpaceFlag = &flags.DirectoryFlag{ MinFreeDiskSpaceFlag = &flags.DirectoryFlag{
Name: "datadir.minfreedisk", Name: "datadir.minfreedisk",

View file

@ -186,6 +186,18 @@ func resolveChainFreezerDir(ancient string) string {
return freezer return freezer
} }
// resolveChainEraDir is a helper function which resolves the absolute path of era database.
func resolveChainEraDir(chainFreezerDir string, era string) string {
switch {
case era == "":
return filepath.Join(chainFreezerDir, "era")
case !filepath.IsAbs(era):
return filepath.Join(chainFreezerDir, era)
default:
return era
}
}
// NewDatabaseWithFreezer creates a high level database on top of a given key- // NewDatabaseWithFreezer creates a high level database on top of a given key-
// value data store with a freezer moving immutable chain segments into cold // value data store with a freezer moving immutable chain segments into cold
// storage. The passed ancient indicates the path of root ancient directory // storage. The passed ancient indicates the path of root ancient directory
@ -198,6 +210,9 @@ func NewDatabaseWithFreezer(db ethdb.KeyValueStore, ancient string, namespace st
if chainFreezerDir != "" { if chainFreezerDir != "" {
chainFreezerDir = resolveChainFreezerDir(chainFreezerDir) chainFreezerDir = resolveChainFreezerDir(chainFreezerDir)
} }
if chainFreezerDir != "" {
eraDir = resolveChainEraDir(chainFreezerDir, eraDir)
}
frdb, err := newChainFreezer(chainFreezerDir, namespace, readonly, eraDir) frdb, err := newChainFreezer(chainFreezerDir, namespace, readonly, eraDir)
if err != nil { if err != nil {
printChainMetadata(db) printChainMetadata(db)

View file

@ -74,7 +74,7 @@ func New(datadir string) (*EraDatabase, error) {
// opened multiple times. // opened multiple times.
db.cache.OnReplaced(closeEra) db.cache.OnReplaced(closeEra)
log.Info("Opened erastore", "datadir", datadir) log.Info("Opened Era store", "datadir", datadir)
return db, nil return db, nil
} }

View file

@ -32,6 +32,9 @@ type openOptions struct {
Type string // "leveldb" | "pebble" Type string // "leveldb" | "pebble"
Directory string // the datadir Directory string // the datadir
AncientsDirectory string // the ancients-dir AncientsDirectory string // the ancients-dir
// The optional Era folder, which can be either a subfolder under
// ancient/chain or a directory specified via an absolute path.
EraDirectory string EraDirectory string
Namespace string // the namespace for database relevant metrics Namespace string // the namespace for database relevant metrics
Cache int // the capacity(in megabytes) of the data caching Cache int // the capacity(in megabytes) of the data caching

View file

@ -746,7 +746,7 @@ func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, ancient
Type: n.config.DBEngine, Type: n.config.DBEngine,
Directory: n.ResolvePath(name), Directory: n.ResolvePath(name),
AncientsDirectory: n.ResolveAncient(name, ancient), AncientsDirectory: n.ResolveAncient(name, ancient),
EraDirectory: n.ResolveEraDirectory(ancient, era), EraDirectory: era,
Namespace: namespace, Namespace: namespace,
Cache: cache, Cache: cache,
Handles: handles, Handles: handles,
@ -775,17 +775,6 @@ func (n *Node) ResolveAncient(name string, ancient string) string {
return ancient return ancient
} }
// ResolveEraDirectory returns the absolute path of the era directory.
func (n *Node) ResolveEraDirectory(ancient, era string) string {
switch {
case era == "":
era = filepath.Join(ancient, "era")
case !filepath.IsAbs(era):
era = n.ResolvePath(era)
}
return era
}
// closeTrackingDB wraps the Close method of a database. When the database is closed by the // closeTrackingDB wraps the Close method of a database. When the database is closed by the
// service, the wrapper removes it from the node's database map. This ensures that Node // service, the wrapper removes it from the node's database map. This ensures that Node
// won't auto-close the database if it is closed by the service that opened it. // won't auto-close the database if it is closed by the service that opened it.