LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_slot_history.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 127 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 6 0.0 %

          Line data    Source code
       1             : #include "fd_sysvar_slot_history.h"
       2             : #include "fd_sysvar.h"
       3             : #include "fd_sysvar_rent.h"
       4             : #include "../fd_executor_err.h"
       5             : #include "../fd_system_ids.h"
       6             : 
       7             : /* FIXME These constants should be header defines */
       8             : 
       9             : static const ulong slot_history_min_account_size = 131097;
      10             : 
      11             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L37 */
      12             : static const ulong slot_history_max_entries = 1024 * 1024;
      13             : 
      14             : /* TODO: move into separate bitvec library */
      15             : static const ulong bits_per_block = 8 * sizeof(ulong);
      16             : 
      17             : void
      18             : fd_sysvar_slot_history_set( fd_slot_history_global_t * history,
      19           0 :                             ulong                      i ) {
      20           0 :   if( FD_UNLIKELY( i > history->next_slot && i - history->next_slot >= slot_history_max_entries ) ) {
      21           0 :     FD_LOG_WARNING(( "Ignoring out of bounds (i=%lu next_slot=%lu)", i, history->next_slot ));
      22           0 :     return;
      23           0 :   }
      24             : 
      25           0 :   ulong * blocks     = (ulong *)((uchar*)history + history->bits_bitvec_offset);
      26           0 :   ulong   blocks_len = history->bits_bitvec_len;
      27             : 
      28             :   // Skipped slots, delete them from history
      29           0 :   for( ulong j = history->next_slot; j < i; j++ ) {
      30           0 :     ulong block_idx = (j / bits_per_block) % (blocks_len);
      31           0 :     blocks[ block_idx ] &= ~( 1UL << ( j % bits_per_block ) );
      32           0 :   }
      33           0 :   ulong block_idx = (i / bits_per_block) % (blocks_len);
      34           0 :   blocks[ block_idx ] |= ( 1UL << ( i % bits_per_block ) );
      35           0 : }
      36             : 
      37             : FD_FN_UNUSED static const ulong blocks_len = slot_history_max_entries / bits_per_block;
      38             : 
      39             : int
      40             : fd_sysvar_slot_history_write_history( fd_exec_slot_ctx_t *       slot_ctx,
      41           0 :                                       fd_slot_history_global_t * history ) {
      42           0 :   ulong sz = slot_history_min_account_size;
      43           0 :   uchar enc[ sz ];
      44           0 :   fd_memset( enc, 0, sz );
      45           0 :   fd_bincode_encode_ctx_t ctx;
      46           0 :   ctx.data    = enc;
      47           0 :   ctx.dataend = enc + sz;
      48           0 :   int err = fd_slot_history_encode_global( history, &ctx );
      49           0 :   if (0 != err)
      50           0 :     return err;
      51           0 :   return fd_sysvar_set( slot_ctx->bank, slot_ctx->funk, slot_ctx->funk_txn, &fd_sysvar_owner_id, &fd_sysvar_slot_history_id, enc, sz, slot_ctx->slot );
      52           0 : }
      53             : 
      54             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L16 */
      55             : void
      56           0 : fd_sysvar_slot_history_init( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime_spad ) {
      57           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
      58             :   /* Create a new slot history instance */
      59             : 
      60             :   /* We need to construct the gaddr-aware slot history object */
      61           0 :   ulong total_sz = sizeof(fd_slot_history_global_t) + alignof(fd_slot_history_global_t) +
      62           0 :                    (sizeof(ulong) + alignof(ulong)) * blocks_len;
      63             : 
      64           0 :   uchar * mem = fd_spad_alloc( runtime_spad, alignof(fd_slot_history_global_t), total_sz );
      65           0 :   fd_slot_history_global_t * history = (fd_slot_history_global_t *)mem;
      66           0 :   ulong *                    blocks  = (ulong *)fd_ulong_align_up( (ulong)((uchar*)history + sizeof(fd_slot_history_global_t)), alignof(ulong) );
      67             : 
      68           0 :   history->next_slot          = slot_ctx->slot + 1UL;
      69           0 :   history->bits_bitvec_offset = (ulong)((uchar*)blocks - (uchar*)history);
      70           0 :   history->bits_len           = slot_history_max_entries;
      71           0 :   history->bits_bitvec_len    = blocks_len;
      72           0 :   memset( blocks, 0, sizeof(ulong) * blocks_len );
      73             : 
      74             :   /* TODO: handle slot != 0 init case */
      75           0 :   fd_sysvar_slot_history_set( history, slot_ctx->slot );
      76           0 :   fd_sysvar_slot_history_write_history( slot_ctx, history );
      77           0 :   } FD_SPAD_FRAME_END;
      78           0 : }
      79             : 
      80             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/runtime/src/bank.rs#L2345 */
      81             : int
      82           0 : fd_sysvar_slot_history_update( fd_exec_slot_ctx_t * slot_ctx, fd_spad_t * runtime_spad ) {
      83             :   /* Set current_slot, and update next_slot */
      84             : 
      85           0 :   fd_pubkey_t const * key = &fd_sysvar_slot_history_id;
      86             : 
      87           0 :   FD_TXN_ACCOUNT_DECL( rec );
      88           0 :   int err = fd_txn_account_init_from_funk_readonly( rec, key, slot_ctx->funk, slot_ctx->funk_txn );
      89           0 :   if (err)
      90           0 :     FD_LOG_CRIT(( "fd_txn_account_init_from_funk_readonly(slot_history) failed: %d", err ));
      91             : 
      92           0 :   fd_bincode_decode_ctx_t ctx = {
      93           0 :     .data    = rec->vt->get_data( rec ),
      94           0 :     .dataend = rec->vt->get_data( rec ) + rec->vt->get_data_len( rec )
      95           0 :   };
      96             : 
      97           0 :   ulong total_sz = 0UL;
      98           0 :   err = fd_slot_history_decode_footprint( &ctx, &total_sz );
      99           0 :   if( FD_UNLIKELY( err ) ) {
     100           0 :     FD_LOG_ERR(( "fd_slot_history_decode_footprint failed" ));
     101           0 :   }
     102             : 
     103           0 :   uchar * mem = fd_spad_alloc( runtime_spad, fd_slot_history_align(), total_sz );
     104           0 :   if( FD_UNLIKELY( !mem ) ) {
     105           0 :     FD_LOG_ERR(( "Unable to allocate memory for slot history" ));
     106           0 :   }
     107             : 
     108           0 :   fd_slot_history_global_t * history = fd_slot_history_decode_global( mem, &ctx );
     109             : 
     110             :   /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L48 */
     111           0 :   fd_sysvar_slot_history_set( history, slot_ctx->slot );
     112           0 :   history->next_slot = slot_ctx->slot + 1;
     113             : 
     114           0 :   ulong sz = slot_history_min_account_size;
     115             : 
     116           0 :   err = fd_txn_account_init_from_funk_mutable( rec, key, slot_ctx->funk, slot_ctx->funk_txn, 1, sz );
     117           0 :   if (err)
     118           0 :     FD_LOG_CRIT(( "fd_txn_account_init_from_funk_mutable(slot_history) failed: %d", err ));
     119             : 
     120           0 :   fd_bincode_encode_ctx_t e_ctx = {
     121           0 :     .data    = rec->vt->get_data_mut( rec ),
     122           0 :     .dataend = rec->vt->get_data_mut( rec )+sz,
     123           0 :   };
     124             : 
     125           0 :   if( FD_UNLIKELY( fd_slot_history_encode_global( history, &e_ctx ) ) ) {
     126           0 :     return FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR;
     127           0 :   }
     128             : 
     129           0 :   fd_rent_t const * rent = fd_bank_rent_query( slot_ctx->bank );
     130           0 :   rec->vt->set_lamports( rec, fd_rent_exempt_minimum_balance( rent, sz ) );
     131             : 
     132           0 :   rec->vt->set_data_len( rec, sz );
     133           0 :   rec->vt->set_owner( rec, &fd_sysvar_owner_id );
     134             : 
     135           0 :   fd_txn_account_mutable_fini( rec, slot_ctx->funk, slot_ctx->funk_txn );
     136             : 
     137           0 :   return 0;
     138           0 : }
     139             : 
     140             : fd_slot_history_global_t *
     141             : fd_sysvar_slot_history_read( fd_funk_t *     funk,
     142             :                              fd_funk_txn_t * funk_txn,
     143           0 :                              fd_spad_t *     spad ) {
     144             : 
     145             :   /* Set current_slot, and update next_slot */
     146             : 
     147           0 :   fd_pubkey_t const * key = &fd_sysvar_slot_history_id;
     148             : 
     149           0 :   FD_TXN_ACCOUNT_DECL( rec );
     150           0 :   int err = fd_txn_account_init_from_funk_readonly( rec, key, funk, funk_txn );
     151           0 :   if( err ) {
     152           0 :     FD_LOG_CRIT(( "fd_txn_account_init_from_funk_readonly(slot_history) failed: %d", err ));
     153           0 :   }
     154             : 
     155             :   /* This check is needed as a quirk of the fuzzer. If a sysvar account
     156             :      exists in the accounts database, but doesn't have any lamports,
     157             :      this means that the account does not exist. This wouldn't happen
     158             :      in a real execution environment. */
     159           0 :   if( FD_UNLIKELY( rec->vt->get_lamports( rec ) == 0UL ) ) {
     160           0 :     return NULL;
     161           0 :   }
     162             : 
     163           0 :   fd_bincode_decode_ctx_t ctx = {
     164           0 :     .data    = rec->vt->get_data( rec ),
     165           0 :     .dataend = rec->vt->get_data( rec ) + rec->vt->get_data_len( rec )
     166           0 :   };
     167             : 
     168           0 :   ulong total_sz = 0UL;
     169           0 :   err = fd_slot_history_decode_footprint( &ctx, &total_sz );
     170           0 :   if( err ) {
     171           0 :     FD_LOG_ERR(( "fd_slot_history_decode_footprint failed" ));
     172           0 :   }
     173             : 
     174           0 :   uchar * mem = fd_spad_alloc( spad, fd_slot_history_align(), total_sz );
     175           0 :   if( !mem ) {
     176           0 :     FD_LOG_ERR(( "Unable to allocate memory for slot history" ));
     177           0 :   }
     178             : 
     179           0 :   return fd_slot_history_decode_global( mem, &ctx );
     180           0 : }
     181             : 
     182             : int
     183             : fd_sysvar_slot_history_find_slot( fd_slot_history_global_t const * history,
     184             :                                   ulong                            slot,
     185           0 :                                   fd_wksp_t *                      wksp ) {
     186           0 :   (void)wksp;
     187           0 :   ulong * blocks = (ulong *)((uchar*)history + history->bits_bitvec_offset);
     188           0 :   if( FD_UNLIKELY( !blocks ) ) {
     189           0 :     FD_LOG_ERR(( "Unable to find slot history blocks" ));
     190           0 :   }
     191           0 :   ulong blocks_len = history->bits_bitvec_len;
     192             : 
     193             : 
     194           0 :   if( slot > history->next_slot - 1UL ) {
     195           0 :     return FD_SLOT_HISTORY_SLOT_FUTURE;
     196           0 :   } else if ( slot < fd_ulong_sat_sub( history->next_slot, slot_history_max_entries ) ) {
     197           0 :     return FD_SLOT_HISTORY_SLOT_TOO_OLD;
     198           0 :   } else {
     199           0 :     ulong block_idx = (slot / bits_per_block) % blocks_len;
     200           0 :     if( blocks[ block_idx ] & ( 1UL << ( slot % bits_per_block ) ) ) {
     201           0 :       return FD_SLOT_HISTORY_SLOT_FOUND;
     202           0 :     } else {
     203           0 :       return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
     204           0 :     }
     205           0 :   }
     206           0 : }

Generated by: LCOV version 1.14