This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
/
defaults.go
112 lines (103 loc) · 3.34 KB
/
defaults.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/nat"
"github.com/ethereum/go-ethereum/rpc"
)
const (
//DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server
//DefaultWSHost = "localhost" // Default host interface for the websocket RPC server
//DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server
DefaultP2PPort = 5050 // Default p2p port for listening
DefaultHTTPPort = 18545 // Default TCP port for the HTTP RPC server
DefaultWSPort = 18546 // Default TCP port for the websocket RPC server
DefaultGraphQLPort = 18547 // Default TCP port for the GraphQL server
)
func overrideFlags() {
utils.ListenPortFlag.Value = DefaultP2PPort
utils.HTTPPortFlag.Value = DefaultHTTPPort
utils.LegacyRPCPortFlag.Value = DefaultHTTPPort
utils.WSPortFlag.Value = DefaultWSPort
utils.LegacyWSPortFlag.Value = DefaultWSPort
utils.LegacyGraphQLPortFlag.Value = DefaultGraphQLPort
utils.LegacyGraphQLPortFlag.Value = DefaultGraphQLPort
}
// NodeDefaultConfig contains reasonable default settings.
var NodeDefaultConfig = node.Config{
DataDir: DefaultDataDir(),
HTTPPort: DefaultHTTPPort,
HTTPModules: []string{},
HTTPVirtualHosts: []string{"localhost"},
HTTPTimeouts: rpc.DefaultHTTPTimeouts,
WSPort: DefaultWSPort,
WSModules: []string{},
GraphQLVirtualHosts: []string{"localhost"},
P2P: p2p.Config{
NoDiscovery: false, // enable discovery v4 by default
DiscoveryV5: true, // enable discovery v5 by default
ListenAddr: fmt.Sprintf(":%d", DefaultP2PPort),
MaxPeers: 50,
NAT: nat.Any(),
},
}
// DefaultDataDir is the default data directory to use for the databases and other
// persistence requirements.
func DefaultDataDir() string {
// Try to place the data folder in the user's home dir
home := homeDir()
if home != "" {
switch runtime.GOOS {
case "darwin":
return filepath.Join(home, "Library", "Lachesis")
case "windows":
// We used to put everything in %HOME%\AppData\Roaming, but this caused
// problems with non-typical setups. If this fallback location exists and
// is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%.
fallback := filepath.Join(home, "AppData", "Roaming", "Lachesis")
appdata := windowsAppData()
if appdata == "" || isNonEmptyDir(fallback) {
return fallback
}
return filepath.Join(appdata, "Lachesis")
default:
return filepath.Join(home, ".lachesis")
}
}
// As we cannot guess a stable location, return empty and handle later
return ""
}
func windowsAppData() string {
v := os.Getenv("LOCALAPPDATA")
if v == "" {
// Windows XP and below don't have LocalAppData. Crash here because
// we don't support Windows XP and undefining the variable will cause
// other issues.
panic("environment variable LocalAppData is undefined")
}
return v
}
func isNonEmptyDir(dir string) bool {
f, err := os.Open(dir)
if err != nil {
return false
}
names, _ := f.Readdir(1)
f.Close()
return len(names) > 0
}
func homeDir() string {
if home := os.Getenv("HOME"); home != "" {
return home
}
if usr, err := user.Current(); err == nil {
return usr.HomeDir
}
return ""
}