service.go 3.9 KB

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