github.com/klaytn/klaytn@v1.10.2/node/service.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2014 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from node/service.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package node
    22  
    23  import (
    24  	"crypto/ecdsa"
    25  	"reflect"
    26  
    27  	"github.com/klaytn/klaytn/accounts"
    28  	"github.com/klaytn/klaytn/common"
    29  	"github.com/klaytn/klaytn/event"
    30  	"github.com/klaytn/klaytn/networks/p2p"
    31  	"github.com/klaytn/klaytn/networks/rpc"
    32  	"github.com/klaytn/klaytn/storage/database"
    33  )
    34  
    35  type ServiceContext struct {
    36  	config         *Config
    37  	services       map[reflect.Type]Service
    38  	EventMux       *event.TypeMux
    39  	AccountManager *accounts.Manager
    40  }
    41  
    42  func NewServiceContext(conf *Config, srv map[reflect.Type]Service, mux *event.TypeMux, am *accounts.Manager) *ServiceContext {
    43  	return &ServiceContext{conf, srv, mux, am}
    44  }
    45  
    46  // OpenDatabase opens an existing database with the given name (or creates one
    47  // if no previous can be found) from within the node's data directory. If the
    48  // node is an ephemeral one, a memory database is returned.
    49  func (ctx *ServiceContext) OpenDatabase(dbc *database.DBConfig) database.DBManager {
    50  	if ctx.config.DataDir == "" {
    51  		return database.NewMemoryDBManager()
    52  	}
    53  	dbc.Dir = ctx.config.ResolvePath(dbc.Dir)
    54  	return database.NewDBManager(dbc)
    55  }
    56  
    57  // ResolvePath resolves a user path into the data directory if that was relative
    58  // and if the user actually uses persistent storage. It will return an empty string
    59  // for emphemeral storage and the user's own input for absolute paths.
    60  func (ctx *ServiceContext) ResolvePath(path string) string {
    61  	return ctx.config.ResolvePath(path)
    62  }
    63  
    64  // Service retrieves a currently running service registered of a specific type.
    65  func (ctx *ServiceContext) Service(service interface{}) error {
    66  	element := reflect.ValueOf(service).Elem()
    67  	if running, ok := ctx.services[element.Type()]; ok {
    68  		element.Set(reflect.ValueOf(running))
    69  		return nil
    70  	}
    71  	return ErrServiceUnknown
    72  }
    73  
    74  // NodeKey returns node key from config
    75  func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey {
    76  	return ctx.config.NodeKey()
    77  }
    78  
    79  func (ctx *ServiceContext) NodeType() common.ConnType {
    80  	return ctx.config.P2P.ConnectionType
    81  }
    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  
    87  // Service is an individual protocol that can be registered into a node.
    88  //
    89  // Notes:
    90  //
    91  // • Service life-cycle management is delegated to the node. The service is allowed to
    92  // initialize itself upon creation, but no goroutines should be spun up outside of the
    93  // Start method.
    94  //
    95  // • Restart logic is not required as the node will create a fresh instance
    96  // every time a service is started.
    97  type Service interface {
    98  	// Protocols retrieves the P2P protocols the service wishes to start.
    99  	Protocols() []p2p.Protocol
   100  
   101  	// APIs retrieves the list of RPC descriptors the service provides
   102  	APIs() []rpc.API
   103  
   104  	// Start is called after all services have been constructed and the networking
   105  	// layer was also initialized to spawn any goroutines required by the service.
   106  	Start(server p2p.Server) error
   107  
   108  	// Stop terminates all goroutines belonging to the service, blocking until they
   109  	// are all terminated.
   110  	Stop() error
   111  
   112  	// retrieve components (blockchain, txpool, ..) from core service
   113  	Components() []interface{}
   114  
   115  	// set components (blockchain, txpool, ..) in core service
   116  	SetComponents(components []interface{})
   117  }