github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/node/node1.go (about)

     1  package node
     2  
     3  import (
     4  	"github.com/intfoundation/intchain/log"
     5  	"github.com/intfoundation/intchain/p2p"
     6  	"github.com/intfoundation/intchain/rpc"
     7  	"reflect"
     8  )
     9  
    10  func (n *Node) RpcAPIs() []rpc.API {
    11  	return n.rpcAPIs
    12  }
    13  
    14  func (n *Node) Start1() error {
    15  	n.lock.Lock()
    16  	defer n.lock.Unlock()
    17  
    18  	if err := n.openDataDir(); err != nil {
    19  		return err
    20  	}
    21  
    22  	// Short circuit if the node's server not set
    23  	if n.server == nil {
    24  		return ErrNodeStopped
    25  	}
    26  
    27  	//service should be gathered before
    28  	services := n.services
    29  	//server should be started before
    30  	running := n.server
    31  	// Start each of the services
    32  	started := []reflect.Type{}
    33  	for kind, service := range services {
    34  		// Start the next service, stopping all previous upon failure
    35  		if err := service.Start(running); err != nil {
    36  			for _, kind := range started {
    37  				services[kind].Stop()
    38  			}
    39  			running.Stop()
    40  
    41  			return err
    42  		}
    43  		// Mark the service started for potential cleanup
    44  		started = append(started, kind)
    45  	}
    46  
    47  	// Lastly start the configured RPC interfaces
    48  	if err := n.startRPC1(services); err != nil {
    49  		for _, service := range services {
    50  			service.Stop()
    51  		}
    52  		running.Stop()
    53  		return err
    54  	}
    55  	// Finish initializing the startup
    56  	n.services = services
    57  	n.server = running
    58  	n.stop = make(chan struct{})
    59  
    60  	return nil
    61  }
    62  
    63  func (n *Node) GatherServices() error {
    64  
    65  	// Otherwise copy and specialize the P2P configuration
    66  	services := make(map[reflect.Type]Service)
    67  	for _, constructor := range n.serviceFuncs {
    68  		// Create a new context for the particular service
    69  		ctx := &ServiceContext{
    70  			config:         n.config,
    71  			services:       make(map[reflect.Type]Service),
    72  			EventMux:       n.eventmux,
    73  			AccountManager: n.accman,
    74  		}
    75  		for kind, s := range services { // copy needed for threaded access
    76  			ctx.services[kind] = s
    77  		}
    78  		// Construct and save the service
    79  		service, err := constructor(ctx)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		kind := reflect.TypeOf(service)
    84  		if _, exists := services[kind]; exists {
    85  			return &DuplicateServiceError{Kind: kind}
    86  		}
    87  		services[kind] = service
    88  	}
    89  
    90  	n.services = services
    91  
    92  	return nil
    93  }
    94  
    95  func (n *Node) GatherProtocols() []p2p.Protocol {
    96  	// Gather the protocols and start the freshly assembled P2P server
    97  	protocols := make([]p2p.Protocol, 0)
    98  	for _, service := range n.services {
    99  		protocols = append(protocols, service.Protocols()...)
   100  	}
   101  
   102  	return protocols
   103  }
   104  
   105  func (n *Node) GetHTTPHandler() (*rpc.Server, error) {
   106  
   107  	// Generate the whitelist based on the allowed modules
   108  	whitelist := make(map[string]bool)
   109  	for _, module := range n.config.HTTPModules {
   110  		whitelist[module] = true
   111  	}
   112  
   113  	// Register all the APIs exposed by the services
   114  	handler := rpc.NewServer()
   115  	for _, api := range n.rpcAPIs {
   116  		if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
   117  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   118  				return nil, err
   119  			}
   120  			n.log.Debug("HTTP registered", "service", api.Service, "namespace", api.Namespace)
   121  		}
   122  	}
   123  
   124  	// All listeners booted successfully
   125  	n.httpEndpoint = ""
   126  	n.httpListener = nil
   127  	n.httpHandler = nil
   128  
   129  	return handler, nil
   130  }
   131  
   132  func (n *Node) GetWSHandler() (*rpc.Server, error) {
   133  	// Generate the whitelist based on the allowed modules
   134  	whitelist := make(map[string]bool)
   135  	for _, module := range n.config.WSModules {
   136  		whitelist[module] = true
   137  	}
   138  	// Register all the APIs exposed by the services
   139  	handler := rpc.NewServer()
   140  	for _, api := range n.rpcAPIs {
   141  		if n.config.WSExposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
   142  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   143  				return nil, err
   144  			}
   145  			log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
   146  		}
   147  	}
   148  
   149  	// All listeners booted successfully
   150  	n.wsEndpoint = ""
   151  	n.wsListener = nil
   152  	n.wsHandler = nil
   153  
   154  	return handler, nil
   155  }
   156  
   157  func (n *Node) startRPC1(services map[reflect.Type]Service) error {
   158  	// Gather all the possible APIs to surface
   159  	apis := n.apis()
   160  	for _, service := range services {
   161  		apis = append(apis, service.APIs()...)
   162  	}
   163  
   164  	// Start the various API endpoints, terminating all in case of errors
   165  	if err := n.startInProc(apis); err != nil {
   166  		return err
   167  	}
   168  	if err := n.startIPC(apis); err != nil {
   169  		n.stopInProc()
   170  		return err
   171  	}
   172  	// All API endpoints started successfully
   173  	n.rpcAPIs = apis
   174  	return nil
   175  }
   176  
   177  func (n *Node) GetLogger() log.Logger {
   178  	return n.log
   179  }