github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/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 "reflect" 21 22 "github.com/ethereum/go-ethereum/accounts" 23 "github.com/ethereum/go-ethereum/ethdb" 24 "github.com/ethereum/go-ethereum/event" 25 "github.com/ethereum/go-ethereum/p2p" 26 "github.com/ethereum/go-ethereum/rpc" 27 ) 28 29 // ServiceContext is a collection of service independent options inherited from 30 // the protocol stack, that is passed to all constructors to be optionally used; 31 // as well as utility methods to operate on the service environment. 32 type ServiceContext struct { 33 config *Config 34 services map[reflect.Type]Service // Index of the already constructed services 35 EventMux *event.TypeMux // Event multiplexer used for decoupled notifications 36 AccountManager *accounts.Manager // Account manager created by the node. 37 } 38 39 // OpenDatabase opens an existing database with the given name (or creates one 40 // if no previous can be found) from within the node's data directory. If the 41 // node is an ephemeral one, a memory database is returned. 42 func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ethdb.Database, error) { 43 if ctx.config.DataDir == "" { 44 return ethdb.NewMemDatabase() 45 } 46 return ethdb.NewLDBDatabase(ctx.config.resolvePath(name), cache, handles) 47 } 48 49 // Service retrieves a currently running service registered of a specific type. 50 func (ctx *ServiceContext) Service(service interface{}) error { 51 element := reflect.ValueOf(service).Elem() 52 if running, ok := ctx.services[element.Type()]; ok { 53 element.Set(reflect.ValueOf(running)) 54 return nil 55 } 56 return ErrServiceUnknown 57 } 58 59 // ServiceConstructor is the function signature of the constructors needed to be 60 // registered for service instantiation. 61 type ServiceConstructor func(ctx *ServiceContext) (Service, error) 62 63 // Service is an individual protocol that can be registered into a node. 64 // 65 // Notes: 66 // 67 // • Service life-cycle management is delegated to the node. The service is allowed to 68 // initialize itself upon creation, but no goroutines should be spun up outside of the 69 // Start method. 70 // 71 // • Restart logic is not required as the node will create a fresh instance 72 // every time a service is started. 73 type Service interface { 74 // Protocols retrieves the P2P protocols the service wishes to start. 75 Protocols() []p2p.Protocol 76 77 // APIs retrieves the list of RPC descriptors the service provides 78 APIs() []rpc.API 79 80 // Start is called after all services have been constructed and the networking 81 // layer was also initialized to spawn any goroutines required by the service. 82 Start(server *p2p.Server) error 83 84 // Stop terminates all goroutines belonging to the service, blocking until they 85 // are all terminated. 86 Stop() error 87 }