MCPcopy Create free account
hub / github.com/CPChain/chain / Start

Method Start

node/node.go:128–232  ·  view source on GitHub ↗

Start creates a p2p node and begins the service

()

Source from the content-addressed store, hash-verified

126
127// Start creates a p2p node and begins the service
128func (n *Node) Start() error {
129 n.lock.Lock()
130 defer n.lock.Unlock()
131
132 // Short circuit if the node's already running
133 if n.server != nil {
134 return ErrNodeRunning
135 }
136
137 if err := n.openDataDir(); err != nil {
138 return err
139 }
140
141 // p2p setup
142 n.serverConfig = n.config.P2P
143 n.serverConfig.Name = n.config.NodeName()
144 n.serverConfig.Logger = n.log
145 n.serverConfig.PrivateKey = n.config.NodeKey()
146 if n.serverConfig.StaticNodes == nil {
147 n.serverConfig.StaticNodes = n.config.StaticNodes()
148 }
149 if n.serverConfig.TrustedNodes == nil {
150 n.serverConfig.TrustedNodes = n.config.TrustedNodes()
151 }
152 if n.serverConfig.NodeDatabase == "" {
153 // e.g., cpchain/nodes directory
154 n.serverConfig.NodeDatabase = n.config.NodeDB()
155 }
156 p2pServer := &p2p.Server{Config: n.serverConfig}
157 n.log.Info("Starting peer-to-peer node", "instance", n.serverConfig.Name)
158
159 // create the services
160 services := make(map[reflect.Type]Service)
161 for _, constructor := range n.serviceFuncs {
162 // Create a new context for the particular service
163 ctx := &ServiceContext{
164 config: n.config,
165 services: make(map[reflect.Type]Service),
166 EventMux: n.eventmux,
167 AccountManager: n.accman,
168 }
169 for kind, s := range services {
170 ctx.services[kind] = s
171 }
172 // construct and save the service
173 service, err := constructor(ctx)
174 if err != nil {
175 return err
176 }
177 kind := reflect.TypeOf(service)
178 if _, ok := services[kind]; ok {
179 return &DuplicateServiceError{Kind: kind}
180 }
181 // accumulates the constructed services.
182 services[kind] = service
183 }
184
185 // gather the protocols and start the freshly assembled P2P server

Callers 1

RestartMethod · 0.95

Calls 15

openDataDirMethod · 0.95
StopMethod · 0.95
startRPCMethod · 0.95
convertFileLockErrorFunction · 0.85
LockMethod · 0.80
UnlockMethod · 0.80
NodeNameMethod · 0.80
NodeKeyMethod · 0.80
StaticNodesMethod · 0.80
TrustedNodesMethod · 0.80
NodeDBMethod · 0.80
InfoMethod · 0.65

Tested by

no test coverage detected