New creates a new P2P node, ready for protocol registration.
(conf *Config)
| 64 | |
| 65 | // New creates a new P2P node, ready for protocol registration. |
| 66 | func 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. |