-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathbc-manager.go
More file actions
532 lines (468 loc) · 16.7 KB
/
bc-manager.go
File metadata and controls
532 lines (468 loc) · 16.7 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
package services
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/fatih/color"
"github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/smartnode/shared/services/beacon"
"github.com/rocket-pool/smartnode/shared/services/beacon/client"
"github.com/rocket-pool/smartnode/shared/services/config"
"github.com/rocket-pool/smartnode/shared/types/api"
cfgtypes "github.com/rocket-pool/smartnode/shared/types/config"
"github.com/rocket-pool/smartnode/shared/utils/log"
)
// This is a proxy for multiple Beacon clients, providing natural fallback support if one of them fails.
type BeaconClientManager struct {
primaryBc beacon.Client
fallbackBc beacon.Client
logger log.ColorLogger
primaryReady bool
fallbackReady bool
ignoreSyncCheck bool
preferFallback bool
}
// This is a signature for a wrapped Beacon client function that only returns an error
type bcFunction0 func(beacon.Client) error
// This is a signature for a wrapped Beacon client function that returns 1 var and an error
type bcFunction1 func(beacon.Client) (interface{}, error)
// This is a signature for a wrapped Beacon client function that returns 2 vars and an error
type bcFunction2 func(beacon.Client) (interface{}, interface{}, error)
// Creates a new BeaconClientManager instance based on the Rocket Pool config
func NewBeaconClientManager(cfg *config.RocketPoolConfig) (*BeaconClientManager, error) {
var preferFallback bool
// Primary CC
var primaryProvider string
var selectedCC cfgtypes.ConsensusClient
if cfg.IsNativeMode {
primaryProvider = cfg.Native.CcHttpUrl.Value.(string)
selectedCC = cfg.Native.ConsensusClient.Value.(cfgtypes.ConsensusClient)
} else if cfg.ConsensusClientMode.Value.(cfgtypes.Mode) == cfgtypes.Mode_Local {
primaryProvider = fmt.Sprintf("http://%s:%d", BnContainerName, cfg.ConsensusCommon.ApiPort.Value.(uint16))
selectedCC = cfg.ConsensusClient.Value.(cfgtypes.ConsensusClient)
} else if cfg.ConsensusClientMode.Value.(cfgtypes.Mode) == cfgtypes.Mode_External {
selectedConsensusConfig, err := cfg.GetSelectedConsensusClientConfig()
if err != nil {
return nil, err
}
primaryProvider = selectedConsensusConfig.(cfgtypes.ExternalConsensusConfig).GetApiUrl()
selectedCC = cfg.ExternalConsensusClient.Value.(cfgtypes.ConsensusClient)
} else {
return nil, fmt.Errorf("Unknown Consensus client mode '%v'", cfg.ConsensusClientMode.Value)
}
// Fallback CC
var fallbackProvider string
if cfg.UseFallbackClients.Value == true {
if cfg.IsNativeMode {
fallbackProvider = cfg.FallbackNormal.CcHttpUrl.Value.(string)
preferFallback = cfg.PrioritizeValidation.Value.(bool)
} else {
switch selectedCC {
case cfgtypes.ConsensusClient_Prysm:
fallbackProvider = cfg.FallbackPrysm.CcHttpUrl.Value.(string)
default:
fallbackProvider = cfg.FallbackNormal.CcHttpUrl.Value.(string)
}
preferFallback = cfg.PrioritizeValidation.Value.(bool)
}
}
var primaryBc beacon.Client
var fallbackBc beacon.Client
primaryBc = client.NewStandardHttpClient(primaryProvider)
if fallbackProvider != "" {
fallbackBc = client.NewStandardHttpClient(fallbackProvider)
}
return &BeaconClientManager{
primaryBc: primaryBc,
fallbackBc: fallbackBc,
logger: log.NewColorLogger(color.FgHiBlue),
primaryReady: true,
fallbackReady: fallbackBc != nil,
preferFallback: preferFallback,
}, nil
}
/// ======================
/// BeaconClient Functions
/// ======================
// Get the client's process mode
func (m *BeaconClientManager) GetClientType() (beacon.BeaconClientType, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetClientType()
})
if err != nil {
return beacon.Unknown, err
}
return result.(beacon.BeaconClientType), nil
}
// Get the client's sync status
func (m *BeaconClientManager) GetSyncStatus() (beacon.SyncStatus, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetSyncStatus()
})
if err != nil {
return beacon.SyncStatus{}, err
}
return result.(beacon.SyncStatus), nil
}
// Get the Beacon configuration
func (m *BeaconClientManager) GetEth2Config() (beacon.Eth2Config, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetEth2Config()
})
if err != nil {
return beacon.Eth2Config{}, err
}
return result.(beacon.Eth2Config), nil
}
// Get the Beacon configuration
func (m *BeaconClientManager) GetEth2DepositContract() (beacon.Eth2DepositContract, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetEth2DepositContract()
})
if err != nil {
return beacon.Eth2DepositContract{}, err
}
return result.(beacon.Eth2DepositContract), nil
}
// Get the attestations in a Beacon chain block
func (m *BeaconClientManager) GetAttestations(blockId string) ([]beacon.AttestationInfo, bool, error) {
result1, result2, err := m.runFunction2(func(client beacon.Client) (interface{}, interface{}, error) {
return client.GetAttestations(blockId)
})
if err != nil {
return nil, false, err
}
return result1.([]beacon.AttestationInfo), result2.(bool), nil
}
// Get a Beacon chain block
func (m *BeaconClientManager) GetBeaconBlock(blockId string) (beacon.BeaconBlock, bool, error) {
result1, result2, err := m.runFunction2(func(client beacon.Client) (interface{}, interface{}, error) {
return client.GetBeaconBlock(blockId)
})
if err != nil {
return beacon.BeaconBlock{}, false, err
}
return result1.(beacon.BeaconBlock), result2.(bool), nil
}
// Get the Beacon chain's head information
func (m *BeaconClientManager) GetBeaconHead() (beacon.BeaconHead, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetBeaconHead()
})
if err != nil {
return beacon.BeaconHead{}, err
}
return result.(beacon.BeaconHead), nil
}
// Get a validator's status by its index
func (m *BeaconClientManager) GetValidatorStatusByIndex(index string, opts *beacon.ValidatorStatusOptions) (beacon.ValidatorStatus, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorStatusByIndex(index, opts)
})
if err != nil {
return beacon.ValidatorStatus{}, err
}
return result.(beacon.ValidatorStatus), nil
}
// Get a validator's status by its pubkey
func (m *BeaconClientManager) GetValidatorStatus(pubkey types.ValidatorPubkey, opts *beacon.ValidatorStatusOptions) (beacon.ValidatorStatus, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorStatus(pubkey, opts)
})
if err != nil {
return beacon.ValidatorStatus{}, err
}
return result.(beacon.ValidatorStatus), nil
}
// Get the statuses of multiple validators by their pubkeys
func (m *BeaconClientManager) GetValidatorStatuses(pubkeys []types.ValidatorPubkey, opts *beacon.ValidatorStatusOptions) (map[types.ValidatorPubkey]beacon.ValidatorStatus, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorStatuses(pubkeys, opts)
})
if err != nil {
return nil, err
}
return result.(map[types.ValidatorPubkey]beacon.ValidatorStatus), nil
}
// Get a validator's index
func (m *BeaconClientManager) GetValidatorIndex(pubkey types.ValidatorPubkey) (string, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorIndex(pubkey)
})
if err != nil {
return "", err
}
return result.(string), nil
}
// Get a validator's sync duties
func (m *BeaconClientManager) GetValidatorSyncDuties(indices []string, epoch uint64) (map[string]bool, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorSyncDuties(indices, epoch)
})
if err != nil {
return nil, err
}
return result.(map[string]bool), nil
}
// Get a validator's proposer duties
func (m *BeaconClientManager) GetValidatorProposerDuties(indices []string, epoch uint64) (map[string]uint64, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetValidatorProposerDuties(indices, epoch)
})
if err != nil {
return nil, err
}
return result.(map[string]uint64), nil
}
// Get the Beacon chain's domain data
func (m *BeaconClientManager) GetDomainData(domainType []byte, epoch uint64, useGenesisFork bool) ([]byte, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetDomainData(domainType, epoch, useGenesisFork)
})
if err != nil {
return nil, err
}
return result.([]byte), nil
}
// Voluntarily exit a validator
func (m *BeaconClientManager) ExitValidator(validatorIndex string, epoch uint64, signature types.ValidatorSignature) error {
err := m.runFunction0(func(client beacon.Client) error {
return client.ExitValidator(validatorIndex, epoch, signature)
})
return err
}
// Close the connection to the Beacon client
func (m *BeaconClientManager) Close() error {
err := m.runFunction0(func(client beacon.Client) error {
return client.Close()
})
return err
}
// Get the EL data for a CL block
func (m *BeaconClientManager) GetEth1DataForEth2Block(blockId string) (beacon.Eth1Data, bool, error) {
result1, result2, err := m.runFunction2(func(client beacon.Client) (interface{}, interface{}, error) {
return client.GetEth1DataForEth2Block(blockId)
})
if err != nil {
return beacon.Eth1Data{}, false, err
}
return result1.(beacon.Eth1Data), result2.(bool), nil
}
// Get the attestation committees for an epoch
func (m *BeaconClientManager) GetCommitteesForEpoch(epoch *uint64) (beacon.Committees, error) {
result, err := m.runFunction1(func(client beacon.Client) (interface{}, error) {
return client.GetCommitteesForEpoch(epoch)
})
if err != nil {
return nil, err
}
return result.(beacon.Committees), nil
}
// Change the withdrawal credentials for a validator
func (m *BeaconClientManager) ChangeWithdrawalCredentials(validatorIndex string, fromBlsPubkey types.ValidatorPubkey, toExecutionAddress common.Address, signature types.ValidatorSignature) error {
err := m.runFunction0(func(client beacon.Client) error {
return client.ChangeWithdrawalCredentials(validatorIndex, fromBlsPubkey, toExecutionAddress, signature)
})
if err != nil {
return err
}
return nil
}
/// ==================
/// Internal Functions
/// ==================
func (m *BeaconClientManager) CheckStatus() *api.ClientManagerStatus {
status := &api.ClientManagerStatus{
FallbackEnabled: m.fallbackBc != nil,
}
// Ignore the sync check and just use the predefined settings if requested
if m.ignoreSyncCheck {
status.PrimaryClientStatus.IsWorking = m.primaryReady
status.PrimaryClientStatus.IsSynced = m.primaryReady
if status.FallbackEnabled {
status.FallbackClientStatus.IsWorking = m.fallbackReady
status.FallbackClientStatus.IsSynced = m.fallbackReady
}
return status
}
// Get the primary BC status
status.PrimaryClientStatus = checkBcStatus(m.primaryBc)
// Get the fallback BC status if applicable
if status.FallbackEnabled {
status.FallbackClientStatus = checkBcStatus(m.fallbackBc)
}
// Flag the ready clients
m.primaryReady = (status.PrimaryClientStatus.IsWorking && status.PrimaryClientStatus.IsSynced)
m.fallbackReady = (status.FallbackEnabled && status.FallbackClientStatus.IsWorking && status.FallbackClientStatus.IsSynced)
return status
}
// Check the client status
func checkBcStatus(client beacon.Client) api.ClientStatus {
status := api.ClientStatus{}
// Get the fallback's sync progress
syncStatus, err := client.GetSyncStatus()
if err != nil {
status.Error = fmt.Sprintf("Sync progress check failed with [%s]", err.Error())
status.IsSynced = false
status.IsWorking = false
return status
}
// Return the sync status
if !syncStatus.Syncing {
status.IsWorking = true
status.IsSynced = true
status.SyncProgress = 1
} else {
status.IsWorking = true
status.IsSynced = false
status.SyncProgress = syncStatus.Progress
}
return status
}
// Attempts to run a function progressively through each client until one succeeds or they all fail.
func (m *BeaconClientManager) runFunction0(function bcFunction0) error {
// Check if we can use the primary
if m.primaryReady {
// Try to run the function on the primary
err := function(m.primaryBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Primary Beacon client disconnected (%s), using fallback...", err.Error())
m.primaryReady = false
return m.runFunction0(function)
}
// If it's a different error, just return it
return err
}
// If there's no error, return the result
return nil
}
if m.fallbackReady {
// Try to run the function on the fallback
err := function(m.fallbackBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Fallback Beacon client disconnected (%s)", err.Error())
m.fallbackReady = false
return fmt.Errorf("all Beacon clients failed")
}
// If it's a different error, just return it
return err
}
// If there's no error, return the result
return nil
}
return fmt.Errorf("no Beacon clients were ready")
}
// Attempts to run a function progressively through each client in the preferred order until one succeeds or they all fail.
func (m *BeaconClientManager) runFunction1(function bcFunction1) (interface{}, error) {
const (
primary = iota
fallback = iota
)
var order []int
if !m.preferFallback {
order = []int{primary, fallback}
} else {
order = []int{fallback, primary}
}
for _, client := range order {
switch client {
case primary:
// Check if we can use the primary
if m.primaryReady {
// Try to run the function on the primary
result, err := function(m.primaryBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Primary Beacon client disconnected (%s), using fallback...", err.Error())
m.primaryReady = false
return m.runFunction1(function)
}
// If it's a different error, just return it
return nil, err
}
// If there's no error, return the result
return result, nil
}
case fallback:
if m.fallbackReady {
// Try to run the function on the fallback
result, err := function(m.fallbackBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Fallback Beacon client disconnected (%s)", err.Error())
m.fallbackReady = false
return m.runFunction1(function)
}
// If it's a different error, just return it
return nil, err
}
// If there's no error, return the result
return result, nil
}
}
}
return nil, fmt.Errorf("no Beacon clients were ready")
}
// Attempts to run a function progressively through each client until one succeeds or they all fail.
func (m *BeaconClientManager) runFunction2(function bcFunction2) (interface{}, interface{}, error) {
const (
primary = iota
fallback = iota
)
var order []int
if !m.preferFallback {
order = []int{primary, fallback}
} else {
order = []int{fallback, primary}
}
for _, client := range order {
switch client {
case primary:
// Check if we can use the primary
if m.primaryReady {
// Try to run the function on the primary
result1, result2, err := function(m.primaryBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Primary Beacon client disconnected (%s), using fallback...", err.Error())
m.primaryReady = false
return m.runFunction2(function)
}
// If it's a different error, just return it
return nil, nil, err
}
// If there's no error, return the result
return result1, result2, nil
}
case fallback:
if m.fallbackReady {
// Try to run the function on the fallback
result1, result2, err := function(m.fallbackBc)
if err != nil {
if m.isDisconnected(err) {
// If it's disconnected, log it and try the fallback
m.logger.Printlnf("WARNING: Fallback Beacon client disconnected (%s)", err.Error())
m.fallbackReady = false
return m.runFunction2(function)
}
// If it's a different error, just return it
return nil, nil, err
}
// If there's no error, return the result
return result1, result2, nil
}
}
}
return nil, nil, fmt.Errorf("no Beacon clients were ready")
}
// Returns true if the error was a connection failure and a backup client is available
func (m *BeaconClientManager) isDisconnected(err error) bool {
return strings.Contains(err.Error(), "dial tcp")
}