gitlab.com/flarenetwork/coreth@v0.1.1/node/node.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2015 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package node 28 29 import ( 30 "sync" 31 32 "github.com/ethereum/go-ethereum/event" 33 34 "gitlab.com/flarenetwork/coreth/accounts" 35 "gitlab.com/flarenetwork/coreth/rpc" 36 ) 37 38 // Node is a container on which services can be registered. 39 type Node struct { 40 eventmux *event.TypeMux 41 config *Config 42 accman *accounts.Manager 43 // log log.Logger 44 // ephemKeystore string // if non-empty, the key directory that will be removed by Stop 45 // dirLock fileutil.Releaser // prevents concurrent use of instance directory 46 // stop chan struct{} // Channel to wait for termination notifications 47 // server *p2p.Server // Currently running P2P networking layer 48 // startStopLock sync.Mutex // Start/Stop are protected by an additional lock 49 // state int // Tracks state of node lifecycle 50 51 lock sync.Mutex 52 rpcAPIs []rpc.API // List of APIs currently provided by the node 53 // inprocHandler *rpc.Server // In-process RPC request handler to process the API requests 54 55 // databases map[*closeTrackingDB]struct{} // All open databases 56 57 corethVersion string 58 } 59 60 // const ( 61 // initializingState = iota 62 // closedState 63 // ) 64 65 // func (n *Node) openDataDir() error { 66 // if n.config.DataDir == "" { 67 // return nil // ephemeral 68 // } 69 // 70 // instdir := filepath.Join(n.config.DataDir, n.config.name()) 71 // if err := os.MkdirAll(instdir, 0700); err != nil { 72 // return err 73 // } 74 // // Lock the instance directory to prevent concurrent use by another instance as well as 75 // // accidental use of the instance directory as a database. 76 // release, _, err := fileutil.Flock(filepath.Join(instdir, "LOCK")) 77 // if err != nil { 78 // return convertFileLockError(err) 79 // } 80 // n.dirLock = release 81 // return nil 82 // } 83 84 // New creates a new P2P node, ready for protocol registration. 85 func New(conf *Config) (*Node, error) { 86 // Copy config and resolve the datadir so future changes to the current 87 // working directory don't affect the node. 88 confCopy := *conf 89 conf = &confCopy 90 91 node := &Node{ 92 config: conf, 93 eventmux: new(event.TypeMux), 94 } 95 96 // Register built-in APIs. 97 node.rpcAPIs = append(node.rpcAPIs, node.apis()...) 98 99 // Ensure that the AccountManager method works before the node has started. We rely on 100 // this in cmd/geth. 101 am, err := makeAccountManager(conf) 102 if err != nil { 103 return nil, err 104 } 105 node.accman = am 106 107 // Configure RPC servers. 108 109 return node, nil 110 } 111 112 // Config returns the configuration of node. 113 func (n *Node) Config() *Config { 114 return n.config 115 } 116 117 // AccountManager retrieves the account manager used by the protocol stack. 118 func (n *Node) AccountManager() *accounts.Manager { 119 return n.accman 120 } 121 122 // EventMux retrieves the event multiplexer used by all the network services in 123 // the current protocol stack. 124 func (n *Node) EventMux() *event.TypeMux { 125 return n.eventmux 126 } 127 128 // RegisterAPIs registers the APIs a service provides on the node. 129 func (n *Node) RegisterAPIs(apis []rpc.API) { 130 n.lock.Lock() 131 defer n.lock.Unlock() 132 133 n.rpcAPIs = append(n.rpcAPIs, apis...) 134 }