-
Notifications
You must be signed in to change notification settings - Fork 649
/
process.go
181 lines (157 loc) · 5.13 KB
/
process.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package process
import (
"fmt"
"sync"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/app"
"github.com/ava-labs/avalanchego/nat"
"github.com/ava-labs/avalanchego/node"
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/perms"
"github.com/ava-labs/avalanchego/utils/ulimit"
)
const (
Header = ` _____ .__ .__
/ _ \___ _______ | | _____ ____ ____ | |__ ____ ,_ o
/ /_\ \ \/ /\__ \ | | \__ \ / \_/ ___\| | \_/ __ \ / //\,
/ | \ / / __ \| |__/ __ \| | \ \___| Y \ ___/ \>> |
\____|__ /\_/ (____ /____(____ /___| /\___ >___| /\___ > \\
\/ \/ \/ \/ \/ \/ \/`
)
var (
stakingPortName = fmt.Sprintf("%s-staking", constants.AppName)
httpPortName = fmt.Sprintf("%s-http", constants.AppName)
_ app.App = (*process)(nil)
)
// process is a wrapper around a node that runs in this process
type process struct {
config node.Config
node *node.Node
exitWG sync.WaitGroup
}
func NewApp(config node.Config) app.App {
return &process{
config: config,
node: &node.Node{},
}
}
// Start the business logic of the node (as opposed to config reading, etc).
// Does not block until the node is done. Errors returned from this method
// are not logged.
func (p *process) Start() error {
// Set the data directory permissions to be read write.
if err := perms.ChmodR(p.config.DatabaseConfig.Path, true, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to restrict the permissions of the database directory with: %w", err)
}
if err := perms.ChmodR(p.config.LoggingConfig.Directory, true, perms.ReadWriteExecute); err != nil {
return fmt.Errorf("failed to restrict the permissions of the log directory with: %w", err)
}
// we want to create the logger after the plugin has started the app
logFactory := logging.NewFactory(p.config.LoggingConfig)
log, err := logFactory.Make("main")
if err != nil {
logFactory.Close()
return err
}
// update fd limit
fdLimit := p.config.FdLimit
if err := ulimit.Set(fdLimit, log); err != nil {
log.Fatal("failed to set fd-limit",
zap.Error(err),
)
logFactory.Close()
return err
}
// Track if sybil control is enforced
if !p.config.EnableStaking {
log.Warn("sybil control is not enforced",
zap.String("reason", "staking is disabled"),
)
}
// TODO move this to config
// SupportsNAT() for NoRouter is false.
// Which means we tried to perform a NAT activity but we were not successful.
if p.config.AttemptedNATTraversal && !p.config.Nat.SupportsNAT() {
log.Warn("UPnP or NAT-PMP router attach failed, you may not be listening publicly. " +
"Please confirm the settings in your router")
}
mapper := nat.NewPortMapper(log, p.config.Nat)
// Open staking port we want for NAT traversal to have the external port
// (config.IP.Port) to connect to our internal listening port
// (config.InternalStakingPort) which should be the same in most cases.
if port := p.config.IPPort.IPPort().Port; port != 0 {
mapper.Map(
port,
port,
stakingPortName,
p.config.IPPort,
p.config.IPResolutionFreq,
)
}
// Open the HTTP port iff the HTTP server is not listening on localhost
if p.config.HTTPHost != "127.0.0.1" && p.config.HTTPHost != "localhost" && p.config.HTTPPort != 0 {
// For NAT traversal we want to route from the external port
// (config.ExternalHTTPPort) to our internal port (config.HTTPPort)
mapper.Map(
p.config.HTTPPort,
p.config.HTTPPort,
httpPortName,
nil,
p.config.IPResolutionFreq,
)
}
// Regularly update our public IP.
// Note that if the node config said to not dynamically resolve and
// update our public IP, [p.config.IPUdater] is a no-op implementation.
go p.config.IPUpdater.Dispatch(log)
if err := p.node.Initialize(&p.config, log, logFactory); err != nil {
log.Fatal("error initializing node",
zap.Error(err),
)
mapper.UnmapAllPorts()
p.config.IPUpdater.Stop()
log.Stop()
logFactory.Close()
return err
}
// [p.ExitCode] will block until [p.exitWG.Done] is called
p.exitWG.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
fmt.Println("caught panic", r)
}
log.Stop()
logFactory.Close()
p.exitWG.Done()
}()
defer func() {
mapper.UnmapAllPorts()
p.config.IPUpdater.Stop()
// If [p.node.Dispatch()] panics, then we should log the panic and
// then re-raise the panic. This is why the above defer is broken
// into two parts.
log.StopOnPanic()
}()
err := p.node.Dispatch()
log.Debug("dispatch returned",
zap.Error(err),
)
}()
return nil
}
// Stop attempts to shutdown the currently running node. This function will
// return immediately.
func (p *process) Stop() error {
p.node.Shutdown(0)
return nil
}
// ExitCode returns the exit code that the node is reporting. This function
// blocks until the node has been shut down.
func (p *process) ExitCode() (int, error) {
p.exitWG.Wait()
return p.node.ExitCode(), nil
}