@@ -2,40 +2,48 @@ use crate::common::{base::get_base_reward, decrease_balance, increase_balance};
22use crate :: per_epoch_processing:: validator_statuses:: {
33 TotalBalances , ValidatorStatus , ValidatorStatuses ,
44} ;
5- use crate :: per_epoch_processing:: Error ;
5+ use crate :: per_epoch_processing:: { Delta , Error } ;
66use safe_arith:: SafeArith ;
7+ use std:: array:: IntoIter as ArrayIter ;
78use types:: { BeaconState , ChainSpec , EthSpec } ;
89
9- /// Use to track the changes to a validators balance.
10+ /// Combination of several deltas for different components of an attestation reward.
11+ ///
12+ /// Exists only for compatibility with EF rewards tests.
1013#[ derive( Default , Clone ) ]
11- pub struct Delta {
12- rewards : u64 ,
13- penalties : u64 ,
14+ pub struct AttestationDelta {
15+ pub source_delta : Delta ,
16+ pub target_delta : Delta ,
17+ pub head_delta : Delta ,
18+ pub inclusion_delay_delta : Delta ,
19+ pub inactivity_penalty_delta : Delta ,
1420}
1521
16- impl Delta {
17- /// Reward the validator with the `reward`.
18- pub fn reward ( & mut self , reward : u64 ) -> Result < ( ) , Error > {
19- self . rewards = self . rewards . safe_add ( reward) ?;
20- Ok ( ( ) )
21- }
22-
23- /// Penalize the validator with the `penalty`.
24- pub fn penalize ( & mut self , penalty : u64 ) -> Result < ( ) , Error > {
25- self . penalties = self . penalties . safe_add ( penalty) ?;
26- Ok ( ( ) )
27- }
28-
29- /// Combine two deltas.
30- fn combine ( & mut self , other : Delta ) -> Result < ( ) , Error > {
31- self . reward ( other. rewards ) ?;
32- self . penalize ( other. penalties )
22+ impl AttestationDelta {
23+ /// Flatten into a single delta.
24+ pub fn flatten ( self ) -> Result < Delta , Error > {
25+ let AttestationDelta {
26+ source_delta,
27+ target_delta,
28+ head_delta,
29+ inclusion_delay_delta,
30+ inactivity_penalty_delta,
31+ } = self ;
32+ let mut result = Delta :: default ( ) ;
33+ for delta in ArrayIter :: new ( [
34+ source_delta,
35+ target_delta,
36+ head_delta,
37+ inclusion_delay_delta,
38+ inactivity_penalty_delta,
39+ ] ) {
40+ result. combine ( delta) ?;
41+ }
42+ Ok ( result)
3343 }
3444}
3545
3646/// Apply attester and proposer rewards.
37- ///
38- /// Spec v0.12.1
3947pub fn process_rewards_and_penalties < T : EthSpec > (
4048 state : & mut BeaconState < T > ,
4149 validator_statuses : & mut ValidatorStatuses ,
@@ -56,28 +64,27 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
5664
5765 // Apply the deltas, erroring on overflow above but not on overflow below (saturating at 0
5866 // instead).
59- for ( i, delta) in deltas. iter ( ) . enumerate ( ) {
60- increase_balance ( state, i, delta. rewards ) ?;
61- decrease_balance ( state, i, delta. penalties ) ?;
67+ for ( i, delta) in deltas. into_iter ( ) . enumerate ( ) {
68+ let combined_delta = delta. flatten ( ) ?;
69+ increase_balance ( state, i, combined_delta. rewards ) ?;
70+ decrease_balance ( state, i, combined_delta. penalties ) ?;
6271 }
6372
6473 Ok ( ( ) )
6574}
6675
6776/// Apply rewards for participation in attestations during the previous epoch.
68- ///
69- /// Spec v0.12.1
70- fn get_attestation_deltas < T : EthSpec > (
77+ pub fn get_attestation_deltas < T : EthSpec > (
7178 state : & BeaconState < T > ,
7279 validator_statuses : & ValidatorStatuses ,
7380 spec : & ChainSpec ,
74- ) -> Result < Vec < Delta > , Error > {
81+ ) -> Result < Vec < AttestationDelta > , Error > {
7582 let finality_delay = state
7683 . previous_epoch ( )
7784 . safe_sub ( state. finalized_checkpoint ( ) . epoch ) ?
7885 . as_u64 ( ) ;
7986
80- let mut deltas = vec ! [ Delta :: default ( ) ; state. validators( ) . len( ) ] ;
87+ let mut deltas = vec ! [ AttestationDelta :: default ( ) ; state. validators( ) . len( ) ] ;
8188
8289 let total_balances = & validator_statuses. total_balances ;
8390
@@ -106,16 +113,19 @@ fn get_attestation_deltas<T: EthSpec>(
106113 let delta = deltas
107114 . get_mut ( index)
108115 . ok_or ( Error :: DeltaOutOfBounds ( index) ) ?;
109- delta. combine ( source_delta) ?;
110- delta. combine ( target_delta) ?;
111- delta. combine ( head_delta) ?;
112- delta. combine ( inclusion_delay_delta) ?;
113- delta. combine ( inactivity_penalty_delta) ?;
116+ delta. source_delta . combine ( source_delta) ?;
117+ delta. target_delta . combine ( target_delta) ?;
118+ delta. head_delta . combine ( head_delta) ?;
119+ delta. inclusion_delay_delta . combine ( inclusion_delay_delta) ?;
120+ delta
121+ . inactivity_penalty_delta
122+ . combine ( inactivity_penalty_delta) ?;
114123
115124 if let Some ( ( proposer_index, proposer_delta) ) = proposer_delta {
116125 deltas
117126 . get_mut ( proposer_index)
118127 . ok_or ( Error :: ValidatorStatusesInconsistent ) ?
128+ . inclusion_delay_delta
119129 . combine ( proposer_delta) ?;
120130 }
121131 }
0 commit comments