@@ -96,30 +96,14 @@ slot_in_year_for_inflation( fd_bank_t const * bank ) {
96
96
static void
97
97
calculate_stake_points_and_credits ( fd_stake_history_t const * stake_history ,
98
98
fd_stake_delegation_t const * stake ,
99
- fd_vote_state_versioned_t * vote_state_versioned ,
99
+ fd_vote_state_ele_t const * vote_state ,
100
100
ulong * new_rate_activation_epoch ,
101
101
fd_calculated_stake_points_t * result ) {
102
102
103
103
ulong credits_in_stake = stake -> credits_observed ;
104
-
105
- fd_vote_epoch_credits_t * epoch_credits ;
106
- switch ( vote_state_versioned -> discriminant ) {
107
- case fd_vote_state_versioned_enum_current :
108
- epoch_credits = vote_state_versioned -> inner .current .epoch_credits ;
109
- break ;
110
- case fd_vote_state_versioned_enum_v0_23_5 :
111
- epoch_credits = vote_state_versioned -> inner .v0_23_5 .epoch_credits ;
112
- break ;
113
- case fd_vote_state_versioned_enum_v1_14_11 :
114
- epoch_credits = vote_state_versioned -> inner .v1_14_11 .epoch_credits ;
115
- break ;
116
- default :
117
- FD_LOG_ERR (( "invalid vote account, should never happen" ));
118
- }
119
-
120
- ulong credits_in_vote = 0UL ;
121
- if ( FD_LIKELY ( !deq_fd_vote_epoch_credits_t_empty ( epoch_credits ) ) ) {
122
- credits_in_vote = deq_fd_vote_epoch_credits_t_peek_tail_const ( epoch_credits )-> credits ;
104
+ ulong credits_in_vote = 0UL ;
105
+ if ( FD_LIKELY ( vote_state -> credits_cnt > 0UL ) ) {
106
+ credits_in_vote = vote_state -> credits [vote_state -> credits_cnt - 1UL ];
123
107
}
124
108
125
109
/* If the Vote account has less credits observed than the Stake account,
@@ -145,15 +129,12 @@ calculate_stake_points_and_credits( fd_stake_history_t const * stake_history
145
129
}
146
130
147
131
/* Calculate the points for each epoch credit */
148
- uint128 points = 0 ;
149
- ulong new_credits_observed = credits_in_stake ;
150
- for ( deq_fd_vote_epoch_credits_t_iter_t iter = deq_fd_vote_epoch_credits_t_iter_init ( epoch_credits );
151
- !deq_fd_vote_epoch_credits_t_iter_done ( epoch_credits , iter );
152
- iter = deq_fd_vote_epoch_credits_t_iter_next ( epoch_credits , iter ) ) {
153
-
154
- fd_vote_epoch_credits_t * ele = deq_fd_vote_epoch_credits_t_iter_ele ( epoch_credits , iter );
155
- ulong final_epoch_credits = ele -> credits ;
156
- ulong initial_epoch_credits = ele -> prev_credits ;
132
+ uint128 points = 0 ;
133
+ ulong new_credits_observed = credits_in_stake ;
134
+ for ( ulong i = 0UL ; i < vote_state -> credits_cnt ; i ++ ) {
135
+
136
+ ulong final_epoch_credits = vote_state -> credits [i ];
137
+ ulong initial_epoch_credits = vote_state -> prev_credits [i ];
157
138
uint128 earned_credits = 0 ;
158
139
if ( FD_LIKELY ( credits_in_stake < initial_epoch_credits ) ) {
159
140
earned_credits = (uint128 )(final_epoch_credits - initial_epoch_credits );
@@ -171,11 +152,17 @@ calculate_stake_points_and_credits( fd_stake_history_t const * stake_history
171
152
.warmup_cooldown_rate = stake -> warmup_cooldown_rate ,
172
153
};
173
154
174
- ulong stake_amount = fd_stake_activating_and_deactivating ( & delegation , ele -> epoch , stake_history , new_rate_activation_epoch ).effective ;
155
+ ulong stake_amount = fd_stake_activating_and_deactivating (
156
+ & delegation ,
157
+ vote_state -> epoch [i ],
158
+ stake_history ,
159
+ new_rate_activation_epoch ).effective ;
175
160
176
161
points += (uint128 )stake_amount * earned_credits ;
162
+
177
163
}
178
164
165
+
179
166
result -> points = points ;
180
167
result -> new_credits_observed = new_credits_observed ;
181
168
result -> force_credits_update_with_skipped_reward = 0 ;
@@ -186,13 +173,19 @@ static int
186
173
calculate_stake_rewards ( fd_stake_history_t const * stake_history ,
187
174
fd_stake_delegation_t const * stake ,
188
175
fd_vote_state_versioned_t * vote_state_versioned ,
176
+ fd_vote_state_ele_t const * vote_state ,
189
177
ulong rewarded_epoch ,
190
178
fd_point_value_t * point_value ,
191
179
ulong * new_rate_activation_epoch ,
192
180
fd_calculated_stake_rewards_t * result ) {
193
181
fd_calculated_stake_points_t stake_points_result = {0 };
194
182
195
- calculate_stake_points_and_credits ( stake_history , stake , vote_state_versioned , new_rate_activation_epoch , & stake_points_result );
183
+ calculate_stake_points_and_credits (
184
+ stake_history ,
185
+ stake ,
186
+ vote_state ,
187
+ new_rate_activation_epoch ,
188
+ & stake_points_result );
196
189
197
190
// Drive credits_observed forward unconditionally when rewards are disabled
198
191
// or when this is the stake's activation epoch
@@ -234,6 +227,7 @@ static int
234
227
redeem_rewards ( fd_stake_history_t const * stake_history ,
235
228
fd_stake_delegation_t const * stake ,
236
229
fd_vote_state_versioned_t * vote_state_versioned ,
230
+ fd_vote_state_ele_t const * vote_state ,
237
231
ulong rewarded_epoch ,
238
232
fd_point_value_t * point_value ,
239
233
ulong * new_rate_activation_epoch ,
@@ -243,6 +237,7 @@ redeem_rewards( fd_stake_history_t const * stake_history,
243
237
stake_history ,
244
238
stake ,
245
239
vote_state_versioned ,
240
+ vote_state ,
246
241
rewarded_epoch ,
247
242
point_value ,
248
243
new_rate_activation_epoch ,
@@ -257,12 +252,16 @@ redeem_rewards( fd_stake_history_t const * stake_history,
257
252
/* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/points.rs#L70 */
258
253
static int
259
254
calculate_points ( fd_stake_delegation_t const * stake ,
260
- fd_vote_state_versioned_t * vote_state_versioned ,
255
+ fd_vote_state_ele_t const * vote_state ,
261
256
fd_stake_history_t const * stake_history ,
262
257
ulong * new_rate_activation_epoch ,
263
258
uint128 * result ) {
264
259
fd_calculated_stake_points_t stake_point_result ;
265
- calculate_stake_points_and_credits ( stake_history , stake , vote_state_versioned , new_rate_activation_epoch , & stake_point_result );
260
+ calculate_stake_points_and_credits ( stake_history ,
261
+ stake ,
262
+ vote_state ,
263
+ new_rate_activation_epoch ,
264
+ & stake_point_result );
266
265
* result = stake_point_result .points ;
267
266
268
267
return FD_EXECUTOR_INSTR_SUCCESS ;
@@ -346,7 +345,6 @@ deserialize_vote_account( fd_exec_slot_ctx_t const * slot_ctx,
346
345
347
346
static uint128
348
347
calculate_points_all ( fd_exec_slot_ctx_t const * slot_ctx ,
349
- fd_spad_t * spad ,
350
348
fd_bank_t * bank ,
351
349
fd_stake_history_t const * stake_history ,
352
350
ulong * new_warmup_cooldown_rate_epoch ,
@@ -370,14 +368,22 @@ calculate_points_all( fd_exec_slot_ctx_t const * slot_ctx,
370
368
continue ;
371
369
}
372
370
373
- /* Check that the vote account is present in our cache */
374
- fd_vote_state_versioned_t * vote_state = deserialize_vote_account ( slot_ctx , & stake_delegation -> vote_account , spad );
375
- if ( !vote_state ) {
371
+ fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query ( slot_ctx -> bank );
372
+ fd_vote_state_ele_t * vote_state_ele = fd_vote_states_query ( vote_states , & stake_delegation -> vote_account );
373
+ if ( FD_UNLIKELY ( !vote_state_ele ) ) {
374
+ FD_LOG_WARNING (( "failed to query vote state" ));
376
375
continue ;
377
376
}
378
377
379
378
uint128 account_points ;
380
- int err = calculate_points ( stake_delegation , vote_state , stake_history , new_warmup_cooldown_rate_epoch , & account_points );
379
+ int err = calculate_points (
380
+ stake_delegation ,
381
+ vote_state_ele ,
382
+ stake_history ,
383
+ new_warmup_cooldown_rate_epoch , & account_points );
384
+
385
+ fd_bank_vote_states_end_locking_query ( slot_ctx -> bank );
386
+
381
387
if ( FD_UNLIKELY ( err ) ) {
382
388
FD_LOG_DEBUG (( "failed to calculate points" ));
383
389
continue ;
@@ -395,7 +401,6 @@ calculate_points_all( fd_exec_slot_ctx_t const * slot_ctx,
395
401
https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L472 */
396
402
static void
397
403
calculate_reward_points_partitioned ( fd_exec_slot_ctx_t * slot_ctx ,
398
- fd_spad_t * spad ,
399
404
fd_stake_history_t const * stake_history ,
400
405
ulong rewards ,
401
406
fd_point_value_t * result ) {
@@ -417,7 +422,6 @@ calculate_reward_points_partitioned( fd_exec_slot_ctx_t * slot_ctx,
417
422
418
423
uint128 points = calculate_points_all (
419
424
slot_ctx ,
420
- spad ,
421
425
slot_ctx -> bank ,
422
426
stake_history ,
423
427
new_warmup_cooldown_rate_epoch ,
@@ -477,16 +481,27 @@ calculate_stake_vote_rewards_account( fd_exec_slot_ctx_t const *
477
481
continue ;
478
482
}
479
483
484
+ fd_vote_states_t const * vote_states = fd_bank_vote_states_locking_query ( slot_ctx -> bank );
485
+ fd_vote_state_ele_t * vote_state_ele = fd_vote_states_query ( vote_states , voter_acc );
486
+ if ( FD_UNLIKELY ( !vote_state_ele ) ) {
487
+ FD_LOG_WARNING (( "failed to query vote state" ));
488
+ continue ;
489
+ }
490
+
480
491
/* Note, this doesn't actually redeem any rewards.. this is a misnomer. */
481
492
fd_calculated_stake_rewards_t calculated_stake_rewards [1 ] = {0 };
482
493
483
- int err = redeem_rewards ( stake_history ,
484
- stake_delegation ,
485
- vote_state ,
486
- rewarded_epoch ,
487
- point_value ,
488
- new_warmup_cooldown_rate_epoch ,
489
- calculated_stake_rewards );
494
+ int err = redeem_rewards (
495
+ stake_history ,
496
+ stake_delegation ,
497
+ vote_state ,
498
+ vote_state_ele ,
499
+ rewarded_epoch ,
500
+ point_value ,
501
+ new_warmup_cooldown_rate_epoch ,
502
+ calculated_stake_rewards );
503
+
504
+ fd_bank_vote_states_end_locking_query ( slot_ctx -> bank );
490
505
if ( FD_UNLIKELY ( err != 0 ) ) {
491
506
FD_LOG_DEBUG (( "redeem_rewards failed for %s with error %d" , FD_BASE58_ENC_32_ALLOCA ( & stake_delegation -> stake_account ), err ));
492
507
continue ;
@@ -705,11 +720,11 @@ calculate_validator_rewards( fd_exec_slot_ctx_t * slot_ctx,
705
720
}
706
721
707
722
/* Calculate the epoch reward points from stake/vote accounts */
708
- calculate_reward_points_partitioned ( slot_ctx ,
709
- runtime_spad ,
710
- stake_history ,
711
- rewards ,
712
- & result -> point_value );
723
+ calculate_reward_points_partitioned (
724
+ slot_ctx ,
725
+ stake_history ,
726
+ rewards ,
727
+ & result -> point_value );
713
728
714
729
if ( capture_ctx ) {
715
730
ulong const epoch = fd_bank_epoch_get ( slot_ctx -> bank );
@@ -721,13 +736,14 @@ calculate_validator_rewards( fd_exec_slot_ctx_t * slot_ctx,
721
736
}
722
737
723
738
/* Calculate the stake and vote rewards for each account */
724
- calculate_stake_vote_rewards ( slot_ctx ,
725
- capture_ctx ,
726
- stake_history ,
727
- rewarded_epoch ,
728
- & result -> point_value ,
729
- & result -> calculate_stake_vote_rewards_result ,
730
- runtime_spad );
739
+ calculate_stake_vote_rewards (
740
+ slot_ctx ,
741
+ capture_ctx ,
742
+ stake_history ,
743
+ rewarded_epoch ,
744
+ & result -> point_value ,
745
+ & result -> calculate_stake_vote_rewards_result ,
746
+ runtime_spad );
731
747
}
732
748
733
749
/* Calculate the number of blocks required to distribute rewards to all stake accounts.
0 commit comments