-
Notifications
You must be signed in to change notification settings - Fork 25
/
process.go
219 lines (194 loc) · 6.38 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package process
import (
"fmt"
"path/filepath"
"sync"
"github.com/flare-foundation/flare/app"
"github.com/flare-foundation/flare/database/leveldb"
"github.com/flare-foundation/flare/database/manager"
"github.com/flare-foundation/flare/database/memdb"
"github.com/flare-foundation/flare/database/rocksdb"
"github.com/flare-foundation/flare/nat"
"github.com/flare-foundation/flare/node"
"github.com/flare-foundation/flare/utils/constants"
"github.com/flare-foundation/flare/utils/dynamicip"
"github.com/flare-foundation/flare/utils/logging"
"github.com/flare-foundation/flare/utils/perms"
"github.com/flare-foundation/flare/version"
)
const (
Header = `
( ( (
)\ ) )\ ) ( )\ )
(()/( (()/( )\ (()/( (
/(_)) /(_))((((_)( /(_)))\
(_))_|(_)) )\ _ )\ (_)) ((_)
| |_ | | (_)_\(_)| _ \| __|
| __| | |__ / _ \ | /| _|
|_| |____| /_/ \_\ |_|_\|___|
`
)
var (
stakingPortName = fmt.Sprintf("%s-staking", constants.AppName)
httpPortName = fmt.Sprintf("%s-http", constants.AppName)
_ app.App = &process{}
)
// 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.
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
}
// start the db manager
var dbManager manager.Manager
switch p.config.DatabaseConfig.Name {
case rocksdb.Name:
path := filepath.Join(p.config.DatabaseConfig.Path, rocksdb.Name)
dbManager, err = manager.NewRocksDB(path, p.config.DatabaseConfig.Config, log, version.CurrentDatabase)
case leveldb.Name:
dbManager, err = manager.NewLevelDB(p.config.DatabaseConfig.Path, p.config.DatabaseConfig.Config, log, version.CurrentDatabase)
case memdb.Name:
dbManager = manager.NewMemDB(version.CurrentDatabase)
default:
err = fmt.Errorf(
"db-type was %q but should have been one of {%s, %s, %s}",
p.config.DatabaseConfig.Name,
leveldb.Name,
rocksdb.Name,
memdb.Name,
)
}
if err != nil {
log.Fatal("couldn't create %q db manager at %s: %s", p.config.DatabaseConfig.Name, p.config.DatabaseConfig.Path, err)
logFactory.Close()
return err
}
// Track if sybil control is enforced
if !p.config.EnableStaking {
log.Warn("Staking is disabled. Sybil control is not enforced.")
}
// Check if transaction signatures should be checked
if !p.config.EnableCrypto {
// TODO: actually disable crypto verification
log.Warn("transaction signatures are not being checked")
}
// Track if assertions should be executed
if p.config.LoggingConfig.Assertions {
log.Debug("assertions are enabled. This may slow down execution")
}
// 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 p.config.IP.IP().Port != 0 {
mapper.Map(
"TCP",
p.config.IP.IP().Port,
p.config.IP.IP().Port,
stakingPortName,
&p.config.IP,
p.config.DynamicUpdateDuration,
)
}
// 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(
"TCP",
p.config.HTTPPort,
p.config.HTTPPort,
httpPortName,
nil,
p.config.DynamicUpdateDuration,
)
}
// Regularly updates our public IP (or does nothing, if configured that way)
externalIPUpdater := dynamicip.NewDynamicIPManager(
p.config.DynamicPublicIPResolver,
p.config.DynamicUpdateDuration,
log,
&p.config.IP,
)
if err := p.node.Initialize(&p.config, dbManager, log, logFactory); err != nil {
log.Fatal("error initializing node: %s", err)
mapper.UnmapAllPorts()
externalIPUpdater.Stop()
if err := dbManager.Close(); err != nil {
log.Warn("failed to close the node's DB: %s", err)
}
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()
externalIPUpdater.Stop()
if err := dbManager.Close(); err != nil {
log.Warn("failed to close the node's DB: %s", err)
}
// 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 with: %s", 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
}