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