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

Function New

node/node.go:66–112  ·  view source on GitHub ↗

New creates a new P2P node, ready for protocol registration.

(conf *Config)

Source from the content-addressed store, hash-verified

64
65// New creates a new P2P node, ready for protocol registration.
66func New(conf *Config) (*Node, error) {
67 // Copy config and resolve the datadir so future changes to the current
68 // working directory don't affect the node.
69 confCopy := *conf
70 conf = &confCopy
71 if conf.DataDir != "" {
72 absdatadir, err := filepath.Abs(conf.DataDir)
73 if err != nil {
74 return nil, err
75 }
76 conf.DataDir = absdatadir
77 }
78 // Ensure that the instance name doesn't cause weird conflicts with
79 // other files in the data directory.
80 if strings.ContainsAny(conf.Name, `/\`) {
81 return nil, errors.New(`Config.Name must not contain '/' or '\'`)
82 }
83 if conf.Name == datadirDefaultKeyStore {
84 return nil, errors.New(`Config.Name cannot be "` + datadirDefaultKeyStore + `"`)
85 }
86 if strings.HasSuffix(conf.Name, ".ipc") {
87 return nil, errors.New(`Config.Name cannot end in ".ipc"`)
88 }
89 // Ensure that the AccountManager method works before the node has started.
90 // We rely on this in cmd/cpchain.
91 am, ephemeralKeystore, err := makeAccountManager(conf)
92 if err != nil {
93 return nil, err
94 }
95 if conf.Logger == nil {
96 // TODO @xumx switch to cpchain logger. need to add the trace function
97 conf.Logger = log.New()
98 }
99 // Note: any interaction with Config that would create/touch files
100 // in the data directory or instance directory is delayed until Start.
101 return &Node{
102 accman: am,
103 ephemeralKeystore: ephemeralKeystore,
104 config: conf,
105 serviceFuncs: []ServiceConstructor{},
106 ipcEndpoint: conf.IPCEndpoint(),
107 httpEndpoint: conf.HTTPEndpoint(),
108 wsEndpoint: conf.WSEndpoint(),
109 eventmux: new(event.TypeMux),
110 log: conf.Logger,
111 }, nil
112}
113
114// Register injects a new service into the node's stack. The service created by
115// the passed constructor must be unique in its type with regard to sibling ones.

Callers 13

TestDatadirCreationFunction · 0.70
TestContextServicesFunction · 0.70
TestNodeLifeCycleFunction · 0.70
TestNodeUsedDataDirFunction · 0.70
TestServiceRegistryFunction · 0.70
TestServiceLifeCycleFunction · 0.70
TestServiceRestartsFunction · 0.70
TestServiceRetrievalFunction · 0.70
TestProtocolGatherFunction · 0.70

Calls 4

makeAccountManagerFunction · 0.85
IPCEndpointMethod · 0.45
HTTPEndpointMethod · 0.45
WSEndpointMethod · 0.45

Tested by 13

TestDatadirCreationFunction · 0.56
TestContextServicesFunction · 0.56
TestNodeLifeCycleFunction · 0.56
TestNodeUsedDataDirFunction · 0.56
TestServiceRegistryFunction · 0.56
TestServiceLifeCycleFunction · 0.56
TestServiceRestartsFunction · 0.56
TestServiceRetrievalFunction · 0.56
TestProtocolGatherFunction · 0.56