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