@@ -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+
530573func 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+ }
0 commit comments