github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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/ethereumproject/go-ethereum/ethdb"
    24  	"github.com/ethereumproject/go-ethereum/event"
    25  	"github.com/ethereumproject/go-ethereum/p2p"
    26  	"github.com/ethereumproject/go-ethereum/rpc"
    27  )
    28  
    29  // ServiceContext is a collection of service independent options inherited from
    30  // the protocol stack, that is passed to all constructors to be optionally used;
    31  // as well as utility methods to operate on the service environment.
    32  type ServiceContext struct {
    33  	datadir  string                   // Data directory for protocol persistence
    34  	services map[reflect.Type]Service // Index of the already constructed services
    35  	EventMux *event.TypeMux           // Event multiplexer used for decoupled notifications
    36  }
    37  
    38  // OpenDatabase opens an existing database with the given name (or creates one
    39  // if no previous can be found) from within the node's data directory. If the
    40  // node is an ephemeral one, a memory database is returned.
    41  func (ctx *ServiceContext) OpenDatabase(name string, cache int, handles int) (ethdb.Database, error) {
    42  	if ctx.datadir == "" {
    43  		return ethdb.NewMemDatabase()
    44  	}
    45  	return ethdb.NewLDBDatabase(filepath.Join(ctx.datadir, name), cache, handles)
    46  }
    47  
    48  // Service retrieves a currently running service registered of a specific type.
    49  func (ctx *ServiceContext) Service(service interface{}) error {
    50  	element := reflect.ValueOf(service).Elem()
    51  	if running, ok := ctx.services[element.Type()]; ok {
    52  		element.Set(reflect.ValueOf(running))
    53  		return nil
    54  	}
    55  	return ErrServiceUnknown
    56  }
    57  
    58  // ServiceConstructor is the function signature of the constructors needed to be
    59  // registered for service instantiation.
    60  type ServiceConstructor func(ctx *ServiceContext) (Service, error)
    61  
    62  // Service is an individual protocol that can be registered into a node.
    63  //
    64  // Notes:
    65  //  - Service life-cycle management is delegated to the node. The service is
    66  //    allowed to initialize itself upon creation, but no goroutines should be
    67  //    spun up outside of the Start method.
    68  //  - Restart logic is not required as the node will create a fresh instance
    69  //    every time a service is started.
    70  type Service interface {
    71  	// Protocols retrieves the P2P protocols the service wishes to start.
    72  	Protocols() []p2p.Protocol
    73  
    74  	// APIs retrieves the list of RPC descriptors the service provides
    75  	APIs() []rpc.API
    76  
    77  	// Start is called after all services have been constructed and the networking
    78  	// layer was also initialized to spawn any goroutines required by the service.
    79  	Start(server *p2p.Server) error
    80  
    81  	// Stop terminates all goroutines belonging to the service, blocking until they
    82  	// are all terminated.
    83  	Stop() error
    84  }