Skip to content

Commit 0e4f0de

Browse files
committed
Add processed deposits handling
1 parent fb32efb commit 0e4f0de

File tree

3 files changed

+163
-6
lines changed

3 files changed

+163
-6
lines changed

metrics/beaconstate.go

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ func (p *BeaconState) Run(
5757
relayRewards *big.Int,
5858
validatorIndexToWithdrawalAmount map[uint64]*big.Int,
5959
proposerTips map[uint64]*big.Int,
60-
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation) error {
60+
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation,
61+
validatorIndexToProcessedDeposit map[uint64]big.Int) error {
6162

6263
if currentBeaconState == nil || prevBeaconState == nil {
6364
return errors.New("current or previous beacon state is nil")
@@ -92,7 +93,8 @@ func (p *BeaconState) Run(
9293
currentBeaconState,
9394
prevBeaconState,
9495
validatorIndexToWithdrawalAmount,
95-
validatorIndexToProcessedConsolidation)
96+
validatorIndexToProcessedConsolidation,
97+
validatorIndexToProcessedDeposit)
9698

9799
if err != nil {
98100
return errors.Wrap(err, "error populating participation and balance")
@@ -183,7 +185,8 @@ func (p *BeaconState) PopulateParticipationAndBalance(
183185
beaconState *spec.VersionedBeaconState,
184186
prevBeaconState *spec.VersionedBeaconState,
185187
validatorIndexToWithdrawalAmount map[uint64]*big.Int,
186-
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation) (schemas.ValidatorPerformanceMetrics, error) {
188+
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation,
189+
validatorIndexToProcessedDeposit map[uint64]big.Int) (schemas.ValidatorPerformanceMetrics, error) {
187190

188191
metrics := schemas.ValidatorPerformanceMetrics{
189192
EarnedBalance: big.NewInt(0),
@@ -217,7 +220,8 @@ func (p *BeaconState) PopulateParticipationAndBalance(
217220
prevBeaconState,
218221
beaconState,
219222
validatorIndexToWithdrawalAmount,
220-
validatorIndexToProcessedConsolidation)
223+
validatorIndexToProcessedConsolidation,
224+
validatorIndexToProcessedDeposit)
221225

222226
if err != nil {
223227
return schemas.ValidatorPerformanceMetrics{}, err
@@ -346,7 +350,8 @@ func (p *BeaconState) GetValidatorsWithLessBalance(
346350
prevBeaconState *spec.VersionedBeaconState,
347351
currentBeaconState *spec.VersionedBeaconState,
348352
validatorIndexToWithdrawalAmount map[uint64]*big.Int,
349-
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation) ([]uint64, *big.Int, *big.Int, error) {
353+
validatorIndexToProcessedConsolidation map[uint64][]*electra.PendingConsolidation,
354+
validatorIndexToProcessedDeposit map[uint64]big.Int) ([]uint64, *big.Int, *big.Int, error) {
350355

351356
prevEpoch := GetSlot(prevBeaconState) / p.networkParameters.slotsInEpoch
352357
currEpoch := GetSlot(currentBeaconState) / p.networkParameters.slotsInEpoch
@@ -386,6 +391,11 @@ func (p *BeaconState) GetValidatorsWithLessBalance(
386391
}
387392
}
388393

394+
// Check if there are deposits and subtract them from the balance
395+
if deposit, ok := validatorIndexToProcessedDeposit[valIdx]; ok {
396+
currentEpochValBalance.Sub(currentEpochValBalance, &deposit)
397+
}
398+
389399
delta := big.NewInt(0).Sub(currentEpochValBalance, prevEpochValBalance)
390400

391401
if delta.Cmp(big.NewInt(0)) == -1 {
@@ -527,6 +537,39 @@ func GetProcessedConsolidations(
527537
return consolidations, nil
528538
}
529539

540+
func GetProcessedDeposits(
541+
prevBeaconState *spec.VersionedBeaconState,
542+
currentBeaconState *spec.VersionedBeaconState,
543+
valKeyToIndex map[string]uint64,
544+
) (map[uint64]big.Int, error) {
545+
deposits := make(map[uint64]big.Int)
546+
prevPendingDeposits := GetPendingDeposits(prevBeaconState)
547+
currPendingDeposits := GetPendingDeposits(currentBeaconState)
548+
549+
if prevPendingDeposits == nil || currPendingDeposits == nil {
550+
return nil, errors.New("state with nil pending deposits found")
551+
}
552+
553+
if len(prevPendingDeposits) == 0 {
554+
return deposits, nil
555+
}
556+
557+
for _, deposit := range prevPendingDeposits {
558+
// When the deposit is in current state pending deposits, we have processed all included deposits
559+
if len(currPendingDeposits) != 0 && currPendingDeposits[0].Pubkey.String() == deposit.Pubkey.String() {
560+
break
561+
}
562+
563+
valIndex, ok := valKeyToIndex[hex.EncodeToString(deposit.Pubkey[:])]
564+
if !ok {
565+
continue
566+
}
567+
deposits[valIndex] = *big.NewInt(0).SetUint64(uint64(deposit.Amount))
568+
569+
}
570+
return deposits, nil
571+
}
572+
530573
func logMetrics(
531574
metrics schemas.ValidatorPerformanceMetrics,
532575
poolName string) {
@@ -697,3 +740,15 @@ func GetPendingConsolidations(beaconState *spec.VersionedBeaconState) []*electra
697740
}
698741
return pendingConsolidations
699742
}
743+
744+
func GetPendingDeposits(beaconState *spec.VersionedBeaconState) []*electra.PendingDeposit {
745+
var pendingDeposits []*electra.PendingDeposit
746+
if beaconState.Electra != nil {
747+
pendingDeposits = beaconState.Electra.PendingDeposits
748+
} else if beaconState.Fulu != nil {
749+
pendingDeposits = beaconState.Fulu.PendingDeposits
750+
} else {
751+
log.Fatal("Beacon state was empty")
752+
}
753+
return pendingDeposits
754+
}

metrics/beaconstate_test.go

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func Test_GetValidatorsWithLessBalance(t *testing.T) {
9090
currentBeaconState,
9191
map[uint64]*big.Int{},
9292
map[uint64][]*electra.PendingConsolidation{},
93+
map[uint64]big.Int{},
9394
)
9495

9596
require.NoError(t, err)
@@ -122,7 +123,8 @@ func Test_GetValidatorsWithLessBalance_NonConsecutive(t *testing.T) {
122123
prevBeaconState,
123124
currentBeaconState,
124125
map[uint64]*big.Int{},
125-
map[uint64][]*electra.PendingConsolidation{})
126+
map[uint64][]*electra.PendingConsolidation{},
127+
map[uint64]big.Int{})
126128

127129
require.Error(t, err)
128130
}
@@ -305,3 +307,97 @@ func Test_GetProcessedConsolidations_NilPendingConsolidations(t *testing.T) {
305307
require.Error(t, err)
306308
require.Nil(t, processedConsolidations)
307309
}
310+
311+
func Test_GetProcessedDeposits(t *testing.T) {
312+
prevBeaconState := &spec.VersionedBeaconState{
313+
Electra: &electra.BeaconState{
314+
PendingDeposits: []*electra.PendingDeposit{
315+
{Pubkey: validator_0,
316+
Amount: 1000000000000000000},
317+
{Pubkey: validator_1,
318+
Amount: 1000000000000000000},
319+
},
320+
},
321+
}
322+
currentBeaconState := &spec.VersionedBeaconState{
323+
Electra: &electra.BeaconState{
324+
PendingDeposits: []*electra.PendingDeposit{
325+
{Pubkey: validator_1,
326+
Amount: 1000000000000000000},
327+
},
328+
},
329+
}
330+
valKeyToIndex := make(map[string]uint64)
331+
valKeyToIndex[hex.EncodeToString(validator_0[:])] = 0
332+
valKeyToIndex[hex.EncodeToString(validator_1[:])] = 1
333+
processedDeposits, err := GetProcessedDeposits(prevBeaconState, currentBeaconState, valKeyToIndex)
334+
require.NoError(t, err)
335+
require.Equal(t, processedDeposits, map[uint64]big.Int{
336+
0: *big.NewInt(1000000000000000000),
337+
})
338+
}
339+
340+
func Test_GetProcessedDeposits_EmptyCurrentPendingDeposits(t *testing.T) {
341+
prevBeaconState := &spec.VersionedBeaconState{
342+
Electra: &electra.BeaconState{
343+
PendingDeposits: []*electra.PendingDeposit{
344+
{Pubkey: validator_0,
345+
Amount: 1000000000000000000},
346+
},
347+
},
348+
}
349+
currentBeaconState := &spec.VersionedBeaconState{
350+
Electra: &electra.BeaconState{
351+
PendingDeposits: []*electra.PendingDeposit{},
352+
},
353+
}
354+
valKeyToIndex := make(map[string]uint64)
355+
valKeyToIndex[hex.EncodeToString(validator_0[:])] = 0
356+
processedDeposits, err := GetProcessedDeposits(prevBeaconState, currentBeaconState, valKeyToIndex)
357+
require.NoError(t, err)
358+
require.Equal(t, processedDeposits, map[uint64]big.Int{
359+
0: *big.NewInt(1000000000000000000),
360+
})
361+
}
362+
363+
func Test_GetProcessedDeposits_EmptyPrevPendingDeposits(t *testing.T) {
364+
prevBeaconState := &spec.VersionedBeaconState{
365+
Electra: &electra.BeaconState{
366+
PendingDeposits: []*electra.PendingDeposit{},
367+
},
368+
}
369+
currentBeaconState := &spec.VersionedBeaconState{
370+
Electra: &electra.BeaconState{
371+
PendingDeposits: []*electra.PendingDeposit{
372+
{Pubkey: validator_0,
373+
Amount: 1000000000000000000},
374+
},
375+
},
376+
}
377+
valKeyToIndex := make(map[string]uint64)
378+
valKeyToIndex[hex.EncodeToString(validator_0[:])] = 0
379+
processedDeposits, err := GetProcessedDeposits(prevBeaconState, currentBeaconState, valKeyToIndex)
380+
require.NoError(t, err)
381+
require.Equal(t, processedDeposits, map[uint64]big.Int{})
382+
}
383+
384+
func Test_GetProcessedDeposits_NilPrevPendingDeposits(t *testing.T) {
385+
prevBeaconState := &spec.VersionedBeaconState{
386+
Electra: &electra.BeaconState{
387+
PendingDeposits: nil,
388+
},
389+
}
390+
currentBeaconState := &spec.VersionedBeaconState{
391+
Electra: &electra.BeaconState{
392+
PendingDeposits: []*electra.PendingDeposit{
393+
{Pubkey: validator_0,
394+
Amount: 1000000000000000000},
395+
},
396+
},
397+
}
398+
valKeyToIndex := make(map[string]uint64)
399+
valKeyToIndex[hex.EncodeToString(validator_0[:])] = 0
400+
processedDeposits, err := GetProcessedDeposits(prevBeaconState, currentBeaconState, valKeyToIndex)
401+
require.Error(t, err)
402+
require.Nil(t, processedDeposits)
403+
}

metrics/metrics.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ func (a *Metrics) ProcessEpoch(
363363
return nil, errors.Wrap(err, "error getting processed consolidations")
364364
}
365365

366+
processedDeposits, err := GetProcessedDeposits(prevBeaconState, currentBeaconState, valKeyToIndex)
367+
if err != nil {
368+
return nil, errors.Wrap(err, "error getting processed deposits")
369+
}
370+
366371
relayRewardsPerPool, slotsWithMEVRewards, err := a.relayRewards.GetRelayRewards(currentEpoch)
367372
if err != nil {
368373
return nil, errors.Wrap(err, "error getting relay rewards")
@@ -399,6 +404,7 @@ func (a *Metrics) ProcessEpoch(
399404
validatorIndexToWithdrawalAmount,
400405
proposerTips,
401406
processedConsolidations,
407+
processedDeposits,
402408
)
403409
if err != nil {
404410
return nil, errors.Wrap(err, "error running beacon state")

0 commit comments

Comments
 (0)