github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/node/config.go (about)

     1  // Copyright 2014 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  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"runtime"
    26  	"strings"
    27  
    28  	"github.com/ethereum/go-ethereum/accounts"
    29  	"github.com/ethereum/go-ethereum/accounts/keystore"
    30  	"github.com/ethereum/go-ethereum/accounts/usbwallet"
    31  	"github.com/ethereum/go-ethereum/common"
    32  	"github.com/ethereum/go-ethereum/crypto"
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/p2p"
    35  	"github.com/ethereum/go-ethereum/p2p/discover"
    36  )
    37  
    38  var (
    39  	datadirPrivateKey      = "nodekey"            // Path within the datadir to the node's private key
    40  	datadirDefaultKeyStore = "keystore"           // Path within the datadir to the keystore
    41  	datadirBootNodes       = "boot-nodes.json"    // Path within the datadir to access boot node because of easy test
    42  	datadirStaticNodes     = "static-nodes.json"  // Path within the datadir to the static node list
    43  	//datadirQmanagerNodes   = "qmanager-nodes.json" // Path within the datadir to the qmananger node list
    44  	                                               // Qmanager is Primary, Sencondary, Reserved ( 1, 2, 3 )
    45  	datadirTrustedNodes    = "trusted-nodes.json" // Path within the datadir to the trusted node list
    46  	datadirNodeDatabase    = "nodes"              // Path within the datadir to store the node infos
    47  )
    48  
    49  // Config represents a small collection of configuration values to fine tune the
    50  // P2P network layer of a protocol stack. These values can be further extended by
    51  // all registered services.
    52  type Config struct {
    53  	// Name sets the instance name of the node. It must not contain the / character and is
    54  	// used in the devp2p node identifier. The instance name of geth is "geth". If no
    55  	// value is specified, the basename of the current executable is used.
    56  	Name string `toml:"-"`
    57  
    58  	// UserIdent, if set, is used as an additional component in the devp2p node identifier.
    59  	UserIdent string `toml:",omitempty"`
    60  
    61  	// Version should be set to the version number of the program. It is used
    62  	// in the devp2p node identifier.
    63  	Version string `toml:"-"`
    64  
    65  	// DataDir is the file system folder the node should use for any data storage
    66  	// requirements. The configured data directory will not be directly shared with
    67  	// registered services, instead those can use utility methods to create/access
    68  	// databases or flat files. This enables ephemeral nodes which can fully reside
    69  	// in memory.
    70  	DataDir string
    71  
    72  	// Governance, if set, is used as perform the functions of the governance node.
    73  	Governance bool
    74  
    75  	// Configuration of peer-to-peer networking.
    76  	P2P p2p.Config
    77  
    78  	// KeyStoreDir is the file system folder that contains private keys. The directory can
    79  	// be specified as a relative path, in which case it is resolved relative to the
    80  	// current directory.
    81  	//
    82  	// If KeyStoreDir is empty, the default location is the "keystore" subdirectory of
    83  	// DataDir. If DataDir is unspecified and KeyStoreDir is empty, an ephemeral directory
    84  	// is created by New and destroyed when the node is stopped.
    85  	KeyStoreDir string `toml:",omitempty"`
    86  
    87  	// UseLightweightKDF lowers the memory and CPU requirements of the key store
    88  	// scrypt KDF at the expense of security.
    89  	UseLightweightKDF bool `toml:",omitempty"`
    90  
    91  	// NoUSB disables hardware wallet monitoring and connectivity.
    92  	NoUSB bool `toml:",omitempty"`
    93  
    94  	// IPCPath is the requested location to place the IPC endpoint. If the path is
    95  	// a simple file name, it is placed inside the data directory (or on the root
    96  	// pipe path on Windows), whereas if it's a resolvable path name (absolute or
    97  	// relative), then that specific path is enforced. An empty path disables IPC.
    98  	IPCPath string `toml:",omitempty"`
    99  
   100  	// HTTPHost is the host interface on which to start the HTTP RPC server. If this
   101  	// field is empty, no HTTP API endpoint will be started.
   102  	HTTPHost string `toml:",omitempty"`
   103  
   104  	// HTTPPort is the TCP port number on which to start the HTTP RPC server. The
   105  	// default zero value is/ valid and will pick a port number randomly (useful
   106  	// for ephemeral nodes).
   107  	HTTPPort int `toml:",omitempty"`
   108  
   109  	// HTTPCors is the Cross-Origin Resource Sharing header to send to requesting
   110  	// clients. Please be aware that CORS is a browser enforced security, it's fully
   111  	// useless for custom HTTP clients.
   112  	HTTPCors []string `toml:",omitempty"`
   113  
   114  	// HTTPModules is a list of API modules to expose via the HTTP RPC interface.
   115  	// If the module list is empty, all RPC API endpoints designated public will be
   116  	// exposed.
   117  	HTTPModules []string `toml:",omitempty"`
   118  
   119  	// WSHost is the host interface on which to start the websocket RPC server. If
   120  	// this field is empty, no websocket API endpoint will be started.
   121  	WSHost string `toml:",omitempty"`
   122  
   123  	// WSPort is the TCP port number on which to start the websocket RPC server. The
   124  	// default zero value is/ valid and will pick a port number randomly (useful for
   125  	// ephemeral nodes).
   126  	WSPort int `toml:",omitempty"`
   127  
   128  	// WSOrigins is the list of domain to accept websocket requests from. Please be
   129  	// aware that the server can only act upon the HTTP request the client sends and
   130  	// cannot verify the validity of the request header.
   131  	WSOrigins []string `toml:",omitempty"`
   132  
   133  	// WSModules is a list of API modules to expose via the websocket RPC interface.
   134  	// If the module list is empty, all RPC API endpoints designated public will be
   135  	// exposed.
   136  	WSModules []string `toml:",omitempty"`
   137  }
   138  
   139  // IPCEndpoint resolves an IPC endpoint based on a configured value, taking into
   140  // account the set data folders as well as the designated platform we're currently
   141  // running on.
   142  func (c *Config) IPCEndpoint() string {
   143  	// Short circuit if IPC has not been enabled
   144  	if c.IPCPath == "" {
   145  		return ""
   146  	}
   147  	// On windows we can only use plain top-level pipes
   148  	if runtime.GOOS == "windows" {
   149  		if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
   150  			return c.IPCPath
   151  		}
   152  		return `\\.\pipe\` + c.IPCPath
   153  	}
   154  	// Resolve names into the data directory full paths otherwise
   155  	if filepath.Base(c.IPCPath) == c.IPCPath {
   156  		if c.DataDir == "" {
   157  			return filepath.Join(os.TempDir(), c.IPCPath)
   158  		}
   159  		return filepath.Join(c.DataDir, c.IPCPath)
   160  	}
   161  	return c.IPCPath
   162  }
   163  
   164  // NodeDB returns the path to the discovery node database.
   165  func (c *Config) NodeDB() string {
   166  	if c.DataDir == "" {
   167  		return "" // ephemeral
   168  	}
   169  	return c.resolvePath("nodes")
   170  }
   171  
   172  // DefaultIPCEndpoint returns the IPC path used by default.
   173  func DefaultIPCEndpoint(clientIdentifier string) string {
   174  	if clientIdentifier == "" {
   175  		clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   176  		if clientIdentifier == "" {
   177  			panic("empty executable name")
   178  		}
   179  	}
   180  	config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"}
   181  	return config.IPCEndpoint()
   182  }
   183  
   184  // HTTPEndpoint resolves an HTTP endpoint based on the configured host interface
   185  // and port parameters.
   186  func (c *Config) HTTPEndpoint() string {
   187  	if c.HTTPHost == "" {
   188  		return ""
   189  	}
   190  	return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
   191  }
   192  
   193  // DefaultHTTPEndpoint returns the HTTP endpoint used by default.
   194  func DefaultHTTPEndpoint() string {
   195  	config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort}
   196  	return config.HTTPEndpoint()
   197  }
   198  
   199  // WSEndpoint resolves an websocket endpoint based on the configured host interface
   200  // and port parameters.
   201  func (c *Config) WSEndpoint() string {
   202  	if c.WSHost == "" {
   203  		return ""
   204  	}
   205  	return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
   206  }
   207  
   208  // DefaultWSEndpoint returns the websocket endpoint used by default.
   209  func DefaultWSEndpoint() string {
   210  	config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort}
   211  	return config.WSEndpoint()
   212  }
   213  
   214  // NodeName returns the devp2p node identifier.
   215  func (c *Config) NodeName() string {
   216  	name := c.name()
   217  	// Backwards compatibility: previous versions used title-cased "Geth", keep that.
   218  	if name == "geth" || name == "geth-testnet" {
   219  		name = "Geth"
   220  	}
   221  	if c.UserIdent != "" {
   222  		name += "/" + c.UserIdent
   223  	}
   224  	if c.Version != "" {
   225  		name += "/v" + c.Version
   226  	}
   227  	name += "/" + runtime.GOOS + "-" + runtime.GOARCH
   228  	name += "/" + runtime.Version()
   229  	return name
   230  }
   231  
   232  func (c *Config) name() string {
   233  	if c.Name == "" {
   234  		progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
   235  		if progname == "" {
   236  			panic("empty executable name, set Config.Name")
   237  		}
   238  		return progname
   239  	}
   240  	return c.Name
   241  }
   242  
   243  // These resources are resolved differently for "geth" instances.
   244  var isOldGethResource = map[string]bool{
   245  	"chaindata":          true,
   246  	"nodes":              true,
   247  	"nodekey":            true,
   248  	"static-nodes.json":  true,
   249  	"qmanager-nodes.json": true,
   250  	"trusted-nodes.json": true,
   251  }
   252  
   253  // resolvePath resolves path in the instance directory.
   254  func (c *Config) resolvePath(path string) string {
   255  	if filepath.IsAbs(path) {
   256  		log.Info("current Directory: ", "path", path)
   257  		return path
   258  	}
   259  	if c.DataDir == "" {
   260  		return ""
   261  	}
   262  	// Backwards-compatibility: ensure that data directory files created
   263  	// by geth 1.4 are used if they exist.
   264  	if c.name() == "geth" && isOldGethResource[path] {
   265  		oldpath := ""
   266  		if c.Name == "geth" {
   267  			oldpath = filepath.Join(c.DataDir, path)
   268  
   269  		}
   270  		if oldpath != "" && common.FileExist(oldpath) {
   271  			// TODO: print warning
   272  
   273  			return oldpath
   274  		}
   275  	}
   276  	return filepath.Join(c.instanceDir(), path)
   277  }
   278  
   279  func (c *Config) instanceDir() string {
   280  	if c.DataDir == "" {
   281  		return ""
   282  	}
   283  	return filepath.Join(c.DataDir, c.name())
   284  }
   285  
   286  // NodeKey retrieves the currently configured private key of the node, checking
   287  // first any manually set key, falling back to the one found in the configured
   288  // data folder. If no key can be found, a new one is generated.
   289  func (c *Config) NodeKey() *ecdsa.PrivateKey {
   290  	// Use any specifically configured key.
   291  	if c.P2P.PrivateKey != nil {
   292  		return c.P2P.PrivateKey
   293  	}
   294  	// Generate ephemeral key if no datadir is being used.
   295  	if c.DataDir == "" {
   296  		key, err := crypto.GenerateKey()
   297  		if err != nil {
   298  			log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err))
   299  		}
   300  		return key
   301  	}
   302  
   303  	keyfile := c.resolvePath(datadirPrivateKey)
   304  	if key, err := crypto.LoadECDSA(keyfile); err == nil {
   305  		return key
   306  	}
   307  	// No persistent key found, generate and store a new one.
   308  	key, err := crypto.GenerateKey()
   309  	if err != nil {
   310  		log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
   311  	}
   312  	instanceDir := filepath.Join(c.DataDir, c.name())
   313  	if err := os.MkdirAll(instanceDir, 0700); err != nil {
   314  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   315  		return key
   316  	}
   317  	keyfile = filepath.Join(instanceDir, datadirPrivateKey)
   318  	if err := crypto.SaveECDSA(keyfile, key); err != nil {
   319  		log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
   320  	}
   321  	return key
   322  }
   323  
   324  func (c *Config) GetDataDir(dirname string) string {
   325      c.Name = datadirDefaultKeyStore
   326      defer func() {
   327          c.Name = ""
   328      }()
   329      return c.resolvePath(dirname)
   330  }
   331  
   332  // StaticNodes returns a list of node enode URLs configured as static nodes.
   333  func (c *Config) StaticNodes() []*discover.Node {
   334  	return c.parsePersistentNodes(c.resolvePath(datadirStaticNodes))
   335  }
   336  
   337  // QmanagerNodes returns a list of node enode URLs configured as Qmanager nodes.
   338  //func (c *Config) QmanagerNodes() []*discover.Node {
   339  //	return c.parsePersistentNodes(c.resolvePath(datadirQmanagerNodes))
   340  //}
   341  
   342  // StaticNodes returns a list of node enode URLs configured as static nodes.
   343  func (c *Config) BootNodes() []*discover.Node {
   344  	return c.parsePersistentNodes(c.resolvePath(datadirBootNodes))
   345  }
   346  
   347  // TrusterNodes returns a list of node enode URLs configured as trusted nodes.
   348  func (c *Config) TrusterNodes() []*discover.Node {
   349  	return c.parsePersistentNodes(c.resolvePath(datadirTrustedNodes))
   350  }
   351  
   352  // parsePersistentNodes parses a list of discovery node URLs loaded from a .json
   353  // file from within the data directory.
   354  func (c *Config) parsePersistentNodes(path string) []*discover.Node {
   355  	// Short circuit if no node config is present
   356  	if c.DataDir == "" {
   357  		return nil
   358  	}
   359  	if _, err := os.Stat(path); err != nil {
   360  		return nil
   361  	}
   362  	// Load the nodes from the config file.
   363  	var nodelist []string
   364  	if err := common.LoadJSON(path, &nodelist); err != nil {
   365  		log.Error(fmt.Sprintf("Can't load node file %s: %v", path, err))
   366  		return nil
   367  	}
   368  	// Interpret the list as a discovery node array
   369  	var nodes []*discover.Node
   370  	for _, url := range nodelist {  //static-nodes.json 을 슬라이스로 읽어들임. nodelist
   371  		if url == "" {
   372  			continue
   373  		}
   374  		node, err := discover.ParseNode(url)
   375  		log.Info(fmt.Sprintf("Node loaded from json files) %s: %v\n", url, err))
   376  		if err != nil {
   377  			log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
   378  			continue
   379  		}
   380  		nodes = append(nodes, node)
   381  		//qmanager node 추가 ?
   382  	}
   383  	return nodes
   384  }
   385  
   386  func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
   387  	scryptN := keystore.StandardScryptN
   388  	scryptP := keystore.StandardScryptP
   389  	if conf.UseLightweightKDF {
   390  		scryptN = keystore.LightScryptN
   391  		scryptP = keystore.LightScryptP
   392  	}
   393  
   394  	var (
   395  		keydir    string
   396  		ephemeral string
   397  		err       error
   398  	)
   399  	switch {
   400  	case filepath.IsAbs(conf.KeyStoreDir):
   401  		keydir = conf.KeyStoreDir
   402  	case conf.DataDir != "":
   403  		if conf.KeyStoreDir == "" {
   404  			keydir = filepath.Join(conf.DataDir, datadirDefaultKeyStore)
   405  		} else {
   406  			keydir, err = filepath.Abs(conf.KeyStoreDir)
   407  		}
   408  	case conf.KeyStoreDir != "":
   409  		keydir, err = filepath.Abs(conf.KeyStoreDir)
   410  	default:
   411  		// There is no datadir.
   412  		keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
   413  		ephemeral = keydir
   414  	}
   415  	if err != nil {
   416  		return nil, "", err
   417  	}
   418  	if err := os.MkdirAll(keydir, 0700); err != nil {
   419  		return nil, "", err
   420  	}
   421  	// Assemble the account manager and supported backends
   422  	backends := []accounts.Backend{
   423  		keystore.NewKeyStore(keydir, scryptN, scryptP),
   424  	}
   425  	if !conf.NoUSB {
   426  		if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil {
   427  			log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err))
   428  		} else {
   429  			backends = append(backends, ledgerhub)
   430  		}
   431  	}
   432  	return accounts.NewManager(backends...), ephemeral, nil
   433  }