github.com/klaytn/klaytn@v1.12.1/node/defaults.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2016 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from node/defaults.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package node
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  	"os/user"
    27  	"path/filepath"
    28  	"runtime"
    29  	"strings"
    30  
    31  	"github.com/klaytn/klaytn/networks/rpc"
    32  
    33  	"github.com/klaytn/klaytn/storage/database"
    34  
    35  	"github.com/klaytn/klaytn/networks/p2p"
    36  	"github.com/klaytn/klaytn/networks/p2p/nat"
    37  )
    38  
    39  const (
    40  	DefaultHTTPHost               = "localhost" // Default host interface for the HTTP RPC server
    41  	DefaultHTTPPort               = 8551        // Default TCP port for the HTTP RPC server
    42  	DefaultWSHost                 = "localhost" // Default host interface for the websocket RPC server
    43  	DefaultWSPort                 = 8552        // Default TCP port for the websocket RPC server
    44  	DefaultGRPCHost               = "localhost" // Default host interface for the gRPC server
    45  	DefaultGRPCPort               = 8553        // Default TCP port for the gRPC server
    46  	DefaultP2PPort                = 32323
    47  	DefaultP2PSubPort             = 32324
    48  	DefaultMaxPhysicalConnections = 10 // Default the max number of node's physical connections
    49  )
    50  
    51  // DefaultConfig contains reasonable default settings.
    52  var DefaultConfig = Config{
    53  	DBType:           DefaultDBType(),
    54  	DataDir:          DefaultDataDir(),
    55  	HTTPPort:         DefaultHTTPPort,
    56  	HTTPModules:      []string{"net", "web3"},
    57  	HTTPVirtualHosts: []string{"localhost"},
    58  	HTTPTimeouts:     rpc.DefaultHTTPTimeouts,
    59  	WSPort:           DefaultWSPort,
    60  	WSModules:        []string{"net", "web3"},
    61  	GRPCPort:         DefaultGRPCPort,
    62  	P2P: p2p.Config{
    63  		ListenAddr:             fmt.Sprintf(":%d", DefaultP2PPort),
    64  		MaxPhysicalConnections: DefaultMaxPhysicalConnections,
    65  		NAT:                    nat.Any(),
    66  	},
    67  }
    68  
    69  func DefaultDBType() database.DBType {
    70  	return database.LevelDB
    71  }
    72  
    73  // DefaultDataDir is the default data directory to use for the databases and other
    74  // persistence requirements.
    75  func DefaultDataDir() string {
    76  	// Try to place the data folder in the user's home dir
    77  	dirname := filepath.Base(os.Args[0])
    78  	if dirname == "" {
    79  		dirname = "klay"
    80  	}
    81  	home := homeDir()
    82  	if home != "" {
    83  		if runtime.GOOS == "darwin" {
    84  			return filepath.Join(home, "Library", strings.ToUpper(dirname))
    85  		} else if runtime.GOOS == "windows" {
    86  			return filepath.Join(home, "AppData", "Roaming", strings.ToUpper(dirname))
    87  		} else {
    88  			return filepath.Join(home, "."+dirname)
    89  		}
    90  	}
    91  	// As we cannot guess a stable location, return empty and handle later
    92  	return ""
    93  }
    94  
    95  func homeDir() string {
    96  	if home := os.Getenv("HOME"); home != "" {
    97  		return home
    98  	}
    99  	if usr, err := user.Current(); err == nil {
   100  		return usr.HomeDir
   101  	}
   102  	return ""
   103  }