github.com/RobustRoundRobin/quorum@v20.10.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 "path/filepath" 22 "reflect" 23 24 "github.com/ethereum/go-ethereum/accounts" 25 "github.com/ethereum/go-ethereum/core/rawdb" 26 "github.com/ethereum/go-ethereum/ethdb" 27 "github.com/ethereum/go-ethereum/event" 28 "github.com/ethereum/go-ethereum/p2p" 29 "github.com/ethereum/go-ethereum/rpc" 30 ) 31 32 // ServiceContext is a collection of service independent options inherited from 33 // the protocol stack, that is passed to all constructors to be optionally used; 34 // as well as utility methods to operate on the service environment. 35 type ServiceContext struct { 36 config *Config 37 services map[reflect.Type]Service // Index of the already constructed services 38 EventMux *event.TypeMux // Event multiplexer used for decoupled notifications 39 AccountManager *accounts.Manager // Account manager created by the node. 40 } 41 42 // OpenDatabase opens an existing database with the given name (or creates one 43 // if no previous can be found) from within the node's data directory. If the 44 // node is an ephemeral one, a memory database is returned. 45 func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, namespace string) (ethdb.Database, error) { 46 if ctx.config.DataDir == "" { 47 return rawdb.NewMemoryDatabase(), nil 48 } 49 return rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace) 50 } 51 52 // OpenDatabaseWithFreezer opens an existing database with the given name (or 53 // creates one if no previous can be found) from within the node's data directory, 54 // also attaching a chain freezer to it that moves ancient chain data from the 55 // database to immutable append-only files. If the node is an ephemeral one, a 56 // memory database is returned. 57 func (ctx *ServiceContext) OpenDatabaseWithFreezer(name string, cache int, handles int, freezer string, namespace string) (ethdb.Database, error) { 58 if ctx.config.DataDir == "" { 59 return rawdb.NewMemoryDatabase(), nil 60 } 61 root := ctx.config.ResolvePath(name) 62 63 switch { 64 case freezer == "": 65 freezer = filepath.Join(root, "ancient") 66 case !filepath.IsAbs(freezer): 67 freezer = ctx.config.ResolvePath(freezer) 68 } 69 return rawdb.NewLevelDBDatabaseWithFreezer(root, cache, handles, freezer, namespace) 70 } 71 72 // ResolvePath resolves a user path into the data directory if that was relative 73 // and if the user actually uses persistent storage. It will return an empty string 74 // for emphemeral storage and the user's own input for absolute paths. 75 func (ctx *ServiceContext) ResolvePath(path string) string { 76 return ctx.config.ResolvePath(path) 77 } 78 79 // Service retrieves a currently running service registered of a specific type. 80 func (ctx *ServiceContext) Service(service interface{}) error { 81 element := reflect.ValueOf(service).Elem() 82 if running, ok := ctx.services[element.Type()]; ok { 83 element.Set(reflect.ValueOf(running)) 84 return nil 85 } 86 return ErrServiceUnknown 87 } 88 89 // NodeKey returns node key from config 90 func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey { 91 return ctx.config.NodeKey() 92 } 93 94 // ExtRPCEnabled returns the indicator whether node enables the external 95 // RPC(http, ws or graphql). 96 func (ctx *ServiceContext) ExtRPCEnabled() bool { 97 return ctx.config.ExtRPCEnabled() 98 } 99 100 // ServiceConstructor is the function signature of the constructors needed to be 101 // registered for service instantiation. 102 type ServiceConstructor func(ctx *ServiceContext) (Service, error) 103 104 // Service is an individual protocol that can be registered into a node. 105 // 106 // Notes: 107 // 108 // • Service life-cycle management is delegated to the node. The service is allowed to 109 // initialize itself upon creation, but no goroutines should be spun up outside of the 110 // Start method. 111 // 112 // • Restart logic is not required as the node will create a fresh instance 113 // every time a service is started. 114 type Service interface { 115 // Protocols retrieves the P2P protocols the service wishes to start. 116 Protocols() []p2p.Protocol 117 118 // APIs retrieves the list of RPC descriptors the service provides 119 APIs() []rpc.API 120 121 // Start is called after all services have been constructed and the networking 122 // layer was also initialized to spawn any goroutines required by the service. 123 Start(server *p2p.Server) error 124 125 // Stop terminates all goroutines belonging to the service, blocking until they 126 // are all terminated. 127 Stop() error 128 }