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