From 639d3f63a05bdcda269b0ed3fa9bf1ff0b9e4312 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 20 Feb 2025 17:18:13 +0800 Subject: [PATCH] node: warn when using deprecated config/resource files (#18199) --- node/config.go | 50 +++++++++++++++++++++++++++++++++++++------------ node/node.go | 4 ++-- node/service.go | 4 ++-- 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/node/config.go b/node/config.go index 186d0bc91f..ef08d490bb 100644 --- a/node/config.go +++ b/node/config.go @@ -23,6 +23,7 @@ import ( "path/filepath" "runtime" "strings" + "sync" "github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/crypto" @@ -159,6 +160,10 @@ type Config struct { // Logger is a custom logger to use with the p2p.Server. Logger log.Logger `toml:",omitempty"` + staticNodesWarning bool + trustedNodesWarning bool + oldGethResourceWarning bool + AnnounceTxs bool `toml:",omitempty"` } @@ -192,7 +197,7 @@ func (c *Config) NodeDB() string { if c.DataDir == "" { return "" // ephemeral } - return c.resolvePath(datadirNodeDatabase) + return c.ResolvePath(datadirNodeDatabase) } // DefaultIPCEndpoint returns the IPC path used by default. @@ -271,12 +276,12 @@ var isOldGethResource = map[string]bool{ "chaindata": true, "nodes": true, "nodekey": true, - "static-nodes.json": true, - "trusted-nodes.json": true, + "static-nodes.json": false, // no warning for these because they have their + "trusted-nodes.json": false, // own separate warning. } -// resolvePath resolves path in the instance directory. -func (c *Config) resolvePath(path string) string { +// ResolvePath resolves path in the instance directory. +func (c *Config) ResolvePath(path string) string { if filepath.IsAbs(path) { return path } @@ -285,13 +290,15 @@ func (c *Config) resolvePath(path string) string { } // Backwards-compatibility: ensure that data directory files created // by geth 1.4 are used if they exist. - if c.name() == "geth" && isOldGethResource[path] { + if warn, isOld := isOldGethResource[path]; isOld { oldpath := "" if c.Name == "geth" { oldpath = filepath.Join(c.DataDir, path) } 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 } } @@ -322,7 +329,7 @@ func (c *Config) NodeKey() *ecdsa.PrivateKey { return key } - keyfile := c.resolvePath(datadirPrivateKey) + keyfile := c.ResolvePath(datadirPrivateKey) if key, err := crypto.LoadECDSA(keyfile); err == nil { return key } @@ -345,17 +352,17 @@ func (c *Config) NodeKey() *ecdsa.PrivateKey { // StaticNodes returns a list of node enode URLs configured as static nodes. 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. 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 // 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 if c.DataDir == "" { return nil @@ -363,10 +370,12 @@ func (c *Config) parsePersistentNodes(path string) []*discover.Node { if _, err := os.Stat(path); err != 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. var nodelist []string 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 } // Interpret the list as a discovery node array @@ -429,3 +438,20 @@ func getKeyStoreDir(conf *Config) (string, bool, error) { 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 +} diff --git a/node/node.go b/node/node.go index d5d1ca433f..0a0443f111 100644 --- a/node/node.go +++ b/node/node.go @@ -674,12 +674,12 @@ func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, r if n.config.DataDir == "" { 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. 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. diff --git a/node/service.go b/node/service.go index 96cbe9fc3b..0bf8974222 100644 --- a/node/service.go +++ b/node/service.go @@ -44,7 +44,7 @@ func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, nam if ctx.config.DataDir == "" { 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 { 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 // for emphemeral storage and the user's own input for absolute paths. 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.