@@ -12,6 +12,7 @@ use types::{
1212 NUM_FLAG_INDICES , PARTICIPATION_FLAG_WEIGHTS , TIMELY_HEAD_FLAG_INDEX ,
1313 TIMELY_TARGET_FLAG_INDEX , WEIGHT_DENOMINATOR ,
1414 } ,
15+ milhouse:: Cow ,
1516 ActivationQueue , BeaconState , BeaconStateError , ChainSpec , Epoch , EthSpec , ExitCache , ForkName ,
1617 ParticipationFlags , ProgressiveBalancesCache , Unsigned , Validator ,
1718} ;
@@ -180,20 +181,16 @@ pub fn process_epoch_single_pass<E: EthSpec>(
180181 previous_epoch_participation. iter( ) ,
181182 current_epoch_participation. iter( ) ,
182183 ) {
183- let ( _, validator_cow ) = validators_iter
184+ let ( _, mut validator ) = validators_iter
184185 . next_cow ( )
185186 . ok_or ( BeaconStateError :: UnknownValidator ( index) ) ?;
186- let ( _, balance_cow ) = balances_iter
187+ let ( _, mut balance ) = balances_iter
187188 . next_cow ( )
188189 . ok_or ( BeaconStateError :: UnknownValidator ( index) ) ?;
189- let ( _, inactivity_score_cow ) = inactivity_scores_iter
190+ let ( _, mut inactivity_score ) = inactivity_scores_iter
190191 . next_cow ( )
191192 . ok_or ( BeaconStateError :: UnknownValidator ( index) ) ?;
192193
193- let validator = validator_cow. to_mut ( ) ;
194- let balance = balance_cow. to_mut ( ) ;
195- let inactivity_score = inactivity_score_cow. to_mut ( ) ;
196-
197194 let is_active_current_epoch = validator. is_active_at ( current_epoch) ;
198195 let is_active_previous_epoch = validator. is_active_at ( previous_epoch) ;
199196 let is_eligible = is_active_previous_epoch
@@ -222,7 +219,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
222219 // `process_inactivity_updates`
223220 if conf. inactivity_updates {
224221 process_single_inactivity_update (
225- inactivity_score,
222+ & mut inactivity_score,
226223 validator_info,
227224 state_ctxt,
228225 spec,
@@ -232,8 +229,8 @@ pub fn process_epoch_single_pass<E: EthSpec>(
232229 // `process_rewards_and_penalties`
233230 if conf. rewards_and_penalties {
234231 process_single_reward_and_penalty (
235- balance,
236- inactivity_score,
232+ & mut balance,
233+ & inactivity_score,
237234 validator_info,
238235 rewards_ctxt,
239236 state_ctxt,
@@ -245,7 +242,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
245242 // `process_registry_updates`
246243 if conf. registry_updates {
247244 process_single_registry_update (
248- validator,
245+ & mut validator,
249246 validator_info,
250247 exit_cache,
251248 activation_queue,
@@ -257,14 +254,14 @@ pub fn process_epoch_single_pass<E: EthSpec>(
257254
258255 // `process_slashings`
259256 if conf. slashings {
260- process_single_slashing ( balance, validator, slashings_ctxt, state_ctxt, spec) ?;
257+ process_single_slashing ( & mut balance, & validator, slashings_ctxt, state_ctxt, spec) ?;
261258 }
262259
263260 // `process_effective_balance_updates`
264261 if conf. effective_balance_updates {
265262 process_single_effective_balance_update (
266263 * balance,
267- validator,
264+ & mut validator,
268265 validator_info,
269266 & mut next_epoch_total_active_balance,
270267 & mut next_epoch_cache,
@@ -289,7 +286,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
289286}
290287
291288fn process_single_inactivity_update (
292- inactivity_score : & mut u64 ,
289+ inactivity_score : & mut Cow < u64 > ,
293290 validator_info : & ValidatorInfo ,
294291 state_ctxt : & StateContext ,
295292 spec : & ChainSpec ,
@@ -302,25 +299,27 @@ fn process_single_inactivity_update(
302299 if validator_info. is_unslashed_participating_index ( TIMELY_TARGET_FLAG_INDEX ) ? {
303300 // Avoid mutating when the inactivity score is 0 and can't go any lower -- the common
304301 // case.
305- if * inactivity_score == 0 {
302+ if * * inactivity_score == 0 {
306303 return Ok ( ( ) ) ;
307304 }
308- inactivity_score. safe_sub_assign ( 1 ) ?;
305+ inactivity_score. make_mut ( ) ? . safe_sub_assign ( 1 ) ?;
309306 } else {
310- inactivity_score. safe_add_assign ( spec. inactivity_score_bias ) ?;
307+ inactivity_score
308+ . make_mut ( ) ?
309+ . safe_add_assign ( spec. inactivity_score_bias ) ?;
311310 }
312311
313312 // Decrease the score of all validators for forgiveness when not during a leak
314313 if !state_ctxt. is_in_inactivity_leak {
315- inactivity_score
316- . safe_sub_assign ( min ( spec . inactivity_score_recovery_rate , * inactivity_score ) ) ?;
314+ let deduction = min ( spec . inactivity_score_recovery_rate , * * inactivity_score) ;
315+ inactivity_score . make_mut ( ) ? . safe_sub_assign ( deduction ) ?;
317316 }
318317
319318 Ok ( ( ) )
320319}
321320
322321fn process_single_reward_and_penalty (
323- balance : & mut u64 ,
322+ balance : & mut Cow < u64 > ,
324323 inactivity_score : & u64 ,
325324 validator_info : & ValidatorInfo ,
326325 rewards_ctxt : & RewardsAndPenaltiesContext ,
@@ -349,8 +348,11 @@ fn process_single_reward_and_penalty(
349348 spec,
350349 ) ?;
351350
352- balance. safe_add_assign ( delta. rewards ) ?;
353- * balance = balance. saturating_sub ( delta. penalties ) ;
351+ if delta. rewards != 0 || delta. penalties != 0 {
352+ let balance = balance. make_mut ( ) ?;
353+ balance. safe_add_assign ( delta. rewards ) ?;
354+ * balance = balance. saturating_sub ( delta. penalties ) ;
355+ }
354356
355357 Ok ( ( ) )
356358}
@@ -449,7 +451,7 @@ impl RewardsAndPenaltiesContext {
449451}
450452
451453fn process_single_registry_update (
452- validator : & mut Validator ,
454+ validator : & mut Cow < Validator > ,
453455 validator_info : & ValidatorInfo ,
454456 exit_cache : & mut ExitCache ,
455457 activation_queue : & BTreeSet < usize > ,
@@ -460,7 +462,7 @@ fn process_single_registry_update(
460462 let current_epoch = state_ctxt. current_epoch ;
461463
462464 if validator. is_eligible_for_activation_queue ( spec) {
463- validator. mutable . activation_eligibility_epoch = current_epoch. safe_add ( 1 ) ?;
465+ validator. make_mut ( ) ? . mutable . activation_eligibility_epoch = current_epoch. safe_add ( 1 ) ?;
464466 }
465467
466468 if validator. is_active_at ( current_epoch)
@@ -470,7 +472,8 @@ fn process_single_registry_update(
470472 }
471473
472474 if activation_queue. contains ( & validator_info. index ) {
473- validator. mutable . activation_epoch = spec. compute_activation_exit_epoch ( current_epoch) ?;
475+ validator. make_mut ( ) ?. mutable . activation_epoch =
476+ spec. compute_activation_exit_epoch ( current_epoch) ?;
474477 }
475478
476479 // Caching: add to speculative activation queue for next epoch.
@@ -485,7 +488,7 @@ fn process_single_registry_update(
485488}
486489
487490fn initiate_validator_exit (
488- validator : & mut Validator ,
491+ validator : & mut Cow < Validator > ,
489492 exit_cache : & mut ExitCache ,
490493 state_ctxt : & StateContext ,
491494 spec : & ChainSpec ,
@@ -506,6 +509,7 @@ fn initiate_validator_exit(
506509 exit_queue_epoch. safe_add_assign ( 1 ) ?;
507510 }
508511
512+ let validator = validator. make_mut ( ) ?;
509513 validator. mutable . exit_epoch = exit_queue_epoch;
510514 validator. mutable . withdrawable_epoch =
511515 exit_queue_epoch. safe_add ( spec. min_validator_withdrawability_delay ) ?;
@@ -538,7 +542,7 @@ impl SlashingsContext {
538542}
539543
540544fn process_single_slashing (
541- balance : & mut u64 ,
545+ balance : & mut Cow < u64 > ,
542546 validator : & Validator ,
543547 slashings_ctxt : & SlashingsContext ,
544548 state_ctxt : & StateContext ,
@@ -556,7 +560,7 @@ fn process_single_slashing(
556560 . safe_div ( state_ctxt. total_active_balance ) ?
557561 . safe_mul ( increment) ?;
558562
559- * balance = balance. saturating_sub ( penalty) ;
563+ * balance. make_mut ( ) ? = balance. saturating_sub ( penalty) ;
560564 }
561565 Ok ( ( ) )
562566}
@@ -580,7 +584,7 @@ impl EffectiveBalancesContext {
580584#[ allow( clippy:: too_many_arguments) ]
581585fn process_single_effective_balance_update (
582586 balance : u64 ,
583- validator : & mut Validator ,
587+ validator : & mut Cow < Validator > ,
584588 validator_info : & ValidatorInfo ,
585589 next_epoch_total_active_balance : & mut u64 ,
586590 next_epoch_cache : & mut PreEpochCache ,
@@ -610,7 +614,7 @@ fn process_single_effective_balance_update(
610614 }
611615
612616 if new_effective_balance != old_effective_balance {
613- validator. mutable . effective_balance = new_effective_balance;
617+ validator. make_mut ( ) ? . mutable . effective_balance = new_effective_balance;
614618
615619 // Update progressive balances cache for the *current* epoch, which will soon become the
616620 // previous epoch once the epoch transition completes.
0 commit comments