Skip to content

Commit

Permalink
Revert "node: add a new non-functional method"
Browse files Browse the repository at this point in the history
This reverts commit a0d106d.

Signed-off-by: jsvisa <delweng@gmail.com>
  • Loading branch information
jsvisa committed Apr 22, 2023
1 parent 1e4691a commit f116f36
Showing 1 changed file with 33 additions and 75 deletions.
108 changes: 33 additions & 75 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down

0 comments on commit f116f36

Please sign in to comment.