From f116f36f848356af14ce2618d605f5b8c68abcab Mon Sep 17 00:00:00 2001 From: jsvisa Date: Sat, 22 Apr 2023 23:51:26 +0800 Subject: [PATCH] Revert "node: add a new non-functional method" This reverts commit a0d106d1b8e7ff31dbf53ec5e4b9a09462a6ee25. Signed-off-by: jsvisa --- node/node.go | 108 ++++++++++++++++----------------------------------- 1 file changed, 33 insertions(+), 75 deletions(-) diff --git a/node/node.go b/node/node.go index 0b98a6617de7..2f89bc1ad274 100644 --- a/node/node.go +++ b/node/node.go @@ -73,22 +73,6 @@ const ( closedState ) -// checkConfigName is used to check whether the name of an Ethereum node configuration is valid. -func checkConfigName(name string) error { - // Ensure that the instance name doesn't cause weird conflicts with - // other files in the data directory. - if strings.ContainsAny(name, `/\`) { - return errors.New(`Config.Name must not contain '/' or '\'`) - } - if name == datadirDefaultKeyStore { - return errors.New(`Config.Name cannot be "` + datadirDefaultKeyStore + `"`) - } - if strings.HasSuffix(name, ".ipc") { - return errors.New(`Config.Name cannot end in ".ipc"`) - } - return nil -} - // New creates a new P2P node, ready for protocol registration. func New(conf *Config) (*Node, error) { // Copy config and resolve the datadir so future changes to the current @@ -105,97 +89,71 @@ func New(conf *Config) (*Node, error) { if conf.Logger == nil { conf.Logger = log.New() } - if err := checkConfigName(conf.Name); err != nil { - return nil, err - } - node := &Node{ - config: conf, - inprocHandler: rpc.NewServer(), - eventmux: new(event.TypeMux), - log: conf.Logger, - stop: make(chan struct{}), - server: &p2p.Server{Config: conf.P2P}, - databases: make(map[*closeTrackingDB]struct{}), - } - - // Acquire the instance directory lock. - if err := node.openDataDir(); err != nil { - return nil, err - } - - if err := node.init(conf); err != nil { - return nil, err + // Ensure that the instance name doesn't cause weird conflicts with + // other files in the data directory. + if strings.ContainsAny(conf.Name, `/\`) { + return nil, errors.New(`Config.Name must not contain '/' or '\'`) } - - return node, nil -} - -// NewNonfunctional creates a new P2P node which is incapable of networking, and is not chain-aware. -func NewNonfunctional(conf *Config) (*Node, error) { - if err := checkConfigName(conf.Name); err != nil { - return nil, err + if conf.Name == datadirDefaultKeyStore { + return nil, errors.New(`Config.Name cannot be "` + datadirDefaultKeyStore + `"`) } - - logger := conf.Logger - if logger == nil { - logger = log.New() + if strings.HasSuffix(conf.Name, ".ipc") { + return nil, errors.New(`Config.Name cannot end in ".ipc"`) } node := &Node{ config: conf, inprocHandler: rpc.NewServer(), eventmux: new(event.TypeMux), - log: logger, + log: conf.Logger, stop: make(chan struct{}), server: &p2p.Server{Config: conf.P2P}, databases: make(map[*closeTrackingDB]struct{}), } - if err := node.init(conf); err != nil { - return nil, err - } - - return node, nil -} -func (n *Node) init(conf *Config) error { // Register built-in APIs. - n.rpcAPIs = append(n.rpcAPIs, n.apis()...) + node.rpcAPIs = append(node.rpcAPIs, node.apis()...) + // Acquire the instance directory lock. + if err := node.openDataDir(); err != nil { + return nil, err + } keyDir, isEphem, err := getKeyStoreDir(conf) if err != nil { - return err + return nil, err } - n.keyDir = keyDir - n.keyDirTemp = isEphem + node.keyDir = keyDir + node.keyDirTemp = isEphem // Creates an empty AccountManager with no backends. Callers (e.g. cmd/geth) // are required to add the backends later on. - n.accman = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed}) + node.accman = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed}) // Initialize the p2p server. This creates the node key and discovery databases. - n.server.Config.PrivateKey = n.config.NodeKey() - n.server.Config.Name = n.config.NodeName() - n.server.Config.Logger = n.log - n.config.checkLegacyFiles() - if n.server.Config.NodeDatabase == "" { - n.server.Config.NodeDatabase = n.config.NodeDB() + node.server.Config.PrivateKey = node.config.NodeKey() + node.server.Config.Name = node.config.NodeName() + node.server.Config.Logger = node.log + node.config.checkLegacyFiles() + if node.server.Config.NodeDatabase == "" { + node.server.Config.NodeDatabase = node.config.NodeDB() } // Check HTTP/WS prefixes are valid. if err := validatePrefix("HTTP", conf.HTTPPathPrefix); err != nil { - return err + return nil, err } if err := validatePrefix("WebSocket", conf.WSPathPrefix); err != nil { - return err + return nil, err } // Configure RPC servers. - n.http = newHTTPServer(n.log, conf.HTTPTimeouts) - n.httpAuth = newHTTPServer(n.log, conf.HTTPTimeouts) - n.ws = newHTTPServer(n.log, rpc.DefaultHTTPTimeouts) - n.wsAuth = newHTTPServer(n.log, rpc.DefaultHTTPTimeouts) - n.ipc = newIPCServer(n.log, conf.IPCEndpoint()) - return nil + node.http = newHTTPServer(node.log, conf.HTTPTimeouts) + node.httpAuth = newHTTPServer(node.log, conf.HTTPTimeouts) + node.ws = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts) + node.wsAuth = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts) + node.ipc = newIPCServer(node.log, conf.IPCEndpoint()) + + return node, nil } // Start starts all registered lifecycles, RPC services and p2p networking.