-
Notifications
You must be signed in to change notification settings - Fork 3
/
ethrewards.go
161 lines (134 loc) · 4.34 KB
/
ethrewards.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
package ethrewards
import (
"fmt"
"sync"
"github.com/gobitfly/eth-rewards/beacon"
"github.com/gobitfly/eth-rewards/elrewards"
"github.com/gobitfly/eth-rewards/types"
"golang.org/x/sync/errgroup"
"github.com/sirupsen/logrus"
)
func GetRewardsForEpoch(epoch uint64, client *beacon.Client, elEndpoint string) (map[uint64]*types.ValidatorEpochIncome, error) {
proposerAssignments, err := client.ProposerAssignments(epoch)
if err != nil {
return nil, err
}
slotsPerEpoch := uint64(len(proposerAssignments.Data))
startSlot := epoch * slotsPerEpoch
endSlot := startSlot + slotsPerEpoch - 1
g := new(errgroup.Group)
g.SetLimit(32)
slotsToProposerIndex := make(map[uint64]uint64)
for _, pa := range proposerAssignments.Data {
slotsToProposerIndex[uint64(pa.Slot)] = uint64(pa.ValidatorIndex)
}
rewardsMux := &sync.Mutex{}
rewards := make(map[uint64]*types.ValidatorEpochIncome)
for i := startSlot; i <= endSlot; i++ {
i := i
g.Go(func() error {
proposer, found := slotsToProposerIndex[i]
if !found {
return fmt.Errorf("assigned proposer for slot %v not found", i)
}
execBlockNumber, err := client.ExecutionBlockNumber(i)
rewardsMux.Lock()
if rewards[proposer] == nil {
rewards[proposer] = &types.ValidatorEpochIncome{}
}
rewardsMux.Unlock()
if err != nil {
if err == types.ErrBlockNotFound {
rewardsMux.Lock()
rewards[proposer].ProposalsMissed += 1
rewardsMux.Unlock()
return nil
} else if err != types.ErrSlotPreMerge { // ignore
logrus.Errorf("error retrieving execution block number for slot %v: %v", i, err)
return err
}
} else {
txFeeIncome, err := elrewards.GetELRewardForBlock(execBlockNumber, elEndpoint)
if err != nil {
return err
}
rewardsMux.Lock()
rewards[proposer].TxFeeRewardWei = txFeeIncome.Bytes()
rewardsMux.Unlock()
}
syncRewards, err := client.SyncCommitteeRewards(i)
if err != nil {
if err != types.ErrSlotPreSyncCommittees {
return err
}
}
rewardsMux.Lock()
if syncRewards != nil {
for _, sr := range syncRewards.Data {
if rewards[sr.ValidatorIndex] == nil {
rewards[sr.ValidatorIndex] = &types.ValidatorEpochIncome{}
}
if sr.Reward > 0 {
rewards[sr.ValidatorIndex].SyncCommitteeReward += uint64(sr.Reward)
} else {
rewards[sr.ValidatorIndex].SyncCommitteePenalty += uint64(sr.Reward * -1)
}
}
}
rewardsMux.Unlock()
rewardsMux.Lock()
blockRewards, err := client.BlockRewards(i)
if err != nil {
rewardsMux.Unlock()
return err
}
if rewards[blockRewards.Data.ProposerIndex] == nil {
rewards[blockRewards.Data.ProposerIndex] = &types.ValidatorEpochIncome{}
}
rewards[blockRewards.Data.ProposerIndex].ProposerAttestationInclusionReward += blockRewards.Data.Attestations
rewards[blockRewards.Data.ProposerIndex].ProposerSlashingInclusionReward += blockRewards.Data.AttesterSlashings + blockRewards.Data.ProposerSlashings
rewards[blockRewards.Data.ProposerIndex].ProposerSyncInclusionReward += blockRewards.Data.SyncAggregate
rewardsMux.Unlock()
return nil
})
}
g.Go(func() error {
ar, err := client.AttestationRewards(epoch)
if err != nil {
return err
}
rewardsMux.Lock()
defer rewardsMux.Unlock()
for _, ar := range ar.Data.TotalRewards {
if rewards[ar.ValidatorIndex] == nil {
rewards[ar.ValidatorIndex] = &types.ValidatorEpochIncome{}
}
if ar.Head >= 0 {
rewards[ar.ValidatorIndex].AttestationHeadReward = uint64(ar.Head)
} else {
return fmt.Errorf("retrieved negative attestation head reward for validator %v: %v", ar.ValidatorIndex, ar.Head)
}
if ar.Source > 0 {
rewards[ar.ValidatorIndex].AttestationSourceReward = uint64(ar.Source)
} else {
rewards[ar.ValidatorIndex].AttestationSourcePenalty = uint64(ar.Source * -1)
}
if ar.Target > 0 {
rewards[ar.ValidatorIndex].AttestationTargetReward = uint64(ar.Target)
} else {
rewards[ar.ValidatorIndex].AttestationTargetPenalty = uint64(ar.Target * -1)
}
if ar.InclusionDelay <= 0 {
rewards[ar.ValidatorIndex].FinalityDelayPenalty = uint64(ar.InclusionDelay * -1)
} else {
return fmt.Errorf("retrieved positive inclusion delay penalty for validator %v: %v", ar.ValidatorIndex, ar.InclusionDelay)
}
}
return nil
})
err = g.Wait()
if err != nil {
return nil, err
}
return rewards, nil
}