service.go 3.7 KB

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