Skip to content

Commit

Permalink
added docs, removed err return from register lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Jul 21, 2020
1 parent e8b3f9a commit 6ed4779
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
4 changes: 3 additions & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion ethstats/ethstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion les/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 25 additions & 28 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"errors"
"fmt"
"github.com/ethereum/go-ethereum/cmd/utils"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion whisper/whisperv6/whisper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6ed4779

Please sign in to comment.