LCOV - code coverage report
Current view: top level - flamenco/stakes - fd_stakes.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 165 0.0 %
Date: 2025-10-16 04:31:23 Functions: 0 6 0.0 %

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

Generated by: LCOV version 1.14