service.go 4.8 KB

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