@@ -4,35 +4,46 @@ use eth2::{
44 lighthouse:: { GlobalValidatorInclusionData , ValidatorInclusionData } ,
55 types:: ValidatorId ,
66} ;
7- use state_processing:: per_epoch_processing:: ValidatorStatuses ;
8- use types:: { Epoch , EthSpec } ;
7+ use state_processing:: per_epoch_processing:: { process_epoch, EpochProcessingSummary } ;
8+ use types:: { BeaconState , ChainSpec , Epoch , EthSpec } ;
9+
10+ fn end_of_epoch_state < T : BeaconChainTypes > (
11+ epoch : Epoch ,
12+ chain : & BeaconChain < T > ,
13+ ) -> Result < BeaconState < T :: EthSpec > , warp:: reject:: Rejection > {
14+ let target_slot = epoch. end_slot ( T :: EthSpec :: slots_per_epoch ( ) ) ;
15+ StateId :: slot ( target_slot) . state ( chain)
16+ }
17+
18+ fn get_epoch_processing_summary < T : EthSpec > (
19+ state : & mut BeaconState < T > ,
20+ spec : & ChainSpec ,
21+ ) -> Result < EpochProcessingSummary , warp:: reject:: Rejection > {
22+ process_epoch ( state, spec)
23+ . map_err ( |e| warp_utils:: reject:: custom_server_error ( format ! ( "{:?}" , e) ) )
24+ }
925
1026/// Returns information about *all validators* (i.e., global) and how they performed during a given
1127/// epoch.
1228pub fn global_validator_inclusion_data < T : BeaconChainTypes > (
1329 epoch : Epoch ,
1430 chain : & BeaconChain < T > ,
1531) -> Result < GlobalValidatorInclusionData , warp:: Rejection > {
16- let target_slot = epoch. end_slot ( T :: EthSpec :: slots_per_epoch ( ) ) ;
17-
18- let state = StateId :: slot ( target_slot) . state ( chain) ?;
19-
20- let mut validator_statuses = ValidatorStatuses :: new ( & state, & chain. spec )
21- . map_err ( warp_utils:: reject:: beacon_state_error) ?;
22- validator_statuses
23- . process_attestations ( & state)
24- . map_err ( warp_utils:: reject:: beacon_state_error) ?;
25-
26- let totals = validator_statuses. total_balances ;
32+ let mut state = end_of_epoch_state ( epoch, chain) ?;
33+ let summary = get_epoch_processing_summary ( & mut state, & chain. spec ) ?;
2734
2835 Ok ( GlobalValidatorInclusionData {
29- current_epoch_active_gwei : totals. current_epoch ( ) ,
30- previous_epoch_active_gwei : totals. previous_epoch ( ) ,
31- current_epoch_attesting_gwei : totals. current_epoch_attesters ( ) ,
32- current_epoch_target_attesting_gwei : totals. current_epoch_target_attesters ( ) ,
33- previous_epoch_attesting_gwei : totals. previous_epoch_attesters ( ) ,
34- previous_epoch_target_attesting_gwei : totals. previous_epoch_target_attesters ( ) ,
35- previous_epoch_head_attesting_gwei : totals. previous_epoch_head_attesters ( ) ,
36+ current_epoch_active_gwei : summary. current_epoch_total_active_balance ( ) ,
37+ previous_epoch_active_gwei : summary. previous_epoch_total_active_balance ( ) ,
38+ current_epoch_target_attesting_gwei : summary
39+ . current_epoch_target_attesting_balance ( )
40+ . map_err ( |e| warp_utils:: reject:: custom_server_error ( format ! ( "{:?}" , e) ) ) ?,
41+ previous_epoch_target_attesting_gwei : summary
42+ . previous_epoch_target_attesting_balance ( )
43+ . map_err ( |e| warp_utils:: reject:: custom_server_error ( format ! ( "{:?}" , e) ) ) ?,
44+ previous_epoch_head_attesting_gwei : summary
45+ . previous_epoch_head_attesting_balance ( )
46+ . map_err ( |e| warp_utils:: reject:: custom_server_error ( format ! ( "{:?}" , e) ) ) ?,
3647 } )
3748}
3849
@@ -42,15 +53,7 @@ pub fn validator_inclusion_data<T: BeaconChainTypes>(
4253 validator_id : & ValidatorId ,
4354 chain : & BeaconChain < T > ,
4455) -> Result < Option < ValidatorInclusionData > , warp:: Rejection > {
45- let target_slot = epoch. end_slot ( T :: EthSpec :: slots_per_epoch ( ) ) ;
46-
47- let mut state = StateId :: slot ( target_slot) . state ( chain) ?;
48-
49- let mut validator_statuses = ValidatorStatuses :: new ( & state, & chain. spec )
50- . map_err ( warp_utils:: reject:: beacon_state_error) ?;
51- validator_statuses
52- . process_attestations ( & state)
53- . map_err ( warp_utils:: reject:: beacon_state_error) ?;
56+ let mut state = end_of_epoch_state ( epoch, chain) ?;
5457
5558 state
5659 . update_pubkey_cache ( )
@@ -70,19 +73,23 @@ pub fn validator_inclusion_data<T: BeaconChainTypes>(
7073 }
7174 } ;
7275
73- Ok ( validator_statuses
74- . statuses
75- . get ( validator_index)
76- . map ( |vote| ValidatorInclusionData {
77- is_slashed : vote. is_slashed ,
78- is_withdrawable_in_current_epoch : vote. is_withdrawable_in_current_epoch ,
79- is_active_in_current_epoch : vote. is_active_in_current_epoch ,
80- is_active_in_previous_epoch : vote. is_active_in_previous_epoch ,
81- current_epoch_effective_balance_gwei : vote. current_epoch_effective_balance ,
82- is_current_epoch_attester : vote. is_current_epoch_attester ,
83- is_current_epoch_target_attester : vote. is_current_epoch_target_attester ,
84- is_previous_epoch_attester : vote. is_previous_epoch_attester ,
85- is_previous_epoch_target_attester : vote. is_previous_epoch_target_attester ,
86- is_previous_epoch_head_attester : vote. is_previous_epoch_head_attester ,
87- } ) )
76+ let validator = if let Ok ( validator) = state. get_validator ( validator_index) {
77+ validator. clone ( )
78+ } else {
79+ return Ok ( None ) ;
80+ } ;
81+
82+ let summary = get_epoch_processing_summary ( & mut state, & chain. spec ) ?;
83+
84+ Ok ( Some ( ValidatorInclusionData {
85+ is_slashed : validator. slashed ,
86+ is_withdrawable_in_current_epoch : validator. is_withdrawable_at ( epoch) ,
87+ is_active_in_current_epoch : summary. is_active_in_current_epoch ( validator_index) ,
88+ is_active_in_previous_epoch : summary. is_active_in_previous_epoch ( validator_index) ,
89+ current_epoch_effective_balance_gwei : validator. effective_balance ,
90+ is_current_epoch_target_attester : summary. is_current_epoch_target_attester ( validator_index) ,
91+ is_previous_epoch_target_attester : summary
92+ . is_previous_epoch_target_attester ( validator_index) ,
93+ is_previous_epoch_head_attester : summary. is_previous_epoch_head_attester ( validator_index) ,
94+ } ) )
8895}
0 commit comments