github.com/theQRL/go-zond@v0.2.1/node/node.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  	crand "crypto/rand"
    21  	"errors"
    22  	"fmt"
    23  	"hash/crc32"
    24  	"net/http"
    25  	"os"
    26  	"path/filepath"
    27  	"reflect"
    28  	"strings"
    29  	"sync"
    30  
    31  	"github.com/gofrs/flock"
    32  	"github.com/theQRL/go-zond/accounts"
    33  	"github.com/theQRL/go-zond/common"
    34  	"github.com/theQRL/go-zond/common/hexutil"
    35  	"github.com/theQRL/go-zond/core/rawdb"
    36  	"github.com/theQRL/go-zond/event"
    37  	"github.com/theQRL/go-zond/log"
    38  	"github.com/theQRL/go-zond/p2p"
    39  	"github.com/theQRL/go-zond/rpc"
    40  	"github.com/theQRL/go-zond/zonddb"
    41  )
    42  
    43  // Node is a container on which services can be registered.
    44  type Node struct {
    45  	eventmux      *event.TypeMux
    46  	config        *Config
    47  	accman        *accounts.Manager
    48  	log           log.Logger
    49  	keyDir        string        // key store directory
    50  	keyDirTemp    bool          // If true, key directory will be removed by Stop
    51  	dirLock       *flock.Flock  // prevents concurrent use of instance directory
    52  	stop          chan struct{} // Channel to wait for termination notifications
    53  	server        *p2p.Server   // Currently running P2P networking layer
    54  	startStopLock sync.Mutex    // Start/Stop are protected by an additional lock
    55  	state         int           // Tracks state of node lifecycle
    56  
    57  	lock          sync.Mutex
    58  	lifecycles    []Lifecycle // All registered backends, services, and auxiliary services that have a lifecycle
    59  	rpcAPIs       []rpc.API   // List of APIs currently provided by the node
    60  	http          *httpServer //
    61  	ws            *httpServer //
    62  	httpAuth      *httpServer //
    63  	wsAuth        *httpServer //
    64  	ipc           *ipcServer  // Stores information about the ipc http server
    65  	inprocHandler *rpc.Server // In-process RPC request handler to process the API requests
    66  
    67  	databases map[*closeTrackingDB]struct{} // All open databases
    68  }
    69  
    70  const (
    71  	initializingState = iota
    72  	runningState
    73  	closedState
    74  )
    75  
    76  // New creates a new P2P node, ready for protocol registration.
    77  func New(conf *Config) (*Node, error) {
    78  	// Copy config and resolve the datadir so future changes to the current
    79  	// working directory don't affect the node.
    80  	confCopy := *conf
    81  	conf = &confCopy
    82  	if conf.DataDir != "" {
    83  		absdatadir, err := filepath.Abs(conf.DataDir)
    84  		if err != nil {
    85  			return nil, err
    86  		}
    87  		conf.DataDir = absdatadir
    88  	}
    89  	if conf.Logger == nil {
    90  		conf.Logger = log.New()
    91  	}
    92  
    93  	// Ensure that the instance name doesn't cause weird conflicts with
    94  	// other files in the data directory.
    95  	if strings.ContainsAny(conf.Name, `/\`) {
    96  		return nil, errors.New(`Config.Name must not contain '/' or '\'`)
    97  	}
    98  	if conf.Name == datadirDefaultKeyStore {
    99  		return nil, errors.New(`Config.Name cannot be "` + datadirDefaultKeyStore + `"`)
   100  	}
   101  	if strings.HasSuffix(conf.Name, ".ipc") {
   102  		return nil, errors.New(`Config.Name cannot end in ".ipc"`)
   103  	}
   104  	server := rpc.NewServer()
   105  	server.SetBatchLimits(conf.BatchRequestLimit, conf.BatchResponseMaxSize)
   106  	node := &Node{
   107  		config:        conf,
   108  		inprocHandler: server,
   109  		eventmux:      new(event.TypeMux),
   110  		log:           conf.Logger,
   111  		stop:          make(chan struct{}),
   112  		server:        &p2p.Server{Config: conf.P2P},
   113  		databases:     make(map[*closeTrackingDB]struct{}),
   114  	}
   115  
   116  	// Register built-in APIs.
   117  	node.rpcAPIs = append(node.rpcAPIs, node.apis()...)
   118  
   119  	// Acquire the instance directory lock.
   120  	if err := node.openDataDir(); err != nil {
   121  		return nil, err
   122  	}
   123  	keyDir, isEphem, err := conf.GetKeyStoreDir()
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  	node.keyDir = keyDir
   128  	node.keyDirTemp = isEphem
   129  	// Creates an empty AccountManager with no backends. Callers (e.g. cmd/gzond)
   130  	// are required to add the backends later on.
   131  	node.accman = accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed})
   132  
   133  	// Initialize the p2p server. This creates the node key and discovery databases.
   134  	node.server.Config.PrivateKey = node.config.NodeKey()
   135  	node.server.Config.Name = node.config.NodeName()
   136  	node.server.Config.Logger = node.log
   137  	node.config.checkLegacyFiles()
   138  	if node.server.Config.NodeDatabase == "" {
   139  		node.server.Config.NodeDatabase = node.config.NodeDB()
   140  	}
   141  
   142  	// Check HTTP/WS prefixes are valid.
   143  	if err := validatePrefix("HTTP", conf.HTTPPathPrefix); err != nil {
   144  		return nil, err
   145  	}
   146  	if err := validatePrefix("WebSocket", conf.WSPathPrefix); err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	// Configure RPC servers.
   151  	node.http = newHTTPServer(node.log, conf.HTTPTimeouts)
   152  	node.httpAuth = newHTTPServer(node.log, conf.HTTPTimeouts)
   153  	node.ws = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts)
   154  	node.wsAuth = newHTTPServer(node.log, rpc.DefaultHTTPTimeouts)
   155  	node.ipc = newIPCServer(node.log, conf.IPCEndpoint())
   156  
   157  	return node, nil
   158  }
   159  
   160  // Start starts all registered lifecycles, RPC services and p2p networking.
   161  // Node can only be started once.
   162  func (n *Node) Start() error {
   163  	n.startStopLock.Lock()
   164  	defer n.startStopLock.Unlock()
   165  
   166  	n.lock.Lock()
   167  	switch n.state {
   168  	case runningState:
   169  		n.lock.Unlock()
   170  		return ErrNodeRunning
   171  	case closedState:
   172  		n.lock.Unlock()
   173  		return ErrNodeStopped
   174  	}
   175  	n.state = runningState
   176  	// open networking and RPC endpoints
   177  	err := n.openEndpoints()
   178  	lifecycles := make([]Lifecycle, len(n.lifecycles))
   179  	copy(lifecycles, n.lifecycles)
   180  	n.lock.Unlock()
   181  
   182  	// Check if endpoint startup failed.
   183  	if err != nil {
   184  		n.doClose(nil)
   185  		return err
   186  	}
   187  	// Start all registered lifecycles.
   188  	var started []Lifecycle
   189  	for _, lifecycle := range lifecycles {
   190  		if err = lifecycle.Start(); err != nil {
   191  			break
   192  		}
   193  		started = append(started, lifecycle)
   194  	}
   195  	// Check if any lifecycle failed to start.
   196  	if err != nil {
   197  		n.stopServices(started)
   198  		n.doClose(nil)
   199  	}
   200  	return err
   201  }
   202  
   203  // Close stops the Node and releases resources acquired in
   204  // Node constructor New.
   205  func (n *Node) Close() error {
   206  	n.startStopLock.Lock()
   207  	defer n.startStopLock.Unlock()
   208  
   209  	n.lock.Lock()
   210  	state := n.state
   211  	n.lock.Unlock()
   212  	switch state {
   213  	case initializingState:
   214  		// The node was never started.
   215  		return n.doClose(nil)
   216  	case runningState:
   217  		// The node was started, release resources acquired by Start().
   218  		var errs []error
   219  		if err := n.stopServices(n.lifecycles); err != nil {
   220  			errs = append(errs, err)
   221  		}
   222  		return n.doClose(errs)
   223  	case closedState:
   224  		return ErrNodeStopped
   225  	default:
   226  		panic(fmt.Sprintf("node is in unknown state %d", state))
   227  	}
   228  }
   229  
   230  // doClose releases resources acquired by New(), collecting errors.
   231  func (n *Node) doClose(errs []error) error {
   232  	// Close databases. This needs the lock because it needs to
   233  	// synchronize with OpenDatabase*.
   234  	n.lock.Lock()
   235  	n.state = closedState
   236  	errs = append(errs, n.closeDatabases()...)
   237  	n.lock.Unlock()
   238  
   239  	if err := n.accman.Close(); err != nil {
   240  		errs = append(errs, err)
   241  	}
   242  	if n.keyDirTemp {
   243  		if err := os.RemoveAll(n.keyDir); err != nil {
   244  			errs = append(errs, err)
   245  		}
   246  	}
   247  
   248  	// Release instance directory lock.
   249  	n.closeDataDir()
   250  
   251  	// Unblock n.Wait.
   252  	close(n.stop)
   253  
   254  	// Report any errors that might have occurred.
   255  	switch len(errs) {
   256  	case 0:
   257  		return nil
   258  	case 1:
   259  		return errs[0]
   260  	default:
   261  		return fmt.Errorf("%v", errs)
   262  	}
   263  }
   264  
   265  // openEndpoints starts all network and RPC endpoints.
   266  func (n *Node) openEndpoints() error {
   267  	// start networking endpoints
   268  	n.log.Info("Starting peer-to-peer node", "instance", n.server.Name)
   269  	if err := n.server.Start(); err != nil {
   270  		return convertFileLockError(err)
   271  	}
   272  	// start RPC endpoints
   273  	err := n.startRPC()
   274  	if err != nil {
   275  		n.stopRPC()
   276  		n.server.Stop()
   277  	}
   278  	return err
   279  }
   280  
   281  // containsLifecycle checks if 'lfs' contains 'l'.
   282  func containsLifecycle(lfs []Lifecycle, l Lifecycle) bool {
   283  	for _, obj := range lfs {
   284  		if obj == l {
   285  			return true
   286  		}
   287  	}
   288  	return false
   289  }
   290  
   291  // stopServices terminates running services, RPC and p2p networking.
   292  // It is the inverse of Start.
   293  func (n *Node) stopServices(running []Lifecycle) error {
   294  	n.stopRPC()
   295  
   296  	// Stop running lifecycles in reverse order.
   297  	failure := &StopError{Services: make(map[reflect.Type]error)}
   298  	for i := len(running) - 1; i >= 0; i-- {
   299  		if err := running[i].Stop(); err != nil {
   300  			failure.Services[reflect.TypeOf(running[i])] = err
   301  		}
   302  	}
   303  
   304  	// Stop p2p networking.
   305  	n.server.Stop()
   306  
   307  	if len(failure.Services) > 0 {
   308  		return failure
   309  	}
   310  	return nil
   311  }
   312  
   313  func (n *Node) openDataDir() error {
   314  	if n.config.DataDir == "" {
   315  		return nil // ephemeral
   316  	}
   317  
   318  	instdir := filepath.Join(n.config.DataDir, n.config.name())
   319  	if err := os.MkdirAll(instdir, 0700); err != nil {
   320  		return err
   321  	}
   322  	// Lock the instance directory to prevent concurrent use by another instance as well as
   323  	// accidental use of the instance directory as a database.
   324  	n.dirLock = flock.New(filepath.Join(instdir, "LOCK"))
   325  
   326  	if locked, err := n.dirLock.TryLock(); err != nil {
   327  		return err
   328  	} else if !locked {
   329  		return ErrDatadirUsed
   330  	}
   331  	return nil
   332  }
   333  
   334  func (n *Node) closeDataDir() {
   335  	// Release instance directory lock.
   336  	if n.dirLock != nil && n.dirLock.Locked() {
   337  		n.dirLock.Unlock()
   338  		n.dirLock = nil
   339  	}
   340  }
   341  
   342  // obtainJWTSecret loads the jwt-secret, either from the provided config,
   343  // or from the default location. If neither of those are present, it generates
   344  // a new secret and stores to the default location.
   345  func (n *Node) obtainJWTSecret(cliParam string) ([]byte, error) {
   346  	fileName := cliParam
   347  	if len(fileName) == 0 {
   348  		// no path provided, use default
   349  		fileName = n.ResolvePath(datadirJWTKey)
   350  	}
   351  	// try reading from file
   352  	if data, err := os.ReadFile(fileName); err == nil {
   353  		jwtSecret := common.FromHex(strings.TrimSpace(string(data)))
   354  		if len(jwtSecret) == 32 {
   355  			log.Info("Loaded JWT secret file", "path", fileName, "crc32", fmt.Sprintf("%#x", crc32.ChecksumIEEE(jwtSecret)))
   356  			return jwtSecret, nil
   357  		}
   358  		log.Error("Invalid JWT secret", "path", fileName, "length", len(jwtSecret))
   359  		return nil, errors.New("invalid JWT secret")
   360  	}
   361  	// Need to generate one
   362  	jwtSecret := make([]byte, 32)
   363  	crand.Read(jwtSecret)
   364  	// if we're in --dev mode, don't bother saving, just show it
   365  	if fileName == "" {
   366  		log.Info("Generated ephemeral JWT secret", "secret", hexutil.Encode(jwtSecret))
   367  		return jwtSecret, nil
   368  	}
   369  	if err := os.WriteFile(fileName, []byte(hexutil.Encode(jwtSecret)), 0600); err != nil {
   370  		return nil, err
   371  	}
   372  	log.Info("Generated JWT secret", "path", fileName)
   373  	return jwtSecret, nil
   374  }
   375  
   376  // startRPC is a helper method to configure all the various RPC endpoints during node
   377  // startup. It's not meant to be called at any time afterwards as it makes certain
   378  // assumptions about the state of the node.
   379  func (n *Node) startRPC() error {
   380  	var apis []rpc.API
   381  	apis = append(apis, n.rpcAPIs...)
   382  	if err := n.startInProc(apis); err != nil {
   383  		return err
   384  	}
   385  
   386  	// Configure IPC.
   387  	if n.ipc.endpoint != "" {
   388  		if err := n.ipc.start(apis); err != nil {
   389  			return err
   390  		}
   391  	}
   392  	var (
   393  		servers           []*httpServer
   394  		openAPIs, allAPIs = n.getAPIs()
   395  	)
   396  
   397  	rpcConfig := rpcEndpointConfig{
   398  		batchItemLimit:         n.config.BatchRequestLimit,
   399  		batchResponseSizeLimit: n.config.BatchResponseMaxSize,
   400  	}
   401  
   402  	initHttp := func(server *httpServer, port int) error {
   403  		if err := server.setListenAddr(n.config.HTTPHost, port); err != nil {
   404  			return err
   405  		}
   406  		if err := server.enableRPC(openAPIs, httpConfig{
   407  			CorsAllowedOrigins: n.config.HTTPCors,
   408  			Vhosts:             n.config.HTTPVirtualHosts,
   409  			Modules:            n.config.HTTPModules,
   410  			prefix:             n.config.HTTPPathPrefix,
   411  			rpcEndpointConfig:  rpcConfig,
   412  		}); err != nil {
   413  			return err
   414  		}
   415  		servers = append(servers, server)
   416  		return nil
   417  	}
   418  
   419  	initWS := func(port int) error {
   420  		server := n.wsServerForPort(port, false)
   421  		if err := server.setListenAddr(n.config.WSHost, port); err != nil {
   422  			return err
   423  		}
   424  		if err := server.enableWS(openAPIs, wsConfig{
   425  			Modules:           n.config.WSModules,
   426  			Origins:           n.config.WSOrigins,
   427  			prefix:            n.config.WSPathPrefix,
   428  			rpcEndpointConfig: rpcConfig,
   429  		}); err != nil {
   430  			return err
   431  		}
   432  		servers = append(servers, server)
   433  		return nil
   434  	}
   435  
   436  	initAuth := func(port int, secret []byte) error {
   437  		// Enable auth via HTTP
   438  		server := n.httpAuth
   439  		if err := server.setListenAddr(n.config.AuthAddr, port); err != nil {
   440  			return err
   441  		}
   442  		sharedConfig := rpcEndpointConfig{
   443  			jwtSecret:              secret,
   444  			batchItemLimit:         engineAPIBatchItemLimit,
   445  			batchResponseSizeLimit: engineAPIBatchResponseSizeLimit,
   446  		}
   447  		if err := server.enableRPC(allAPIs, httpConfig{
   448  			CorsAllowedOrigins: DefaultAuthCors,
   449  			Vhosts:             n.config.AuthVirtualHosts,
   450  			Modules:            DefaultAuthModules,
   451  			prefix:             DefaultAuthPrefix,
   452  			rpcEndpointConfig:  sharedConfig,
   453  		}); err != nil {
   454  			return err
   455  		}
   456  		servers = append(servers, server)
   457  
   458  		// Enable auth via WS
   459  		server = n.wsServerForPort(port, true)
   460  		if err := server.setListenAddr(n.config.AuthAddr, port); err != nil {
   461  			return err
   462  		}
   463  		if err := server.enableWS(allAPIs, wsConfig{
   464  			Modules:           DefaultAuthModules,
   465  			Origins:           DefaultAuthOrigins,
   466  			prefix:            DefaultAuthPrefix,
   467  			rpcEndpointConfig: sharedConfig,
   468  		}); err != nil {
   469  			return err
   470  		}
   471  		servers = append(servers, server)
   472  		return nil
   473  	}
   474  
   475  	// Set up HTTP.
   476  	if n.config.HTTPHost != "" {
   477  		// Configure legacy unauthenticated HTTP.
   478  		if err := initHttp(n.http, n.config.HTTPPort); err != nil {
   479  			return err
   480  		}
   481  	}
   482  	// Configure WebSocket.
   483  	if n.config.WSHost != "" {
   484  		// legacy unauthenticated
   485  		if err := initWS(n.config.WSPort); err != nil {
   486  			return err
   487  		}
   488  	}
   489  	// Configure authenticated API
   490  	if len(openAPIs) != len(allAPIs) {
   491  		jwtSecret, err := n.obtainJWTSecret(n.config.JWTSecret)
   492  		if err != nil {
   493  			return err
   494  		}
   495  		if err := initAuth(n.config.AuthPort, jwtSecret); err != nil {
   496  			return err
   497  		}
   498  	}
   499  	// Start the servers
   500  	for _, server := range servers {
   501  		if err := server.start(); err != nil {
   502  			return err
   503  		}
   504  	}
   505  	return nil
   506  }
   507  
   508  func (n *Node) wsServerForPort(port int, authenticated bool) *httpServer {
   509  	httpServer, wsServer := n.http, n.ws
   510  	if authenticated {
   511  		httpServer, wsServer = n.httpAuth, n.wsAuth
   512  	}
   513  	if n.config.HTTPHost == "" || httpServer.port == port {
   514  		return httpServer
   515  	}
   516  	return wsServer
   517  }
   518  
   519  func (n *Node) stopRPC() {
   520  	n.http.stop()
   521  	n.ws.stop()
   522  	n.httpAuth.stop()
   523  	n.wsAuth.stop()
   524  	n.ipc.stop()
   525  	n.stopInProc()
   526  }
   527  
   528  // startInProc registers all RPC APIs on the inproc server.
   529  func (n *Node) startInProc(apis []rpc.API) error {
   530  	for _, api := range apis {
   531  		if err := n.inprocHandler.RegisterName(api.Namespace, api.Service); err != nil {
   532  			return err
   533  		}
   534  	}
   535  	return nil
   536  }
   537  
   538  // stopInProc terminates the in-process RPC endpoint.
   539  func (n *Node) stopInProc() {
   540  	n.inprocHandler.Stop()
   541  }
   542  
   543  // Wait blocks until the node is closed.
   544  func (n *Node) Wait() {
   545  	<-n.stop
   546  }
   547  
   548  // RegisterLifecycle registers the given Lifecycle on the node.
   549  func (n *Node) RegisterLifecycle(lifecycle Lifecycle) {
   550  	n.lock.Lock()
   551  	defer n.lock.Unlock()
   552  
   553  	if n.state != initializingState {
   554  		panic("can't register lifecycle on running/stopped node")
   555  	}
   556  	if containsLifecycle(n.lifecycles, lifecycle) {
   557  		panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle))
   558  	}
   559  	n.lifecycles = append(n.lifecycles, lifecycle)
   560  }
   561  
   562  // RegisterProtocols adds backend's protocols to the node's p2p server.
   563  func (n *Node) RegisterProtocols(protocols []p2p.Protocol) {
   564  	n.lock.Lock()
   565  	defer n.lock.Unlock()
   566  
   567  	if n.state != initializingState {
   568  		panic("can't register protocols on running/stopped node")
   569  	}
   570  	n.server.Protocols = append(n.server.Protocols, protocols...)
   571  }
   572  
   573  // RegisterAPIs registers the APIs a service provides on the node.
   574  func (n *Node) RegisterAPIs(apis []rpc.API) {
   575  	n.lock.Lock()
   576  	defer n.lock.Unlock()
   577  
   578  	if n.state != initializingState {
   579  		panic("can't register APIs on running/stopped node")
   580  	}
   581  	n.rpcAPIs = append(n.rpcAPIs, apis...)
   582  }
   583  
   584  // getAPIs return two sets of APIs, both the ones that do not require
   585  // authentication, and the complete set
   586  func (n *Node) getAPIs() (unauthenticated, all []rpc.API) {
   587  	for _, api := range n.rpcAPIs {
   588  		if !api.Authenticated {
   589  			unauthenticated = append(unauthenticated, api)
   590  		}
   591  	}
   592  	return unauthenticated, n.rpcAPIs
   593  }
   594  
   595  // RegisterHandler mounts a handler on the given path on the canonical HTTP server.
   596  //
   597  // The name of the handler is shown in a log message when the HTTP server starts
   598  // and should be a descriptive term for the service provided by the handler.
   599  func (n *Node) RegisterHandler(name, path string, handler http.Handler) {
   600  	n.lock.Lock()
   601  	defer n.lock.Unlock()
   602  
   603  	if n.state != initializingState {
   604  		panic("can't register HTTP handler on running/stopped node")
   605  	}
   606  
   607  	n.http.mux.Handle(path, handler)
   608  	n.http.handlerNames[path] = name
   609  }
   610  
   611  // Attach creates an RPC client attached to an in-process API handler.
   612  func (n *Node) Attach() *rpc.Client {
   613  	return rpc.DialInProc(n.inprocHandler)
   614  }
   615  
   616  // RPCHandler returns the in-process RPC request handler.
   617  func (n *Node) RPCHandler() (*rpc.Server, error) {
   618  	n.lock.Lock()
   619  	defer n.lock.Unlock()
   620  
   621  	if n.state == closedState {
   622  		return nil, ErrNodeStopped
   623  	}
   624  	return n.inprocHandler, nil
   625  }
   626  
   627  // Config returns the configuration of node.
   628  func (n *Node) Config() *Config {
   629  	return n.config
   630  }
   631  
   632  // Server retrieves the currently running P2P network layer. This method is meant
   633  // only to inspect fields of the currently running server. Callers should not
   634  // start or stop the returned server.
   635  func (n *Node) Server() *p2p.Server {
   636  	n.lock.Lock()
   637  	defer n.lock.Unlock()
   638  
   639  	return n.server
   640  }
   641  
   642  // DataDir retrieves the current datadir used by the protocol stack.
   643  // Deprecated: No files should be stored in this directory, use InstanceDir instead.
   644  func (n *Node) DataDir() string {
   645  	return n.config.DataDir
   646  }
   647  
   648  // InstanceDir retrieves the instance directory used by the protocol stack.
   649  func (n *Node) InstanceDir() string {
   650  	return n.config.instanceDir()
   651  }
   652  
   653  // KeyStoreDir retrieves the key directory
   654  func (n *Node) KeyStoreDir() string {
   655  	return n.keyDir
   656  }
   657  
   658  // AccountManager retrieves the account manager used by the protocol stack.
   659  func (n *Node) AccountManager() *accounts.Manager {
   660  	return n.accman
   661  }
   662  
   663  // IPCEndpoint retrieves the current IPC endpoint used by the protocol stack.
   664  func (n *Node) IPCEndpoint() string {
   665  	return n.ipc.endpoint
   666  }
   667  
   668  // HTTPEndpoint returns the URL of the HTTP server. Note that this URL does not
   669  // contain the JSON-RPC path prefix set by HTTPPathPrefix.
   670  func (n *Node) HTTPEndpoint() string {
   671  	return "http://" + n.http.listenAddr()
   672  }
   673  
   674  // WSEndpoint returns the current JSON-RPC over WebSocket endpoint.
   675  func (n *Node) WSEndpoint() string {
   676  	if n.http.wsAllowed() {
   677  		return "ws://" + n.http.listenAddr() + n.http.wsConfig.prefix
   678  	}
   679  	return "ws://" + n.ws.listenAddr() + n.ws.wsConfig.prefix
   680  }
   681  
   682  // HTTPAuthEndpoint returns the URL of the authenticated HTTP server.
   683  func (n *Node) HTTPAuthEndpoint() string {
   684  	return "http://" + n.httpAuth.listenAddr()
   685  }
   686  
   687  // WSAuthEndpoint returns the current authenticated JSON-RPC over WebSocket endpoint.
   688  func (n *Node) WSAuthEndpoint() string {
   689  	if n.httpAuth.wsAllowed() {
   690  		return "ws://" + n.httpAuth.listenAddr() + n.httpAuth.wsConfig.prefix
   691  	}
   692  	return "ws://" + n.wsAuth.listenAddr() + n.wsAuth.wsConfig.prefix
   693  }
   694  
   695  // EventMux retrieves the event multiplexer used by all the network services in
   696  // the current protocol stack.
   697  func (n *Node) EventMux() *event.TypeMux {
   698  	return n.eventmux
   699  }
   700  
   701  // OpenDatabase opens an existing database with the given name (or creates one if no
   702  // previous can be found) from within the node's instance directory. If the node is
   703  // ephemeral, a memory database is returned.
   704  func (n *Node) OpenDatabase(name string, cache, handles int, namespace string, readonly bool) (zonddb.Database, error) {
   705  	n.lock.Lock()
   706  	defer n.lock.Unlock()
   707  	if n.state == closedState {
   708  		return nil, ErrNodeStopped
   709  	}
   710  
   711  	var db zonddb.Database
   712  	var err error
   713  	if n.config.DataDir == "" {
   714  		db = rawdb.NewMemoryDatabase()
   715  	} else {
   716  		db, err = rawdb.Open(rawdb.OpenOptions{
   717  			Type:      n.config.DBEngine,
   718  			Directory: n.ResolvePath(name),
   719  			Namespace: namespace,
   720  			Cache:     cache,
   721  			Handles:   handles,
   722  			ReadOnly:  readonly,
   723  		})
   724  	}
   725  
   726  	if err == nil {
   727  		db = n.wrapDatabase(db)
   728  	}
   729  	return db, err
   730  }
   731  
   732  // OpenDatabaseWithFreezer opens an existing database with the given name (or
   733  // creates one if no previous can be found) from within the node's data directory,
   734  // also attaching a chain freezer to it that moves ancient chain data from the
   735  // database to immutable append-only files. If the node is an ephemeral one, a
   736  // memory database is returned.
   737  func (n *Node) OpenDatabaseWithFreezer(name string, cache, handles int, ancient string, namespace string, readonly bool) (zonddb.Database, error) {
   738  	n.lock.Lock()
   739  	defer n.lock.Unlock()
   740  	if n.state == closedState {
   741  		return nil, ErrNodeStopped
   742  	}
   743  	var db zonddb.Database
   744  	var err error
   745  	if n.config.DataDir == "" {
   746  		db = rawdb.NewMemoryDatabase()
   747  	} else {
   748  		db, err = rawdb.Open(rawdb.OpenOptions{
   749  			Type:              n.config.DBEngine,
   750  			Directory:         n.ResolvePath(name),
   751  			AncientsDirectory: n.ResolveAncient(name, ancient),
   752  			Namespace:         namespace,
   753  			Cache:             cache,
   754  			Handles:           handles,
   755  			ReadOnly:          readonly,
   756  		})
   757  	}
   758  
   759  	if err == nil {
   760  		db = n.wrapDatabase(db)
   761  	}
   762  	return db, err
   763  }
   764  
   765  // ResolvePath returns the absolute path of a resource in the instance directory.
   766  func (n *Node) ResolvePath(x string) string {
   767  	return n.config.ResolvePath(x)
   768  }
   769  
   770  // ResolveAncient returns the absolute path of the root ancient directory.
   771  func (n *Node) ResolveAncient(name string, ancient string) string {
   772  	switch {
   773  	case ancient == "":
   774  		ancient = filepath.Join(n.ResolvePath(name), "ancient")
   775  	case !filepath.IsAbs(ancient):
   776  		ancient = n.ResolvePath(ancient)
   777  	}
   778  	return ancient
   779  }
   780  
   781  // closeTrackingDB wraps the Close method of a database. When the database is closed by the
   782  // service, the wrapper removes it from the node's database map. This ensures that Node
   783  // won't auto-close the database if it is closed by the service that opened it.
   784  type closeTrackingDB struct {
   785  	zonddb.Database
   786  	n *Node
   787  }
   788  
   789  func (db *closeTrackingDB) Close() error {
   790  	db.n.lock.Lock()
   791  	delete(db.n.databases, db)
   792  	db.n.lock.Unlock()
   793  	return db.Database.Close()
   794  }
   795  
   796  // wrapDatabase ensures the database will be auto-closed when Node is closed.
   797  func (n *Node) wrapDatabase(db zonddb.Database) zonddb.Database {
   798  	wrapper := &closeTrackingDB{db, n}
   799  	n.databases[wrapper] = struct{}{}
   800  	return wrapper
   801  }
   802  
   803  // closeDatabases closes all open databases.
   804  func (n *Node) closeDatabases() (errors []error) {
   805  	for db := range n.databases {
   806  		delete(n.databases, db)
   807  		if err := db.Database.Close(); err != nil {
   808  			errors = append(errors, err)
   809  		}
   810  	}
   811  	return errors
   812  }