github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/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  	"crypto/ecdsa"
    21  	"github.com/intfoundation/intchain/core/rawdb"
    22  	"reflect"
    23  
    24  	"github.com/intfoundation/intchain/accounts"
    25  	"github.com/intfoundation/intchain/event"
    26  	"github.com/intfoundation/intchain/intdb"
    27  	"github.com/intfoundation/intchain/p2p"
    28  	"github.com/intfoundation/intchain/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) (intdb.Database, error) {
    45  	if ctx.config.DataDir == "" {
    46  		return rawdb.NewMemoryDatabase(), nil
    47  	}
    48  	db, err := rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return db, nil
    53  }
    54  
    55  // ResolvePath resolves a user path into the data directory if that was relative
    56  // and if the user actually uses persistent storage. It will return an empty string
    57  // for emphemeral storage and the user's own input for absolute paths.
    58  func (ctx *ServiceContext) ResolvePath(path string) string {
    59  	return ctx.config.ResolvePath(path)
    60  }
    61  
    62  // Service retrieves a currently running service registered of a specific type.
    63  func (ctx *ServiceContext) Service(service interface{}) error {
    64  	element := reflect.ValueOf(service).Elem()
    65  	if running, ok := ctx.services[element.Type()]; ok {
    66  		element.Set(reflect.ValueOf(running))
    67  		return nil
    68  	}
    69  	return ErrServiceUnknown
    70  }
    71  
    72  // NodeKey returns node key from config
    73  func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey {
    74  	return ctx.config.NodeKey()
    75  }
    76  
    77  // ChainId returns current chain id from config
    78  func (ctx *ServiceContext) ChainId() string {
    79  	return ctx.config.ChainId
    80  }
    81  
    82  // ServiceConstructor is the function signature of the constructors needed to be
    83  // registered for service instantiation.
    84  type ServiceConstructor func(ctx *ServiceContext) (Service, error)
    85  
    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  
   100  	// APIs retrieves the list of RPC descriptors the service provides
   101  	APIs() []rpc.API
   102  
   103  	// Start is called after all services have been constructed and the networking
   104  	// layer was also initialized to spawn any goroutines required by the service.
   105  	Start(server *p2p.Server) error
   106  
   107  	// Stop terminates all goroutines belonging to the service, blocking until they
   108  	// are all terminated.
   109  	Stop() error
   110  }