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: 97 101 96.0 %
Date: 2026-11-15 04:25:17 Functions: 7 7 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 "../fd_accdb_svm.h"
       5             : 
       6             : void
       7             : fd_sysvar_stake_history_init( fd_bank_t *        bank,
       8             :                               fd_accdb_t *       accdb,
       9          33 :                               fd_capture_ctx_t * capture_ctx ) {
      10          33 :   uchar data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
      11          33 :   fd_memset( data, 0, sizeof(data) );
      12          33 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_stake_history_id, data, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
      13          33 : }
      14             : 
      15             : /* https://github.com/anza-xyz/agave/blob/v4.0.0-rc.1/runtime/src/bank.rs#L2452-L2463 */
      16             : 
      17             : void
      18             : fd_sysvar_stake_history_update( fd_bank_t *                      bank,
      19             :                                 fd_accdb_t *                     accdb,
      20             :                                 fd_capture_ctx_t *               capture_ctx,
      21         357 :                                 fd_stake_history_entry_t const * entry ) {
      22         357 :   fd_accdb_svm_update_t update[1];
      23         357 :   fd_acc_t rw = fd_accdb_svm_open_rw( bank, accdb, update, &fd_sysvar_stake_history_id, 1 );
      24             : 
      25         357 :   if( FD_UNLIKELY( !rw.lamports ) ) {
      26             :     /* Initialize account if it did not exist */
      27           3 :     fd_memcpy( rw.owner, fd_sysvar_owner_id.key, sizeof(fd_pubkey_t) );
      28           3 :     rw.lamports = FD_SYSVAR_RENT_UNADJUSTED_INITIAL_BALANCE;
      29           3 :     rw.data_len = FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ;
      30           3 :     fd_memset( rw.data, 0, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
      31             :     /* Now a valid StakeHistory sysvar with zero entries */
      32         354 :   } else {
      33             :     /* Sanity check existing state */
      34         354 :     if( FD_UNLIKELY( 0!=memcmp( rw.owner, &fd_sysvar_owner_id, sizeof(fd_pubkey_t) ) ) ) {
      35           0 :       FD_LOG_ERR(( "stake history sysvar not owned by sysvar owner" ));
      36           0 :     }
      37         354 :   }
      38             : 
      39         357 :   if( FD_UNLIKELY( rw.data_len<8UL ) ) FD_LOG_ERR(( "invalid stake history sysvar" ));
      40             : 
      41         357 :   ulong len = FD_LOAD( ulong, rw.data );
      42         357 :   len = fd_ulong_min( len, FD_SYSVAR_STAKE_HISTORY_CAP );
      43         357 :   ulong min_sz = 8UL + len * sizeof(fd_stake_history_entry_t);
      44         357 :   if( FD_UNLIKELY( rw.data_len<min_sz ) ) {
      45           0 :     FD_LOG_ERR(( "invalid stake history sysvar: data_len too small (%lu, required %lu)", rw.data_len, min_sz ));
      46           0 :   }
      47             : 
      48             :   /* https://github.com/anza-xyz/solana-sdk/blob/account%40v4.3.0/account/src/lib.rs#L618 */
      49         357 :   if( FD_UNLIKELY( rw.data_len!=FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ) ) rw.data_len = FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ;
      50             : 
      51         357 :   fd_stake_history_entry_t * entries = fd_type_pun( rw.data+8UL );
      52             : 
      53             :   /* https://github.com/solana-program/stake/blob/interface%40v4.0.0/interface/src/stake_history.rs#L83 */
      54         357 :   ulong idx   = 0UL;
      55         357 :   int   found = 0;
      56         357 :   {
      57         357 :     ulong lo = 0UL;
      58         357 :     ulong hi = len;
      59         525 :     while( lo < hi ) {
      60         174 :       ulong mid         = lo + (hi - lo) / 2UL;
      61         174 :       ulong probe_epoch = entries[mid].epoch;
      62         174 :       if( entry->epoch == probe_epoch ) {
      63           6 :         idx   = mid;
      64           6 :         found = 1;
      65           6 :         break;
      66         168 :       } else if( entry->epoch > probe_epoch ) {
      67         135 :         hi = mid;
      68         135 :       } else {
      69          33 :         lo = mid + 1UL;
      70          33 :       }
      71         174 :     }
      72         357 :     if( !found ) idx = lo;
      73         357 :   }
      74             : 
      75             :   /* Insert new element */
      76         357 :   ulong new_len = fd_ulong_if( found, len, fd_ulong_min( len+1UL, FD_SYSVAR_STAKE_HISTORY_CAP ) );
      77         357 :   ulong used_sz = 8UL + new_len * sizeof(fd_stake_history_entry_t);
      78             : 
      79         357 :   if( found ) {
      80             :     /* https://github.com/solana-program/stake/blob/interface%40v4.0.0/interface/src/stake_history.rs#L84 */
      81           6 :     entries[ idx ] = *entry;
      82         351 :   } else if( idx < FD_SYSVAR_STAKE_HISTORY_CAP ) {
      83             :     /* https://github.com/solana-program/stake/blob/interface%40v4.0.0/interface/src/stake_history.rs#L85
      84             :        https://github.com/solana-program/stake/blob/interface%40v4.0.0/interface/src/stake_history.rs#L87 */
      85         348 :     ulong shift_count = fd_ulong_min( len, FD_SYSVAR_STAKE_HISTORY_CAP-1UL ) - idx;
      86         348 :     memmove( &entries[ idx+1UL ], &entries[ idx ], shift_count * sizeof(fd_stake_history_entry_t) );
      87         348 :     entries[ idx ] = *entry;
      88         348 :     new_len = fd_ulong_min( len+1UL, FD_SYSVAR_STAKE_HISTORY_CAP );
      89         348 :   }
      90             :   /* else: idx == cap and not found - new entry would be truncated, drop */
      91             : 
      92             :   /* Zero trailing bytes */
      93         357 :   FD_TEST( used_sz <= FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
      94         357 :   fd_memset( rw.data+used_sz, 0, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ-used_sz );
      95             : 
      96         357 :   FD_STORE( ulong, rw.data, new_len );
      97             : 
      98             :   /* Balance is updated later by fd_stake_history_ensure_rent_exempt. */
      99         357 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &rw, update );
     100         357 : }
     101             : 
     102             : void
     103             : fd_stake_history_ensure_rent_exempt( fd_bank_t *        bank,
     104             :                                      fd_accdb_t *       accdb,
     105         327 :                                      fd_capture_ctx_t * capture_ctx ) {
     106         327 :   fd_accdb_svm_update_t update[1];
     107         327 :   fd_acc_t rw = fd_accdb_svm_open_rw( bank, accdb, update, &fd_sysvar_stake_history_id, 0 );
     108         327 :   if( FD_UNLIKELY( !rw.lamports ) ) return; /* already released */
     109             : 
     110         324 :   fd_sysvar_adjust_balance_for_rent( bank, &rw );
     111         324 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &rw, update );
     112         324 : }
     113             : 
     114             : int
     115             : fd_sysvar_stake_history_validate( uchar const * data,
     116       15102 :                                   ulong         sz ) {
     117       15102 :   if( FD_UNLIKELY( sz < 8UL ) ) return 0;
     118       15099 :   ulong len = FD_LOAD( ulong, data );
     119       15099 :   ulong min_sz;
     120       15099 :   if( FD_UNLIKELY( __builtin_umull_overflow( len, 32UL, &min_sz ) ) ) return 0;
     121       15099 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 8UL, &min_sz ) ) ) return 0;
     122       15099 :   if( FD_UNLIKELY( sz < min_sz ) ) return 0;
     123       15099 :   return 1;
     124       15099 : }
     125             : 
     126             : fd_stake_history_t *
     127             : fd_sysvar_stake_history_view( fd_stake_history_t * view,
     128             :                               uchar const *        data,
     129        2082 :                               ulong                sz ) {
     130        2082 :   if( FD_UNLIKELY( !fd_sysvar_stake_history_validate( data, sz ) ) ) return NULL;
     131        2082 :   view->len     = FD_LOAD( ulong, data );
     132        2082 :   view->entries = fd_type_pun_const( data + 8UL );
     133        2082 :   return view;
     134        2082 : }
     135             : 
     136             : fd_stake_history_t *
     137             : fd_sysvar_stake_history_read( fd_accdb_t *         accdb,
     138             :                               fd_accdb_fork_id_t   fork_id,
     139             :                               uchar                data[ static FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ],
     140        2037 :                               fd_stake_history_t * view ) {
     141        2037 :   fd_acc_t ro = fd_accdb_read_one( accdb, fork_id, fd_sysvar_stake_history_id.uc );
     142        2037 :   ulong sz = 0UL;
     143        2037 :   if( FD_LIKELY( ro.lamports ) ) {
     144        2037 :     sz = fd_ulong_min( ro.data_len, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
     145        2037 :     fd_memcpy( data, ro.data, sz );
     146        2037 :   }
     147        2037 :   fd_accdb_unread_one( accdb, &ro );
     148        2037 :   if( FD_UNLIKELY( !sz ) ) return NULL;
     149        2037 :   return fd_sysvar_stake_history_view( view, data, sz );
     150        2037 : }
     151             : 
     152             : fd_stake_history_entry_t const *
     153             : fd_sysvar_stake_history_query( fd_stake_history_t const * view,
     154         942 :                                ulong                      epoch ) {
     155         942 :   if( FD_UNLIKELY( !view || !view->len || epoch>view->entries[0].epoch ) ) return NULL;
     156         525 :   ulong index = view->entries[0].epoch - epoch;
     157         525 :   if( FD_UNLIKELY( index>=view->len ) ) return NULL;
     158          48 :   return &view->entries[index];
     159         525 : }

Generated by: LCOV version 1.14