github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/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/ethereum/go-ethereum/accounts"
    24  	"github.com/ethereum/go-ethereum/ethdb"
    25  	"github.com/ethereum/go-ethereum/event"
    26  	"github.com/ethereum/go-ethereum/log" //debug
    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) (ethdb.Database, error) {
    45  	if ctx.config.DataDir == "" {
    46  		log.Info("OpenDatabase", "ctx.config.DataDir", ctx.config.DataDir) //debugging
    47  		return ethdb.NewMemDatabase()
    48  	}
    49  	return ethdb.NewLDBDatabase(ctx.config.resolvePath(name), cache, handles)
    50  }
    51  
    52  // ResolvePath resolves a user path into the data directory if that was relative
    53  // and if the user actually uses persistent storage. It will return an empty string
    54  // for emphemeral storage and the user's own input for absolute paths.
    55  func (ctx *ServiceContext) ResolvePath(path string) string {
    56  
    57  	return ctx.config.resolvePath(path)
    58  }
    59  
    60  // Service retrieves a currently running service registered of a specific type.
    61  func (ctx *ServiceContext) Service(service interface{}) error {
    62  	element := reflect.ValueOf(service).Elem()
    63  	if running, ok := ctx.services[element.Type()]; ok {
    64  		element.Set(reflect.ValueOf(running))
    65  		return nil
    66  	}
    67  	return ErrServiceUnknown
    68  }
    69  
    70  // NodeKey returns node key from config
    71  func (ctx *ServiceContext) NodeKey() *ecdsa.PrivateKey {
    72  	return ctx.config.NodeKey()
    73  }
    74  
    75  // ServiceConstructor is the function signature of the constructors needed to be
    76  // registered for service instantiation.
    77  type ServiceConstructor func(ctx *ServiceContext) (Service, error)
    78  
    79  // Service is an individual protocol that can be registered into a node.
    80  //
    81  // Notes:
    82  //
    83  // • Service life-cycle management is delegated to the node. The service is allowed to
    84  // initialize itself upon creation, but no goroutines should be spun up outside of the
    85  // Start method.
    86  //
    87  // • Restart logic is not required as the node will create a fresh instance
    88  // every time a service is started.
    89  type Service interface {
    90  	// Protocols retrieves the P2P protocols the service wishes to start.
    91  	Protocols() []p2p.Protocol
    92  
    93  	// APIs retrieves the list of RPC descriptors the service provides
    94  	APIs() []rpc.API
    95  
    96  	// Start is called after all services have been constructed and the networking
    97  	// layer was also initialized to spawn any goroutines required by the service.
    98  	Start(server *p2p.Server) error
    99  
   100  	// Stop terminates all goroutines belonging to the service, blocking until they
   101  	// are all terminated.
   102  	Stop() error
   103  }