LCOV - code coverage report
Current view: top level - flamenco/rewards - fd_rewards.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 391 694 56.3 %
Date: 2026-03-30 06:35:28 Functions: 23 26 88.5 %

          Line data    Source code
       1             : #include "fd_rewards.h"
       2             : #include "fd_stake_rewards.h"
       3             : #include <math.h>
       4             : 
       5             : #include "../runtime/sysvar/fd_sysvar_epoch_rewards.h"
       6             : #include "../runtime/sysvar/fd_sysvar_epoch_schedule.h"
       7             : #include "../stakes/fd_stakes.h"
       8             : #include "../runtime/sysvar/fd_sysvar_stake_history.h"
       9             : #include "../runtime/sysvar/fd_sysvar_cache.h"
      10             : #include "../capture/fd_capture_ctx.h"
      11             : #include "../runtime/fd_runtime_stack.h"
      12             : #include "../runtime/fd_runtime.h"
      13             : #include "../accdb/fd_accdb_sync.h"
      14             : 
      15             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/sdk/src/inflation.rs#L85 */
      16             : static double
      17          18 : total( fd_inflation_t const * inflation, double year ) {
      18          18 :   if ( FD_UNLIKELY( year == 0.0 ) ) {
      19           0 :     FD_LOG_ERR(( "inflation year 0" ));
      20           0 :   }
      21          18 :   double tapered = inflation->initial * pow( (1.0 - inflation->taper), year );
      22          18 :   return (tapered > inflation->terminal) ? tapered : inflation->terminal;
      23          18 : }
      24             : 
      25             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/sdk/src/inflation.rs#L102 */
      26             : static double
      27          27 : foundation( fd_inflation_t const * inflation, double year ) {
      28          27 :   return (year < inflation->foundation_term) ? inflation->foundation * total(inflation, year) : 0.0;
      29          27 : }
      30             : 
      31             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/sdk/src/inflation.rs#L97 */
      32             : static double
      33           9 : validator( fd_inflation_t const * inflation, double year) {
      34             :   /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/sdk/src/inflation.rs#L96-L99 */
      35           9 :   FD_LOG_DEBUG(("Validator Rate: %.16f %.16f %.16f %.16f %.16f", year, total( inflation, year ), foundation( inflation, year ), inflation->taper, inflation->initial));
      36           9 :   return total( inflation, year ) - foundation( inflation, year );
      37           9 : }
      38             : 
      39             : /* Calculates the starting slot for inflation from the activation slot. The activation slot is the earliest
      40             :     activation slot of the following features:
      41             :     - devnet_and_testnet
      42             :     - full_inflation_enable, if full_inflation_vote has been activated
      43             : 
      44             :     https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/runtime/src/bank.rs#L2095 */
      45             : static FD_FN_CONST ulong
      46           9 : get_inflation_start_slot( fd_bank_t const * bank ) {
      47           9 :   ulong devnet_and_testnet = FD_FEATURE_ACTIVE_BANK( bank, devnet_and_testnet )
      48           9 :       ? bank->f.features.devnet_and_testnet
      49           9 :       : ULONG_MAX;
      50             : 
      51           9 :   ulong enable = bank->f.features.full_inflation_enable;
      52             : 
      53           9 :   ulong min_slot = fd_ulong_min( enable, devnet_and_testnet );
      54           9 :   if( min_slot == ULONG_MAX ) {
      55           9 :     if( FD_FEATURE_ACTIVE_BANK( bank, pico_inflation ) ) {
      56           0 :       min_slot = bank->f.features.pico_inflation;
      57           9 :     } else {
      58           9 :       min_slot = 0;
      59           9 :     }
      60           9 :   }
      61           9 :   return min_slot;
      62           9 : }
      63             : 
      64             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/runtime/src/bank.rs#L2110 */
      65             : static ulong
      66             : get_inflation_num_slots( fd_bank_t const *           bank,
      67             :                          fd_epoch_schedule_t const * epoch_schedule,
      68           9 :                          ulong                       slot ) {
      69           9 :   ulong inflation_activation_slot = get_inflation_start_slot( bank );
      70           9 :   ulong inflation_start_slot      = fd_epoch_slot0( epoch_schedule,
      71           9 :                                                     fd_ulong_sat_sub( fd_slot_to_epoch( epoch_schedule,
      72           9 :                                                                                         inflation_activation_slot, NULL ),
      73           9 :                                                                       1UL ) );
      74             : 
      75           9 :   ulong epoch = fd_slot_to_epoch( epoch_schedule, slot, NULL );
      76             : 
      77           9 :   return fd_epoch_slot0( epoch_schedule, epoch ) - inflation_start_slot;
      78           9 : }
      79             : 
      80             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/runtime/src/bank.rs#L2121 */
      81             : static double
      82           9 : slot_in_year_for_inflation( fd_bank_t const * bank ) {
      83           9 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
      84           9 :   ulong num_slots = get_inflation_num_slots( bank, epoch_schedule, bank->f.slot );
      85           9 :   return (double)num_slots / (double)bank->f.slots_per_year;
      86           9 : }
      87             : 
      88             : 
      89             : static void
      90             : get_credits( uchar const *       account_data,
      91             :              ulong               account_data_len,
      92             :              uchar *             buf,
      93           0 :              fd_epoch_credits_t * epoch_credits ) {
      94             : 
      95           0 :   fd_bincode_decode_ctx_t ctx = {
      96           0 :     .data    = account_data,
      97           0 :     .dataend = account_data + account_data_len,
      98           0 :   };
      99             : 
     100           0 :   fd_vote_state_versioned_t * vsv = fd_vote_state_versioned_decode( buf, &ctx );
     101           0 :   if( FD_UNLIKELY( vsv==NULL ) ) {
     102           0 :     FD_LOG_CRIT(( "unable to decode vote state versioned" ));
     103           0 :   }
     104             : 
     105           0 :   fd_vote_epoch_credits_t * vote_credits = NULL;
     106             : 
     107           0 :   switch( vsv->discriminant ) {
     108           0 :   case fd_vote_state_versioned_enum_v1_14_11:
     109           0 :     vote_credits = vsv->inner.v1_14_11.epoch_credits;
     110           0 :     break;
     111           0 :   case fd_vote_state_versioned_enum_v3:
     112           0 :     vote_credits = vsv->inner.v3.epoch_credits;
     113           0 :     break;
     114           0 :   case fd_vote_state_versioned_enum_v4:
     115           0 :     vote_credits = vsv->inner.v4.epoch_credits;
     116           0 :     break;
     117           0 :   default:
     118           0 :     FD_LOG_CRIT(( "invalid vote state version %u", vsv->discriminant ));
     119           0 :   }
     120             : 
     121           0 :   epoch_credits->cnt = 0UL;
     122           0 :   for( deq_fd_vote_epoch_credits_t_iter_t iter = deq_fd_vote_epoch_credits_t_iter_init( vote_credits );
     123           0 :        !deq_fd_vote_epoch_credits_t_iter_done( vote_credits, iter );
     124           0 :        iter = deq_fd_vote_epoch_credits_t_iter_next( vote_credits, iter ) ) {
     125           0 :     fd_vote_epoch_credits_t * ele = deq_fd_vote_epoch_credits_t_iter_ele( vote_credits, iter );
     126           0 :     epoch_credits->epoch[ epoch_credits->cnt ]        = (ushort)ele->epoch;
     127           0 :     epoch_credits->credits[ epoch_credits->cnt ]      = ele->credits;
     128           0 :     epoch_credits->prev_credits[ epoch_credits->cnt ] = ele->prev_credits;
     129           0 :     epoch_credits->cnt++;
     130           0 :   }
     131           0 : }
     132             : 
     133             : /* For a given stake and vote_state, calculate how many points were earned (credits * stake) and new value
     134             :    for credits_observed were the points paid
     135             : 
     136             :     https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/points.rs#L109 */
     137             : static void
     138             : calculate_stake_points_and_credits( fd_epoch_credits_t *           epoch_credits,
     139             :                                     fd_stake_history_t const *     stake_history,
     140             :                                     fd_stake_delegation_t const *  stake,
     141             :                                     ulong *                        new_rate_activation_epoch,
     142           9 :                                     fd_calculated_stake_points_t * result ) {
     143             : 
     144           9 :   ulong credits_in_stake = stake->credits_observed;
     145           9 :   ulong credits_cnt      = epoch_credits->cnt;
     146           9 :   ulong credits_in_vote  = credits_cnt > 0UL ? epoch_credits->credits[ credits_cnt - 1UL ] : 0UL;
     147             : 
     148             : 
     149             :   /* If the Vote account has less credits observed than the Stake account,
     150             :       something is wrong and we need to force an update.
     151             : 
     152             :       https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/points.rs#L142 */
     153           9 :   if( FD_UNLIKELY( credits_in_vote < credits_in_stake ) ) {
     154           0 :     result->points.ud = 0;
     155           0 :     result->new_credits_observed = credits_in_vote;
     156           0 :     result->force_credits_update_with_skipped_reward = 1;
     157           0 :     return;
     158           0 :   }
     159             : 
     160             :   /* If the Vote account has the same amount of credits observed as the Stake account,
     161             :       then the Vote account hasn't earnt any credits and so there is nothing to update.
     162             : 
     163             :       https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/points.rs#L148 */
     164           9 :   if( FD_UNLIKELY( credits_in_vote == credits_in_stake ) ) {
     165           9 :     result->points.ud = 0;
     166           9 :     result->new_credits_observed = credits_in_vote;
     167           9 :     result->force_credits_update_with_skipped_reward = 0;
     168           9 :     return;
     169           9 :   }
     170             : 
     171             :   /* Calculate the points for each epoch credit */
     172           0 :   uint128 points               = 0;
     173           0 :   ulong   new_credits_observed = credits_in_stake;
     174           0 :   for( ulong i=0UL; i<epoch_credits->cnt; i++ ) {
     175             : 
     176           0 :     ulong final_epoch_credits   = epoch_credits->credits[ i ];
     177           0 :     ulong initial_epoch_credits = epoch_credits->prev_credits[ i ];
     178             : 
     179             :     /* Vote account credits can only increase or stay the same, so
     180             :        initial_epoch_credits <= final_epoch_credits always holds. */
     181           0 :     FD_TEST( initial_epoch_credits<=final_epoch_credits );
     182             : 
     183             :     /* If final_epoch_credits <= credits_in_stake, then:
     184             :         initial_epoch_credits <= final_epoch_credits <= credits_in_stake
     185             : 
     186             :        * earned_credits = 0 since both conditions are false.
     187             :        * new_credits_observed stays the same since it is already set
     188             :          to credits_in_stake and final_epoch_credits <= credits_in_stake
     189             : 
     190             :        Since earned_credits = 0 and new_credits_observed stays the same,
     191             :        points computation can be skipped. */
     192           0 :     if( FD_LIKELY( final_epoch_credits<=credits_in_stake ) ) continue;
     193             : 
     194           0 :     uint128 earned_credits = 0;
     195           0 :     if( FD_LIKELY( credits_in_stake < initial_epoch_credits ) ) {
     196           0 :       earned_credits = (uint128)(final_epoch_credits - initial_epoch_credits);
     197           0 :     } else if( FD_UNLIKELY( credits_in_stake < final_epoch_credits ) ) {
     198           0 :       earned_credits = (uint128)(final_epoch_credits - new_credits_observed);
     199           0 :     }
     200             : 
     201           0 :     new_credits_observed = fd_ulong_max( new_credits_observed, final_epoch_credits );
     202             : 
     203           0 :     ulong stake_amount = fd_stakes_activating_and_deactivating(
     204           0 :         stake,
     205           0 :         epoch_credits->epoch[ i ],
     206           0 :         stake_history,
     207           0 :         new_rate_activation_epoch ).effective;
     208             : 
     209           0 :     points += (uint128)stake_amount * earned_credits;
     210           0 :   }
     211             : 
     212           0 :   result->points.ud = points;
     213           0 :   result->new_credits_observed = new_credits_observed;
     214           0 :   result->force_credits_update_with_skipped_reward = 0;
     215           0 : }
     216             : 
     217             : struct fd_commission_split {
     218             :   ulong voter_portion;
     219             :   ulong staker_portion;
     220             :   uint  is_split;
     221             : };
     222             : typedef struct fd_commission_split fd_commission_split_t;
     223             : 
     224             : /// returns commission split as (voter_portion, staker_portion, was_split) tuple
     225             : ///
     226             : /// if commission calculation is 100% one way or other, indicate with false for was_split
     227             : 
     228             : // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L543
     229             : void
     230             : fd_vote_commission_split( uchar                   commission,
     231             :                           ulong                   on,
     232           0 :                           fd_commission_split_t * result ) {
     233           0 :   uint commission_split = fd_uint_min( (uint)commission, 100 );
     234           0 :   result->is_split      = (commission_split != 0 && commission_split != 100);
     235             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L545
     236           0 :   if( commission_split==0U ) {
     237           0 :     result->voter_portion  = 0;
     238           0 :     result->staker_portion = on;
     239           0 :     return;
     240           0 :   }
     241             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L546
     242           0 :   if( commission_split==100U ) {
     243           0 :     result->voter_portion  = on;
     244           0 :     result->staker_portion = 0;
     245           0 :     return;
     246           0 :   }
     247             :   /* Note: order of operations may matter for int division. That's why I didn't make the
     248             :    * optimization of getting out the common calculations */
     249             : 
     250             :   // ... This is copied from the solana comments...
     251             :   //
     252             :   // Calculate mine and theirs independently and symmetrically instead
     253             :   // of using the remainder of the other to treat them strictly
     254             :   // equally. This is also to cancel the rewarding if either of the
     255             :   // parties should receive only fractional lamports, resulting in not
     256             :   // being rewarded at all. Thus, note that we intentionally discard
     257             :   // any residual fractional lamports.
     258             : 
     259             :   // https://github.com/anza-xyz/agave/blob/v2.0.1/sdk/program/src/vote/state/mod.rs#L548
     260           0 :   result->voter_portion =
     261           0 :       (ulong)((uint128)on * (uint128)commission_split / (uint128)100);
     262           0 :   result->staker_portion =
     263           0 :       (ulong)((uint128)on * (uint128)( 100 - commission_split ) / (uint128)100);
     264           0 : }
     265             : 
     266             : /* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/rewards.rs#L33 */
     267             : static int
     268             : redeem_rewards( fd_stake_delegation_t const *   stake,
     269             :                 ulong                           vote_state_idx,
     270             :                 ulong                           rewarded_epoch,
     271             :                 ulong                           total_rewards,
     272             :                 uint128                         total_points,
     273             :                 fd_runtime_stack_t *            runtime_stack,
     274             :                 fd_calculated_stake_points_t *  stake_points_result,
     275             :                 fd_calculated_stake_rewards_t * result,
     276           9 :                 int                             delay_commission_updates ) {
     277             : 
     278             :   /* The firedancer implementation of redeem_rewards inlines a lot of
     279             :      the helper functions that the Agave implementation uses.
     280             :      In Agave: redeem_rewards calls redeem_stake_rewards which calls
     281             :      calculate_stake_rewards. */
     282             : 
     283             :   // Drive credits_observed forward unconditionally when rewards are disabled
     284             :   // or when this is the stake's activation epoch
     285           9 :   if( total_rewards==0UL || stake->activation_epoch==rewarded_epoch ) {
     286           9 :       stake_points_result->force_credits_update_with_skipped_reward = 1;
     287           9 :   }
     288             : 
     289           9 :   if( stake_points_result->force_credits_update_with_skipped_reward ) {
     290           9 :     result->staker_rewards       = 0;
     291           9 :     result->voter_rewards        = 0;
     292           9 :     result->new_credits_observed = stake_points_result->new_credits_observed;
     293           9 :     return 0;
     294           9 :   }
     295           0 :   if( stake_points_result->points.ud==0 || total_points==0 ) {
     296           0 :     return 1;
     297           0 :   }
     298             : 
     299           0 :   uint128 rewards_u128;
     300           0 :   if( FD_UNLIKELY( __builtin_mul_overflow( stake_points_result->points.ud, (uint128)(total_rewards), &rewards_u128 ) ) ) {
     301           0 :     FD_LOG_ERR(( "Rewards intermediate calculation should fit within u128" ));
     302           0 :   }
     303             : 
     304           0 :   FD_TEST( total_points );
     305           0 :   rewards_u128 /=  (uint128) total_points;
     306             : 
     307           0 :   if( FD_UNLIKELY( rewards_u128>(uint128)ULONG_MAX ) ) {
     308           0 :     FD_LOG_ERR(( "Rewards should fit within u64" ));
     309           0 :   }
     310             : 
     311           0 :   ulong rewards = (ulong)rewards_u128;
     312           0 :   if( rewards == 0 ) {
     313           0 :     return 1;
     314           0 :   }
     315             : 
     316           0 :   uchar commission = delay_commission_updates ? runtime_stack->stakes.vote_ele[ vote_state_idx ].commission_t_2 : runtime_stack->stakes.vote_ele[ vote_state_idx ].commission_t_1;
     317           0 :   fd_commission_split_t split_result;
     318           0 :   fd_vote_commission_split( commission, rewards, &split_result );
     319           0 :   if( split_result.is_split && (split_result.voter_portion == 0 || split_result.staker_portion == 0) ) {
     320           0 :     return 1;
     321           0 :   }
     322             : 
     323           0 :   result->staker_rewards       = split_result.staker_portion;
     324           0 :   result->voter_rewards        = split_result.voter_portion;
     325           0 :   result->new_credits_observed = stake_points_result->new_credits_observed;
     326           0 :   return 0;
     327           0 : }
     328             : 
     329             : /* Returns the length of the given epoch in slots
     330             : 
     331             :    https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/sdk/program/src/epoch_schedule.rs#L103 */
     332             : static ulong
     333             : get_slots_in_epoch( ulong                       epoch,
     334          24 :                     fd_epoch_schedule_t const * epoch_schedule ) {
     335          24 :   return epoch < epoch_schedule->first_normal_epoch ?
     336           0 :          1UL << fd_ulong_sat_add( epoch, FD_EPOCH_LEN_MIN_TRAILING_ZERO ) :
     337          24 :          epoch_schedule->slots_per_epoch;
     338          24 : }
     339             : 
     340             : /* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank.rs#L2082 */
     341             : static double
     342             : epoch_duration_in_years( fd_bank_t const * bank,
     343           9 :                          ulong             prev_epoch ) {
     344           9 :   ulong slots_in_epoch = get_slots_in_epoch( prev_epoch, &bank->f.epoch_schedule );
     345           9 :   return (double)slots_in_epoch / (double)bank->f.slots_per_year;
     346           9 : }
     347             : 
     348             : /* https://github.com/anza-xyz/agave/blob/7117ed9653ce19e8b2dea108eff1f3eb6a3378a7/runtime/src/bank.rs#L2128 */
     349             : static void
     350             : calculate_previous_epoch_inflation_rewards( fd_bank_t const *                   bank,
     351             :                                             ulong                               prev_epoch_capitalization,
     352             :                                             ulong                               prev_epoch,
     353           9 :                                             fd_prev_epoch_inflation_rewards_t * rewards ) {
     354           9 :   double slot_in_year = slot_in_year_for_inflation( bank );
     355             : 
     356           9 :   rewards->validator_rate               = validator( &bank->f.inflation, slot_in_year );
     357           9 :   rewards->foundation_rate              = foundation( &bank->f.inflation, slot_in_year );
     358           9 :   rewards->prev_epoch_duration_in_years = epoch_duration_in_years( bank, prev_epoch );
     359           9 :   rewards->validator_rewards            = (ulong)(rewards->validator_rate * (double)prev_epoch_capitalization * rewards->prev_epoch_duration_in_years);
     360           9 :   FD_LOG_DEBUG(( "Rewards %lu, Rate %.16f, Duration %.18f Capitalization %lu Slot in year %.16f", rewards->validator_rewards, rewards->validator_rate, rewards->prev_epoch_duration_in_years, prev_epoch_capitalization, slot_in_year ));
     361           9 : }
     362             : 
     363             : /* https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/programs/stake/src/lib.rs#L29 */
     364             : static ulong
     365          18 : get_minimum_stake_delegation( fd_bank_t * bank ) {
     366          18 :   if( !FD_FEATURE_ACTIVE_BANK( bank, stake_minimum_delegation_for_rewards ) ) {
     367          18 :     return 0UL;
     368          18 :   }
     369             : 
     370           0 :   if( FD_FEATURE_ACTIVE_BANK( bank, stake_raise_minimum_delegation_to_1_sol ) ) {
     371           0 :     return LAMPORTS_PER_SOL;
     372           0 :   }
     373             : 
     374           0 :   return 1;
     375           0 : }
     376             : 
     377             : /* Calculate the number of blocks required to distribute rewards to all stake accounts.
     378             : 
     379             :     https://github.com/anza-xyz/agave/blob/9a7bf72940f4b3cd7fc94f54e005868ce707d53d/runtime/src/bank/partitioned_epoch_rewards/mod.rs#L214
     380             :  */
     381             :  static uint
     382             :  get_reward_distribution_num_blocks( fd_epoch_schedule_t const * epoch_schedule,
     383             :                                      ulong                       slot,
     384           9 :                                      ulong                       total_stake_accounts ) {
     385             :    /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1250-L1267 */
     386           9 :    if( epoch_schedule->warmup &&
     387           9 :        fd_slot_to_epoch( epoch_schedule, slot, NULL ) < epoch_schedule->first_normal_epoch ) {
     388           0 :      return 1UL;
     389           0 :    }
     390             : 
     391           9 :    ulong num_chunks = total_stake_accounts / (ulong)STAKE_ACCOUNT_STORES_PER_BLOCK + (total_stake_accounts % STAKE_ACCOUNT_STORES_PER_BLOCK != 0);
     392           9 :    num_chunks       = fd_ulong_max( num_chunks, 1UL );
     393           9 :    num_chunks       = fd_ulong_min( num_chunks,
     394           9 :                                     fd_ulong_max( epoch_schedule->slots_per_epoch / (ulong)MAX_FACTOR_OF_REWARD_BLOCKS_IN_EPOCH, 1UL ) );
     395           9 :    return (uint)num_chunks;
     396           9 :  }
     397             : 
     398             : /* Calculates epoch reward points from stake/vote accounts.
     399             :    https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L445 */
     400             : static uint128
     401             : calculate_reward_points_partitioned( fd_accdb_user_t *              accdb,
     402             :                                      fd_funk_txn_xid_t const *      xid,
     403             :                                      fd_bank_t *                    bank,
     404             :                                      fd_stake_delegations_t const * stake_delegations,
     405             :                                      fd_stake_history_t const *     stake_history,
     406           9 :                                      fd_runtime_stack_t *           runtime_stack ) {
     407           9 :   ulong minimum_stake_delegation = get_minimum_stake_delegation( bank );
     408             : 
     409             :   /* Calculate the points for each stake delegation */
     410           9 :   uint128 total_points = 0;
     411             : 
     412           9 :   fd_vote_rewards_t *     vote_ele     = runtime_stack->stakes.vote_ele;
     413           9 :   fd_vote_rewards_map_t * vote_ele_map = runtime_stack->stakes.vote_map;
     414             : 
     415           9 :   fd_stake_delegations_iter_t iter_[1];
     416           9 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     417          18 :        !fd_stake_delegations_iter_done( iter );
     418           9 :        fd_stake_delegations_iter_next( iter ) ) {
     419           9 :     fd_stake_delegation_t const * stake_delegation     = fd_stake_delegations_iter_ele( iter );
     420           9 :     ulong                         stake_delegation_idx = fd_stake_delegations_iter_idx( iter );
     421             : 
     422           9 :     if( FD_UNLIKELY( stake_delegation->stake<minimum_stake_delegation ) ) {
     423           0 :       continue;
     424           0 :     }
     425             : 
     426           9 :     uint idx = (uint)fd_vote_rewards_map_idx_query( vote_ele_map, &stake_delegation->vote_account, UINT_MAX, vote_ele );
     427           9 :     if( FD_UNLIKELY( idx==UINT_MAX ) ) continue;
     428             : 
     429           9 :     fd_calculated_stake_points_t   stake_points_result_[1];
     430           9 :     fd_calculated_stake_points_t * stake_points_result;
     431           9 :     if( FD_UNLIKELY( stake_delegation_idx>=runtime_stack->expected_stake_accounts ) ) {
     432           0 :       stake_points_result = stake_points_result_;
     433           9 :     } else {
     434           9 :       stake_points_result = &runtime_stack->stakes.stake_points_result[ stake_delegation_idx ];
     435           9 :     }
     436             : 
     437           9 :     fd_epoch_credits_t   epoch_credits_;
     438           9 :     fd_epoch_credits_t * epoch_credits = NULL;
     439           9 :     if( idx>=runtime_stack->expected_vote_accounts ) {
     440           0 :       fd_vote_rewards_t * vote_ele = &runtime_stack->stakes.vote_ele[ idx ];
     441           0 :       fd_accdb_ro_t vote_ro[1];
     442           0 :       FD_TEST( fd_accdb_open_ro( accdb, vote_ro, xid, &vote_ele->pubkey ) );
     443             : 
     444           0 :       uchar __attribute__((aligned(128))) vsv_buf[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ];
     445           0 :       get_credits( fd_accdb_ref_data_const( vote_ro ), fd_accdb_ref_data_sz( vote_ro ), vsv_buf, &epoch_credits_ );
     446           0 :       fd_accdb_close_ro( accdb, vote_ro );
     447           0 :       epoch_credits = &epoch_credits_;
     448           9 :     } else {
     449           9 :       epoch_credits = &runtime_stack->stakes.epoch_credits[ idx ];
     450           9 :     }
     451             : 
     452           9 :     calculate_stake_points_and_credits( epoch_credits,
     453           9 :                                         stake_history,
     454           9 :                                         stake_delegation,
     455           9 :                                         &bank->f.warmup_cooldown_rate_epoch,
     456           9 :                                         stake_points_result );
     457             : 
     458           9 :     total_points += stake_points_result->points.ud;
     459           9 :   }
     460             : 
     461           9 :   return total_points;
     462           9 : }
     463             : 
     464             : /* Calculates epoch rewards for stake/vote accounts.
     465             :    Returns vote rewards, stake rewards, and the sum of all stake rewards
     466             :    in lamports.
     467             : 
     468             :    In the future, the calculation will be cached in the snapshot, but
     469             :    for now we just re-calculate it (as Agave does).
     470             :    calculate_stake_vote_rewards is responsible for calculating
     471             :    stake account rewards based off of a combination of the
     472             :    stake delegation state as well as the vote account. If this
     473             :    calculation is done at the end of an epoch, we can just use the
     474             :    vote states at the end of the current epoch. However, because we
     475             :    are presumably booting up a node in the middle of rewards
     476             :    distribution, we need to make sure that we are using the vote
     477             :    states from the end of the previous epoch.
     478             : 
     479             :    https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L323 */
     480             : static void
     481             : calculate_stake_vote_rewards( fd_accdb_user_t *              accdb,
     482             :                               fd_funk_txn_xid_t const *      xid,
     483             :                               fd_bank_t *                    bank,
     484             :                               fd_stake_delegations_t const * stake_delegations,
     485             :                               fd_capture_ctx_t *             capture_ctx FD_PARAM_UNUSED,
     486             :                               fd_stake_history_t const *     stake_history,
     487             :                               ulong                          rewarded_epoch,
     488             :                               ulong                          total_rewards,
     489             :                               uint128                        total_points,
     490             :                               fd_runtime_stack_t *           runtime_stack,
     491           9 :                               int                            is_recalculation ) {
     492             : 
     493           9 :   int delay_commission_updates = FD_FEATURE_ACTIVE_BANK( bank, delay_commission_updates );
     494             : 
     495           9 :   ulong minimum_stake_delegation = get_minimum_stake_delegation( bank );
     496             : 
     497           9 :   runtime_stack->stakes.stake_rewards_cnt = 0UL;
     498             : 
     499           9 :   fd_calculated_stake_rewards_t calculated_stake_rewards_[1];
     500             : 
     501           9 :   uchar __attribute__((aligned(128))) vsv_buf[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ];
     502             : 
     503           9 :   fd_stake_delegations_iter_t iter_[1];
     504           9 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     505          18 :        !fd_stake_delegations_iter_done( iter );
     506           9 :        fd_stake_delegations_iter_next( iter ) ) {
     507           9 :     fd_stake_delegation_t const * stake_delegation     = fd_stake_delegations_iter_ele( iter );
     508           9 :     ulong                         stake_delegation_idx = fd_stake_delegations_iter_idx( iter );
     509             : 
     510           9 :     if( FD_FEATURE_ACTIVE_BANK( bank, stake_minimum_delegation_for_rewards ) ) {
     511           0 :       if( stake_delegation->stake<minimum_stake_delegation ) {
     512           0 :         continue;
     513           0 :       }
     514           0 :     }
     515             : 
     516           9 :     fd_calculated_stake_rewards_t * calculated_stake_rewards = NULL;
     517           9 :     if( stake_delegation_idx>=runtime_stack->expected_stake_accounts ) {
     518           0 :       calculated_stake_rewards = calculated_stake_rewards_;
     519           9 :     } else {
     520           9 :       calculated_stake_rewards = &runtime_stack->stakes.stake_rewards_result[ stake_delegation_idx ];
     521           9 :     }
     522           9 :     calculated_stake_rewards->success = 0;
     523             : 
     524           9 :     fd_vote_rewards_t * vote_ele = runtime_stack->stakes.vote_ele;
     525           9 :     fd_vote_rewards_map_t * vote_ele_map = runtime_stack->stakes.vote_map;
     526           9 :     uint idx = (uint)fd_vote_rewards_map_idx_query( vote_ele_map, &stake_delegation->vote_account, UINT_MAX, vote_ele );
     527           9 :     if( FD_UNLIKELY( idx==UINT_MAX ) ) continue;
     528             : 
     529           9 :     fd_calculated_stake_points_t   stake_points_result_[1];
     530           9 :     fd_calculated_stake_points_t * stake_points_result;
     531           9 :     if( is_recalculation || FD_UNLIKELY( stake_delegation_idx>=runtime_stack->expected_stake_accounts ) ) {
     532           0 :       fd_vote_rewards_t * vote_ele = &runtime_stack->stakes.vote_ele[ idx ];
     533             : 
     534           0 :       fd_epoch_credits_t   epoch_credits_;
     535           0 :       fd_epoch_credits_t * epoch_credits = NULL;
     536           0 :       if( idx<runtime_stack->expected_vote_accounts ) {
     537           0 :         epoch_credits = &runtime_stack->stakes.epoch_credits[ idx ];
     538           0 :       } else {
     539           0 :         fd_accdb_ro_t vote_ro[1];
     540           0 :         FD_TEST( fd_accdb_open_ro( accdb, vote_ro, xid, &vote_ele->pubkey ) );
     541           0 :         get_credits( fd_accdb_ref_data_const( vote_ro ), fd_accdb_ref_data_sz( vote_ro ), vsv_buf, &epoch_credits_ );
     542           0 :         fd_accdb_close_ro( accdb, vote_ro );
     543           0 :         epoch_credits = &epoch_credits_;
     544           0 :       }
     545             : 
     546             :       /* We have not cached the stake points yet if we are recalculating
     547             :          stake rewards so we need to recalculate them. */
     548           0 :       calculate_stake_points_and_credits(
     549           0 :           epoch_credits,
     550           0 :           stake_history,
     551           0 :           stake_delegation,
     552           0 :           &bank->f.warmup_cooldown_rate_epoch,
     553           0 :           stake_points_result_ );
     554           0 :       stake_points_result = stake_points_result_;
     555           9 :     } else {
     556           9 :       stake_points_result = &runtime_stack->stakes.stake_points_result[ stake_delegation_idx ];
     557           9 :     }
     558             : 
     559             :     /* redeem_rewards is actually just responsible for calculating the
     560             :        vote and stake rewards for each stake account.  It does not do
     561             :        rewards redemption: it is a misnomer. */
     562           9 :     int err = redeem_rewards(
     563           9 :         stake_delegation,
     564           9 :         idx,
     565           9 :         rewarded_epoch,
     566           9 :         total_rewards,
     567           9 :         total_points,
     568           9 :         runtime_stack,
     569           9 :         stake_points_result,
     570           9 :         calculated_stake_rewards,
     571           9 :         delay_commission_updates );
     572             : 
     573           9 :     if( FD_UNLIKELY( err!=0 ) ) {
     574           0 :       continue;
     575           0 :     }
     576             : 
     577           9 :     calculated_stake_rewards->success = 1;
     578             : 
     579           9 :     if( capture_ctx && capture_ctx->capture_solcap ) {
     580           0 :       uchar commission = delay_commission_updates ? runtime_stack->stakes.vote_ele[ idx ].commission_t_2 : runtime_stack->stakes.vote_ele[ idx ].commission_t_1;
     581           0 :       fd_capture_link_write_stake_reward_event( capture_ctx,
     582           0 :                                                 bank->f.slot,
     583           0 :                                                 stake_delegation->stake_account,
     584           0 :                                                 stake_delegation->vote_account,
     585           0 :                                                 commission,
     586           0 :                                                 (long)calculated_stake_rewards->voter_rewards,
     587           0 :                                                 (long)calculated_stake_rewards->staker_rewards,
     588           0 :                                                 (long)calculated_stake_rewards->new_credits_observed );
     589           0 :     }
     590             : 
     591           9 :     runtime_stack->stakes.vote_ele[ idx ].vote_rewards += calculated_stake_rewards->voter_rewards;
     592           9 :     runtime_stack->stakes.stake_rewards_cnt++;
     593           9 :   }
     594           9 : }
     595             : 
     596             : static void
     597             : setup_stake_partitions( fd_accdb_user_t *              accdb,
     598             :                         fd_funk_txn_xid_t const *      xid,
     599             :                         fd_bank_t *                    bank,
     600             :                         fd_stake_history_t const *     stake_history,
     601             :                         fd_stake_delegations_t const * stake_delegations,
     602             :                         fd_runtime_stack_t *           runtime_stack,
     603             :                         fd_hash_t const *              parent_blockhash,
     604             :                         ulong                          starting_block_height,
     605             :                         uint                           num_partitions,
     606             :                         ulong                          rewarded_epoch,
     607             :                         ulong                          total_rewards,
     608           9 :                         uint128                        total_points ) {
     609             : 
     610           9 :   fd_stake_rewards_t * stake_rewards = fd_bank_stake_rewards_modify( bank );
     611           9 :   uchar fork_idx = fd_stake_rewards_init( stake_rewards, bank->f.epoch, parent_blockhash, starting_block_height, (uint)num_partitions );
     612           9 :   bank->stake_rewards_fork_id = fork_idx;
     613             : 
     614           9 :   uchar __attribute__((aligned(128))) vsv_buf[ FD_VOTE_STATE_VERSIONED_FOOTPRINT ];
     615             : 
     616           9 :   int delay_commission_updates = FD_FEATURE_ACTIVE_BANK( bank, delay_commission_updates );
     617             : 
     618           9 :   fd_stake_delegations_iter_t iter_[1];
     619           9 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     620          18 :        !fd_stake_delegations_iter_done( iter );
     621           9 :        fd_stake_delegations_iter_next( iter ) ) {
     622           9 :     fd_stake_delegation_t const * stake_delegation     = fd_stake_delegations_iter_ele( iter );
     623           9 :     ulong                         stake_delegation_idx = fd_stake_delegations_iter_idx( iter );
     624             : 
     625           9 :     fd_calculated_stake_rewards_t calculated_stake_rewards_[1];
     626           9 :     fd_calculated_stake_rewards_t * calculated_stake_rewards = NULL;
     627             : 
     628           9 :     if( FD_UNLIKELY( stake_delegation_idx>=runtime_stack->expected_stake_accounts ) ) {
     629             : 
     630           0 :       calculated_stake_rewards = calculated_stake_rewards_;
     631             : 
     632           0 :       fd_vote_rewards_t * vote_ele = runtime_stack->stakes.vote_ele;
     633           0 :       fd_vote_rewards_map_t * vote_ele_map = runtime_stack->stakes.vote_map;
     634           0 :       uint idx = (uint)fd_vote_rewards_map_idx_query( vote_ele_map, &stake_delegation->vote_account, UINT_MAX, vote_ele );
     635           0 :       if( FD_UNLIKELY( idx==UINT_MAX ) ) continue;
     636             : 
     637           0 :       fd_epoch_credits_t   epoch_credits_;
     638           0 :       fd_epoch_credits_t * epoch_credits = NULL;
     639           0 :       if( idx>=runtime_stack->expected_vote_accounts ) {
     640           0 :         fd_vote_rewards_t * vote_ele = &runtime_stack->stakes.vote_ele[ idx ];
     641           0 :         fd_accdb_ro_t vote_ro[1];
     642           0 :         FD_TEST( fd_accdb_open_ro( accdb, vote_ro, xid, &vote_ele->pubkey ) );
     643           0 :         get_credits( fd_accdb_ref_data_const( vote_ro ), fd_accdb_ref_data_sz( vote_ro ), vsv_buf, &epoch_credits_ );
     644           0 :         fd_accdb_close_ro( accdb, vote_ro );
     645           0 :         epoch_credits = &epoch_credits_;
     646           0 :       } else {
     647           0 :         epoch_credits = &runtime_stack->stakes.epoch_credits[ idx ];
     648           0 :       }
     649             : 
     650           0 :       fd_calculated_stake_points_t stake_points_result[1];
     651           0 :       calculate_stake_points_and_credits(
     652           0 :           epoch_credits,
     653           0 :           stake_history,
     654           0 :           stake_delegation,
     655           0 :           &bank->f.warmup_cooldown_rate_epoch,
     656           0 :           stake_points_result );
     657             : 
     658             :       /* redeem_rewards is actually just responsible for calculating the
     659             :          vote and stake rewards for each stake account.  It does not do
     660             :          rewards redemption: it is a misnomer. */
     661           0 :       int err = redeem_rewards(
     662           0 :           stake_delegation,
     663           0 :           idx,
     664           0 :           rewarded_epoch,
     665           0 :           total_rewards,
     666           0 :           total_points,
     667           0 :           runtime_stack,
     668           0 :           stake_points_result,
     669           0 :           calculated_stake_rewards,
     670           0 :           delay_commission_updates );
     671           0 :       calculated_stake_rewards->success = err==0;
     672           9 :     } else {
     673           9 :       calculated_stake_rewards = &runtime_stack->stakes.stake_rewards_result[ stake_delegation_idx ];
     674           9 :     }
     675             : 
     676           9 :     if( FD_UNLIKELY( !calculated_stake_rewards->success ) ) continue;
     677             : 
     678           9 :     fd_stake_rewards_insert(
     679           9 :       stake_rewards,
     680           9 :       fork_idx,
     681           9 :       &stake_delegation->stake_account,
     682           9 :       calculated_stake_rewards->staker_rewards,
     683           9 :       calculated_stake_rewards->new_credits_observed
     684           9 :     );
     685           9 :   }
     686           9 : }
     687             : 
     688             : /* Calculate epoch reward and return vote and stake rewards.
     689             : 
     690             :    https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L273 */
     691             : static uint128
     692             : calculate_validator_rewards( fd_bank_t *                    bank,
     693             :                              fd_accdb_user_t *              accdb,
     694             :                              fd_funk_txn_xid_t const *      xid,
     695             :                              fd_runtime_stack_t *           runtime_stack,
     696             :                              fd_stake_delegations_t const * stake_delegations,
     697             :                              fd_capture_ctx_t *             capture_ctx,
     698             :                              ulong                          rewarded_epoch,
     699           9 :                              ulong *                        rewards_out ) {
     700             : 
     701           9 :   fd_stake_history_t stake_history[1];
     702           9 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, xid, stake_history ) ) ) {
     703           0 :     FD_LOG_ERR(( "Unable to read and decode stake history sysvar" ));
     704           0 :   }
     705             : 
     706             :   /* Calculate the epoch reward points from stake/vote accounts */
     707           9 :   uint128 total_points = calculate_reward_points_partitioned(
     708           9 :       accdb,
     709           9 :       xid,
     710           9 :       bank,
     711           9 :       stake_delegations,
     712           9 :       stake_history,
     713           9 :       runtime_stack );
     714             : 
     715             :   /* If there are no points, then we set the rewards to 0. */
     716           9 :   *rewards_out = total_points>0UL ? *rewards_out: 0UL;
     717             : 
     718           9 :   if( capture_ctx && capture_ctx->capture_solcap ) {
     719           0 :     ulong epoch = bank->f.epoch;
     720           0 :     ulong slot  = bank->f.slot;
     721           0 :     fd_capture_link_write_stake_rewards_begin( capture_ctx,
     722           0 :                                                slot,
     723           0 :                                                epoch,
     724           0 :                                                epoch-1UL, /* FIXME: this is not strictly correct */
     725           0 :                                                *rewards_out,
     726           0 :                                                (ulong)total_points );
     727           0 :   }
     728             : 
     729             :   /* Calculate the stake and vote rewards for each account. We want to
     730             :      use the vote states from the end of the current_epoch. */
     731           9 :   calculate_stake_vote_rewards(
     732           9 :       accdb,
     733           9 :       xid,
     734           9 :       bank,
     735           9 :       stake_delegations,
     736           9 :       capture_ctx,
     737           9 :       stake_history,
     738           9 :       rewarded_epoch,
     739           9 :       *rewards_out,
     740           9 :       total_points,
     741           9 :       runtime_stack,
     742           9 :       0 );
     743             : 
     744           9 :   fd_hash_t const * parent_blockhash      = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
     745           9 :   ulong             starting_block_height = bank->f.block_height + REWARD_CALCULATION_NUM_BLOCKS;
     746           9 :   uint              num_partitions        = get_reward_distribution_num_blocks( &bank->f.epoch_schedule,
     747           9 :                                                                                 bank->f.slot,
     748           9 :                                                                                 runtime_stack->stakes.stake_rewards_cnt );
     749             : 
     750           9 :   setup_stake_partitions(
     751           9 :       accdb,
     752           9 :       xid,
     753           9 :       bank,
     754           9 :       stake_history,
     755           9 :       stake_delegations,
     756           9 :       runtime_stack,
     757           9 :       parent_blockhash,
     758           9 :       starting_block_height,
     759           9 :       num_partitions,
     760           9 :       rewarded_epoch,
     761           9 :       *rewards_out,
     762           9 :       total_points );
     763             : 
     764           9 :   return total_points;
     765           9 : }
     766             : 
     767             : /* Calculate rewards from previous epoch to prepare for partitioned distribution.
     768             : 
     769             :    https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L277 */
     770             : static void
     771             : calculate_rewards_for_partitioning( fd_bank_t *                            bank,
     772             :                                     fd_accdb_user_t *                      accdb,
     773             :                                     fd_funk_txn_xid_t const *              xid,
     774             :                                     fd_runtime_stack_t *                   runtime_stack,
     775             :                                     fd_stake_delegations_t const *         stake_delegations,
     776             :                                     fd_capture_ctx_t *                     capture_ctx,
     777             :                                     ulong                                  prev_epoch,
     778           9 :                                     fd_partitioned_rewards_calculation_t * result ) {
     779           9 :   fd_prev_epoch_inflation_rewards_t rewards;
     780             : 
     781           9 :   calculate_previous_epoch_inflation_rewards( bank,
     782           9 :                                               bank->f.capitalization,
     783           9 :                                               prev_epoch,
     784           9 :                                               &rewards );
     785             : 
     786           9 :   ulong total_rewards = rewards.validator_rewards;
     787             : 
     788           9 :   uint128 points = calculate_validator_rewards( bank,
     789           9 :                                                 accdb,
     790           9 :                                                 xid,
     791           9 :                                                 runtime_stack,
     792           9 :                                                 stake_delegations,
     793           9 :                                                 capture_ctx,
     794           9 :                                                 prev_epoch,
     795           9 :                                                 &total_rewards );
     796             : 
     797             :   /* The agave client does not partition the stake rewards until the
     798             :      first distribution block.  We calculate the partitions during the
     799             :      boundary. */
     800           9 :   result->validator_points             = points;
     801           9 :   result->validator_rewards            = total_rewards;
     802           9 :   result->validator_rate               = rewards.validator_rate;
     803           9 :   result->foundation_rate              = rewards.foundation_rate;
     804           9 :   result->prev_epoch_duration_in_years = rewards.prev_epoch_duration_in_years;
     805           9 :   result->capitalization               = bank->f.capitalization;
     806           9 : }
     807             : 
     808             : /* Calculate rewards from previous epoch and distribute vote rewards
     809             :    https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L148 */
     810             : static void
     811             : calculate_rewards_and_distribute_vote_rewards( fd_bank_t *                    bank,
     812             :                                                fd_accdb_user_t *              accdb,
     813             :                                                fd_funk_txn_xid_t const *      xid,
     814             :                                                fd_runtime_stack_t *           runtime_stack,
     815             :                                                fd_stake_delegations_t const * stake_delegations,
     816             :                                                fd_capture_ctx_t *             capture_ctx,
     817           9 :                                                ulong                          prev_epoch ) {
     818             : 
     819           9 :   fd_vote_rewards_t *     vote_ele_pool = runtime_stack->stakes.vote_ele;
     820           9 :   fd_vote_rewards_map_t * vote_ele_map  = runtime_stack->stakes.vote_map;
     821             : 
     822             :   /* First we must compute the stake and vote rewards for the just
     823             :      completed epoch.  We store the stake account rewards and vote
     824             :      states rewards in the bank */
     825             : 
     826           9 :   fd_partitioned_rewards_calculation_t rewards_calc_result[1] = {0};
     827           9 :   calculate_rewards_for_partitioning( bank,
     828           9 :                                       accdb,
     829           9 :                                       xid,
     830           9 :                                       runtime_stack,
     831           9 :                                       stake_delegations,
     832           9 :                                       capture_ctx,
     833           9 :                                       prev_epoch,
     834           9 :                                       rewards_calc_result );
     835             : 
     836             : 
     837             :   /* Iterate over all the vote reward nodes and distribute the rewards
     838             :      to the vote accounts.  After each reward has been paid out,
     839             :      calcualte the lthash for each vote account. */
     840           9 :   ulong distributed_rewards = 0UL;
     841           9 :   for( fd_vote_rewards_map_iter_t iter = fd_vote_rewards_map_iter_init( vote_ele_map, vote_ele_pool );
     842          18 :        !fd_vote_rewards_map_iter_done( iter, vote_ele_map, vote_ele_pool );
     843           9 :        iter = fd_vote_rewards_map_iter_next( iter, vote_ele_map, vote_ele_pool ) ) {
     844             : 
     845           9 :     uint idx = (uint)fd_vote_rewards_map_iter_idx( iter, vote_ele_map, vote_ele_pool );
     846           9 :     fd_vote_rewards_t * ele = &vote_ele_pool[idx];
     847             : 
     848           9 :     ulong rewards = runtime_stack->stakes.vote_ele[ idx ].vote_rewards;
     849           9 :     if( rewards==0UL ) {
     850           9 :       continue;
     851           9 :     }
     852             : 
     853             :     /* Credit rewards to vote account (creating a new system account if
     854             :        it does not exist) */
     855           0 :     fd_pubkey_t const * vote_pubkey = &ele->pubkey;
     856           0 :     fd_accdb_rw_t rw[1];
     857           0 :     fd_accdb_open_rw( accdb, rw, xid, vote_pubkey, 0UL, FD_ACCDB_FLAG_CREATE );
     858           0 :     fd_lthash_value_t prev_hash[1];
     859           0 :     fd_hashes_account_lthash( vote_pubkey, rw->meta, fd_accdb_ref_data_const( rw->ro ), prev_hash );
     860           0 :     ulong acc_lamports = fd_accdb_ref_lamports( rw->ro );
     861           0 :     if( FD_UNLIKELY( __builtin_uaddl_overflow( acc_lamports, rewards, &acc_lamports ) ) ) {
     862           0 :       FD_BASE58_ENCODE_32_BYTES( vote_pubkey->key, addr_b58 );
     863           0 :       FD_LOG_EMERG(( "integer overflow while crediting %lu vote reward lamports to %s (previous balance %lu)",
     864           0 :                      rewards, addr_b58, fd_accdb_ref_lamports( rw->ro ) ));
     865           0 :     }
     866           0 :     fd_accdb_ref_lamports_set( rw, acc_lamports );
     867           0 :     fd_hashes_update_lthash( vote_pubkey, rw->meta, prev_hash,bank, capture_ctx );
     868           0 :     fd_accdb_close_rw( accdb, rw );
     869             : 
     870           0 :     distributed_rewards = fd_ulong_sat_add( distributed_rewards, rewards );
     871           0 :   }
     872             : 
     873             :   /* Verify that we didn't pay any more than we expected to */
     874           9 :   fd_stake_rewards_t * stake_rewards = fd_bank_stake_rewards_modify( bank );
     875           9 :   ulong total_stake_rewards = fd_stake_rewards_total_rewards( stake_rewards, bank->stake_rewards_fork_id );
     876             : 
     877           9 :   ulong total_rewards = fd_ulong_sat_add( distributed_rewards, total_stake_rewards );
     878           9 :   if( FD_UNLIKELY( rewards_calc_result->validator_rewards<total_rewards ) ) {
     879           0 :     FD_LOG_CRIT(( "Unexpected rewards calculation result" ));
     880           0 :   }
     881             : 
     882           9 :   bank->f.capitalization = bank->f.capitalization + distributed_rewards;
     883             : 
     884           9 :   runtime_stack->stakes.distributed_rewards = distributed_rewards;
     885           9 :   runtime_stack->stakes.total_rewards       = rewards_calc_result->validator_rewards;
     886           9 :   runtime_stack->stakes.total_points.ud     = rewards_calc_result->validator_points;
     887           9 : }
     888             : 
     889             : /* Distributes a single partitioned reward to a single stake account */
     890             : static int
     891             : distribute_epoch_reward_to_stake_acc( fd_bank_t *               bank,
     892             :                                       fd_accdb_user_t *         accdb,
     893             :                                       fd_funk_txn_xid_t const * xid,
     894             :                                       fd_capture_ctx_t *        capture_ctx,
     895             :                                       fd_pubkey_t *             stake_pubkey,
     896             :                                       ulong                     reward_lamports,
     897           6 :                                       ulong                     new_credits_observed ) {
     898             : 
     899           6 :   fd_accdb_rw_t rw[1];
     900           6 :   if( FD_UNLIKELY( !fd_accdb_open_rw( accdb, rw, xid, stake_pubkey, 0UL, 0 ) ) ) {
     901           0 :     return 1;  /* account does not exist */
     902           0 :   }
     903             : 
     904           6 :   fd_lthash_value_t prev_hash[1];
     905           6 :   fd_hashes_account_lthash( stake_pubkey, rw->meta, fd_accdb_ref_data_const( rw->ro ), prev_hash );
     906           6 :   fd_stake_state_t const * stake_state_orig = fd_stakes_get_state( rw->meta );
     907           6 :   if( !stake_state_orig || stake_state_orig->stake_type != FD_STAKE_STATE_STAKE ) {
     908           0 :     fd_accdb_close_rw( accdb, rw );
     909           0 :     return 1;  /* not a valid stake account */
     910           0 :   }
     911           6 :   fd_stake_state_t stake_state[1] = { *stake_state_orig };
     912             : 
     913             :   /* Credit rewards to stake account */
     914           6 :   ulong acc_lamports = fd_accdb_ref_lamports( rw->ro );
     915           6 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( acc_lamports, reward_lamports, &acc_lamports ) ) ) {
     916           0 :     FD_BASE58_ENCODE_32_BYTES( stake_pubkey->key, addr_b58 );
     917           0 :     FD_LOG_EMERG(( "integer overflow while crediting %lu stake reward lamports to %s (previous balance %lu)",
     918           0 :                     reward_lamports, addr_b58, fd_accdb_ref_lamports( rw->ro ) ));
     919           0 :   }
     920           6 :   fd_accdb_ref_lamports_set( rw, acc_lamports );
     921             : 
     922           6 :   ulong old_credits_observed                = stake_state->stake.stake.credits_observed;
     923           6 :   stake_state->stake.stake.credits_observed = new_credits_observed;
     924           6 :   stake_state->stake.stake.delegation.stake = fd_ulong_sat_add( stake_state->stake.stake.delegation.stake, reward_lamports );
     925             : 
     926           6 :   fd_stake_delegations_t * stake_delegations_upd = fd_bank_stake_delegations_modify( bank );
     927           6 :   fd_stake_delegations_fork_update( stake_delegations_upd,
     928           6 :                                     bank->stake_delegations_fork_id,
     929           6 :                                     stake_pubkey,
     930           6 :                                     &stake_state->stake.stake.delegation.voter_pubkey,
     931           6 :                                     stake_state->stake.stake.delegation.stake,
     932           6 :                                     stake_state->stake.stake.delegation.activation_epoch,
     933           6 :                                     stake_state->stake.stake.delegation.deactivation_epoch,
     934           6 :                                     stake_state->stake.stake.credits_observed,
     935           6 :                                     stake_state->stake.stake.delegation.warmup_cooldown_rate );
     936             : 
     937           6 :   if( capture_ctx && capture_ctx->capture_solcap ) {
     938           0 :     fd_capture_link_write_stake_account_payout( capture_ctx,
     939           0 :                                                 bank->f.slot,
     940           0 :                                                 *stake_pubkey,
     941           0 :                                                 bank->f.slot,
     942           0 :                                                 acc_lamports,
     943           0 :                                                 (long)reward_lamports,
     944           0 :                                                 new_credits_observed,
     945           0 :                                                 (long)( new_credits_observed - old_credits_observed ),
     946           0 :                                                 stake_state->stake.stake.delegation.stake,
     947           0 :                                                 (long)reward_lamports );
     948           0 :   }
     949             : 
     950           6 :   FD_STORE( fd_stake_state_t, fd_accdb_ref_data( rw ), *stake_state );
     951           6 :   fd_hashes_update_lthash( stake_pubkey, rw->meta, prev_hash, bank, capture_ctx );
     952           6 :   fd_accdb_close_rw( accdb, rw );
     953             : 
     954           6 :   return 0;
     955           6 : }
     956             : 
     957             : /* Process reward credits for a partition of rewards.  Store the rewards
     958             :    to AccountsDB, update reward history record and total capitalization
     959             :    https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank/partitioned_epoch_rewards/distribution.rs#L88 */
     960             : static void
     961             : distribute_epoch_rewards_in_partition( fd_stake_rewards_t *      stake_rewards,
     962             :                                        ulong                     partition_idx,
     963             :                                        fd_bank_t *               bank,
     964             :                                        fd_accdb_user_t *         accdb,
     965             :                                        fd_funk_txn_xid_t const * xid,
     966           6 :                                        fd_capture_ctx_t *        capture_ctx ) {
     967             : 
     968           6 :   ulong lamports_distributed = 0UL;
     969           6 :   ulong lamports_burned      = 0UL;
     970             : 
     971           6 :   for( fd_stake_rewards_iter_init( stake_rewards, bank->stake_rewards_fork_id, (ushort)partition_idx );
     972          12 :        !fd_stake_rewards_iter_done( stake_rewards );
     973           6 :        fd_stake_rewards_iter_next( stake_rewards, bank->stake_rewards_fork_id ) ) {
     974           6 :     fd_pubkey_t pubkey;
     975           6 :     ulong       lamports;
     976           6 :     ulong       credits_observed;
     977           6 :     fd_stake_rewards_iter_ele( stake_rewards, bank->stake_rewards_fork_id, &pubkey, &lamports, &credits_observed );
     978             : 
     979           6 :     if( FD_LIKELY( !distribute_epoch_reward_to_stake_acc( bank,
     980           6 :                                                           accdb,
     981           6 :                                                           xid,
     982           6 :                                                           capture_ctx,
     983           6 :                                                           &pubkey,
     984           6 :                                                           lamports,
     985           6 :                                                           credits_observed ) )  ) {
     986           6 :       lamports_distributed += lamports;
     987           6 :     } else {
     988           0 :       lamports_burned += lamports;
     989           0 :     }
     990           6 :   }
     991             : 
     992             :   /* Update the epoch rewards sysvar with the amount distributed and burnt */
     993           6 :   fd_sysvar_epoch_rewards_distribute( bank, accdb, xid, capture_ctx, lamports_distributed + lamports_burned );
     994             : 
     995           6 :   FD_LOG_DEBUG(( "lamports burned: %lu, lamports distributed: %lu", lamports_burned, lamports_distributed ));
     996             : 
     997           6 :   bank->f.capitalization = bank->f.capitalization + lamports_distributed;
     998           6 : }
     999             : 
    1000             : /* Process reward distribution for the block if it is inside reward interval.
    1001             : 
    1002             :    https://github.com/anza-xyz/agave/blob/cbc8320d35358da14d79ebcada4dfb6756ffac79/runtime/src/bank/partitioned_epoch_rewards/distribution.rs#L42 */
    1003             : void
    1004             : fd_distribute_partitioned_epoch_rewards( fd_bank_t *               bank,
    1005             :                                          fd_accdb_user_t *         accdb,
    1006             :                                          fd_funk_txn_xid_t const * xid,
    1007         108 :                                          fd_capture_ctx_t *        capture_ctx ) {
    1008         108 :   if( FD_LIKELY( bank->stake_rewards_fork_id==UCHAR_MAX ) ) return;
    1009             : 
    1010          15 :   fd_stake_rewards_t * stake_rewards = fd_bank_stake_rewards_modify( bank );
    1011             : 
    1012          15 :   ulong block_height                       = bank->f.block_height;
    1013          15 :   ulong distribution_starting_block_height = fd_stake_rewards_starting_block_height( stake_rewards, bank->stake_rewards_fork_id );
    1014          15 :   ulong distribution_end_exclusive         = fd_stake_rewards_exclusive_ending_block_height( stake_rewards, bank->stake_rewards_fork_id );
    1015             : 
    1016          15 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
    1017          15 :   ulong                       epoch          = bank->f.epoch;
    1018             : 
    1019          15 :   if( FD_UNLIKELY( get_slots_in_epoch( epoch, epoch_schedule ) <= fd_stake_rewards_num_partitions( stake_rewards, bank->stake_rewards_fork_id ) ) ) {
    1020           0 :     FD_LOG_CRIT(( "Should not be distributing rewards" ));
    1021           0 :   }
    1022             : 
    1023          15 :   if( FD_UNLIKELY( block_height>=distribution_starting_block_height && block_height<distribution_end_exclusive ) ) {
    1024             : 
    1025           6 :     ulong partition_idx = block_height-distribution_starting_block_height;
    1026           6 :     distribute_epoch_rewards_in_partition( stake_rewards, partition_idx, bank, accdb, xid, capture_ctx );
    1027             : 
    1028             :     /* If we have finished distributing rewards, set the status to inactive */
    1029           6 :     if( fd_ulong_sat_add( block_height, 1UL )>=distribution_end_exclusive ) {
    1030           6 :       fd_sysvar_epoch_rewards_set_inactive( bank, accdb, xid, capture_ctx );
    1031           6 :       bank->stake_rewards_fork_id = UCHAR_MAX;
    1032           6 :     }
    1033           6 :   }
    1034          15 : }
    1035             : 
    1036             : /* Partitioned epoch rewards entry-point.
    1037             : 
    1038             :    https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L102
    1039             : */
    1040             : void
    1041             : fd_begin_partitioned_rewards( fd_bank_t *                    bank,
    1042             :                               fd_accdb_user_t *              accdb,
    1043             :                               fd_funk_txn_xid_t const *      xid,
    1044             :                               fd_runtime_stack_t *           runtime_stack,
    1045             :                               fd_capture_ctx_t *             capture_ctx,
    1046             :                               fd_stake_delegations_t const * stake_delegations,
    1047             :                               fd_hash_t const *              parent_blockhash,
    1048           9 :                               ulong                          parent_epoch ) {
    1049             : 
    1050           9 :   calculate_rewards_and_distribute_vote_rewards(
    1051           9 :       bank,
    1052           9 :       accdb,
    1053           9 :       xid,
    1054           9 :       runtime_stack,
    1055           9 :       stake_delegations,
    1056           9 :       capture_ctx,
    1057           9 :       parent_epoch );
    1058             : 
    1059             :   /* Once the rewards for vote accounts have been distributed and stake
    1060             :      account rewards have been calculated, we can now set our epoch
    1061             :      reward status to be active and we can initialize the epoch rewards
    1062             :      sysvar.  This sysvar is then deleted once all of the partitioned
    1063             :      stake rewards have been distributed.
    1064             : 
    1065             :      The Agave client calculates the partitions for each stake reward
    1066             :      when the first distribution block is reached.  The Firedancer
    1067             :      client differs here since we hash the partitions during the epoch
    1068             :      boundary. */
    1069             : 
    1070           9 :   ulong distribution_starting_block_height = bank->f.block_height + REWARD_CALCULATION_NUM_BLOCKS;
    1071           9 :   uint  num_partitions                     = fd_stake_rewards_num_partitions( fd_bank_stake_rewards_query( bank ), bank->stake_rewards_fork_id );
    1072             : 
    1073           9 :   fd_sysvar_epoch_rewards_init(
    1074           9 :       bank,
    1075           9 :       accdb,
    1076           9 :       xid,
    1077           9 :       capture_ctx,
    1078           9 :       runtime_stack->stakes.distributed_rewards,
    1079           9 :       distribution_starting_block_height,
    1080           9 :       num_partitions,
    1081           9 :       runtime_stack->stakes.total_rewards,
    1082           9 :       runtime_stack->stakes.total_points.ud,
    1083           9 :       parent_blockhash );
    1084           9 : }
    1085             : 
    1086             : /*
    1087             :     Re-calculates partitioned stake rewards.
    1088             :     This updates the slot context's epoch reward status with the recalculated partitioned rewards.
    1089             : 
    1090             :     https://github.com/anza-xyz/agave/blob/v2.2.14/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L521 */
    1091             : void
    1092             : fd_rewards_recalculate_partitioned_rewards( fd_banks_t *              banks,
    1093             :                                             fd_bank_t *               bank,
    1094             :                                             fd_accdb_user_t *         accdb,
    1095             :                                             fd_funk_txn_xid_t const * xid,
    1096             :                                             fd_runtime_stack_t *      runtime_stack,
    1097           0 :                                             fd_capture_ctx_t *        capture_ctx ) {
    1098             : 
    1099           0 :   fd_sysvar_epoch_rewards_t epoch_rewards_sysvar[1];
    1100           0 :   if( FD_UNLIKELY( !fd_sysvar_epoch_rewards_read( accdb, xid, epoch_rewards_sysvar ) ) ) {
    1101           0 :     FD_LOG_DEBUG(( "Failed to read or decode epoch rewards sysvar - may not have been created yet" ));
    1102           0 :     return;
    1103           0 :   }
    1104             : 
    1105           0 :   FD_LOG_DEBUG(( "recalculating partitioned rewards" ));
    1106             : 
    1107           0 :   if( FD_UNLIKELY( !epoch_rewards_sysvar->active ) ) {
    1108           0 :     FD_LOG_DEBUG(( "epoch rewards is inactive" ));
    1109           0 :     return;
    1110           0 :   }
    1111             : 
    1112             :   /* If partitioned rewards are active, the rewarded epoch is always the immediately
    1113             :       preceeding epoch.
    1114             : 
    1115             :       https://github.com/anza-xyz/agave/blob/2316fea4c0852e59c071f72d72db020017ffd7d0/runtime/src/bank/partitioned_epoch_rewards/calculation.rs#L566 */
    1116           0 :   FD_LOG_DEBUG(( "epoch rewards is active" ));
    1117             : 
    1118           0 :   ulong const epoch          = bank->f.epoch;
    1119           0 :   ulong const rewarded_epoch = fd_ulong_sat_sub( epoch, 1UL );
    1120             : 
    1121           0 :   fd_stake_history_t stake_history[1];
    1122           0 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, xid, stake_history ) ) ) {
    1123           0 :     FD_LOG_ERR(( "Unable to read and decode stake history sysvar" ));
    1124           0 :   }
    1125             : 
    1126           0 :   fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
    1127           0 :   if( FD_UNLIKELY( !stake_delegations ) ) {
    1128           0 :     FD_LOG_CRIT(( "stake_delegations is NULL" ));
    1129           0 :   }
    1130             : 
    1131           0 :   calculate_stake_vote_rewards(
    1132           0 :       accdb,
    1133           0 :       xid,
    1134           0 :       bank,
    1135           0 :       stake_delegations,
    1136           0 :       capture_ctx,
    1137           0 :       stake_history,
    1138           0 :       rewarded_epoch,
    1139           0 :       epoch_rewards_sysvar->total_rewards,
    1140           0 :       epoch_rewards_sysvar->total_points.ud,
    1141           0 :       runtime_stack,
    1142           0 :       1 );
    1143             : 
    1144           0 :   setup_stake_partitions(
    1145           0 :       accdb,
    1146           0 :       xid,
    1147           0 :       bank,
    1148           0 :       stake_history,
    1149           0 :       stake_delegations,
    1150           0 :       runtime_stack,
    1151           0 :       &epoch_rewards_sysvar->parent_blockhash,
    1152           0 :       epoch_rewards_sysvar->distribution_starting_block_height,
    1153           0 :       (uint)epoch_rewards_sysvar->num_partitions,
    1154           0 :       rewarded_epoch,
    1155           0 :       epoch_rewards_sysvar->total_rewards,
    1156           0 :       epoch_rewards_sysvar->total_points.ud );
    1157             : 
    1158           0 :   fd_bank_stake_delegations_end_frontier_query( banks, bank );
    1159           0 : }

Generated by: LCOV version 1.14