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: 42 125 33.6 %
Date: 2026-11-26 02:16:43 Functions: 2 5 40.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         611 : #define FD_SLOT_HISTORY_BITS_PER_BLOCK (8UL * sizeof(ulong))
       7             : 
       8           0 : #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           0 :                              fd_capture_ctx_t * capture_ctx ) {
      16           0 :   uchar data[ FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ ];
      17           0 :   uchar * p = data;
      18             : 
      19             :   /* has_bits */
      20           0 :   *p = 1;
      21           0 :   p++;
      22             : 
      23             :   /* bits_bitvec_len */
      24           0 :   FD_STORE( ulong, p, FD_SLOT_HISTORY_BLOCKS_LEN );
      25           0 :   p += sizeof(ulong);
      26             : 
      27             :   /* content */
      28           0 :   fd_memset( p, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
      29           0 :   p += FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong);
      30             : 
      31             :   /* bits_len */
      32           0 :   FD_STORE( ulong, p, FD_SLOT_HISTORY_MAX_ENTRIES );
      33           0 :   p += sizeof(ulong);
      34             : 
      35             :   /* next_slot */
      36           0 :   FD_STORE( ulong, p, bank->f.slot + 1UL );
      37           0 :   p += sizeof(ulong);
      38             : 
      39           0 :   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           0 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_slot_history_id, data, FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ );
      42           0 : }
      43             : 
      44             : void
      45             : fd_sysvar_slot_history_update( fd_bank_t *        bank,
      46             :                                fd_accdb_t *       accdb,
      47         611 :                                fd_capture_ctx_t * capture_ctx ) {
      48         611 :   fd_accdb_svm_update_t update[1];
      49         611 :   fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, &fd_sysvar_slot_history_id, 1 );
      50         611 :   if( FD_UNLIKELY( !acc.lamports ) ) {
      51             :     /* SlotHistory::default() contains slot zero, with next_slot=1. */
      52           0 :     acc.data_len = FD_SYSVAR_SLOT_HISTORY_BINCODE_SZ;
      53           0 :     acc.executable = 0;
      54           0 :     fd_memcpy( acc.owner, fd_sysvar_owner_id.uc, sizeof(fd_pubkey_t) );
      55           0 :     fd_memset( acc.data, 0, acc.data_len );
      56           0 :     acc.data[0] = 1;
      57           0 :     FD_STORE( ulong, acc.data+1UL, FD_SLOT_HISTORY_BLOCKS_LEN );
      58           0 :     FD_STORE( ulong, acc.data+9UL, 1UL );
      59           0 :     FD_STORE( ulong, acc.data+9UL+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong), FD_SLOT_HISTORY_MAX_ENTRIES );
      60           0 :     FD_STORE( ulong, acc.data+17UL+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong), 1UL );
      61           0 :   }
      62         611 :   FD_TEST( !memcmp( acc.owner, fd_sysvar_owner_id.uc, sizeof(fd_pubkey_t) ) ); /* Slot history account must be owned by sysvar owner */
      63         611 :   if( FD_UNLIKELY( !fd_sysvar_slot_history_validate( acc.data, acc.data_len ) ) ) {
      64           0 :     FD_LOG_HEXDUMP_ERR(( "invalid slot history sysvar", acc.data, acc.data_len ));
      65           0 :   }
      66             : 
      67         611 :   if( FD_UNLIKELY( acc.data[ 0 ]!=1 ) ) {
      68             :     /* initialize if !has_bits */
      69           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 ));
      70           0 :     acc.data[ 0 ] = 1;
      71           0 :     FD_STORE( ulong, acc.data+1, FD_SLOT_HISTORY_BLOCKS_LEN );
      72           0 :     fd_memset( acc.data+9, 0, FD_SLOT_HISTORY_BLOCKS_LEN * sizeof(ulong) );
      73           0 :     FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong), FD_SLOT_HISTORY_MAX_ENTRIES );
      74           0 :     FD_STORE( ulong, acc.data+9+FD_SLOT_HISTORY_BLOCKS_LEN*sizeof(ulong)+8UL, 0UL );
      75           0 :   }
      76             : 
      77         611 :   ulong bits_bitvec_len = FD_LOAD( ulong, acc.data+1UL );
      78         611 :   if( FD_UNLIKELY( !bits_bitvec_len ) ) {
      79           0 :     fd_sysvar_adjust_balance_for_rent( bank, &acc );
      80           0 :     fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
      81           0 :     return;
      82           0 :   }
      83         611 :   ulong min_sz;
      84         611 :   if( FD_UNLIKELY( __builtin_umull_overflow( bits_bitvec_len, sizeof(ulong), &min_sz ) ) ) {
      85           0 :     FD_LOG_ERR(( "invalid slot history sysvar: bits_bitvec_len overflow (%lu)", bits_bitvec_len ));
      86           0 :   }
      87         611 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) {
      88           0 :     FD_LOG_ERR(( "invalid slot history sysvar: min_sz overflow" ));
      89           0 :   }
      90         611 :   if( FD_UNLIKELY( acc.data_len < min_sz ) ) {
      91           0 :     FD_LOG_ERR(( "invalid slot history sysvar: data_sz too small (%lu, required %lu)", acc.data_len, min_sz ));
      92           0 :   }
      93         611 :   uchar * bits      = acc.data + 9UL;
      94         611 :   uchar * footer    = acc.data + 9UL + bits_bitvec_len * sizeof(ulong);
      95         611 :   ulong   next_slot = FD_LOAD( ulong, footer+8UL );
      96             : 
      97             :   /* https://github.com/anza-xyz/solana-sdk/blob/slot-history%40v2.2.1/slot-history/src/lib.rs#L62-L74 */
      98         611 :   ulong cur_slot = bank->f.slot;
      99         611 :   if( FD_UNLIKELY( cur_slot>next_slot && cur_slot-next_slot>=FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
     100           0 :     fd_memset( bits, 0, bits_bitvec_len * sizeof(ulong) );
     101         611 :   } else {
     102         611 :     for( ulong i=next_slot; i<cur_slot; i++ ) {
     103           0 :       ulong   block_idx = (i / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
     104           0 :       uchar * word      = &bits[ block_idx*sizeof(ulong) ];
     105           0 :       FD_STORE( ulong, word, FD_LOAD( ulong, word ) & (~(1UL << (i % FD_SLOT_HISTORY_BITS_PER_BLOCK))) );
     106           0 :     }
     107         611 :   }
     108             : 
     109             :   /* new slot */
     110         611 :   ulong   block_idx = (cur_slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % bits_bitvec_len;
     111         611 :   uchar * word      = &bits[ block_idx*sizeof(ulong) ];
     112         611 :   FD_STORE( ulong, word, FD_LOAD( ulong, word ) | (1UL << (cur_slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) );
     113             : 
     114         611 :   FD_STORE( ulong, footer+8UL, cur_slot+1UL );
     115             : 
     116         611 :   fd_sysvar_adjust_balance_for_rent( bank, &acc );
     117         611 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
     118         611 : }
     119             : 
     120             : int
     121             : fd_sysvar_slot_history_validate( uchar const * data,
     122         610 :                                  ulong         sz ) {
     123         610 :   if( FD_UNLIKELY( sz < 17UL ) ) return 0;
     124         610 :   uchar has_bits = data[0];
     125         610 :   if( FD_UNLIKELY( has_bits>1 ) ) return 0;
     126         610 :   if( !has_bits ) return 1;
     127         610 :   if( FD_UNLIKELY( sz < 25UL ) ) return 0;
     128         610 :   ulong blocks_len = FD_LOAD( ulong, data+1 );
     129         610 :   ulong min_sz;
     130         610 :   if( FD_UNLIKELY( __builtin_umull_overflow( blocks_len, sizeof(ulong), &min_sz ) ) ) return 0;
     131         610 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( min_sz, 25UL, &min_sz ) ) ) return 0;
     132         610 :   if( FD_UNLIKELY( sz < min_sz ) ) return 0;
     133         610 :   return 1;
     134         610 : }
     135             : 
     136             : fd_slot_history_view_t *
     137             : fd_sysvar_slot_history_view( fd_slot_history_view_t * view,
     138             :                              uchar const *            data,
     139           0 :                              ulong                    sz ) {
     140           0 :   if( FD_UNLIKELY( !fd_sysvar_slot_history_validate( data, sz ) ) ) return NULL;
     141           0 :   if( FD_UNLIKELY( !data[0] ) ) {
     142           0 :     view->bits       = NULL;
     143           0 :     view->blocks_len = 0UL;
     144           0 :     view->bits_len   = FD_LOAD( ulong, data+1    );
     145           0 :     view->next_slot  = FD_LOAD( ulong, data+1+8UL );
     146           0 :     return view;
     147           0 :   }
     148           0 :   ulong blocks_len = FD_LOAD( ulong, data+1 );
     149           0 :   uchar const * footer = data + 9UL + blocks_len * sizeof(ulong);
     150           0 :   view->bits       = data + 9UL;
     151           0 :   view->blocks_len = blocks_len;
     152           0 :   view->bits_len   = FD_LOAD( ulong, footer    );
     153           0 :   view->next_slot  = FD_LOAD( ulong, footer+8UL );
     154           0 :   return view;
     155           0 : }
     156             : 
     157             : int
     158             : fd_sysvar_slot_history_find_slot( fd_slot_history_view_t const * view,
     159           0 :                                   ulong                          slot ) {
     160           0 :   if( FD_UNLIKELY( !view->blocks_len ) ) return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
     161           0 :   if( slot > view->next_slot - 1UL ) {
     162           0 :     return FD_SLOT_HISTORY_SLOT_FUTURE;
     163           0 :   }
     164           0 :   if( slot < fd_ulong_sat_sub( view->next_slot, FD_SLOT_HISTORY_MAX_ENTRIES ) ) {
     165           0 :     return FD_SLOT_HISTORY_SLOT_TOO_OLD;
     166           0 :   }
     167           0 :   ulong block_idx = (slot / FD_SLOT_HISTORY_BITS_PER_BLOCK) % view->blocks_len;
     168           0 :   ulong word      = FD_LOAD( ulong, view->bits + block_idx*sizeof(ulong) );
     169           0 :   if( word & (1UL << (slot % FD_SLOT_HISTORY_BITS_PER_BLOCK)) ) {
     170           0 :     return FD_SLOT_HISTORY_SLOT_FOUND;
     171           0 :   }
     172           0 :   return FD_SLOT_HISTORY_SLOT_NOT_FOUND;
     173           0 : }

Generated by: LCOV version 1.14