forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (159 loc) · 5.28 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"fmt"
"github.com/ava-labs/avalanchego/nat"
"github.com/ava-labs/avalanchego/node"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/crypto"
"github.com/ava-labs/avalanchego/utils/dynamicip"
"github.com/ava-labs/avalanchego/utils/logging"
)
const (
header = "" +
` _____ .__ .__` + "\n" +
` / _ \___ _______ | | _____ ____ ____ | |__ ____ ,_ o` + "\n" +
` / /_\ \ \/ /\__ \ | | \__ \ / \_/ ___\| | \_/ __ \ / //\,` + "\n" +
` / | \ / / __ \| |__/ __ \| | \ \___| Y \ ___/ \>> |` + "\n" +
` \____|__ /\_/ (____ /____(____ /___| /\___ >___| /\___ > \\` + "\n" +
` \/ \/ \/ \/ \/ \/ \/`
)
var (
stakingPortName = fmt.Sprintf("%s-staking", constants.AppName)
httpPortName = fmt.Sprintf("%s-http", constants.AppName)
)
// main is the primary entry point to Avalanche.
func main() {
// parse config using viper
if err := parseViper(); err != nil {
fmt.Printf("parsing parameters returned with error %s\n", err)
return
}
logFactory := logging.NewFactory(Config.LoggingConfig)
defer logFactory.Close()
log, err := logFactory.Make()
if err != nil {
fmt.Printf("starting logger failed with: %s\n", err)
return
}
fmt.Println(header)
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered panic from", r)
}
}()
defer func() {
if err := Config.DB.Close(); err != nil {
log.Warn("failed to close the node's DB: %s", err)
}
log.StopOnPanic()
log.Stop()
}()
// Track if sybil control is enforced
if !Config.EnableStaking && Config.EnableP2PTLS {
log.Warn("Staking is disabled. Sybil control is not enforced.")
}
if !Config.EnableStaking && !Config.EnableP2PTLS {
log.Warn("Staking and p2p encryption are disabled. Packet spoofing is possible.")
}
// Check if transaction signatures should be checked
if !Config.EnableCrypto {
log.Warn("transaction signatures are not being checked")
}
crypto.EnableCrypto = Config.EnableCrypto
if err := Config.ConsensusParams.Valid(); err != nil {
log.Error("consensus parameters are invalid: %s", err)
return
}
// Track if assertions should be executed
if Config.LoggingConfig.Assertions {
log.Debug("assertions are enabled. This may slow down execution")
}
// SupportsNAT() for NoRouter is false.
// Which means we tried to perform a NAT activity but we were not successful.
if Config.AttemptedNATTraversal && !Config.Nat.SupportsNAT() {
log.Error("UPnP or NAT-PMP router attach failed, you may not be listening publicly," +
" please confirm the settings in your router")
}
mapper := nat.NewPortMapper(log, Config.Nat)
defer mapper.UnmapAllPorts()
// Open staking port we want for NAT Traversal to have the external port
// (Config.StakingIP.Port) to connect to our internal listening port
// (Config.InternalStakingPort) which should be the same in most cases.
mapper.Map(
"TCP",
Config.StakingIP.IP().Port,
Config.StakingIP.IP().Port,
stakingPortName,
&Config.StakingIP,
Config.DynamicUpdateDuration,
)
// Open the HTTP port iff the HTTP server is not listening on localhost
if Config.HTTPHost != "127.0.0.1" && Config.HTTPHost != "localhost" {
// For NAT Traversal we want to route from the external port
// (Config.ExternalHTTPPort) to our internal port (Config.HTTPPort)
mapper.Map(
"TCP",
Config.HTTPPort,
Config.HTTPPort,
httpPortName,
nil,
Config.DynamicUpdateDuration,
)
}
// Regularly updates our public IP (or does nothing, if configured that way)
externalIPUpdater := dynamicip.NewDynamicIPManager(
Config.DynamicPublicIPResolver,
Config.DynamicUpdateDuration,
log,
&Config.StakingIP,
)
defer externalIPUpdater.Stop()
log.Info("this node's IP is set to: %s", Config.StakingIP.IP())
for {
shouldRestart, err := run(log, logFactory)
if err != nil {
break
}
// If the node says it should restart, do that. Otherwise, end the program.
if !shouldRestart {
break
}
log.Info("restarting node")
}
}
// Initialize and run the node.
// Returns true if the node should restart after this function returns.
func run(log logging.Logger, logFactory logging.Factory) (bool, error) {
log.Info("initializing node")
node := node.Node{}
restarter := &restarter{
node: &node,
shouldRestart: &utils.AtomicBool{},
}
if err := node.Initialize(&Config, log, logFactory, restarter); err != nil {
log.Error("error initializing node: %s", err)
return restarter.shouldRestart.GetValue(), err
}
log.Debug("dispatching node handlers")
err := node.Dispatch()
if err != nil {
log.Debug("node dispatch returned: %s", err)
}
return restarter.shouldRestart.GetValue(), nil
}
// restarter implements utils.Restarter
type restarter struct {
node *node.Node
// If true, node should restart after shutting down
shouldRestart *utils.AtomicBool
}
// Restarter shuts down the node and marks that it should restart
// It is safe to call Restart multiple times
func (r *restarter) Restart() {
r.shouldRestart.SetValue(true)
// Shutdown is safe to call multiple times because it uses sync.Once
r.node.Shutdown()
}