-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator-impl-v2.go
1306 lines (1142 loc) · 48.6 KB
/
generator-impl-v2.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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rewards
import (
"context"
"encoding/hex"
"fmt"
"math/big"
"sort"
"time"
"github.com/Seb369888/poolsea-go/dao/trustednode"
"github.com/Seb369888/poolsea-go/minipool"
"github.com/Seb369888/poolsea-go/node"
"github.com/Seb369888/poolsea-go/rewards"
"github.com/Seb369888/poolsea-go/rocketpool"
tnsettings "github.com/Seb369888/poolsea-go/settings/trustednode"
rptypes "github.com/Seb369888/poolsea-go/types"
"github.com/Seb369888/poolsea-go/utils/eth"
"github.com/Seb369888/smartnode/shared/services/beacon"
"github.com/Seb369888/smartnode/shared/services/config"
"github.com/Seb369888/smartnode/shared/services/state"
"github.com/Seb369888/smartnode/shared/utils/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/wealdtech/go-merkletree"
"github.com/wealdtech/go-merkletree/keccak256"
"golang.org/x/sync/errgroup"
)
const (
FarEpoch uint64 = 18446744073709551615
)
// Implementation for tree generator ruleset v2
type treeGeneratorImpl_v2 struct {
rewardsFile *RewardsFile
elSnapshotHeader *types.Header
log log.ColorLogger
logPrefix string
rp *rocketpool.RocketPool
cfg *config.RocketPoolConfig
bc beacon.Client
opts *bind.CallOpts
nodeAddresses []common.Address
nodeDetails []*NodeSmoothingDetails
smoothingPoolBalance *big.Int
smoothingPoolAddress common.Address
intervalDutiesInfo *IntervalDutiesInfo
slotsPerEpoch uint64
validatorIndexMap map[uint64]*MinipoolInfo
elStartTime time.Time
elEndTime time.Time
validNetworkCache map[uint64]bool
epsilon *big.Int
intervalSeconds *big.Int
beaconConfig beacon.Eth2Config
}
// Create a new tree generator
func newTreeGeneratorImpl_v2(log log.ColorLogger, logPrefix string, index uint64, startTime time.Time, endTime time.Time, consensusBlock uint64, elSnapshotHeader *types.Header, intervalsPassed uint64) *treeGeneratorImpl_v2 {
return &treeGeneratorImpl_v2{
rewardsFile: &RewardsFile{
RewardsFileVersion: 1,
RulesetVersion: 2,
Index: index,
StartTime: startTime.UTC(),
EndTime: endTime.UTC(),
ConsensusEndBlock: consensusBlock,
ExecutionEndBlock: elSnapshotHeader.Number.Uint64(),
IntervalsPassed: intervalsPassed,
TotalRewards: &TotalRewards{
ProtocolDaoRpl: NewQuotedBigInt(0),
TotalCollateralRpl: NewQuotedBigInt(0),
TotalOracleDaoRpl: NewQuotedBigInt(0),
TotalSmoothingPoolEth: NewQuotedBigInt(0),
PoolStakerSmoothingPoolEth: NewQuotedBigInt(0),
NodeOperatorSmoothingPoolEth: NewQuotedBigInt(0),
},
NetworkRewards: map[uint64]*NetworkRewardsInfo{},
NodeRewards: map[common.Address]*NodeRewardsInfo{},
InvalidNetworkNodes: map[common.Address]uint64{},
MinipoolPerformanceFile: MinipoolPerformanceFile{
Index: index,
StartTime: startTime.UTC(),
EndTime: endTime.UTC(),
ConsensusEndBlock: consensusBlock,
ExecutionEndBlock: elSnapshotHeader.Number.Uint64(),
MinipoolPerformance: map[common.Address]*SmoothingPoolMinipoolPerformance{},
},
},
elSnapshotHeader: elSnapshotHeader,
log: log,
logPrefix: logPrefix,
}
}
// Get the version of the ruleset used by this generator
func (r *treeGeneratorImpl_v2) getRulesetVersion() uint64 {
return r.rewardsFile.RulesetVersion
}
func (r *treeGeneratorImpl_v2) generateTree(rp *rocketpool.RocketPool, cfg *config.RocketPoolConfig, bc beacon.Client) (*RewardsFile, error) {
r.log.Printlnf("%s Generating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)
// Provision some struct params
r.rp = rp
r.cfg = cfg
r.bc = bc
r.validNetworkCache = map[uint64]bool{
0: true,
}
// Set the network name
r.rewardsFile.Network = fmt.Sprint(cfg.Smartnode.Network.Value)
r.rewardsFile.MinipoolPerformanceFile.Network = r.rewardsFile.Network
// Get the addresses for all nodes
r.opts = &bind.CallOpts{
BlockNumber: r.elSnapshotHeader.Number,
}
nodeAddresses, err := node.GetNodeAddresses(rp, r.opts)
if err != nil {
return nil, fmt.Errorf("Error getting node addresses: %w", err)
}
r.log.Printlnf("%s Creating tree for %d nodes", r.logPrefix, len(nodeAddresses))
r.nodeAddresses = nodeAddresses
// Get the minipool count - this will be used for an error epsilon due to division truncation
minipoolCount, err := minipool.GetMinipoolCount(rp, r.opts)
if err != nil {
return nil, fmt.Errorf("Error getting minipool count: %w", err)
}
r.epsilon = big.NewInt(int64(minipoolCount))
// Calculate the RPL rewards
err = r.calculateRplRewards()
if err != nil {
return nil, fmt.Errorf("Error calculating RPL rewards: %w", err)
}
// Calculate the ETH rewards
err = r.calculateEthRewards(true)
if err != nil {
return nil, fmt.Errorf("Error calculating ETH rewards: %w", err)
}
// Calculate the network reward map and the totals
r.updateNetworksAndTotals()
// Generate the Merkle Tree
err = r.generateMerkleTree()
if err != nil {
return nil, fmt.Errorf("Error generating Merkle tree: %w", err)
}
// Sort all of the missed attestations so the files are always generated in the same state
for _, minipoolInfo := range r.rewardsFile.MinipoolPerformanceFile.MinipoolPerformance {
sort.Slice(minipoolInfo.MissingAttestationSlots, func(i, j int) bool {
return minipoolInfo.MissingAttestationSlots[i] < minipoolInfo.MissingAttestationSlots[j]
})
}
return r.rewardsFile, nil
}
// Quickly calculates an approximate of the staker's share of the smoothing pool balance without processing Beacon performance
// Used for approximate returns in the rETH ratio update
func (r *treeGeneratorImpl_v2) approximateStakerShareOfSmoothingPool(rp *rocketpool.RocketPool, cfg *config.RocketPoolConfig, bc beacon.Client) (*big.Int, error) {
r.log.Printlnf("%s Approximating tree using Ruleset v%d.", r.logPrefix, r.rewardsFile.RulesetVersion)
r.rp = rp
r.cfg = cfg
r.bc = bc
r.validNetworkCache = map[uint64]bool{
0: true,
}
// Set the network name
r.rewardsFile.Network = fmt.Sprint(cfg.Smartnode.Network.Value)
r.rewardsFile.MinipoolPerformanceFile.Network = r.rewardsFile.Network
// Get the addresses for all nodes
r.opts = &bind.CallOpts{
BlockNumber: r.elSnapshotHeader.Number,
}
nodeAddresses, err := node.GetNodeAddresses(rp, r.opts)
if err != nil {
return nil, fmt.Errorf("Error getting node addresses: %w", err)
}
r.log.Printlnf("%s Creating tree for %d nodes", r.logPrefix, len(nodeAddresses))
r.nodeAddresses = nodeAddresses
// Get the minipool count - this will be used for an error epsilon due to division truncation
minipoolCount, err := minipool.GetMinipoolCount(rp, r.opts)
if err != nil {
return nil, fmt.Errorf("Error getting minipool count: %w", err)
}
r.epsilon = big.NewInt(int64(minipoolCount))
// Calculate the ETH rewards
err = r.calculateEthRewards(false)
if err != nil {
return nil, fmt.Errorf("error calculating ETH rewards: %w", err)
}
return &r.rewardsFile.TotalRewards.PoolStakerSmoothingPoolEth.Int, nil
}
// Generates a merkle tree from the provided rewards map
func (r *treeGeneratorImpl_v2) generateMerkleTree() error {
// Generate the leaf data for each node
totalData := make([][]byte, 0, len(r.rewardsFile.NodeRewards))
for address, rewardsForNode := range r.rewardsFile.NodeRewards {
// Ignore nodes that didn't receive any rewards
zero := big.NewInt(0)
if rewardsForNode.CollateralRpl.Cmp(zero) == 0 && rewardsForNode.OracleDaoRpl.Cmp(zero) == 0 && rewardsForNode.SmoothingPoolEth.Cmp(zero) == 0 {
continue
}
// Node data is address[20] :: network[32] :: RPL[32] :: ETH[32]
nodeData := make([]byte, 0, 20+32*3)
// Node address
addressBytes := address.Bytes()
nodeData = append(nodeData, addressBytes...)
// Node network
network := big.NewInt(0).SetUint64(rewardsForNode.RewardNetwork)
networkBytes := make([]byte, 32)
network.FillBytes(networkBytes)
nodeData = append(nodeData, networkBytes...)
// RPL rewards
rplRewards := big.NewInt(0)
rplRewards.Add(&rewardsForNode.CollateralRpl.Int, &rewardsForNode.OracleDaoRpl.Int)
rplRewardsBytes := make([]byte, 32)
rplRewards.FillBytes(rplRewardsBytes)
nodeData = append(nodeData, rplRewardsBytes...)
// ETH rewards
ethRewardsBytes := make([]byte, 32)
rewardsForNode.SmoothingPoolEth.FillBytes(ethRewardsBytes)
nodeData = append(nodeData, ethRewardsBytes...)
// Assign it to the node rewards tracker and add it to the leaf data slice
rewardsForNode.MerkleData = nodeData
totalData = append(totalData, nodeData)
}
// Generate the tree
tree, err := merkletree.NewUsing(totalData, keccak256.New(), false, true)
if err != nil {
return fmt.Errorf("error generating Merkle Tree: %w", err)
}
// Generate the proofs for each node
for address, rewardsForNode := range r.rewardsFile.NodeRewards {
// Get the proof
proof, err := tree.GenerateProof(rewardsForNode.MerkleData, 0)
if err != nil {
return fmt.Errorf("error generating proof for node %s: %w", address.Hex(), err)
}
// Convert the proof into hex strings
proofStrings := make([]string, len(proof.Hashes))
for i, hash := range proof.Hashes {
proofStrings[i] = fmt.Sprintf("0x%s", hex.EncodeToString(hash))
}
// Assign the hex strings to the node rewards struct
rewardsForNode.MerkleProof = proofStrings
}
r.rewardsFile.MerkleTree = tree
r.rewardsFile.MerkleRoot = common.BytesToHash(tree.Root()).Hex()
return nil
}
// Calculates the per-network distribution amounts and the total reward amounts
func (r *treeGeneratorImpl_v2) updateNetworksAndTotals() {
// Get the highest network index with valid rewards
highestNetworkIndex := uint64(0)
for network := range r.rewardsFile.NetworkRewards {
if network > highestNetworkIndex {
highestNetworkIndex = network
}
}
// Create the map for each network, including unused ones
for network := uint64(0); network <= highestNetworkIndex; network++ {
rewardsForNetwork, exists := r.rewardsFile.NetworkRewards[network]
if !exists {
rewardsForNetwork = &NetworkRewardsInfo{
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NetworkRewards[network] = rewardsForNetwork
}
}
}
// Calculates the RPL rewards for the given interval
func (r *treeGeneratorImpl_v2) calculateRplRewards() error {
snapshotBlockTime := time.Unix(int64(r.elSnapshotHeader.Time), 0)
intervalDuration, err := state.GetClaimIntervalTime(r.cfg, r.rewardsFile.Index, r.rp, r.opts)
if err != nil {
return fmt.Errorf("error getting required registration time: %w", err)
}
// Handle node operator rewards
nodeOpPercent, err := state.GetNodeOperatorRewardsPercent(r.cfg, r.rewardsFile.Index, r.rp, r.opts)
if err != nil {
return err
}
pendingRewards, err := state.GetPendingRPLRewards(r.cfg, r.rewardsFile.Index, r.rp, r.opts)
if err != nil {
return err
}
r.log.Printlnf("%s Pending RPL rewards: %s (%.3f)", r.logPrefix, pendingRewards.String(), eth.WeiToEth(pendingRewards))
totalNodeRewards := big.NewInt(0)
totalNodeRewards.Mul(pendingRewards, nodeOpPercent)
totalNodeRewards.Div(totalNodeRewards, eth.EthToWei(1))
r.log.Printlnf("%s Approx. total collateral RPL rewards: %s (%.3f)", r.logPrefix, totalNodeRewards.String(), eth.WeiToEth(totalNodeRewards))
// Calculate the true effective stake of each node based on their participation in this interval
totalNodeEffectiveStake := big.NewInt(0)
trueNodeEffectiveStakes := map[common.Address]*big.Int{}
intervalDurationBig := big.NewInt(int64(intervalDuration.Seconds()))
r.log.Printlnf("%s Calculating true total collateral rewards (progress is reported every 100 nodes)", r.logPrefix)
nodesDone := 0
startTime := time.Now()
nodeCount := len(r.nodeAddresses)
for i, address := range r.nodeAddresses {
if nodesDone == 100 {
timeTaken := time.Since(startTime)
r.log.Printlnf("%s On Node %d of %d (%.2f%%)... (%s so far)", r.logPrefix, i, nodeCount, float64(i)/float64(nodeCount)*100.0, timeTaken)
nodesDone = 0
}
// Get the node's effective stake
nodeStake, err := node.GetNodeEffectiveRPLStake(r.rp, address, r.opts)
if err != nil {
return fmt.Errorf("error getting effective stake for node %s: %w", address.Hex(), err)
}
// Get the timestamp of the node's registration
regTime, err := node.GetNodeRegistrationTime(r.rp, address, r.opts)
if err != nil {
return fmt.Errorf("error getting registration time for node %s: %w", address, err)
}
// Get the actual effective stake, scaled based on participation
eligibleDuration := snapshotBlockTime.Sub(regTime)
if eligibleDuration < intervalDuration {
eligibleSeconds := big.NewInt(int64(eligibleDuration / time.Second))
nodeStake.Mul(nodeStake, eligibleSeconds)
nodeStake.Div(nodeStake, intervalDurationBig)
}
trueNodeEffectiveStakes[address] = nodeStake
// Add it to the total
totalNodeEffectiveStake.Add(totalNodeEffectiveStake, nodeStake)
nodesDone++
}
r.log.Printlnf("%s Calculating individual collateral rewards (progress is reported every 100 nodes)", r.logPrefix)
nodesDone = 0
startTime = time.Now()
for i, address := range r.nodeAddresses {
if nodesDone == 100 {
timeTaken := time.Since(startTime)
r.log.Printlnf("%s On Node %d of %d (%.2f%%)... (%s so far)", r.logPrefix, i, nodeCount, float64(i)/float64(nodeCount)*100.0, timeTaken)
nodesDone = 0
}
// Get how much RPL goes to this node: (true effective stake) * (total node rewards) / (total true effective stake)
nodeRplRewards := big.NewInt(0)
nodeRplRewards.Mul(trueNodeEffectiveStakes[address], totalNodeRewards)
nodeRplRewards.Div(nodeRplRewards, totalNodeEffectiveStake)
// If there are pending rewards, add it to the map
if nodeRplRewards.Cmp(big.NewInt(0)) == 1 {
rewardsForNode, exists := r.rewardsFile.NodeRewards[address]
if !exists {
// Get the network the rewards should go to
network, err := node.GetRewardNetwork(r.rp, address, r.opts)
if err != nil {
return err
}
validNetwork, err := r.validateNetwork(network)
if err != nil {
return err
}
if !validNetwork {
r.rewardsFile.InvalidNetworkNodes[address] = network
network = 0
}
rewardsForNode = &NodeRewardsInfo{
RewardNetwork: network,
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NodeRewards[address] = rewardsForNode
}
rewardsForNode.CollateralRpl.Add(&rewardsForNode.CollateralRpl.Int, nodeRplRewards)
// Add the rewards to the running total for the specified network
rewardsForNetwork, exists := r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork]
if !exists {
rewardsForNetwork = &NetworkRewardsInfo{
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork] = rewardsForNetwork
}
rewardsForNetwork.CollateralRpl.Add(&rewardsForNetwork.CollateralRpl.Int, nodeRplRewards)
}
nodesDone++
}
// Sanity check to make sure we arrived at the correct total
delta := big.NewInt(0)
totalCalculatedNodeRewards := big.NewInt(0)
for _, networkRewards := range r.rewardsFile.NetworkRewards {
totalCalculatedNodeRewards.Add(totalCalculatedNodeRewards, &networkRewards.CollateralRpl.Int)
}
delta.Sub(totalNodeRewards, totalCalculatedNodeRewards).Abs(delta)
if delta.Cmp(r.epsilon) == 1 {
return fmt.Errorf("error calculating collateral RPL: total was %s, but expected %s; error was too large", totalCalculatedNodeRewards.String(), totalNodeRewards.String())
}
r.rewardsFile.TotalRewards.TotalCollateralRpl.Int = *totalCalculatedNodeRewards
r.log.Printlnf("%s Calculated rewards: %s (error = %s wei)", r.logPrefix, totalCalculatedNodeRewards.String(), delta.String())
// Handle Oracle DAO rewards
oDaoPercent, err := state.GetTrustedNodeOperatorRewardsPercent(r.cfg, r.rewardsFile.Index, r.rp, r.opts)
if err != nil {
return err
}
totalODaoRewards := big.NewInt(0)
totalODaoRewards.Mul(pendingRewards, oDaoPercent)
totalODaoRewards.Div(totalODaoRewards, eth.EthToWei(1))
r.log.Printlnf("%s Total Oracle DAO RPL rewards: %s (%.3f)", r.logPrefix, totalODaoRewards.String(), eth.WeiToEth(totalODaoRewards))
oDaoAddresses, err := trustednode.GetMemberAddresses(r.rp, r.opts)
if err != nil {
return err
}
// Calculate the true effective time of each oDAO node based on their participation in this interval
totalODaoNodeTime := big.NewInt(0)
trueODaoNodeTimes := map[common.Address]*big.Int{}
for _, address := range oDaoAddresses {
// Get the timestamp of the node's registration
regTime, err := node.GetNodeRegistrationTime(r.rp, address, r.opts)
if err != nil {
return fmt.Errorf("error getting registration time for node %s: %w", address, err)
}
// Get the actual effective time, scaled based on participation
participationTime := big.NewInt(0).Set(intervalDurationBig)
eligibleDuration := snapshotBlockTime.Sub(regTime)
if eligibleDuration < intervalDuration {
participationTime = big.NewInt(int64(eligibleDuration.Seconds()))
}
trueODaoNodeTimes[address] = participationTime
// Add it to the total
totalODaoNodeTime.Add(totalODaoNodeTime, participationTime)
}
for _, address := range oDaoAddresses {
// Calculate the oDAO rewards for the node: (participation time) * (total oDAO rewards) / (total participation time)
individualOdaoRewards := big.NewInt(0)
individualOdaoRewards.Mul(trueODaoNodeTimes[address], totalODaoRewards)
individualOdaoRewards.Div(individualOdaoRewards, totalODaoNodeTime)
rewardsForNode, exists := r.rewardsFile.NodeRewards[address]
if !exists {
// Get the network the rewards should go to
network, err := node.GetRewardNetwork(r.rp, address, r.opts)
if err != nil {
return err
}
validNetwork, err := r.validateNetwork(network)
if err != nil {
return err
}
if !validNetwork {
r.rewardsFile.InvalidNetworkNodes[address] = network
network = 0
}
rewardsForNode = &NodeRewardsInfo{
RewardNetwork: network,
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NodeRewards[address] = rewardsForNode
}
rewardsForNode.OracleDaoRpl.Add(&rewardsForNode.OracleDaoRpl.Int, individualOdaoRewards)
// Add the rewards to the running total for the specified network
rewardsForNetwork, exists := r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork]
if !exists {
rewardsForNetwork = &NetworkRewardsInfo{
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork] = rewardsForNetwork
}
rewardsForNetwork.OracleDaoRpl.Add(&rewardsForNetwork.OracleDaoRpl.Int, individualOdaoRewards)
}
// Sanity check to make sure we arrived at the correct total
totalCalculatedOdaoRewards := big.NewInt(0)
delta = big.NewInt(0)
for _, networkRewards := range r.rewardsFile.NetworkRewards {
totalCalculatedOdaoRewards.Add(totalCalculatedOdaoRewards, &networkRewards.OracleDaoRpl.Int)
}
delta.Sub(totalODaoRewards, totalCalculatedOdaoRewards).Abs(delta)
if delta.Cmp(r.epsilon) == 1 {
return fmt.Errorf("error calculating ODao RPL: total was %s, but expected %s; error was too large", totalCalculatedOdaoRewards.String(), totalODaoRewards.String())
}
r.rewardsFile.TotalRewards.TotalOracleDaoRpl.Int = *totalCalculatedOdaoRewards
r.log.Printlnf("%s Calculated rewards: %s (error = %s wei)", r.logPrefix, totalCalculatedOdaoRewards.String(), delta.String())
// Get expected Protocol DAO rewards
pDaoPercent, err := state.GetProtocolDaoRewardsPercent(r.cfg, r.rewardsFile.Index, r.rp, r.opts)
if err != nil {
return err
}
pDaoRewards := NewQuotedBigInt(0)
pDaoRewards.Mul(pendingRewards, pDaoPercent)
pDaoRewards.Div(&pDaoRewards.Int, eth.EthToWei(1))
r.log.Printlnf("%s Expected Protocol DAO rewards: %s (%.3f)", r.logPrefix, pDaoRewards.String(), eth.WeiToEth(&pDaoRewards.Int))
// Get actual protocol DAO rewards
pDaoRewards.Sub(pendingRewards, totalCalculatedNodeRewards)
pDaoRewards.Sub(&pDaoRewards.Int, totalCalculatedOdaoRewards)
r.rewardsFile.TotalRewards.ProtocolDaoRpl = pDaoRewards
r.log.Printlnf("%s Actual Protocol DAO rewards: %s to account for truncation", r.logPrefix, pDaoRewards.String())
return nil
}
// Calculates the ETH rewards for the given interval
func (r *treeGeneratorImpl_v2) calculateEthRewards(checkBeaconPerformance bool) error {
// Get the Smoothing Pool contract's balance
smoothingPoolContract, err := r.rp.GetContract("poolseaSmoothingPool", r.opts)
if err != nil {
return fmt.Errorf("error getting smoothing pool contract: %w", err)
}
r.smoothingPoolAddress = *smoothingPoolContract.Address
r.smoothingPoolBalance, err = r.rp.Client.BalanceAt(context.Background(), *smoothingPoolContract.Address, r.elSnapshotHeader.Number)
if err != nil {
return fmt.Errorf("error getting smoothing pool balance: %w", err)
}
r.log.Printlnf("%s Smoothing Pool Balance: %s (%.3f)", r.logPrefix, r.smoothingPoolBalance.String(), eth.WeiToEth(r.smoothingPoolBalance))
// Ignore the ETH calculation if there are no rewards
if r.smoothingPoolBalance.Cmp(big.NewInt(0)) == 0 {
return nil
}
if r.rewardsFile.Index == 0 {
// This is the first interval, Smoothing Pool rewards are ignored on the first interval since it doesn't have a discrete start time
return nil
}
// Get the Beacon config
r.beaconConfig, err = r.bc.GetEth2Config()
if err != nil {
return err
}
r.slotsPerEpoch = r.beaconConfig.SlotsPerEpoch
// Get the start time of this interval based on the event from the previous one
previousIntervalEvent, err := GetRewardSnapshotEvent(r.rp, r.cfg, r.rewardsFile.Index-1)
if err != nil {
return err
}
startElBlockHeader, err := r.getStartBlocksForInterval(previousIntervalEvent)
if err != nil {
return err
}
r.elStartTime = time.Unix(int64(startElBlockHeader.Time), 0)
r.elEndTime = time.Unix(int64(r.elSnapshotHeader.Time), 0)
r.intervalSeconds = big.NewInt(int64(r.elEndTime.Sub(r.elStartTime) / time.Second))
// Get the details for nodes eligible for Smoothing Pool rewards
// This should be all of the eth1 calls, so do them all at the start of Smoothing Pool calculation to prevent the need for an archive node during normal operations
err = r.getSmoothingPoolNodeDetails()
if err != nil {
return err
}
eligible := 0
for _, nodeInfo := range r.nodeDetails {
if nodeInfo.IsEligible {
eligible++
}
}
r.log.Printlnf("%s %d / %d nodes were eligible for Smoothing Pool rewards", r.logPrefix, eligible, len(r.nodeDetails))
// Process the attestation performance for each minipool during this interval
r.intervalDutiesInfo = &IntervalDutiesInfo{
Index: r.rewardsFile.Index,
Slots: map[uint64]*SlotInfo{},
}
if checkBeaconPerformance {
err = r.processAttestationsForInterval()
if err != nil {
return err
}
} else {
// Attestation processing is disabled, just give each minipool 1 good attestation and complete slot activity so they're all scored the same
// Used for approximating rETH's share during balances calculation
for _, nodeInfo := range r.nodeDetails {
if nodeInfo.IsEligible {
for _, minipool := range nodeInfo.Minipools {
minipool.GoodAttestations = 1
minipool.StartSlot = r.rewardsFile.ConsensusStartBlock
minipool.EndSlot = r.rewardsFile.ConsensusEndBlock
}
}
}
}
// Determine how much ETH each node gets and how much the pool stakers get
poolStakerETH, nodeOpEth, err := r.calculateNodeRewards()
if err != nil {
return err
}
// Update the rewards maps
for _, nodeInfo := range r.nodeDetails {
if nodeInfo.IsEligible && nodeInfo.SmoothingPoolEth.Cmp(big.NewInt(0)) > 0 {
rewardsForNode, exists := r.rewardsFile.NodeRewards[nodeInfo.Address]
if !exists {
network := nodeInfo.RewardsNetwork
validNetwork, err := r.validateNetwork(network)
if err != nil {
return err
}
if !validNetwork {
r.rewardsFile.InvalidNetworkNodes[nodeInfo.Address] = network
network = 0
}
rewardsForNode = &NodeRewardsInfo{
RewardNetwork: network,
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NodeRewards[nodeInfo.Address] = rewardsForNode
}
rewardsForNode.SmoothingPoolEth.Add(&rewardsForNode.SmoothingPoolEth.Int, nodeInfo.SmoothingPoolEth)
rewardsForNode.SmoothingPoolEligibilityRate = float64(nodeInfo.EndSlot-nodeInfo.StartSlot) / float64(r.rewardsFile.ConsensusEndBlock-r.rewardsFile.ConsensusStartBlock)
// Add minipool rewards to the JSON
for _, minipoolInfo := range nodeInfo.Minipools {
performance := &SmoothingPoolMinipoolPerformance{
Pubkey: minipoolInfo.ValidatorPubkey.Hex(),
StartSlot: minipoolInfo.StartSlot,
EndSlot: minipoolInfo.EndSlot,
ActiveFraction: float64(minipoolInfo.EndSlot-minipoolInfo.StartSlot) / float64(r.rewardsFile.ConsensusEndBlock-r.rewardsFile.ConsensusStartBlock),
SuccessfulAttestations: minipoolInfo.GoodAttestations,
MissedAttestations: minipoolInfo.MissedAttestations,
EthEarned: eth.WeiToEth(minipoolInfo.MinipoolShare),
MissingAttestationSlots: []uint64{},
}
if minipoolInfo.GoodAttestations+minipoolInfo.MissedAttestations == 0 {
performance.ParticipationRate = 0
} else {
performance.ParticipationRate = float64(minipoolInfo.GoodAttestations) / float64(minipoolInfo.GoodAttestations+minipoolInfo.MissedAttestations)
}
for slot := range minipoolInfo.MissingAttestationSlots {
performance.MissingAttestationSlots = append(performance.MissingAttestationSlots, slot)
}
r.rewardsFile.MinipoolPerformanceFile.MinipoolPerformance[minipoolInfo.Address] = performance
}
// Add the rewards to the running total for the specified network
rewardsForNetwork, exists := r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork]
if !exists {
rewardsForNetwork = &NetworkRewardsInfo{
CollateralRpl: NewQuotedBigInt(0),
OracleDaoRpl: NewQuotedBigInt(0),
SmoothingPoolEth: NewQuotedBigInt(0),
}
r.rewardsFile.NetworkRewards[rewardsForNode.RewardNetwork] = rewardsForNetwork
}
rewardsForNetwork.SmoothingPoolEth.Add(&rewardsForNetwork.SmoothingPoolEth.Int, nodeInfo.SmoothingPoolEth)
}
}
// Set the totals
r.rewardsFile.TotalRewards.PoolStakerSmoothingPoolEth.Int = *poolStakerETH
r.rewardsFile.TotalRewards.NodeOperatorSmoothingPoolEth.Int = *nodeOpEth
r.rewardsFile.TotalRewards.TotalSmoothingPoolEth.Int = *r.smoothingPoolBalance
return nil
}
// Calculate the distribution of Smoothing Pool ETH to each node
func (r *treeGeneratorImpl_v2) calculateNodeRewards() (*big.Int, *big.Int, error) {
// Get the average fee for all eligible minipools and calculate their weighted share
one := big.NewInt(1e18) // 100%, used for dividing percentages properly
feeTotal := big.NewInt(0)
minipoolCount := int64(0)
minipoolShareTotal := big.NewInt(0)
intervalSlots := r.rewardsFile.ConsensusEndBlock - r.rewardsFile.ConsensusStartBlock
intervalSlotsBig := big.NewInt(int64(intervalSlots))
for _, nodeInfo := range r.nodeDetails {
if nodeInfo.IsEligible {
for _, minipool := range nodeInfo.Minipools {
if minipool.GoodAttestations+minipool.MissedAttestations == 0 || !minipool.WasActive {
// Ignore minipools that weren't active for the interval
minipool.StartSlot = 0
minipool.EndSlot = 0
minipool.WasActive = false
minipool.MinipoolShare = big.NewInt(0)
continue
}
// Used for average fee calculation
feeTotal.Add(feeTotal, minipool.Fee)
minipoolCount++
// Minipool share calculation
minipoolShare := big.NewInt(0).Add(one, minipool.Fee) // Start with 1 + fee
if uint64(minipool.EndSlot-minipool.StartSlot) < intervalSlots {
// Prorate the minipool based on its number of active slots
activeSlots := big.NewInt(int64(minipool.EndSlot - minipool.StartSlot))
minipoolShare.Mul(minipoolShare, activeSlots)
minipoolShare.Div(minipoolShare, intervalSlotsBig)
}
if minipool.MissedAttestations > 0 {
// Calculate the participation rate if there are any missed attestations
goodCount := big.NewInt(int64(minipool.GoodAttestations))
missedCount := big.NewInt(int64(minipool.MissedAttestations))
totalCount := big.NewInt(0).Add(goodCount, missedCount)
minipoolShare.Mul(minipoolShare, goodCount)
minipoolShare.Div(minipoolShare, totalCount)
}
minipoolShareTotal.Add(minipoolShareTotal, minipoolShare)
minipool.MinipoolShare = minipoolShare
}
}
}
averageFee := big.NewInt(0).Div(feeTotal, big.NewInt(minipoolCount))
r.log.Printlnf("%s Fee Total: %s (%.3f)", r.logPrefix, feeTotal.String(), eth.WeiToEth(feeTotal))
r.log.Printlnf("%s Minipool Count: %d", r.logPrefix, minipoolCount)
r.log.Printlnf("%s Average Fee: %s (%.3f)", r.logPrefix, averageFee.String(), eth.WeiToEth(averageFee))
// Calculate the staking pool share and the node op share
halfSmoothingPool := big.NewInt(0).Div(r.smoothingPoolBalance, big.NewInt(2))
commission := big.NewInt(0)
commission.Mul(halfSmoothingPool, averageFee)
commission.Div(commission, one)
poolStakerShare := big.NewInt(0).Sub(halfSmoothingPool, commission)
nodeOpShare := big.NewInt(0).Sub(r.smoothingPoolBalance, poolStakerShare)
// Calculate the amount of ETH to give each minipool based on their share
totalEthForMinipools := big.NewInt(0)
for _, nodeInfo := range r.nodeDetails {
nodeInfo.SmoothingPoolEth = big.NewInt(0)
if nodeInfo.IsEligible {
for _, minipool := range nodeInfo.Minipools {
if minipool.EndSlot-minipool.StartSlot == 0 {
continue
}
// Minipool ETH = NO amount * minipool share / total minipool share
minipoolEth := big.NewInt(0).Set(nodeOpShare)
minipoolEth.Mul(minipoolEth, minipool.MinipoolShare)
minipoolEth.Div(minipoolEth, minipoolShareTotal)
nodeInfo.SmoothingPoolEth.Add(nodeInfo.SmoothingPoolEth, minipoolEth)
minipool.MinipoolShare = minipoolEth // Set the minipool share to the normalized fraction for the JSON
}
totalEthForMinipools.Add(totalEthForMinipools, nodeInfo.SmoothingPoolEth)
}
}
// This is how much actually goes to the pool stakers - it should ideally be equal to poolStakerShare but this accounts for any cumulative floating point errors
truePoolStakerAmount := big.NewInt(0).Sub(r.smoothingPoolBalance, totalEthForMinipools)
// Sanity check to make sure we arrived at the correct total
delta := big.NewInt(0).Sub(totalEthForMinipools, nodeOpShare)
delta.Abs(delta)
if delta.Cmp(r.epsilon) == 1 {
return nil, nil, fmt.Errorf("error calculating smoothing pool ETH: total was %s, but expected %s; error was too large (%s wei)", totalEthForMinipools.String(), nodeOpShare.String(), delta.String())
}
r.log.Printlnf("%s Pool staker ETH: %s (%.3f)", r.logPrefix, poolStakerShare.String(), eth.WeiToEth(poolStakerShare))
r.log.Printlnf("%s Node Op ETH: %s (%.3f)", r.logPrefix, nodeOpShare.String(), eth.WeiToEth(nodeOpShare))
r.log.Printlnf("%s Calculated NO ETH: %s (error = %s wei)", r.logPrefix, totalEthForMinipools.String(), delta.String())
r.log.Printlnf("%s Adjusting pool staker ETH to %s to account for truncation", r.logPrefix, truePoolStakerAmount.String())
return truePoolStakerAmount, totalEthForMinipools, nil
}
// Get all of the duties for a range of epochs
func (r *treeGeneratorImpl_v2) processAttestationsForInterval() error {
startEpoch := r.rewardsFile.ConsensusStartBlock / r.beaconConfig.SlotsPerEpoch
endEpoch := r.rewardsFile.ConsensusEndBlock / r.beaconConfig.SlotsPerEpoch
// Determine the validator indices of each minipool
err := r.createMinipoolIndexMap()
if err != nil {
return err
}
// Check all of the attestations for each epoch
r.log.Printlnf("%s Checking participation of %d minipools for epochs %d to %d", r.logPrefix, len(r.validatorIndexMap), startEpoch, endEpoch)
r.log.Printlnf("%s NOTE: this will take a long time, progress is reported every 100 epochs", r.logPrefix)
epochsDone := 0
reportStartTime := time.Now()
for epoch := startEpoch; epoch < endEpoch+1; epoch++ {
if epochsDone == 100 {
timeTaken := time.Since(reportStartTime)
r.log.Printlnf("%s On Epoch %d of %d (%.2f%%)... (%s so far)", r.logPrefix, epoch, endEpoch, float64(epoch-startEpoch)/float64(endEpoch-startEpoch)*100.0, timeTaken)
epochsDone = 0
}
err := r.processEpoch(true, epoch)
if err != nil {
return err
}
epochsDone++
}
// Check the epoch after the end of the interval for any lingering attestations
epoch := endEpoch + 1
err = r.processEpoch(false, epoch)
if err != nil {
return err
}
r.log.Printlnf("%s Finished participation check (total time = %s)", r.logPrefix, time.Since(reportStartTime))
return nil
}
// Process an epoch, optionally getting the duties for all eligible minipools in it and checking each one's attestation performance
func (r *treeGeneratorImpl_v2) processEpoch(getDuties bool, epoch uint64) error {
// Get the committee info and attestation records for this epoch
var committeeData []beacon.Committee
attestationsPerSlot := make([][]beacon.AttestationInfo, r.slotsPerEpoch)
var wg errgroup.Group
if getDuties {
wg.Go(func() error {
var err error
committeeData, err = r.bc.GetCommitteesForEpoch(&epoch)
return err
})
}
for i := uint64(0); i < r.slotsPerEpoch; i++ {
i := i
slot := epoch*r.slotsPerEpoch + i
wg.Go(func() error {
attestations, found, err := r.bc.GetAttestations(fmt.Sprint(slot))
if err != nil {
return err
}
if found {
attestationsPerSlot[i] = attestations
} else {
attestationsPerSlot[i] = []beacon.AttestationInfo{}
}
return nil
})
}
err := wg.Wait()
if err != nil {
return fmt.Errorf("Error getting committee and attestaion records for epoch %d: %w", epoch, err)
}
if getDuties {
// Get all of the expected duties for the epoch
err = r.getDutiesForEpoch(committeeData)
if err != nil {
return fmt.Errorf("Error getting duties for epoch %d: %w", epoch, err)
}
}
// Process all of the slots in the epoch
for i := uint64(0); i < r.slotsPerEpoch; i++ {
attestations := attestationsPerSlot[i]
if len(attestations) > 0 {
r.checkDutiesForSlot(attestations)
}
}
return nil
}
// Handle all of the attestations in the given slot
func (r *treeGeneratorImpl_v2) checkDutiesForSlot(attestations []beacon.AttestationInfo) error {
// Go through the attestations for the block
for _, attestation := range attestations {
// Get the RP committees for this attestation's slot and index
slotInfo, exists := r.intervalDutiesInfo.Slots[attestation.SlotIndex]
if exists {
rpCommittee, exists := slotInfo.Committees[attestation.CommitteeIndex]
if exists {
// Check if each RP validator attested successfully
for position, validator := range rpCommittee.Positions {
if attestation.AggregationBits.BitAt(uint64(position)) {
// We have a winner - remove this duty and update the scores
delete(rpCommittee.Positions, position)
if len(rpCommittee.Positions) == 0 {
delete(slotInfo.Committees, attestation.CommitteeIndex)
}
if len(slotInfo.Committees) == 0 {
delete(r.intervalDutiesInfo.Slots, attestation.SlotIndex)
}
validator.MissedAttestations--
validator.GoodAttestations++
delete(validator.MissingAttestationSlots, attestation.SlotIndex)
}
}
}
}
}
return nil
}
// Maps out the attestaion duties for the given epoch
func (r *treeGeneratorImpl_v2) getDutiesForEpoch(committees []beacon.Committee) error {
// Crawl the committees
for _, committee := range committees {
slotIndex := committee.Slot
if slotIndex < r.rewardsFile.ConsensusStartBlock || slotIndex > r.rewardsFile.ConsensusEndBlock {
// Ignore slots that are out of bounds
continue
}
committeeIndex := committee.Index
// Check if there are any RP validators in this committee
rpValidators := map[int]*MinipoolInfo{}
for position, validator := range committee.Validators {
minipoolInfo, exists := r.validatorIndexMap[validator]
if exists {
rpValidators[position] = minipoolInfo
minipoolInfo.MissedAttestations += 1 // Consider this attestation missed until it's seen later
minipoolInfo.MissingAttestationSlots[slotIndex] = true
}
}
// If there are some RP validators, add this committee to the map
if len(rpValidators) > 0 {
slotInfo, exists := r.intervalDutiesInfo.Slots[slotIndex]
if !exists {
slotInfo = &SlotInfo{
Index: slotIndex,
Committees: map[uint64]*CommitteeInfo{},
}
r.intervalDutiesInfo.Slots[slotIndex] = slotInfo
}
slotInfo.Committees[committeeIndex] = &CommitteeInfo{
Index: committeeIndex,
Positions: rpValidators,
}
}
}
return nil