forked from marioevz/eth-clients
-
Notifications
You must be signed in to change notification settings - Fork 4
/
versioned_beacon_state_response.go
609 lines (571 loc) · 17 KB
/
versioned_beacon_state_response.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package beacon
import (
"fmt"
"github.com/protolambda/eth2api"
"github.com/protolambda/zrnt/eth2/beacon/altair"
"github.com/protolambda/zrnt/eth2/beacon/bellatrix"
"github.com/protolambda/zrnt/eth2/beacon/capella"
"github.com/protolambda/zrnt/eth2/beacon/common"
"github.com/protolambda/zrnt/eth2/beacon/deneb"
"github.com/protolambda/zrnt/eth2/beacon/phase0"
"github.com/protolambda/ztyp/tree"
)
type VersionedBeaconStateResponse struct {
*eth2api.VersionedBeaconState
spec *common.Spec
}
func (vbs *VersionedBeaconStateResponse) Root() tree.Root {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.HashTreeRoot(vbs.spec, tree.GetHashFn())
case *altair.BeaconState:
return state.HashTreeRoot(vbs.spec, tree.GetHashFn())
case *bellatrix.BeaconState:
return state.HashTreeRoot(vbs.spec, tree.GetHashFn())
case *capella.BeaconState:
return state.HashTreeRoot(vbs.spec, tree.GetHashFn())
case *deneb.BeaconState:
return state.HashTreeRoot(vbs.spec, tree.GetHashFn())
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) GenesisTime() common.Timestamp {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.GenesisTime
case *altair.BeaconState:
return state.GenesisTime
case *bellatrix.BeaconState:
return state.GenesisTime
case *capella.BeaconState:
return state.GenesisTime
case *deneb.BeaconState:
return state.GenesisTime
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) GenesisValidatorsRoot() common.Root {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.GenesisValidatorsRoot
case *altair.BeaconState:
return state.GenesisValidatorsRoot
case *bellatrix.BeaconState:
return state.GenesisValidatorsRoot
case *capella.BeaconState:
return state.GenesisValidatorsRoot
case *deneb.BeaconState:
return state.GenesisValidatorsRoot
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Fork() common.Fork {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Fork
case *altair.BeaconState:
return state.Fork
case *bellatrix.BeaconState:
return state.Fork
case *capella.BeaconState:
return state.Fork
case *deneb.BeaconState:
return state.Fork
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) CurrentVersion() common.Version {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Fork.CurrentVersion
case *altair.BeaconState:
return state.Fork.CurrentVersion
case *bellatrix.BeaconState:
return state.Fork.CurrentVersion
case *capella.BeaconState:
return state.Fork.CurrentVersion
case *deneb.BeaconState:
return state.Fork.CurrentVersion
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) PreviousVersion() common.Version {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Fork.PreviousVersion
case *altair.BeaconState:
return state.Fork.PreviousVersion
case *bellatrix.BeaconState:
return state.Fork.PreviousVersion
case *capella.BeaconState:
return state.Fork.PreviousVersion
case *deneb.BeaconState:
return state.Fork.PreviousVersion
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) LatestBlockHeader() common.BeaconBlockHeader {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.LatestBlockHeader
case *altair.BeaconState:
return state.LatestBlockHeader
case *bellatrix.BeaconState:
return state.LatestBlockHeader
case *capella.BeaconState:
return state.LatestBlockHeader
case *deneb.BeaconState:
return state.LatestBlockHeader
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) BlockRoots() phase0.HistoricalBatchRoots {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.BlockRoots
case *altair.BeaconState:
return state.BlockRoots
case *bellatrix.BeaconState:
return state.BlockRoots
case *capella.BeaconState:
return state.BlockRoots
case *deneb.BeaconState:
return state.BlockRoots
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) StateRoots() phase0.HistoricalBatchRoots {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.StateRoots
case *altair.BeaconState:
return state.StateRoots
case *bellatrix.BeaconState:
return state.StateRoots
case *capella.BeaconState:
return state.StateRoots
case *deneb.BeaconState:
return state.StateRoots
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) HistoricalRoots() phase0.HistoricalRoots {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.HistoricalRoots
case *altair.BeaconState:
return state.HistoricalRoots
case *bellatrix.BeaconState:
return state.HistoricalRoots
case *capella.BeaconState:
return state.HistoricalRoots
case *deneb.BeaconState:
return state.HistoricalRoots
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Eth1Data() common.Eth1Data {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Eth1Data
case *altair.BeaconState:
return state.Eth1Data
case *bellatrix.BeaconState:
return state.Eth1Data
case *capella.BeaconState:
return state.Eth1Data
case *deneb.BeaconState:
return state.Eth1Data
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Eth1DataVotes() phase0.Eth1DataVotes {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Eth1DataVotes
case *altair.BeaconState:
return state.Eth1DataVotes
case *bellatrix.BeaconState:
return state.Eth1DataVotes
case *capella.BeaconState:
return state.Eth1DataVotes
case *deneb.BeaconState:
return state.Eth1DataVotes
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Eth1DepositIndex() common.DepositIndex {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Eth1DepositIndex
case *altair.BeaconState:
return state.Eth1DepositIndex
case *bellatrix.BeaconState:
return state.Eth1DepositIndex
case *capella.BeaconState:
return state.Eth1DepositIndex
case *deneb.BeaconState:
return state.Eth1DepositIndex
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Balances() phase0.Balances {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Balances
case *altair.BeaconState:
return state.Balances
case *bellatrix.BeaconState:
return state.Balances
case *capella.BeaconState:
return state.Balances
case *deneb.BeaconState:
return state.Balances
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Balance(
id common.ValidatorIndex,
) common.Gwei {
balances := vbs.Balances()
if int(id) >= len(balances) {
panic("invalid validator requested")
}
return balances[id]
}
func (vbs *VersionedBeaconStateResponse) Validators() phase0.ValidatorRegistry {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Validators
case *altair.BeaconState:
return state.Validators
case *bellatrix.BeaconState:
return state.Validators
case *capella.BeaconState:
return state.Validators
case *deneb.BeaconState:
return state.Validators
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) RandaoMixes() phase0.RandaoMixes {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.RandaoMixes
case *altair.BeaconState:
return state.RandaoMixes
case *bellatrix.BeaconState:
return state.RandaoMixes
case *capella.BeaconState:
return state.RandaoMixes
case *deneb.BeaconState:
return state.RandaoMixes
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) Slashings() phase0.SlashingsHistory {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Slashings
case *altair.BeaconState:
return state.Slashings
case *bellatrix.BeaconState:
return state.Slashings
case *capella.BeaconState:
return state.Slashings
case *deneb.BeaconState:
return state.Slashings
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) PreviousEpochAttestations() phase0.PendingAttestations {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.PreviousEpochAttestations
case *altair.BeaconState:
return nil
case *bellatrix.BeaconState:
return nil
case *capella.BeaconState:
return nil
case *deneb.BeaconState:
return nil
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) CurrentEpochAttestations() phase0.PendingAttestations {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.CurrentEpochAttestations
case *altair.BeaconState:
return nil
case *bellatrix.BeaconState:
return nil
case *capella.BeaconState:
return nil
case *deneb.BeaconState:
return nil
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) PreviousEpochParticipation() altair.ParticipationRegistry {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return nil
case *altair.BeaconState:
return state.PreviousEpochParticipation
case *bellatrix.BeaconState:
return state.PreviousEpochParticipation
case *capella.BeaconState:
return state.PreviousEpochParticipation
case *deneb.BeaconState:
return state.PreviousEpochParticipation
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) CurrentEpochParticipation() altair.ParticipationRegistry {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return nil
case *altair.BeaconState:
return state.CurrentEpochParticipation
case *bellatrix.BeaconState:
return state.CurrentEpochParticipation
case *capella.BeaconState:
return state.CurrentEpochParticipation
case *deneb.BeaconState:
return state.CurrentEpochParticipation
}
panic("badly formatted beacon state")
}
// Finality
func (vbs *VersionedBeaconStateResponse) JustificationBits() common.JustificationBits {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.JustificationBits
case *altair.BeaconState:
return state.JustificationBits
case *bellatrix.BeaconState:
return state.JustificationBits
case *capella.BeaconState:
return state.JustificationBits
case *deneb.BeaconState:
return state.JustificationBits
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) PreviousJustifiedCheckpoint() common.Checkpoint {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.PreviousJustifiedCheckpoint
case *altair.BeaconState:
return state.PreviousJustifiedCheckpoint
case *bellatrix.BeaconState:
return state.PreviousJustifiedCheckpoint
case *capella.BeaconState:
return state.PreviousJustifiedCheckpoint
case *deneb.BeaconState:
return state.PreviousJustifiedCheckpoint
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) CurrentJustifiedCheckpoint() common.Checkpoint {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.CurrentJustifiedCheckpoint
case *altair.BeaconState:
return state.CurrentJustifiedCheckpoint
case *bellatrix.BeaconState:
return state.CurrentJustifiedCheckpoint
case *capella.BeaconState:
return state.CurrentJustifiedCheckpoint
case *deneb.BeaconState:
return state.CurrentJustifiedCheckpoint
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) FinalizedCheckpoint() common.Checkpoint {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.FinalizedCheckpoint
case *altair.BeaconState:
return state.FinalizedCheckpoint
case *bellatrix.BeaconState:
return state.FinalizedCheckpoint
case *capella.BeaconState:
return state.FinalizedCheckpoint
case *deneb.BeaconState:
return state.FinalizedCheckpoint
}
panic("badly formatted beacon state")
}
// Altair
// Inactivity
func (vbs *VersionedBeaconStateResponse) InactivityScores() altair.InactivityScores {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return nil
case *altair.BeaconState:
return state.InactivityScores
case *bellatrix.BeaconState:
return state.InactivityScores
case *capella.BeaconState:
return state.InactivityScores
case *deneb.BeaconState:
return state.InactivityScores
}
panic("badly formatted beacon state")
}
// Sync
func (vbs *VersionedBeaconStateResponse) CurrentSyncCommittee() *common.SyncCommittee {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return nil
case *altair.BeaconState:
return &state.CurrentSyncCommittee
case *bellatrix.BeaconState:
return &state.CurrentSyncCommittee
case *capella.BeaconState:
return &state.CurrentSyncCommittee
case *deneb.BeaconState:
return &state.CurrentSyncCommittee
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) NextSyncCommittee() *common.SyncCommittee {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return nil
case *altair.BeaconState:
return &state.NextSyncCommittee
case *bellatrix.BeaconState:
return &state.NextSyncCommittee
case *capella.BeaconState:
return &state.NextSyncCommittee
case *deneb.BeaconState:
return &state.NextSyncCommittee
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) StateSlot() common.Slot {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return state.Slot
case *altair.BeaconState:
return state.Slot
case *bellatrix.BeaconState:
return state.Slot
case *capella.BeaconState:
return state.Slot
case *deneb.BeaconState:
return state.Slot
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) LatestExecutionPayloadHeaderHash() tree.Root {
switch state := vbs.Data.(type) {
case *phase0.BeaconState:
return tree.Root{}
case *altair.BeaconState:
return tree.Root{}
case *bellatrix.BeaconState:
return state.LatestExecutionPayloadHeader.BlockHash
case *capella.BeaconState:
return state.LatestExecutionPayloadHeader.BlockHash
case *deneb.BeaconState:
return state.LatestExecutionPayloadHeader.BlockHash
}
panic("badly formatted beacon state")
}
func (vbs *VersionedBeaconStateResponse) NextWithdrawalIndex() (common.WithdrawalIndex, error) {
var wIndex common.WithdrawalIndex
switch state := vbs.Data.(type) {
case *capella.BeaconState:
wIndex = state.NextWithdrawalIndex
case *deneb.BeaconState:
wIndex = state.NextWithdrawalIndex
}
return wIndex, nil
}
func (vbs *VersionedBeaconStateResponse) NextWithdrawalValidatorIndex() (common.ValidatorIndex, error) {
var wIndex common.ValidatorIndex
switch state := vbs.Data.(type) {
case *capella.BeaconState:
wIndex = state.NextWithdrawalValidatorIndex
case *deneb.BeaconState:
wIndex = state.NextWithdrawalValidatorIndex
}
return wIndex, nil
}
func (vbs *VersionedBeaconStateResponse) NextWithdrawals(
slot common.Slot,
) (common.Withdrawals, error) {
var (
withdrawalIndex common.WithdrawalIndex
validatorIndex common.ValidatorIndex
validators phase0.ValidatorRegistry
balances phase0.Balances
epoch = vbs.spec.SlotToEpoch(slot)
)
switch state := vbs.Data.(type) {
case *bellatrix.BeaconState:
// withdrawalIndex and validatorIndex start at zero
validators = state.Validators
balances = state.Balances
case *capella.BeaconState:
withdrawalIndex = state.NextWithdrawalIndex
validatorIndex = state.NextWithdrawalValidatorIndex
validators = state.Validators
balances = state.Balances
case *deneb.BeaconState:
withdrawalIndex = state.NextWithdrawalIndex
validatorIndex = state.NextWithdrawalValidatorIndex
validators = state.Validators
balances = state.Balances
default:
return nil, fmt.Errorf("badly formatted beacon state")
}
validatorCount := uint64(len(validators))
withdrawals := make(common.Withdrawals, 0)
i := uint64(0)
for {
if validatorIndex >= common.ValidatorIndex(validatorCount) ||
validatorIndex >= common.ValidatorIndex(len(balances)) {
return nil, fmt.Errorf("invalid validator index")
}
validator := validators[validatorIndex]
if validator == nil {
return nil, fmt.Errorf("invalid validator")
}
balance := balances[validatorIndex]
if i >= validatorCount ||
i >= uint64(vbs.spec.MAX_VALIDATORS_PER_WITHDRAWALS_SWEEP) {
break
}
if IsFullyWithdrawableValidator(validator, balance, epoch) {
withdrawals = append(withdrawals, common.Withdrawal{
Index: withdrawalIndex,
ValidatorIndex: validatorIndex,
Address: Eth1WithdrawalCredential(validator),
Amount: balance,
})
withdrawalIndex += 1
} else if IsPartiallyWithdrawableValidator(vbs.spec, validator, balance, epoch) {
withdrawals = append(withdrawals, common.Withdrawal{
Index: withdrawalIndex,
ValidatorIndex: validatorIndex,
Address: Eth1WithdrawalCredential(validator),
Amount: balance - vbs.spec.MAX_EFFECTIVE_BALANCE,
})
withdrawalIndex += 1
}
if len(withdrawals) == int(vbs.spec.MAX_WITHDRAWALS_PER_PAYLOAD) {
break
}
validatorIndex = common.ValidatorIndex(
uint64(validatorIndex+1) % validatorCount,
)
i += 1
}
return withdrawals, nil
}