github.com/hyperion-hyn/go-ethereum@v2.4.0+incompatible/node/service.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package node 18 19 import ( 20 "crypto/ecdsa" 21 "reflect" 22 23 "github.com/ethereum/go-ethereum/accounts" 24 "github.com/ethereum/go-ethereum/ethdb" 25 "github.com/ethereum/go-ethereum/event" 26 "github.com/ethereum/go-ethereum/p2p" 27 "github.com/ethereum/go-ethereum/rpc" 28 ) 29 30 // ServiceContext is a collection of service independent options inherited from 31 // the protocol stack, that is passed to all constructors to be optionally used; 32 // as well as utility methods to operate on the service environment. 33 type ServiceContext struct { 34 config *Config 35 services map[reflect.Type]Service // Index of the already constructed services 36 EventMux *event.TypeMux // Event multiplexer used for decoupled notifications 37 AccountManager *accounts.Manager // Account manager created by the node. 38 } 39 40 // OpenDatabase opens an existing database with the given name (or creates one 41 // if no previous can be found) from within the node's data directory. If the 42 // node is an ephemeral one, a memory database is returned. 43 func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ethdb.Database, error) { 44 if ctx.config.DataDir == "" { 45 return ethdb.NewMemDatabase(), nil 46 } 47 db, err := ethdb.NewLDBDatabase(ctx.config.ResolvePath(name), cache, handles) 48 if err != nil { 49 return nil, err 50 } 51 return db, nil 52 } 53 54 // ResolvePath resolves a user path into the data directory if that was relative 55 // and if the user actually uses persistent storage. It will return an empty string 56 // for emphemeral storage and the user's own input for absolute paths. 57 func (ctx *ServiceContext) ResolvePath(path string) string { 58 return ctx.config.ResolvePath(path) 59 } 60 61 // Service retrieves a currently running service registered of a specific type. 62 func (ctx *ServiceContext) Service(service interface{}) error { 63 element := reflect.ValueOf(service).Elem() 64 if running, ok := ctx.services[element.Type()]; ok { 65 element.Set(reflect.ValueOf(running)) 66 return nil 67 } 68 return ErrServiceUnknown 69 } 70 71 // NodeKey returns node key from config 72 func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey { 73 return ctx.config.NodeKey() 74 } 75 76 // ServiceConstructor is the function signature of the constructors needed to be 77 // registered for service instantiation. 78 type ServiceConstructor func(ctx *ServiceContext) (Service, error) 79 80 // Service is an individual protocol that can be registered into a node. 81 // 82 // Notes: 83 // 84 // • Service life-cycle management is delegated to the node. The service is allowed to 85 // initialize itself upon creation, but no goroutines should be spun up outside of the 86 // Start method. 87 // 88 // • Restart logic is not required as the node will create a fresh instance 89 // every time a service is started. 90 type Service interface { 91 // Protocols retrieves the P2P protocols the service wishes to start. 92 Protocols() []p2p.Protocol 93 94 // APIs retrieves the list of RPC descriptors the service provides 95 APIs() []rpc.API 96 97 // Start is called after all services have been constructed and the networking 98 // layer was also initialized to spawn any goroutines required by the service. 99 Start(server *p2p.Server) error 100 101 // Stop terminates all goroutines belonging to the service, blocking until they 102 // are all terminated. 103 Stop() error 104 }