LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_stake_history.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 41 51 80.4 %
Date: 2026-02-22 05:44:04 Functions: 4 4 100.0 %

          Line data    Source code
       1             : #include "fd_sysvar_stake_history.h"
       2             : #include "fd_sysvar.h"
       3             : #include "../fd_system_ids.h"
       4             : #include "../../accdb/fd_accdb_sync.h"
       5             : 
       6             : /* Ensure that the size declared by our header matches the minimum size
       7             :    of the corresponding fd_types entry. */
       8             : 
       9             : static void
      10             : write_stake_history( fd_bank_t *               bank,
      11             :                      fd_accdb_user_t *         accdb,
      12             :                      fd_funk_txn_xid_t const * xid,
      13             :                      fd_capture_ctx_t *        capture_ctx,
      14         105 :                      fd_stake_history_t *      stake_history ) {
      15             :   /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/sysvar/stake_history.rs#L12 */
      16         105 :   uchar __attribute__((aligned(FD_STAKE_HISTORY_ALIGN))) enc[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ] = {0};
      17         105 :   fd_bincode_encode_ctx_t encode =
      18         105 :     { .data    = enc,
      19         105 :       .dataend = enc + sizeof(enc) };
      20         105 :   if( FD_UNLIKELY( fd_stake_history_encode( stake_history, &encode )!=FD_BINCODE_SUCCESS ) )
      21           0 :     FD_LOG_ERR(("fd_stake_history_encode failed"));
      22             : 
      23         105 :   fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_stake_history_id, enc, sizeof(enc) );
      24         105 : }
      25             : 
      26             : fd_stake_history_t *
      27             : fd_sysvar_stake_history_read( fd_accdb_user_t *         accdb,
      28             :                               fd_funk_txn_xid_t const * xid,
      29         186 :                               fd_stake_history_t *      stake_history ) {
      30         186 :   fd_accdb_ro_t ro[1];
      31         186 :   if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, &fd_sysvar_stake_history_id ) ) ) {
      32           0 :     return NULL;
      33           0 :   }
      34             : 
      35             :   /* This check is needed as a quirk of the fuzzer. If a sysvar account
      36             :      exists in the accounts database, but doesn't have any lamports,
      37             :      this means that the account does not exist. This wouldn't happen
      38             :      in a real execution environment. */
      39         186 :   if( FD_UNLIKELY( fd_accdb_ref_lamports( ro )==0UL ) ) {
      40           0 :     fd_accdb_close_ro( accdb, ro );
      41           0 :     return NULL;
      42           0 :   }
      43             : 
      44         186 :   stake_history = fd_bincode_decode_static(
      45         186 :       stake_history, stake_history,
      46         186 :       fd_accdb_ref_data_const( ro ),
      47         186 :       fd_accdb_ref_data_sz( ro ),
      48         186 :       NULL );
      49         186 :   fd_accdb_close_ro( accdb, ro );
      50         186 :   return stake_history;
      51         186 : }
      52             : 
      53             : void
      54             : fd_sysvar_stake_history_init( fd_bank_t *               bank,
      55             :                               fd_accdb_user_t *         accdb,
      56             :                               fd_funk_txn_xid_t const * xid,
      57          54 :                               fd_capture_ctx_t *        capture_ctx ) {
      58          54 :   fd_stake_history_t stake_history;
      59          54 :   fd_stake_history_new( &stake_history );
      60          54 :   write_stake_history( bank, accdb, xid, capture_ctx, &stake_history );
      61          54 : }
      62             : 
      63             : void
      64             : fd_sysvar_stake_history_update( fd_bank_t *                                 bank,
      65             :                                 fd_accdb_user_t *                           accdb,
      66             :                                 fd_funk_txn_xid_t const *                   xid,
      67             :                                 fd_capture_ctx_t *                          capture_ctx,
      68          51 :                                 fd_epoch_stake_history_entry_pair_t const * pair ) {
      69             : 
      70          51 :   fd_stake_history_t stake_history[1];
      71          51 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_read( accdb, xid, stake_history ) ) ) {
      72           0 :     FD_LOG_CRIT(( "Failed to read stake history sysvar" ));
      73           0 :   }
      74             : 
      75          51 :   if( stake_history->fd_stake_history_offset == 0 ) {
      76          51 :     stake_history->fd_stake_history_offset = stake_history->fd_stake_history_size - 1;
      77          51 :   } else {
      78           0 :     stake_history->fd_stake_history_offset--;
      79           0 :   }
      80             : 
      81          51 :   if( stake_history->fd_stake_history_len < stake_history->fd_stake_history_size ) {
      82          51 :     stake_history->fd_stake_history_len++;
      83          51 :   }
      84             : 
      85             :   // This should be done with a bit mask
      86          51 :   ulong idx = stake_history->fd_stake_history_offset;
      87             : 
      88          51 :   stake_history->fd_stake_history[ idx ].epoch              = pair->epoch;
      89          51 :   stake_history->fd_stake_history[ idx ].entry.activating   = pair->entry.activating;
      90          51 :   stake_history->fd_stake_history[ idx ].entry.effective    = pair->entry.effective;
      91          51 :   stake_history->fd_stake_history[ idx ].entry.deactivating = pair->entry.deactivating;
      92             : 
      93          51 :   write_stake_history( bank, accdb, xid, capture_ctx, stake_history );
      94             : 
      95          51 : }

Generated by: LCOV version 1.14