Skip to content

Commit

Permalink
node: revert drop support for static & trusted node list files (ether…
Browse files Browse the repository at this point in the history
  • Loading branch information
jakub-freebit committed Nov 6, 2023
1 parent 8eba19c commit d149d5e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
90 changes: 87 additions & 3 deletions node/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import (
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/rpc"
)

Expand Down Expand Up @@ -192,6 +194,10 @@ type Config struct {
// Logger is a custom logger to use with the p2p.Server.
Logger log.Logger `toml:",omitempty"`

// MODIFIED by Jakub Pajek BEG (revert revert drop support for static & trusted node list files)
staticNodesWarning bool
trustedNodesWarning bool
// MODIFIED by Jakub Pajek END (revert revert drop support for static & trusted node list files)
oldGethResourceWarning bool

// AllowUnprotectedTxs allows non EIP-155 protected transactions to be send over RPC.
Expand Down Expand Up @@ -341,10 +347,17 @@ func (c *Config) ResolvePath(path string) string {
oldpath = filepath.Join(c.DataDir, path)
}
if oldpath != "" && common.FileExist(oldpath) {
if warn && !c.oldGethResourceWarning {
c.oldGethResourceWarning = true
log.Warn("Using deprecated resource file, please move this file to the 'geth' subdirectory of datadir.", "file", oldpath)
// MODIFIED by Jakub Pajek BEG (revert revert drop support for static & trusted node list files)
/*
if warn && !c.oldGethResourceWarning {
c.oldGethResourceWarning = true
log.Warn("Using deprecated resource file, please move this file to the 'geth' subdirectory of datadir.", "file", oldpath)
}
*/
if warn {
c.warnOnce(&c.oldGethResourceWarning, "Using deprecated resource file %s, please move this file to the 'geth' subdirectory of datadir.", oldpath)
}
// MODIFIED by Jakub Pajek END (revert revert drop support for static & trusted node list files)
return oldpath
}
}
Expand Down Expand Up @@ -396,13 +409,27 @@ func (c *Config) NodeKey() *ecdsa.PrivateKey {
return key
}

// MODIFIED by Jakub Pajek BEG (revert revert drop support for static & trusted node list files)
// CheckLegacyFiles inspects the datadir for signs of legacy static-nodes
// and trusted-nodes files. If they exist it raises an error.
/*
func (c *Config) checkLegacyFiles() {
c.checkLegacyFile(c.ResolvePath(datadirStaticNodes))
c.checkLegacyFile(c.ResolvePath(datadirTrustedNodes))
}
*/

// StaticNodes returns a list of node enode URLs configured as static nodes.
func (c *Config) StaticNodes() []*enode.Node {
return c.parsePersistentNodes(&c.staticNodesWarning, c.ResolvePath(datadirStaticNodes))
}

// TrustedNodes returns a list of node enode URLs configured as trusted nodes.
func (c *Config) TrustedNodes() []*enode.Node {
return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes))
}

/*
// checkLegacyFile will only raise an error if a file at the given path exists.
func (c *Config) checkLegacyFile(path string) {
// Short circuit if no node config is present
Expand All @@ -426,6 +453,43 @@ func (c *Config) checkLegacyFile(path string) {
logger.Error("Ignoring deprecated file.", "file", path)
}
}
*/

// parsePersistentNodes parses a list of discovery node URLs loaded from a .json
// file from within the data directory.
func (c *Config) parsePersistentNodes(w *bool, path string) []*enode.Node {
// Short circuit if no node config is present
if c.DataDir == "" {
return nil
}
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 list file: %v", err))
return nil
}
// Interpret the list as a discovery node array
var nodes []*enode.Node
for _, url := range nodelist {
if url == "" {
continue
}
node, err := enode.Parse(enode.ValidSchemes, url)
if err != nil {
log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
continue
}
nodes = append(nodes, node)
}
return nodes
}

// MODIFIED by Jakub Pajek END (revert revert drop support for static & trusted node list files)

// KeyDirConfig determines the settings for keydirectory
func (c *Config) KeyDirConfig() (string, error) {
Expand Down Expand Up @@ -471,3 +535,23 @@ func getKeyStoreDir(conf *Config) (string, bool, error) {

return keydir, isEphemeral, nil
}

// MODIFIED by Jakub Pajek BEG (revert revert drop support for static & trusted node list files)
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
}

// MODIFIED by Jakub Pajek END (revert revert drop support for static & trusted node list files)
10 changes: 9 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,15 @@ func New(conf *Config) (*Node, error) {
node.server.Config.PrivateKey = node.config.NodeKey()
node.server.Config.Name = node.config.NodeName()
node.server.Config.Logger = node.log
node.config.checkLegacyFiles()
// MODIFIED by Jakub Pajek BEG (revert revert drop support for static & trusted node list files)
//node.config.checkLegacyFiles()
if node.server.Config.StaticNodes == nil {
node.server.Config.StaticNodes = node.config.StaticNodes()
}
if node.server.Config.TrustedNodes == nil {
node.server.Config.TrustedNodes = node.config.TrustedNodes()
}
// MODIFIED by Jakub Pajek END (revert revert drop support for static & trusted node list files)
if node.server.Config.NodeDatabase == "" {
node.server.Config.NodeDatabase = node.config.NodeDB()
}
Expand Down

0 comments on commit d149d5e

Please sign in to comment.