11use crate :: { BeaconChain , BeaconChainError , BeaconChainTypes } ;
22use eth2:: lighthouse:: attestation_rewards:: { IdealAttestationRewards , TotalAttestationRewards } ;
33use eth2:: lighthouse:: StandardAttestationRewards ;
4- use participation_cache:: ParticipationCache ;
54use safe_arith:: SafeArith ;
65use serde_utils:: quoted_u64:: Quoted ;
76use slog:: debug;
@@ -10,7 +9,7 @@ use state_processing::per_epoch_processing::altair::{
109} ;
1110use state_processing:: {
1211 common:: altair:: BaseRewardPerIncrement ,
13- per_epoch_processing:: altair:: { participation_cache , rewards_and_penalties:: get_flag_weight} ,
12+ per_epoch_processing:: altair:: rewards_and_penalties:: get_flag_weight,
1413} ;
1514use std:: collections:: HashMap ;
1615use store:: consts:: altair:: {
@@ -134,8 +133,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
134133 let spec = & self . spec ;
135134
136135 // Calculate ideal_rewards
137- let participation_cache = ParticipationCache :: new ( & state, spec)
138- . map_err ( |_| BeaconChainError :: AttestationRewardsError ) ?;
139136 process_justification_and_finalization ( & state) ?. apply_changes_to_state ( & mut state) ;
140137 process_inactivity_updates_slow ( & mut state, spec) ?;
141138
@@ -147,14 +144,14 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
147144 let weight = get_flag_weight ( flag_index)
148145 . map_err ( |_| BeaconChainError :: AttestationRewardsError ) ?;
149146
150- let unslashed_participating_balance = participation_cache
151- . previous_epoch_flag_attesting_balance ( flag_index )
152- . map_err ( |_| BeaconChainError :: AttestationRewardsError ) ?;
147+ let unslashed_participating_balance = state
148+ . progressive_balances_cache ( )
149+ . previous_epoch_flag_attesting_balance ( flag_index ) ?;
153150
154151 let unslashed_participating_increments =
155152 unslashed_participating_balance. safe_div ( spec. effective_balance_increment ) ?;
156153
157- let total_active_balance = participation_cache . current_epoch_total_active_balance ( ) ;
154+ let total_active_balance = state . get_total_active_balance ( ) ? ;
158155
159156 let active_increments =
160157 total_active_balance. safe_div ( spec. effective_balance_increment ) ?;
@@ -190,15 +187,15 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
190187 let mut total_rewards: Vec < TotalAttestationRewards > = Vec :: new ( ) ;
191188
192189 let validators = if validators. is_empty ( ) {
193- participation_cache . eligible_validator_indices ( ) . to_vec ( )
190+ Self :: all_eligible_validator_indices ( & state , previous_epoch ) ?
194191 } else {
195192 Self :: validators_ids_to_indices ( & mut state, validators) ?
196193 } ;
197194
198195 for & validator_index in & validators {
199196 // Return 0s for unknown/inactive validator indices. This is a bit different from stable
200197 // where we error for unknown pubkeys.
201- let Ok ( validator) = participation_cache . get_validator ( validator_index) else {
198+ let Ok ( validator) = state . get_validator ( validator_index) else {
202199 debug ! (
203200 self . log,
204201 "No rewards for inactive/unknown validator" ;
@@ -215,7 +212,11 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
215212 } ) ;
216213 continue ;
217214 } ;
218- let eligible = validator. is_eligible ;
215+ let previous_epoch_participation_flags = state
216+ . previous_epoch_participation ( ) ?
217+ . get ( validator_index)
218+ . ok_or ( BeaconChainError :: AttestationRewardsError ) ?;
219+ let eligible = state. is_eligible_validator ( previous_epoch, validator) ?;
219220 let mut head_reward = 0i64 ;
220221 let mut target_reward = 0i64 ;
221222 let mut source_reward = 0i64 ;
@@ -228,9 +229,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
228229 let ( ideal_reward, penalty) = ideal_rewards_hashmap
229230 . get ( & ( flag_index, effective_balance) )
230231 . ok_or ( BeaconChainError :: AttestationRewardsError ) ?;
231- let voted_correctly = validator
232- . is_unslashed_participating_index ( flag_index)
233- . map_err ( |_| BeaconChainError :: AttestationRewardsError ) ?;
232+ let voted_correctly = !validator. slashed
233+ && previous_epoch_participation_flags. has_flag ( flag_index) ?;
234234 if voted_correctly {
235235 if flag_index == TIMELY_HEAD_FLAG_INDEX {
236236 head_reward += * ideal_reward as i64 ;
@@ -314,6 +314,24 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
314314 Ok ( max_steps)
315315 }
316316
317+ fn all_eligible_validator_indices (
318+ state : & BeaconState < T :: EthSpec > ,
319+ previous_epoch : Epoch ,
320+ ) -> Result < Vec < usize > , BeaconChainError > {
321+ state
322+ . validators ( )
323+ . iter ( )
324+ . enumerate ( )
325+ . filter_map ( |( i, validator) | {
326+ state
327+ . is_eligible_validator ( previous_epoch, validator)
328+ . map ( |eligible| eligible. then_some ( i) )
329+ . map_err ( BeaconChainError :: BeaconStateError )
330+ . transpose ( )
331+ } )
332+ . collect ( )
333+ }
334+
317335 fn validators_ids_to_indices (
318336 state : & mut BeaconState < T :: EthSpec > ,
319337 validators : Vec < ValidatorId > ,
0 commit comments