From 6ed4779afd475196fa7081f8a2b1c751b26f5c5a Mon Sep 17 00:00:00 2001 From: renaynay <41963722+renaynay@users.noreply.github.com> Date: Tue, 19 May 2020 14:08:50 +0200 Subject: [PATCH] added docs, removed err return from register lifecycle --- eth/backend.go | 4 ++- ethstats/ethstats.go | 3 +- les/client.go | 4 ++- node/node.go | 53 +++++++++++++++++------------------- whisper/whisperv6/whisper.go | 4 ++- 5 files changed, 36 insertions(+), 32 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index eeeefcfda56b..beb8de26e6c3 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -240,7 +240,9 @@ func New(stack *node.Node, config *Config) (*Ethereum, error) { if err := stack.RegisterProtocols(eth.Protocols()); err != nil { return nil, err } - return eth, stack.RegisterLifecycle(eth) + stack.RegisterLifecycle(eth) + + return eth, nil } func makeExtraData(extra []byte) []byte { diff --git a/ethstats/ethstats.go b/ethstats/ethstats.go index f1328f5ed9a4..bada875b0bbb 100644 --- a/ethstats/ethstats.go +++ b/ethstats/ethstats.go @@ -106,8 +106,9 @@ func New(node *node.Node, ethBackend *eth.Ethereum, lesBackend *les.LightEthereu pongCh: make(chan struct{}), histCh: make(chan []uint64, 1), } + node.RegisterLifecycle(ethstats) - return node.RegisterLifecycle(ethstats) + return nil } // Start implements node.Service, starting up the monitoring and reporting daemon. diff --git a/les/client.go b/les/client.go index 63114fceb696..bd4c8d7f6bd3 100644 --- a/les/client.go +++ b/les/client.go @@ -177,7 +177,9 @@ func New(stack *node.Node, config *eth.Config) (*LightEthereum, error) { if err := stack.RegisterProtocols(leth.Protocols()); err != nil { return nil, err } - return leth, stack.RegisterLifecycle(leth) + stack.RegisterLifecycle(leth) + + return leth, nil } // vtSubscription implements serverPeerSubscriber diff --git a/node/node.go b/node/node.go index da28d8f6a8b7..755c4e161393 100644 --- a/node/node.go +++ b/node/node.go @@ -20,6 +20,7 @@ import ( "context" "errors" "fmt" + "github.com/ethereum/go-ethereum/cmd/utils" "net" "net/http" "os" @@ -48,7 +49,6 @@ type Node struct { ephemeralKeystore string // if non-empty, the key directory that will be removed by Stop instanceDirLock fileutil.Releaser // prevents concurrent use of instance directory - // TODO: removed p2pConfig b/c p2pServer already contains p2pConfig (is there a reason for it to be duplicated? server *p2p.Server // Currently running P2P networking layer ServiceContext *ServiceContext @@ -60,7 +60,7 @@ type Node struct { httpServers []*HTTPServer // Stores information about all http servers (if any), including http, ws, and graphql - ipc *HTTPServer // TODO + ipc *HTTPServer // Stores information about the ipc http server stop chan struct{} // Channel to wait for termination notifications lock sync.RWMutex @@ -147,7 +147,7 @@ func New(conf *Config) (*Node, error) { endpoint: conf.HTTPEndpoint(), host: conf.HTTPHost, port: conf.HTTPPort, - RPCAllowed: true, + RPCAllowed: true, } // check if ws is enabled and if ws port is the same as http port if conf.WSHost != "" && conf.WSPort == conf.HTTPPort { @@ -162,12 +162,12 @@ func New(conf *Config) (*Node, error) { if conf.WSHost != "" { node.httpServers = append(node.httpServers, &HTTPServer{ WsOrigins: conf.WSOrigins, - Whitelist: conf.WSModules, - Srv: rpc.NewServer(), - endpoint: conf.WSEndpoint(), - host: conf.WSHost, - port: conf.WSPort, - WSAllowed: true, + Whitelist: conf.WSModules, + Srv: rpc.NewServer(), + endpoint: conf.WSEndpoint(), + host: conf.WSHost, + port: conf.WSPort, + WSAllowed: true, }) } @@ -197,44 +197,42 @@ func (n *Node) Close() error { } } -// TODO document -func (n *Node) RegisterLifecycle(lifecycle Lifecycle) error { - for _, existing := range n.lifecycles { // TODO is checking for duplicates a good idea? +// RegisterLifecycle registers the given Lifecycle on the node +func (n *Node) RegisterLifecycle(lifecycle Lifecycle) { + for _, existing := range n.lifecycles { if existing == lifecycle { - return errors.New("Lifecycle already registered") + utils.Fatalf("Lifecycle cannot be registered more than once", lifecycle) } } - n.lifecycles = append(n.lifecycles, lifecycle) - return nil } +// RegisterProtocols adds backend's protocols to the node's p2p server func (n *Node) RegisterProtocols(protocols []p2p.Protocol) error { - // TODO check for duplicates? - - // add backend's protocols to the p2p server n.server.Protocols = append(n.server.Protocols, protocols...) return nil } +// RegisterAPIs registers the APIs a service provides on the node func (n *Node) RegisterAPIs(apis []rpc.API) { n.rpcAPIs = append(n.rpcAPIs, apis...) } +// RegisterHTTPServer registers the given HTTP server on the node +func (n *Node) RegisterHTTPServer(server *HTTPServer) { + n.httpServers = append(n.httpServers, server) +} + +// ExistingHTTPServer checks if an HTTP server is already configured on the given endpoint func (n *Node) ExistingHTTPServer(endpoint string) *HTTPServer { for _, httpServer := range n.httpServers { if endpoint == httpServer.endpoint { return httpServer } } - return nil } -func (n *Node) RegisterHTTPServer(server *HTTPServer) { - n.httpServers = append(n.httpServers, server) -} - // CreateHTTPServer creates an http.Server and adds it to the given HTTPServer // TODO improve? func (n *Node) CreateHTTPServer(h *HTTPServer, exposeAll bool) error { // register apis and create handler stack @@ -302,7 +300,7 @@ func (n *Node) Start() error { } // Lastly, start the configured RPC interfaces - if err := n.startRPC(); err != nil { + if err := n.configureRPC(); err != nil { n.stopLifecycles(n.lifecycles) n.server.Stop() return err @@ -316,7 +314,7 @@ func (n *Node) Start() error { return nil } -// TODO document +// stopLifecycles stops the node's running Lifecycles func (n *Node) stopLifecycles(started []Lifecycle) { for _, lifecycle := range started { lifecycle.Stop() @@ -347,10 +345,10 @@ func (n *Node) openDataDir() error { return nil } -// startRPC is a helper method to start all the various RPC endpoints during node +// configureRPC is a helper method to configure all the various RPC endpoints during node // startup. It's not meant to be called at any time afterwards as it makes certain // assumptions about the state of the node. -func (n *Node) startRPC() error { +func (n *Node) configureRPC() error { n.RegisterAPIs(n.apis()) // Start the various API endpoints, terminating all in case of errors @@ -497,7 +495,6 @@ func (n *Node) stopServer(server *HTTPServer) { } } - //// stopHTTP terminates the HTTP RPC endpoint. //func (n *Node) stopHTTP() { // for _, server := range n.httpServers { diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index 6d2c9cf154fe..8613893eb4a8 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -137,7 +137,9 @@ func New(stack *node.Node, cfg *Config) error { if err := stack.RegisterProtocols(whisper.Protocols()); err != nil { return err } - return stack.RegisterLifecycle(whisper) + stack.RegisterLifecycle(whisper) + + return nil } // MinPow returns the PoW value required by this node.