LCOV - code coverage report
Current view: top level - flamenco/stakes - fd_stakes.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 166 0.0 %
Date: 2025-11-29 04:46:19 Functions: 0 6 0.0 %

          Line data    Source code
       1             : #include "fd_stakes.h"
       2             : #include "../runtime/fd_bank.h"
       3             : #include "../runtime/program/fd_stake_program.h"
       4             : #include "../runtime/program/fd_vote_program.h"
       5             : #include "../runtime/sysvar/fd_sysvar_stake_history.h"
       6             : #include "fd_stake_delegations.h"
       7             : #include "../accdb/fd_accdb_impl_v1.h"
       8             : 
       9             : ulong
      10             : fd_stake_weights_by_node( fd_vote_states_t const * vote_states,
      11           0 :                           fd_vote_stake_weight_t * weights ) {
      12             : 
      13           0 :   ulong weights_cnt = 0;
      14           0 :   fd_vote_states_iter_t iter_[1];
      15           0 :   for( fd_vote_states_iter_t * iter = fd_vote_states_iter_init( iter_, vote_states ); !fd_vote_states_iter_done( iter ); fd_vote_states_iter_next( iter ) ) {
      16           0 :     fd_vote_state_ele_t const * vote_state = fd_vote_states_iter_ele( iter );
      17           0 :     if( FD_UNLIKELY( !vote_state->stake ) ) continue;
      18             : 
      19           0 :     fd_memcpy( weights[ weights_cnt ].vote_key.uc, &vote_state->vote_account, sizeof(fd_pubkey_t) );
      20           0 :     fd_memcpy( weights[ weights_cnt ].id_key.uc, &vote_state->node_account, sizeof(fd_pubkey_t) );
      21           0 :     weights[ weights_cnt ].stake = vote_state->stake;
      22           0 :     weights_cnt++;
      23           0 :   }
      24           0 :   sort_vote_weights_by_stake_vote_inplace( weights, weights_cnt );
      25           0 :   return weights_cnt;
      26           0 : }
      27             : 
      28             : /* We need to update the amount of stake that each vote account has for
      29             :    the given epoch.  This can only be done after the stake history
      30             :    sysvar has been updated.  We also cache the stakes for each of the
      31             :    vote accounts for the previous epoch.
      32             : 
      33             :    https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/stakes.rs#L471 */
      34             : void
      35             : fd_refresh_vote_accounts( fd_bank_t *                    bank,
      36             :                           fd_stake_delegations_t const * stake_delegations,
      37             :                           fd_stake_history_t const *     history,
      38           0 :                           ulong *                        new_rate_activation_epoch ) {
      39             : 
      40           0 :   ulong epoch = fd_bank_epoch_get( bank );
      41             : 
      42           0 :   ulong total_stake = 0UL;
      43             : 
      44           0 :   fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
      45           0 :   if( FD_UNLIKELY( !vote_states ) ) {
      46           0 :     FD_LOG_CRIT(( "vote_states is NULL" ));
      47           0 :   }
      48             : 
      49             :   /* Reset the vote stakes so we can re-compute them based on the most
      50             :      current stake delegation values. */
      51           0 :   fd_vote_states_reset_stakes( vote_states );
      52             : 
      53           0 :   fd_stake_delegations_iter_t iter_[1];
      54           0 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
      55           0 :        !fd_stake_delegations_iter_done( iter );
      56           0 :        fd_stake_delegations_iter_next( iter ) ) {
      57           0 :     fd_stake_delegation_t const * stake_delegation = fd_stake_delegations_iter_ele( iter );
      58             : 
      59           0 :     fd_delegation_t delegation = {
      60           0 :       .voter_pubkey         = stake_delegation->vote_account,
      61           0 :       .stake                = stake_delegation->stake,
      62           0 :       .deactivation_epoch   = stake_delegation->deactivation_epoch,
      63           0 :       .activation_epoch     = stake_delegation->activation_epoch,
      64           0 :       .warmup_cooldown_rate = stake_delegation->warmup_cooldown_rate,
      65           0 :     };
      66             : 
      67           0 :     fd_stake_history_entry_t new_entry = fd_stake_activating_and_deactivating(
      68           0 :         &delegation,
      69           0 :         epoch,
      70           0 :         history,
      71           0 :         new_rate_activation_epoch );
      72             : 
      73           0 :     fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states, &stake_delegation->vote_account );
      74           0 :     if( FD_LIKELY( vote_state ) ) {
      75           0 :       total_stake       += new_entry.effective;
      76           0 :       vote_state->stake += new_entry.effective;
      77           0 :     }
      78           0 :   }
      79             : 
      80           0 :   fd_bank_total_epoch_stake_set( bank, total_stake );
      81             : 
      82           0 :   fd_bank_vote_states_end_locking_modify( bank );
      83           0 : }
      84             : 
      85             : /* https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/stakes.rs#L280 */
      86             : void
      87             : fd_stakes_activate_epoch( fd_bank_t *                    bank,
      88             :                           fd_accdb_user_t *              accdb,
      89             :                           fd_funk_txn_xid_t const *      xid,
      90             :                           fd_capture_ctx_t *             capture_ctx,
      91             :                           fd_stake_delegations_t const * stake_delegations,
      92           0 :                           ulong *                        new_rate_activation_epoch ) {
      93             : 
      94             :   /* First, we need to accumulate the stats for the current amount of
      95             :      effective, activating, and deactivating stake for the current
      96             :      epoch.  Once this is computed, we can add update our stake history
      97             :      sysvar.  Afterward, we can refresh the stake values for the vote
      98             :      accounts for the new epoch. */
      99             : 
     100           0 :   fd_funk_t * funk = fd_accdb_user_v1_funk( accdb );
     101           0 :   fd_stake_history_t stake_history[1];
     102           0 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( funk, xid, stake_history ) ) ) {
     103           0 :     FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
     104           0 :   }
     105             : 
     106           0 :   fd_epoch_stake_history_entry_pair_t new_elem = {
     107           0 :     .epoch = fd_bank_epoch_get( bank ),
     108           0 :     .entry = {
     109           0 :       .effective    = 0UL,
     110           0 :       .activating   = 0UL,
     111           0 :       .deactivating = 0UL
     112           0 :     }
     113           0 :   };
     114             : 
     115           0 :   fd_stake_delegations_iter_t iter_[1];
     116           0 :   for( fd_stake_delegations_iter_t * iter = fd_stake_delegations_iter_init( iter_, stake_delegations );
     117           0 :        !fd_stake_delegations_iter_done( iter );
     118           0 :        fd_stake_delegations_iter_next( iter ) ) {
     119           0 :     fd_stake_delegation_t const * stake_delegation = fd_stake_delegations_iter_ele( iter );
     120             : 
     121           0 :     fd_delegation_t delegation = {
     122           0 :       .voter_pubkey         = stake_delegation->vote_account,
     123           0 :       .stake                = stake_delegation->stake,
     124           0 :       .activation_epoch     = stake_delegation->activation_epoch,
     125           0 :       .deactivation_epoch   = stake_delegation->deactivation_epoch,
     126           0 :       .warmup_cooldown_rate = stake_delegation->warmup_cooldown_rate,
     127           0 :     };
     128             : 
     129           0 :     fd_stake_history_entry_t new_entry = fd_stake_activating_and_deactivating(
     130           0 :         &delegation,
     131           0 :         fd_bank_epoch_get( bank ),
     132           0 :         stake_history,
     133           0 :         new_rate_activation_epoch );
     134           0 :     new_elem.entry.effective    += new_entry.effective;
     135           0 :     new_elem.entry.activating   += new_entry.activating;
     136           0 :     new_elem.entry.deactivating += new_entry.deactivating;
     137           0 :   }
     138             : 
     139           0 :   fd_sysvar_stake_history_update( bank, accdb, xid, capture_ctx, &new_elem );
     140             : 
     141           0 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( funk, xid, stake_history ) ) ) {
     142           0 :     FD_LOG_ERR(( "StakeHistory sysvar is missing from sysvar cache" ));
     143           0 :   }
     144             : 
     145             :   /* Now increment the epoch and recompute the stakes for the vote
     146             :      accounts for the new epoch value. */
     147             : 
     148           0 :   fd_bank_epoch_set( bank, fd_bank_epoch_get( bank ) + 1UL );
     149             : 
     150           0 :   fd_refresh_vote_accounts( bank,
     151           0 :                             stake_delegations,
     152           0 :                             stake_history,
     153           0 :                             new_rate_activation_epoch );
     154             : 
     155           0 : }
     156             : 
     157             : int
     158             : write_stake_state( fd_txn_account_t *    stake_acc_rec,
     159           0 :                    fd_stake_state_v2_t * stake_state ) {
     160             : 
     161           0 :   ulong encoded_stake_state_size = fd_stake_state_v2_size(stake_state);
     162             : 
     163           0 :   fd_bincode_encode_ctx_t ctx = {
     164           0 :     .data    = fd_txn_account_get_data_mut( stake_acc_rec ),
     165           0 :     .dataend = fd_txn_account_get_data_mut( stake_acc_rec ) + encoded_stake_state_size,
     166           0 :   };
     167           0 :   if( FD_UNLIKELY( fd_stake_state_v2_encode( stake_state, &ctx ) != FD_BINCODE_SUCCESS ) ) {
     168           0 :     FD_LOG_ERR(( "fd_stake_state_encode failed" ));
     169           0 :   }
     170             : 
     171           0 :   return 0;
     172           0 : }
     173             : 
     174             : void
     175             : fd_stakes_update_stake_delegation( fd_txn_account_t * stake_account,
     176           0 :                                    fd_bank_t *        bank ) {
     177             : 
     178           0 :   if( !stake_account->is_mutable ) return;
     179             : 
     180           0 :   fd_stake_delegations_t * stake_delegations_delta = fd_bank_stake_delegations_delta_locking_modify( bank );
     181           0 :   if( FD_UNLIKELY( !stake_delegations_delta ) ) {
     182           0 :     FD_LOG_CRIT(( "unable to retrieve join to stake delegation delta" ));
     183           0 :   }
     184             : 
     185           0 :   if( fd_txn_account_get_lamports( stake_account )==0UL ) {
     186           0 :     fd_stake_delegations_remove( stake_delegations_delta, stake_account->pubkey );
     187           0 :     fd_bank_stake_delegations_delta_end_locking_modify( bank );
     188           0 :     return;
     189           0 :   }
     190             : 
     191           0 :   fd_stake_state_v2_t stake_state;
     192           0 :   int err = fd_stake_get_state( stake_account, &stake_state );
     193           0 :   if( FD_UNLIKELY( err!=0 ) ) {
     194           0 :     fd_stake_delegations_remove( stake_delegations_delta, stake_account->pubkey );
     195           0 :     fd_bank_stake_delegations_delta_end_locking_modify( bank );
     196           0 :     return;
     197           0 :   }
     198             : 
     199           0 :   if( FD_UNLIKELY( !fd_stake_state_v2_is_stake( &stake_state ) ) ) {
     200           0 :     fd_stake_delegations_remove( stake_delegations_delta, stake_account->pubkey );
     201           0 :     fd_bank_stake_delegations_delta_end_locking_modify( bank );
     202           0 :     return;
     203           0 :   }
     204             : 
     205           0 :   if( FD_UNLIKELY( fd_stake_state_v2_is_uninitialized( &stake_state ) ) ) {
     206           0 :     fd_stake_delegations_remove( stake_delegations_delta, stake_account->pubkey );
     207           0 :     fd_bank_stake_delegations_delta_end_locking_modify( bank );
     208           0 :     return;
     209           0 :   }
     210             : 
     211           0 :   if( FD_UNLIKELY( stake_state.inner.stake.stake.delegation.stake==0UL ) ) {
     212           0 :     fd_stake_delegations_remove( stake_delegations_delta, stake_account->pubkey );
     213           0 :     fd_bank_stake_delegations_delta_end_locking_modify( bank );
     214           0 :     return;
     215           0 :   }
     216             : 
     217           0 :   fd_stake_delegations_update( stake_delegations_delta,
     218           0 :                                stake_account->pubkey,
     219           0 :                                &stake_state.inner.stake.stake.delegation.voter_pubkey,
     220           0 :                                stake_state.inner.stake.stake.delegation.stake,
     221           0 :                                stake_state.inner.stake.stake.delegation.activation_epoch,
     222           0 :                                stake_state.inner.stake.stake.delegation.deactivation_epoch,
     223           0 :                                stake_state.inner.stake.stake.credits_observed,
     224           0 :                                stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
     225             : 
     226           0 :   fd_bank_stake_delegations_delta_end_locking_modify( bank );
     227           0 : }
     228             : 
     229             : void
     230             : fd_stakes_update_vote_state( fd_txn_account_t * vote_account,
     231           0 :                              fd_bank_t *        bank ) {
     232             : 
     233           0 :   if( !vote_account->is_mutable ) return;
     234             : 
     235           0 :   fd_vote_states_t * vote_states = fd_bank_vote_states_locking_modify( bank );
     236             : 
     237           0 :   if( fd_txn_account_get_lamports( vote_account )==0UL ) {
     238           0 :     fd_vote_states_remove( vote_states, vote_account->pubkey );
     239           0 :     fd_bank_vote_states_end_locking_modify( bank );
     240           0 :     return;
     241           0 :   }
     242             : 
     243           0 :   if( !fd_vote_state_versions_is_correct_and_initialized( vote_account ) ) {
     244           0 :     fd_vote_states_remove( vote_states, vote_account->pubkey );
     245           0 :     fd_bank_vote_states_end_locking_modify( bank );
     246           0 :     return;
     247           0 :   }
     248             : 
     249           0 :   fd_vote_states_update_from_account( vote_states,
     250           0 :                                       vote_account->pubkey,
     251           0 :                                       fd_txn_account_get_data( vote_account ),
     252           0 :                                       fd_txn_account_get_data_len( vote_account ) );
     253           0 :   fd_bank_vote_states_end_locking_modify( bank );
     254           0 : }

Generated by: LCOV version 1.14