github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/node/defaults.go (about)

     1  // Copyright 2016 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  	"os"
    21  	"os/user"
    22  	"path/filepath"
    23  	"runtime"
    24  
    25  	"github.com/ethereum/go-ethereum/p2p"
    26  	"github.com/ethereum/go-ethereum/p2p/nat"
    27  	"github.com/ethereum/go-ethereum/rpc"
    28  )
    29  
    30  const (
    31  	DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
    32  	DefaultHTTPPort = 8545        // Default TCP port for the HTTP RPC server
    33  	DefaultWSHost   = "localhost" // Default host interface for the websocket RPC server
    34  	DefaultWSPort   = 8546        // Default TCP port for the websocket RPC server
    35  	DefaultAuthHost = "localhost" // Default host interface for the authenticated apis
    36  	DefaultAuthPort = 8551        // Default port for the authenticated apis
    37  )
    38  
    39  const (
    40  	// Engine API batch limits: these are not configurable by users, and should cover the
    41  	// needs of all CLs.
    42  	engineAPIBatchItemLimit         = 2000
    43  	engineAPIBatchResponseSizeLimit = 250 * 1000 * 1000
    44  	engineAPIBodyLimit              = 128 * 1024 * 1024
    45  )
    46  
    47  var (
    48  	DefaultAuthCors    = []string{"localhost"} // Default cors domain for the authenticated apis
    49  	DefaultAuthVhosts  = []string{"localhost"} // Default virtual hosts for the authenticated apis
    50  	DefaultAuthOrigins = []string{"localhost"} // Default origins for the authenticated apis
    51  	DefaultAuthPrefix  = ""                    // Default prefix for the authenticated apis
    52  	DefaultAuthModules = []string{"eth", "engine"}
    53  )
    54  
    55  // DefaultConfig contains reasonable default settings.
    56  var DefaultConfig = Config{
    57  	DataDir:              DefaultDataDir(),
    58  	HTTPPort:             DefaultHTTPPort,
    59  	AuthAddr:             DefaultAuthHost,
    60  	AuthPort:             DefaultAuthPort,
    61  	AuthVirtualHosts:     DefaultAuthVhosts,
    62  	HTTPModules:          []string{"net", "web3"},
    63  	HTTPVirtualHosts:     []string{"localhost"},
    64  	HTTPTimeouts:         rpc.DefaultHTTPTimeouts,
    65  	WSPort:               DefaultWSPort,
    66  	WSModules:            []string{"net", "web3"},
    67  	BatchRequestLimit:    1000,
    68  	BatchResponseMaxSize: 25 * 1000 * 1000,
    69  	GraphQLVirtualHosts:  []string{"localhost"},
    70  	P2P: p2p.Config{
    71  		ListenAddr: ":30303",
    72  		MaxPeers:   50,
    73  		NAT:        nat.Any(),
    74  	},
    75  	DBEngine: "", // Use whatever exists, will default to Pebble if non-existent and supported
    76  }
    77  
    78  // DefaultDataDir is the default data directory to use for the databases and other
    79  // persistence requirements.
    80  func DefaultDataDir() string {
    81  	// Try to place the data folder in the user's home dir
    82  	home := homeDir()
    83  	if home != "" {
    84  		switch runtime.GOOS {
    85  		case "darwin":
    86  			return filepath.Join(home, "Library", "Ethereum")
    87  		case "windows":
    88  			// We used to put everything in %HOME%\AppData\Roaming, but this caused
    89  			// problems with non-typical setups. If this fallback location exists and
    90  			// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
    91  			fallback := filepath.Join(home, "AppData", "Roaming", "Ethereum")
    92  			appdata := windowsAppData()
    93  			if appdata == "" || isNonEmptyDir(fallback) {
    94  				return fallback
    95  			}
    96  			return filepath.Join(appdata, "Ethereum")
    97  		default:
    98  			return filepath.Join(home, ".ethereum")
    99  		}
   100  	}
   101  	// As we cannot guess a stable location, return empty and handle later
   102  	return ""
   103  }
   104  
   105  func windowsAppData() string {
   106  	v := os.Getenv("LOCALAPPDATA")
   107  	if v == "" {
   108  		// Windows XP and below don't have LocalAppData. Crash here because
   109  		// we don't support Windows XP and undefining the variable will cause
   110  		// other issues.
   111  		panic("environment variable LocalAppData is undefined")
   112  	}
   113  	return v
   114  }
   115  
   116  func isNonEmptyDir(dir string) bool {
   117  	f, err := os.Open(dir)
   118  	if err != nil {
   119  		return false
   120  	}
   121  	names, _ := f.Readdir(1)
   122  	f.Close()
   123  	return len(names) > 0
   124  }
   125  
   126  func homeDir() string {
   127  	if home := os.Getenv("HOME"); home != "" {
   128  		return home
   129  	}
   130  	if usr, err := user.Current(); err == nil {
   131  		return usr.HomeDir
   132  	}
   133  	return ""
   134  }