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: 93 112 83.0 %
Date: 2026-08-17 04:34:10 Functions: 5 5 100.0 %

          Line data    Source code
       1             : #include "fd_sysvar_slot_history.h"
       2             : #include "fd_sysvar.h"
       3             : #include "../fd_system_ids.h"
       4             : #include "../fd_accdb_svm.h"
       5             : 
       6        1692 : #define FD_SLOT_HISTORY_BITS_PER_BLOCK (8UL * sizeof(ulong))
       7             : 
       8          18 : #define FD_SLOT_HISTORY_BLOCKS_LEN (FD_SLOT_HISTORY_MAX_ENTRIES / FD_SLOT_HISTORY_BITS_PER_BLOCK)
       9             : 
      10             : /* https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/slot_history.rs#L16 */
      11             : 
      12             : void
      13             : fd_sysvar_slot_history_init( fd_bank_t *        bank,
      14             :                              fd_accdb_t *       accdb,
      15           9 :                              fd_capture_ctx_t * capture_ctx ) {
      16           9 :   uchar data[ FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ];
      17           9 :   uchar * p = data;
      18             : 
      19             :   /* has_bits */
      20           9 :   *p = 1;
      21           9 :   p++;
      22             : 
      23             :   /* bits_bitvec_len */
      24           9 :   FD_STORE( ulong, p, FD_SLOT_HISTORY_BLOCKS_LEN );
      25           9 :   p += sizeof(ulong);
      26             : 
      27             :   /* content */
      28           9 :   fd_memset( p, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
      29           9 :   p += FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong);
      30             : 
      31             :   /* bits_len */
      32           9 :   FD_STORE( ulong, p, FD_SLOT_HISTORY_MAX_ENTRIES );
      33           9 :   p += sizeof(ulong);
      34             : 
      35             :   /* next_slot */
      36           9 :   FD_STORE( ulong, p, bank->f.slot + 1UL );
      37           9 :   p += sizeof(ulong);
      38             : 
      39           9 :   FD_STATIC_ASSERT( FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ == 1 + sizeof(ulong) + FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) + sizeof(ulong) + sizeof(ulong), "bin code size mismatch" );
      40             : 
      41           9 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_slot_history_id, data, FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ );
      42           9 : }
      43             : 
      44             : void
      45             : fd_sysvar_slot_history_update( fd_bank_t *        bank,
      46             :                                fd_accdb_t *       accdb,
      47         357 :                                fd_capture_ctx_t * capture_ctx ) {
      48         357 :   fd_accdb_svm_update_t update[1];
      49         357 :   fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, &fd_sysvar_slot_history_id, 0 );
      50         357 :   FD_TEST( acc.lamports ); /* Slot history account must exist */
      51         357 :   FD_TEST( !memcmp( acc.owner, fd_sysvar_owner_id.uc, sizeof(fd_pubkey_t) ) ); /* Slot history account must be owned by sysvar owner */
      52             : 
      53         357 :   if( FD_UNLIKELY( acc.data[ 0 ]!=1 ) ) {
      54             :     /* initialize if !has_bits */
      55           0 :     if( FD_UNLIKELY( acc.data_len<FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ) ) FD_LOG_HEXDUMP_ERR(( "invalid slot history sysvar (data_sz too small)", acc.data, acc.data_len ));
      56           0 :     acc.data[ 0 ] = 1;
      57           0 :     FD_STORE( ulong, acc.data+1, FD_SLOT_HISTORY_BLOCKS_LEN );
      58           0 :     fd_memset( acc.data+9, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
      59           0 :     FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong), FD_SLOT_HISTORY_MAX_ENTRIES );
      60           0 :     FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong)+8UL, 0UL );
      61           0 :   }
      62             : 
      63         357 :   ulong bits_bitvec_len = FD_LOAD( ulong, acc.data+1UL );
      64         357 :   if( FD_UNLIKELY( !bits_bitvec_len ) ) {
      65           3 :     fd_sysvar_adjust_balance_for_rent( bank, &acc );
      66           3 :     fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
      67           3 :     return;
      68           3 :   }
      69         354 :   ulong min_sz;
      70         354 :   if( FD_UNLIKELY( __builtin_umull_overflow( bits_bitvec_len, sizeof(ulong), &min_sz ) ) ) {
      71           0 :     FD_LOG_ERR(( "invalid slot history sysvar: bits_bitvec_len overflow (%lu)", bits_bitvec_len ));
      72           0 :   }
      73         354 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) {
      74           0 :     FD_LOG_ERR(( "invalid slot history sysvar: min_sz overflow" ));
      75           0 :   }
      76         354 :   if( FD_UNLIKELY( acc.data_len < min_sz ) ) {
      77           0 :     FD_LOG_ERR(( "invalid slot history sysvar: data_sz too small (%lu, required %lu)", acc.data_len, min_sz ));
      78           0 :   }
      79         354 :   uchar * bits      = acc.data + 9UL;
      80         354 :   uchar * footer    = acc.data + 9UL + bits_bitvec_len * sizeof(ulong);
      81         354 :   ulong   next_slot = FD_LOAD( ulong, footer+8UL );
      82             : 
      83             :   /* https://github.com/anza-xyz/solana-sdk/blob/slot-history%40v2.2.1/slot-history/src/lib.rs#L62-L74 */
      84         354 :   ulong cur_slot = bank->f.slot;
      85         354 :   if( FD_UNLIKELY( cur_slot>next_slot && cur_slot-next_slot>=FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
      86           3 :     fd_memset( bits, 0, bits_bitvec_len * sizeof(ulong) );
      87         351 :   } else {
      88        1581 :     for( ulong i=next_slot; i<cur_slot; i++ ) {
      89        1230 :       ulong   block_idx = (i / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
      90        1230 :       uchar * word      = &bits[ block_idx*sizeof(ulong) ];
      91        1230 :       FD_STORE( ulong, word, FD_LOAD( ulong, word ) & (~(1UL << (i % FD_SLOT_HISTORY_BITS_PER_BLOCK))) );
      92        1230 :     }
      93         351 :   }
      94             : 
      95             :   /* new slot */
      96         354 :   ulong   block_idx = (cur_slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
      97         354 :   uchar * word      = &bits[ block_idx*sizeof(ulong) ];
      98         354 :   FD_STORE( ulong, word, FD_LOAD( ulong, word ) | (1UL << (cur_slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) );
      99             : 
     100         354 :   FD_STORE( ulong, footer+8UL, cur_slot+1UL );
     101             : 
     102         354 :   fd_sysvar_adjust_balance_for_rent( bank, &acc );
     103         354 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
     104         354 : }
     105             : 
     106             : int
     107             : fd_sysvar_slot_history_validate( uchar const * data,
     108        8058 :                                  ulong         sz ) {
     109        8058 :   if( FD_UNLIKELY( sz < 17UL ) ) return 0;
     110        8049 :   uchar has_bits = data[0];
     111        8049 :   if( FD_UNLIKELY( has_bits>1 ) ) return 0;
     112        8046 :   if( !has_bits ) return 1;
     113        8046 :   if( FD_UNLIKELY( sz < 25UL ) ) return 0;
     114        8037 :   ulong blocks_len = FD_LOAD( ulong, data+1 );
     115        8037 :   ulong min_sz;
     116        8037 :   if( FD_UNLIKELY( __builtin_umull_overflow( blocks_len, sizeof(ulong), &min_sz ) ) ) return 0;
     117        8034 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) return 0;
     118        8031 :   if( FD_UNLIKELY( sz < min_sz ) ) return 0;
     119        8028 :   return 1;
     120        8031 : }
     121             : 
     122             : fd_slot_history_view_t *
     123             : fd_sysvar_slot_history_view( fd_slot_history_view_t * view,
     124             :                              uchar const *            data,
     125          33 :                              ulong                    sz ) {
     126          33 :   if( FD_UNLIKELY( !fd_sysvar_slot_history_validate( data, sz ) ) ) return NULL;
     127          27 :   if( FD_UNLIKELY( !data[0] ) ) {
     128           0 :     view->bits       = NULL;
     129           0 :     view->blocks_len = 0UL;
     130           0 :     view->bits_len   = FD_LOAD( ulong, data+1    );
     131           0 :     view->next_slot  = FD_LOAD( ulong, data+1+8UL );
     132           0 :     return view;
     133           0 :   }
     134          27 :   ulong blocks_len = FD_LOAD( ulong, data+1 );
     135          27 :   uchar const * footer = data + 9UL + blocks_len * sizeof(ulong);
     136          27 :   view->bits       = data + 9UL;
     137          27 :   view->blocks_len = blocks_len;
     138          27 :   view->bits_len   = FD_LOAD( ulong, footer    );
     139          27 :   view->next_slot  = FD_LOAD( ulong, footer+8UL );
     140          27 :   return view;
     141          27 : }
     142             : 
     143             : int
     144             : fd_sysvar_slot_history_find_slot( fd_slot_history_view_t const * view,
     145          81 :                                   ulong                          slot ) {
     146          81 :   if( FD_UNLIKELY( !view->blocks_len ) ) return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
     147          78 :   if( slot > view->next_slot - 1UL ) {
     148          21 :     return FD_SLOT_HISTORY_SLOT_FUTURE;
     149          21 :   }
     150          57 :   if( slot < fd_ulong_sat_sub( view->next_slot, FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
     151          12 :     return FD_SLOT_HISTORY_SLOT_TOO_OLD;
     152          12 :   }
     153          45 :   ulong block_idx = (slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % view->blocks_len;
     154          45 :   ulong word      = FD_LOAD( ulong, view->bits + block_idx*sizeof(ulong) );
     155          45 :   if( word & (1UL << (slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) ) {
     156          21 :     return FD_SLOT_HISTORY_SLOT_FOUND;
     157          21 :   }
     158          24 :   return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
     159          45 : }

Generated by: LCOV version 1.14