Skip to content

Commit 2bb6f23

Browse files
wip using our own type now
1 parent 64d9a7b commit 2bb6f23

File tree

2 files changed

+78
-62
lines changed

2 files changed

+78
-62
lines changed

src/flamenco/rewards/fd_rewards.c

Lines changed: 75 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -96,30 +96,14 @@ slot_in_year_for_inflation( fd_bank_t const * bank ) {
9696
static void
9797
calculate_stake_points_and_credits( fd_stake_history_t const * stake_history,
9898
fd_stake_delegation_t const * stake,
99-
fd_vote_state_versioned_t * vote_state_versioned,
99+
fd_vote_state_ele_t const * vote_state,
100100
ulong * new_rate_activation_epoch,
101101
fd_calculated_stake_points_t * result ) {
102102

103103
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];
123107
}
124108

125109
/* 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
145129
}
146130

147131
/* 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];
157138
uint128 earned_credits = 0;
158139
if( FD_LIKELY( credits_in_stake < initial_epoch_credits ) ) {
159140
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
171152
.warmup_cooldown_rate = stake->warmup_cooldown_rate,
172153
};
173154

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;
175160

176161
points += (uint128)stake_amount * earned_credits;
162+
177163
}
178164

165+
179166
result->points = points;
180167
result->new_credits_observed = new_credits_observed;
181168
result->force_credits_update_with_skipped_reward = 0;
@@ -186,13 +173,19 @@ static int
186173
calculate_stake_rewards( fd_stake_history_t const * stake_history,
187174
fd_stake_delegation_t const * stake,
188175
fd_vote_state_versioned_t * vote_state_versioned,
176+
fd_vote_state_ele_t const * vote_state,
189177
ulong rewarded_epoch,
190178
fd_point_value_t * point_value,
191179
ulong * new_rate_activation_epoch,
192180
fd_calculated_stake_rewards_t * result ) {
193181
fd_calculated_stake_points_t stake_points_result = {0};
194182

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);
196189

197190
// Drive credits_observed forward unconditionally when rewards are disabled
198191
// or when this is the stake's activation epoch
@@ -234,6 +227,7 @@ static int
234227
redeem_rewards( fd_stake_history_t const * stake_history,
235228
fd_stake_delegation_t const * stake,
236229
fd_vote_state_versioned_t * vote_state_versioned,
230+
fd_vote_state_ele_t const * vote_state,
237231
ulong rewarded_epoch,
238232
fd_point_value_t * point_value,
239233
ulong * new_rate_activation_epoch,
@@ -243,6 +237,7 @@ redeem_rewards( fd_stake_history_t const * stake_history,
243237
stake_history,
244238
stake,
245239
vote_state_versioned,
240+
vote_state,
246241
rewarded_epoch,
247242
point_value,
248243
new_rate_activation_epoch,
@@ -257,12 +252,16 @@ redeem_rewards( fd_stake_history_t const * stake_history,
257252
/* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/points.rs#L70 */
258253
static int
259254
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,
261256
fd_stake_history_t const * stake_history,
262257
ulong * new_rate_activation_epoch,
263258
uint128 * result ) {
264259
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 );
266265
*result = stake_point_result.points;
267266

268267
return FD_EXECUTOR_INSTR_SUCCESS;
@@ -346,7 +345,6 @@ deserialize_vote_account( fd_exec_slot_ctx_t const * slot_ctx,
346345

347346
static uint128
348347
calculate_points_all( fd_exec_slot_ctx_t const * slot_ctx,
349-
fd_spad_t * spad,
350348
fd_bank_t * bank,
351349
fd_stake_history_t const * stake_history,
352350
ulong * new_warmup_cooldown_rate_epoch,
@@ -370,14 +368,22 @@ calculate_points_all( fd_exec_slot_ctx_t const * slot_ctx,
370368
continue;
371369
}
372370

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" ));
376375
continue;
377376
}
378377

379378
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+
381387
if( FD_UNLIKELY( err ) ) {
382388
FD_LOG_DEBUG(( "failed to calculate points" ));
383389
continue;
@@ -395,7 +401,6 @@ calculate_points_all( fd_exec_slot_ctx_t const * slot_ctx,
395401
https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L472 */
396402
static void
397403
calculate_reward_points_partitioned( fd_exec_slot_ctx_t * slot_ctx,
398-
fd_spad_t * spad,
399404
fd_stake_history_t const * stake_history,
400405
ulong rewards,
401406
fd_point_value_t * result ) {
@@ -417,7 +422,6 @@ calculate_reward_points_partitioned( fd_exec_slot_ctx_t * slot_ctx,
417422

418423
uint128 points = calculate_points_all(
419424
slot_ctx,
420-
spad,
421425
slot_ctx->bank,
422426
stake_history,
423427
new_warmup_cooldown_rate_epoch,
@@ -477,16 +481,27 @@ calculate_stake_vote_rewards_account( fd_exec_slot_ctx_t const *
477481
continue;
478482
}
479483

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+
480491
/* Note, this doesn't actually redeem any rewards.. this is a misnomer. */
481492
fd_calculated_stake_rewards_t calculated_stake_rewards[1] = {0};
482493

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 );
490505
if( FD_UNLIKELY( err!=0 ) ) {
491506
FD_LOG_DEBUG(( "redeem_rewards failed for %s with error %d", FD_BASE58_ENC_32_ALLOCA( &stake_delegation->stake_account ), err ));
492507
continue;
@@ -705,11 +720,11 @@ calculate_validator_rewards( fd_exec_slot_ctx_t * slot_ctx,
705720
}
706721

707722
/* 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 );
713728

714729
if( capture_ctx ) {
715730
ulong const epoch = fd_bank_epoch_get( slot_ctx->bank );
@@ -721,13 +736,14 @@ calculate_validator_rewards( fd_exec_slot_ctx_t * slot_ctx,
721736
}
722737

723738
/* 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 );
731747
}
732748

733749
/* Calculate the number of blocks required to distribute rewards to all stake accounts.

src/flamenco/stakes/fd_vote_states.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ fd_vote_states_remove( fd_vote_states_t * vote_states,
149149
account. Returns NULL if the account does not exist. */
150150

151151
static inline fd_vote_state_ele_t *
152-
fd_vote_states_query( fd_vote_states_t * self, fd_pubkey_t * vote_account ) {
152+
fd_vote_states_query( fd_vote_states_t const * self, fd_pubkey_t const * vote_account ) {
153153

154154
fd_vote_state_ele_t * vote_state = fd_vote_state_map_ele_query(
155155
fd_vote_states_get_map( self ),
156156
vote_account,
157-
158-
NULL, fd_vote_states_get_pool( self ) );
157+
NULL,
158+
fd_vote_states_get_pool( self ) );
159159
if( FD_UNLIKELY( !vote_state ) ) {
160160
return NULL;
161161
}

0 commit comments

Comments
 (0)