Merge pull request #875 from gzliudan/node_deprecation_warning

node: warn when using deprecated config/resource files
This commit is contained in:
Daniel Liu 2025-02-21 15:31:25 +08:00 committed by GitHub
commit 659659f320
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 16 deletions

View file

@ -23,6 +23,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/crypto" "github.com/XinFinOrg/XDPoSChain/crypto"
@ -159,6 +160,10 @@ type Config struct {
// Logger is a custom logger to use with the p2p.Server. // Logger is a custom logger to use with the p2p.Server.
Logger log.Logger `toml:",omitempty"` Logger log.Logger `toml:",omitempty"`
staticNodesWarning bool
trustedNodesWarning bool
oldGethResourceWarning bool
AnnounceTxs bool `toml:",omitempty"` AnnounceTxs bool `toml:",omitempty"`
} }
@ -192,7 +197,7 @@ func (c *Config) NodeDB() string {
if c.DataDir == "" { if c.DataDir == "" {
return "" // ephemeral return "" // ephemeral
} }
return c.resolvePath(datadirNodeDatabase) return c.ResolvePath(datadirNodeDatabase)
} }
// DefaultIPCEndpoint returns the IPC path used by default. // DefaultIPCEndpoint returns the IPC path used by default.
@ -271,12 +276,12 @@ var isOldGethResource = map[string]bool{
"chaindata": true, "chaindata": true,
"nodes": true, "nodes": true,
"nodekey": true, "nodekey": true,
"static-nodes.json": true, "static-nodes.json": false, // no warning for these because they have their
"trusted-nodes.json": true, "trusted-nodes.json": false, // own separate warning.
} }
// resolvePath resolves path in the instance directory. // ResolvePath resolves path in the instance directory.
func (c *Config) resolvePath(path string) string { func (c *Config) ResolvePath(path string) string {
if filepath.IsAbs(path) { if filepath.IsAbs(path) {
return path return path
} }
@ -285,13 +290,15 @@ func (c *Config) resolvePath(path string) string {
} }
// Backwards-compatibility: ensure that data directory files created // Backwards-compatibility: ensure that data directory files created
// by geth 1.4 are used if they exist. // by geth 1.4 are used if they exist.
if c.name() == "geth" && isOldGethResource[path] { if warn, isOld := isOldGethResource[path]; isOld {
oldpath := "" oldpath := ""
if c.Name == "geth" { if c.Name == "geth" {
oldpath = filepath.Join(c.DataDir, path) oldpath = filepath.Join(c.DataDir, path)
} }
if oldpath != "" && common.FileExist(oldpath) { if oldpath != "" && common.FileExist(oldpath) {
// TODO: print warning if warn {
c.warnOnce(&c.oldGethResourceWarning, "Using deprecated resource file %s, please move this file to the 'geth' subdirectory of datadir.", oldpath)
}
return oldpath return oldpath
} }
} }
@ -322,7 +329,7 @@ func (c *Config) NodeKey() *ecdsa.PrivateKey {
return key return key
} }
keyfile := c.resolvePath(datadirPrivateKey) keyfile := c.ResolvePath(datadirPrivateKey)
if key, err := crypto.LoadECDSA(keyfile); err == nil { if key, err := crypto.LoadECDSA(keyfile); err == nil {
return key return key
} }
@ -345,17 +352,17 @@ func (c *Config) NodeKey() *ecdsa.PrivateKey {
// StaticNodes returns a list of node enode URLs configured as static nodes. // StaticNodes returns a list of node enode URLs configured as static nodes.
func (c *Config) StaticNodes() []*discover.Node { func (c *Config) StaticNodes() []*discover.Node {
return c.parsePersistentNodes(c.resolvePath(datadirStaticNodes)) return c.parsePersistentNodes(&c.staticNodesWarning, c.ResolvePath(datadirStaticNodes))
} }
// TrustedNodes returns a list of node enode URLs configured as trusted nodes. // TrustedNodes returns a list of node enode URLs configured as trusted nodes.
func (c *Config) TrustedNodes() []*discover.Node { func (c *Config) TrustedNodes() []*discover.Node {
return c.parsePersistentNodes(c.resolvePath(datadirTrustedNodes)) return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes))
} }
// parsePersistentNodes parses a list of discovery node URLs loaded from a .json // parsePersistentNodes parses a list of discovery node URLs loaded from a .json
// file from within the data directory. // file from within the data directory.
func (c *Config) parsePersistentNodes(path string) []*discover.Node { func (c *Config) parsePersistentNodes(w *bool, path string) []*discover.Node {
// Short circuit if no node config is present // Short circuit if no node config is present
if c.DataDir == "" { if c.DataDir == "" {
return nil return nil
@ -363,10 +370,12 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node {
if _, err := os.Stat(path); err != nil { if _, err := os.Stat(path); err != nil {
return nil return nil
} }
c.warnOnce(w, "Found deprecated node list file %s, please use the TOML config file instead.", path)
// Load the nodes from the config file. // Load the nodes from the config file.
var nodelist []string var nodelist []string
if err := common.LoadJSON(path, &nodelist); err != nil { if err := common.LoadJSON(path, &nodelist); err != nil {
log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err)) log.Error(fmt.Sprintf("Can't load node list file: %v", err))
return nil return nil
} }
// Interpret the list as a discovery node array // Interpret the list as a discovery node array
@ -429,3 +438,20 @@ func getKeyStoreDir(conf *Config) (string, bool, error) {
return keydir, isEphemeral, nil return keydir, isEphemeral, nil
} }
var warnLock sync.Mutex
func (c *Config) warnOnce(w *bool, format string, args ...interface{}) {
warnLock.Lock()
defer warnLock.Unlock()
if *w {
return
}
l := c.Logger
if l == nil {
l = log.Root()
}
l.Warn(fmt.Sprintf(format, args...))
*w = true
}

View file

@ -674,12 +674,12 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r
if n.config.DataDir == "" { if n.config.DataDir == "" {
return rawdb.NewMemoryDatabase(), nil return rawdb.NewMemoryDatabase(), nil
} }
return rawdb.NewLevelDBDatabase(n.config.resolvePath(name), cache, handles, namespace, readonly) return rawdb.NewLevelDBDatabase(n.config.ResolvePath(name), cache, handles, namespace, readonly)
} }
// ResolvePath returns the absolute path of a resource in the instance directory. // ResolvePath returns the absolute path of a resource in the instance directory.
func (n *Node) ResolvePath(x string) string { func (n *Node) ResolvePath(x string) string {
return n.config.resolvePath(x) return n.config.ResolvePath(x)
} }
// apis returns the collection of RPC descriptors this node offers. // apis returns the collection of RPC descriptors this node offers.

View file

@ -44,7 +44,7 @@ func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, nam
if ctx.config.DataDir == "" { if ctx.config.DataDir == "" {
return rawdb.NewMemoryDatabase(), nil return rawdb.NewMemoryDatabase(), nil
} }
db, err := rawdb.NewLevelDBDatabase(ctx.config.resolvePath(name), cache, handles, namespace, readonly) db, err := rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace, readonly)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -55,7 +55,7 @@ func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, nam
// and if the user actually uses persistent storage. It will return an empty string // and if the user actually uses persistent storage. It will return an empty string
// for emphemeral storage and the user's own input for absolute paths. // for emphemeral storage and the user's own input for absolute paths.
func (ctx *ServiceContext) ResolvePath(path string) string { func (ctx *ServiceContext) ResolvePath(path string) string {
return ctx.config.resolvePath(path) return ctx.config.ResolvePath(path)
} }
// Service retrieves a currently running service registered of a specific type. // Service retrieves a currently running service registered of a specific type.