-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathnode.go
More file actions
568 lines (502 loc) · 16.1 KB
/
node.go
File metadata and controls
568 lines (502 loc) · 16.1 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
package node
import (
"context"
"fmt"
"math/big"
"net/http"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/urfave/cli/v3"
"github.com/rocket-pool/smartnode/bindings/utils"
"github.com/rocket-pool/smartnode/rocketpool/node/collectors"
"github.com/rocket-pool/smartnode/shared/services"
"github.com/rocket-pool/smartnode/shared/services/alerting"
"github.com/rocket-pool/smartnode/shared/services/connectivity"
"github.com/rocket-pool/smartnode/shared/services/state"
"github.com/rocket-pool/smartnode/shared/services/wallet/keystore/lighthouse"
"github.com/rocket-pool/smartnode/shared/services/wallet/keystore/nimbus"
"github.com/rocket-pool/smartnode/shared/services/wallet/keystore/prysm"
"github.com/rocket-pool/smartnode/shared/services/wallet/keystore/teku"
"github.com/rocket-pool/smartnode/shared/utils/log"
)
// Config
var (
tasksInterval, _ = time.ParseDuration("5m")
taskCooldown, _ = time.ParseDuration("1s")
)
const (
MaxConcurrentEth1Requests = 200
DownloadRewardsTreesColor = color.FgGreen
MetricsColor = color.FgHiYellow
ManageFeeRecipientColor = color.FgHiCyan
DefendPdaoPropsColor = color.FgYellow
VerifyPdaoPropsColor = color.FgYellow
DistributeMinipoolsColor = color.FgHiGreen
ErrorColor = color.FgRed
WarningColor = color.FgYellow
UpdateColor = color.FgHiWhite
PrestakeMegapoolValidatorColor = color.FgHiGreen
StakeMegapoolValidatorColor = color.FgHiBlue
NotifyValidatorExitColor = color.FgHiYellow
NotifyFinalBalanceColor = color.FgHiMagenta
DefendChallengeExitColor = color.FgHiGreen
ProvisionExpressTickets = color.FgMagenta
SetUseLatestDelegateColor = color.FgBlue
CheckPortConnectivityColor = color.FgHiYellow
)
// Register node command
func RegisterCommands(app *cli.Command, name string, aliases []string) {
app.Commands = append(app.Commands, &cli.Command{
Name: name,
Aliases: aliases,
Usage: "Run Rocket Pool node activity daemon",
Action: func(ctx context.Context, c *cli.Command) error {
return run(c)
},
})
}
// Run daemon
func run(c *cli.Command) error {
// Handle the initial fee recipient file deployment
err := deployDefaultFeeRecipientFile(c)
if err != nil {
return err
}
// Clean up old fee recipient files
err = removeLegacyFeeRecipientFiles(c)
if err != nil {
return err
}
// Configure
configureHTTP()
// Load config early so we can start the HTTP API server before blocking
// on wallet/service readiness.
cfg, err := services.GetConfig(c)
if err != nil {
return err
}
// Print the current mode
if cfg.IsNativeMode {
fmt.Println("Starting node daemon in Native Mode.")
} else {
fmt.Println("Starting node daemon in Docker Mode.")
}
// Create a context that is cancelled on SIGINT/SIGTERM so the HTTP server
// and other background goroutines can shut down gracefully.
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer cancel()
// Start the HTTP API server immediately so the CLI can reach it while
// the daemon waits for the wallet and services to become ready.
startHTTP(ctx, c, cfg)
for {
// Exit if the process received SIGINT/SIGTERM
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Check the EC status
err := services.WaitEthClientSynced(c, false) // Force refresh the primary / fallback EC status
if err != nil {
if !sleepWithContext(ctx, taskCooldown) {
return err
}
continue
}
// Check the BC status
err = services.WaitBeaconClientSynced(c, false) // Force refresh the primary / fallback BC status
if err != nil {
if !sleepWithContext(ctx, taskCooldown) {
return err
}
continue
}
break
}
// Wait until the node wallet stored on disk is registered
if err := services.WaitNodeRegistered(ctx, c, true); err != nil {
return err
}
// Get services
rp, err := services.GetRocketPool(c)
if err != nil {
return err
}
w, err := services.GetHdWallet(c)
if err != nil {
return err
}
bc, err := services.GetBeaconClient(c)
if err != nil {
return err
}
protocolVersion, err := utils.GetCurrentVersion(rp, nil)
if err != nil {
return fmt.Errorf("error getting protocol version: %w", err)
}
fmt.Printf("Protocol version: %s\n", protocolVersion)
nodeAccount, err := w.GetNodeAccount()
if err != nil {
return fmt.Errorf("error getting node account: %w", err)
}
// Initialize loggers
errorLog := log.NewColorLogger(ErrorColor)
updateLog := log.NewColorLogger(UpdateColor)
// Create the state provider. In live mode this is a NetworkStateManager
// backed by the real EC/BC; in --network-state mode it is a
// StaticNetworkStateProvider that serves from the pre-loaded snapshot.
var m state.NetworkStateProvider
if services.IsStaticStateMode(c) {
m, err = services.GetNetworkStateProvider(c)
if err != nil {
return fmt.Errorf("error getting network state provider: %w", err)
}
} else {
m = state.NewNetworkStateManager(rp, cfg.Smartnode.GetStateManagerContracts(), bc, &updateLog)
}
stateLocker := collectors.NewStateLocker()
// Initialize tasks
manageFeeRecipient, err := newManageFeeRecipient(c, log.NewColorLogger(ManageFeeRecipientColor))
if err != nil {
return err
}
defendChallengeExit, err := newDefendChallengeExit(c, log.NewColorLogger(DefendChallengeExitColor))
if err != nil {
return err
}
distributeMinipools, err := newDistributeMinipools(c, log.NewColorLogger(DistributeMinipoolsColor))
if err != nil {
return err
}
setUseLatestDelegate, err := newSetUseLatestDelegate(c, log.NewColorLogger(SetUseLatestDelegateColor))
if err != nil {
return err
}
stakeMegapoolValidators, err := newStakeMegapoolValidator(c, log.NewColorLogger(StakeMegapoolValidatorColor))
if err != nil {
return err
}
notifyValidatorExit, err := newNotifyValidatorExit(c, log.NewColorLogger(NotifyValidatorExitColor))
if err != nil {
return err
}
notifyFinalBalance, err := newNotifyFinalBalance(c, log.NewColorLogger(NotifyFinalBalanceColor))
if err != nil {
return err
}
downloadRewardsTrees, err := newDownloadRewardsTrees(c, log.NewColorLogger(DownloadRewardsTreesColor))
if err != nil {
return err
}
defendPdaoProps, err := newDefendPdaoProps(c, log.NewColorLogger(DefendPdaoPropsColor))
if err != nil {
return err
}
provisionExpressTickets, err := newProvisionExpressTickets(c, log.NewColorLogger(ProvisionExpressTickets))
if err != nil {
return err
}
var verifyPdaoProps *verifyPdaoProps
// Make sure the user opted into this duty
verifyEnabled := cfg.Smartnode.VerifyProposals.Value.(bool)
if verifyEnabled {
verifyPdaoProps, err = newVerifyPdaoProps(c, log.NewColorLogger(VerifyPdaoPropsColor))
if err != nil {
return err
}
}
var prestakeMegapoolValidator *prestakeMegapoolValidator
prestakeMegapoolValidator, err = newPrestakeMegapoolValidator(c, log.NewColorLogger(PrestakeMegapoolValidatorColor))
if err != nil {
return err
}
var checkPorts *connectivity.CheckPortConnectivity
checkPorts, err = connectivity.NewCheckPortConnectivity(c, cfg, log.NewColorLogger(CheckPortConnectivityColor))
if err != nil {
return err
}
// Wait group to handle the various threads
wg := new(sync.WaitGroup)
wg.Add(2)
// Run task loop
go func() {
defer wg.Done()
// we assume clients are synced on startup so that we don't send unnecessary alerts
wasExecutionClientSynced := true
wasBeaconClientSynced := true
for {
// Exit if the process received SIGINT/SIGTERM
select {
case <-ctx.Done():
return
default:
}
// Check the EC status
err := services.WaitEthClientSynced(c, false) // Force refresh the primary / fallback EC status
if err != nil {
wasExecutionClientSynced = false
errorLog.Printlnf("Execution client not synced: %s. Waiting for sync...", err.Error())
if !sleepWithContext(ctx, taskCooldown) {
return
}
continue
}
if !wasExecutionClientSynced {
updateLog.Println("Execution client is now synced.")
wasExecutionClientSynced = true
err := alerting.AlertExecutionClientSyncComplete(cfg)
if err != nil {
errorLog.Printlnf("error alerting execution client sync complete: %v", err)
}
}
// Check the BC status
err = services.WaitBeaconClientSynced(c, false) // Force refresh the primary / fallback BC status
if err != nil {
// NOTE: if not synced, it returns an error - so there isn't necessarily an underlying issue
wasBeaconClientSynced = false
errorLog.Printlnf("Beacon client not synced: %s. Waiting for sync...", err.Error())
if !sleepWithContext(ctx, taskCooldown) {
return
}
continue
}
if !wasBeaconClientSynced {
updateLog.Println("Beacon client is now synced.")
wasBeaconClientSynced = true
err := alerting.AlertBeaconClientSyncComplete(cfg)
if err != nil {
errorLog.Printlnf("error alerting beacon client sync complete: %v", err)
}
}
// Check if the protocol version has changed
newProtocolVersion, err := utils.GetCurrentVersion(rp, nil)
if err != nil {
errorLog.Println(err)
if !sleepWithContext(ctx, taskCooldown) {
return
}
continue
}
if newProtocolVersion.Compare(protocolVersion) != 0 {
updateLog.Printlnf("Protocol version changed to: %s\n", newProtocolVersion)
updateLog.Println("Exiting daemon to load the new contracts...")
os.Exit(0)
}
// Update the network state
state, err := updateNetworkState(m, &updateLog, nodeAccount.Address)
if err != nil {
errorLog.Println(err)
if !sleepWithContext(ctx, taskCooldown) {
return
}
continue
}
stateLocker.UpdateState(state)
// Manage the fee recipient for the node
if err := manageFeeRecipient.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the defend challenge exit task
if err := defendChallengeExit.run(state); err != nil {
errorLog.Println(err)
}
// Run the rewards download check
if err := downloadRewardsTrees.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the pDAO proposal defender
if err := defendPdaoProps.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the pDAO proposal verifier
if verifyPdaoProps != nil {
if err := verifyPdaoProps.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
}
// Run the megapool prestake check
if prestakeMegapoolValidator != nil {
if err := prestakeMegapoolValidator.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
}
// Run the megapool stake check
if err := stakeMegapoolValidators.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the megapool notify validator exit check
if err := notifyValidatorExit.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the megapool notify final balance check
if err := notifyFinalBalance.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the megapool provision express ticket check
if err := provisionExpressTickets.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the balance distribution check
if err := distributeMinipools.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the set use latest delegate check
if err := setUseLatestDelegate.run(state); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
// Run the port connectivity check
if err := checkPorts.Run(); err != nil {
errorLog.Println(err)
}
if !sleepWithContext(ctx, taskCooldown) {
return
}
if !sleepWithContext(ctx, tasksInterval) {
return
}
}
}()
// Run metrics loop
go func() {
defer wg.Done()
if err := runMetricsServer(ctx, c, log.NewColorLogger(MetricsColor), stateLocker); err != nil {
errorLog.Println(err)
}
}()
// Wait for both threads to stop
wg.Wait()
return nil
}
// sleepWithContext sleeps for d or until ctx is cancelled, returning false if cancelled.
func sleepWithContext(ctx context.Context, d time.Duration) bool {
select {
case <-ctx.Done():
return false
case <-time.After(d):
return true
}
}
// Configure HTTP transport settings
func configureHTTP() {
// The daemon makes a large number of concurrent RPC requests to the Eth1 client
// The HTTP transport is set to cache connections for future re-use equal to the maximum expected number of concurrent requests
// This prevents issues related to memory consumption and address allowance from repeatedly opening and closing connections
http.DefaultTransport.(*http.Transport).MaxIdleConnsPerHost = MaxConcurrentEth1Requests
}
// Copy the default fee recipient file into the proper location
func deployDefaultFeeRecipientFile(c *cli.Command) error {
cfg, err := services.GetConfig(c)
if err != nil {
return err
}
feeRecipientPath := cfg.Smartnode.GetGlobalFeeRecipientFilePath()
_, err = os.Stat(feeRecipientPath)
if os.IsNotExist(err) {
// Make sure the validators dir is created
validatorsFolder := filepath.Dir(feeRecipientPath)
err = os.MkdirAll(validatorsFolder, 0o755)
if err != nil {
return fmt.Errorf("could not create validators directory: %w", err)
}
// Create the file
var defaultFeeRecipientFileContents string
if cfg.IsNativeMode {
// Native mode needs an environment variable definition
defaultFeeRecipientFileContents = fmt.Sprintf("FEE_RECIPIENT=%s", cfg.Smartnode.GetRethAddress().Hex())
} else {
// Docker and Hybrid just need the address itself
defaultFeeRecipientFileContents = cfg.Smartnode.GetRethAddress().Hex()
}
err := os.WriteFile(feeRecipientPath, []byte(defaultFeeRecipientFileContents), 0o664)
if err != nil {
return fmt.Errorf("could not write default fee recipient file to %s: %w", feeRecipientPath, err)
}
} else if err != nil {
return fmt.Errorf("Error checking fee recipient file status: %w", err)
}
return nil
}
// Remove the old fee recipient files that were created in v1.5.0
func removeLegacyFeeRecipientFiles(c *cli.Command) error {
legacyFeeRecipientFile := "rp-fee-recipient.txt"
cfg, err := services.GetConfig(c)
if err != nil {
return err
}
validatorsFolder := cfg.Smartnode.GetValidatorKeychainPath()
// Remove the legacy files
keystoreDirs := []string{lighthouse.KeystoreDir, nimbus.KeystoreDir, prysm.KeystoreDir, teku.KeystoreDir}
for _, keystoreDir := range keystoreDirs {
oldFile := filepath.Join(validatorsFolder, keystoreDir, legacyFeeRecipientFile)
_, err = os.Stat(oldFile)
if !os.IsNotExist(err) {
err = os.Remove(oldFile)
if err != nil {
fmt.Printf("NOTE: Couldn't remove old fee recipient file (%s): %s\nThis file is no longer used, you may remove it manually if you wish.\n", oldFile, err.Error())
}
}
}
return nil
}
// Update the latest network state at each cycle
func updateNetworkState(m state.NetworkStateProvider, log *log.ColorLogger, nodeAddress common.Address) (*state.NetworkState, error) {
// Get the state of the network
state, err := m.GetHeadStateForNode(nodeAddress)
if err != nil {
return nil, fmt.Errorf("error updating network state: %w", err)
}
return state, nil
}
// Checks if the user-inputted priorityFee is greater than the oracle based maxFee
// If so, return the min(priorityFee, 25% of the oracle based maxFee)
func GetPriorityFee(priorityFee *big.Int, maxFee *big.Int) *big.Int {
// Check if priorityFee is less than maxFee
if priorityFee.Cmp(maxFee) < 0 {
return priorityFee
}
quarterMaxFee := new(big.Int).Div(maxFee, big.NewInt(4))
// Gets the min(priorityFee, 25% of the oracle based maxFee)
if priorityFee.Cmp(quarterMaxFee) < 0 {
return priorityFee
}
return quarterMaxFee
}