github.com/Gessiux/neatchain@v1.3.1/network/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  	"reflect"
    22  
    23  	"github.com/Gessiux/neatchain/chain/core/rawdb"
    24  
    25  	"github.com/Gessiux/neatchain/chain/accounts"
    26  	"github.com/Gessiux/neatchain/neatdb"
    27  	"github.com/Gessiux/neatchain/network/p2p"
    28  	"github.com/Gessiux/neatchain/network/rpc"
    29  	"github.com/Gessiux/neatchain/utilities/event"
    30  )
    31  
    32  // ServiceContext is a collection of service independent options inherited from
    33  // the protocol stack, that is passed to all constructors to be optionally used;
    34  // as well as utility methods to operate on the service environment.
    35  type ServiceContext struct {
    36  	config         *Config
    37  	services       map[reflect.Type]Service // Index of the already constructed services
    38  	EventMux       *event.TypeMux           // Event multiplexer used for decoupled notifications
    39  	AccountManager *accounts.Manager        // Account manager created by the node.
    40  }
    41  
    42  // OpenDatabase opens an existing database with the given name (or creates one
    43  // if no previous can be found) from within the node's data directory. If the
    44  // node is an ephemeral one, a memory database is returned.
    45  func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int, namespace string) (neatdb.Database, error) {
    46  	if ctx.config.DataDir == "" {
    47  		return rawdb.NewMemoryDatabase(), nil
    48  	}
    49  	db, err := rawdb.NewLevelDBDatabase(ctx.config.ResolvePath(name), cache, handles, namespace)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return db, nil
    54  }
    55  
    56  // ResolvePath resolves a user path into the data directory if that was relative
    57  // and if the user actually uses persistent storage. It will return an empty string
    58  // for emphemeral storage and the user's own input for absolute paths.
    59  func (ctx *ServiceContext) ResolvePath(path string) string {
    60  	return ctx.config.ResolvePath(path)
    61  }
    62  
    63  // Service retrieves a currently running service registered of a specific type.
    64  func (ctx *ServiceContext) Service(service interface{}) error {
    65  	element := reflect.ValueOf(service).Elem()
    66  	if running, ok := ctx.services[element.Type()]; ok {
    67  		element.Set(reflect.ValueOf(running))
    68  		return nil
    69  	}
    70  	return ErrServiceUnknown
    71  }
    72  
    73  // NodeKey returns node key from config
    74  func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey {
    75  	return ctx.config.NodeKey()
    76  }
    77  
    78  // ChainId returns current chain id from config
    79  func (ctx *ServiceContext) ChainId() string {
    80  	return ctx.config.ChainId
    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  }