LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_cache.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 100 136 73.5 %
Date: 2026-11-11 12:40:43 Functions: 18 21 85.7 %

          Line data    Source code
       1             : #include "fd_sysvar_cache.h"
       2             : #include "fd_sysvar_cache_private.h"
       3             : #include "fd_sysvar_recent_hashes.h"
       4             : #include "fd_sysvar_slot_hashes.h"
       5             : #include "fd_sysvar_stake_history.h"
       6             : 
       7             : #include <errno.h>
       8             : 
       9             : void *
      10       64411 : fd_sysvar_cache_new( void * mem ) {
      11             : 
      12       64411 :   if( FD_UNLIKELY( !mem ) ) {
      13           0 :     FD_LOG_WARNING(( "NULL mem" ));
      14           0 :     return NULL;
      15           0 :   }
      16       64411 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, alignof(fd_sysvar_cache_t) ) ) ) {
      17           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      18           0 :     return NULL;
      19           0 :   }
      20             : 
      21       64411 :   fd_sysvar_cache_t * sysvar_cache = mem;
      22       64411 :   sysvar_cache->magic = 0UL;
      23       64411 :   memset( sysvar_cache->desc, 0, FD_SYSVAR_CACHE_ENTRY_CNT*sizeof(fd_sysvar_desc_t) );
      24             : 
      25       64411 :   FD_COMPILER_MFENCE();
      26       64411 :   sysvar_cache->magic = FD_SYSVAR_CACHE_MAGIC;
      27       64411 :   FD_COMPILER_MFENCE();
      28             : 
      29       64411 :   return sysvar_cache;
      30       64411 : }
      31             : 
      32             : fd_sysvar_cache_t *
      33       64401 : fd_sysvar_cache_join( void * mem ) {
      34             :   /* FIXME This is a good place to ref-count writable joins */
      35       64401 :   return (fd_sysvar_cache_t *)fd_sysvar_cache_join_const( mem );
      36       64401 : }
      37             : 
      38             : fd_sysvar_cache_t const *
      39       64410 : fd_sysvar_cache_join_const( void const * mem ) {
      40             : 
      41       64410 :   if( FD_UNLIKELY( !mem ) ) {
      42           0 :     FD_LOG_WARNING(( "NULL mem" ));
      43           0 :     return NULL;
      44           0 :   }
      45       64410 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, alignof(fd_sysvar_cache_t) ) ) ) {
      46           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      47           0 :     return NULL;
      48           0 :   }
      49       64410 :   fd_sysvar_cache_t const * sysvar_cache = mem;
      50       64410 :   if( FD_UNLIKELY( sysvar_cache->magic != FD_SYSVAR_CACHE_MAGIC ) ) {
      51           0 :     FD_LOG_WARNING(( "bad magic" ));
      52           0 :     return NULL;
      53           0 :   }
      54             : 
      55       64410 :   return sysvar_cache;
      56       64410 : }
      57             : 
      58             : void *
      59        8732 : fd_sysvar_cache_leave( fd_sysvar_cache_t * sysvar_cache ) {
      60        8732 :   return sysvar_cache;
      61        8732 : }
      62             : 
      63             : void const *
      64           0 : fd_sysvar_cache_leave_const( fd_sysvar_cache_t const * sysvar_cache ) {
      65           0 :   return sysvar_cache;
      66           0 : }
      67             : 
      68             : void *
      69           0 : fd_sysvar_cache_delete( void * mem ) {
      70             : 
      71           0 :   if( FD_UNLIKELY( !mem ) ) {
      72           0 :     FD_LOG_WARNING(( "NULL mem" ));
      73           0 :     return NULL;
      74           0 :   }
      75           0 :   fd_sysvar_cache_t * sysvar_cache = mem;
      76           0 :   if( FD_UNLIKELY( sysvar_cache->magic != FD_SYSVAR_CACHE_MAGIC ) ) {
      77           0 :     FD_LOG_WARNING(( "bad magic" ));
      78           0 :     return NULL;
      79           0 :   }
      80             : 
      81           0 :   memset( sysvar_cache, 0, sizeof(fd_sysvar_cache_t) );
      82             : 
      83           0 :   return mem;
      84           0 : }
      85             : 
      86             : uchar const *
      87             : fd_sysvar_cache_data_query(
      88             :     fd_sysvar_cache_t const * sysvar_cache,
      89             :     void const *              address, /* 32 bytes */
      90             :     ulong *                   psz
      91          76 : ) {
      92          76 :   *psz = 0UL;
      93          76 :   fd_pubkey_t const pubkey = FD_LOAD( fd_pubkey_t, address );
      94          76 :   sysvar_tbl_t const * entry = sysvar_map_query( &pubkey, NULL );
      95          76 :   if( FD_UNLIKELY( !entry ) ) return NULL; /* address is not a sysvar */
      96          76 :   fd_sysvar_desc_t const * desc = &sysvar_cache->desc[ entry->desc_idx ];
      97          76 :   fd_sysvar_pos_t const *  pos  = &fd_sysvar_pos_tbl [ entry->desc_idx ];
      98          76 :   if( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) return NULL; /* sysvar data invalid */
      99          73 :   *psz = desc->data_sz;
     100          73 :   return (uchar const *)sysvar_cache + pos->data_off;
     101          76 : }
     102             : 
     103             : /* Generate accessors for sysvars that are backed by POD structs. */
     104             : 
     105             : #define SIMPLE_SYSVAR_READ( name, name2, typet )                       \
     106             :   typet *                                                              \
     107             :   fd_sysvar_cache_##name##_read( fd_sysvar_cache_t const * cache,      \
     108      191436 :                                  typet *                   out ) {     \
     109      191436 :     ulong const idx = FD_SYSVAR_##name##_IDX;                          \
     110      191436 :     fd_sysvar_desc_t const * desc = &cache->desc[ idx ];               \
     111      191436 :     fd_sysvar_pos_t const *  pos  = &fd_sysvar_pos_tbl[ idx ];         \
     112      191436 :     if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL; \
     113      191436 :     memcpy( out, (uchar *)cache+pos->data_off, pos->data_max );        \
     114      191428 :     return out;                                                        \
     115      191436 :   }
     116             : 
     117             : #define SIMPLE_SYSVAR( name, name2, type ) \
     118             :   SIMPLE_SYSVAR_READ( name, name2, fd_##type##_t )
     119             : FD_SYSVAR_SIMPLE_ITER( SIMPLE_SYSVAR )
     120             : #undef SIMPLE_SYSVAR
     121             : #undef SIMPLE_SYSVAR_READ
     122             : 
     123             : ulong const *
     124          90 : fd_sysvar_cache_last_restart_slot_read( fd_sysvar_cache_t const * cache ) {
     125          90 :   ulong const idx = FD_SYSVAR_last_restart_slot_IDX;
     126          90 :   fd_sysvar_desc_t const * desc = &cache->desc[ idx ];
     127          90 :   fd_sysvar_pos_t const *  pos  = &fd_sysvar_pos_tbl[ idx ];
     128          90 :   if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
     129          69 :   return fd_type_pun_const( (uchar const *)cache + pos->data_off );
     130          90 : }
     131             : 
     132             : int
     133       56610 : fd_sysvar_cache_recent_hashes_is_empty( fd_sysvar_cache_t const * sysvar_cache ) {
     134       56610 :   fd_sysvar_desc_t const * desc = &sysvar_cache->desc[ FD_SYSVAR_recent_hashes_IDX ];
     135       56610 :   if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return 1;
     136       56610 :   FD_TEST( desc->data_sz >= sizeof(ulong) );
     137       56610 :   ulong len = FD_LOAD( ulong, sysvar_cache->bin_recent_hashes );
     138       56610 :   return len == 0UL;
     139       56610 : }
     140             : 
     141             : int
     142             : fd_sysvar_obj_restore( fd_sysvar_cache_t *     cache,
     143             :                        fd_sysvar_desc_t *      desc,
     144      324569 :                        fd_sysvar_pos_t const * pos ) {
     145      324569 :   desc->flags &= ~FD_SYSVAR_FLAG_VALID;
     146             : 
     147      324569 :   uchar const * data    = (uchar const *)cache + pos->data_off;
     148      324569 :   ulong const   data_sz = desc->data_sz;
     149             : 
     150      324569 :   if( FD_UNLIKELY( !pos->validate( data, data_sz ) ) ) {
     151         133 :     FD_LOG_DEBUG(( "Failed to validate sysvar %s with data_sz=%lu",
     152         133 :                     pos->name, data_sz ));
     153         133 :     return EINVAL;
     154         133 :   }
     155      324436 :   desc->flags |= FD_SYSVAR_FLAG_VALID;
     156      324436 :   return 0;
     157      324569 : }
     158             : 
     159             : static int
     160       64965 : fd_sysvar_validate_clock( uchar const * data, ulong data_sz ) {
     161       64965 :   (void)data;
     162       64965 :   return data_sz >= sizeof(fd_sol_sysvar_clock_t );
     163       64965 : }
     164             : 
     165             : static int
     166       64973 : fd_sysvar_validate_rent( uchar const * data, ulong data_sz ) {
     167       64973 :   (void)data;
     168       64973 :   return data_sz >= sizeof(fd_rent_t );
     169       64973 : }
     170             : 
     171             : static int
     172       26857 : fd_sysvar_validate_last_restart_slot( uchar const * data, ulong data_sz ) {
     173       26857 :   (void)data;
     174       26857 :   return data_sz >= sizeof(ulong);
     175       26857 : }
     176             : 
     177             : static int
     178        7781 : fd_sysvar_validate_epoch_rewards( uchar const * data, ulong data_sz ) {
     179        7781 :   if( FD_UNLIKELY( data_sz < sizeof(fd_sysvar_epoch_rewards_t) ) ) return 0;
     180        7696 :   fd_sysvar_epoch_rewards_t ew = FD_LOAD( fd_sysvar_epoch_rewards_t, data );
     181        7696 :   uchar active = ew.active;
     182        7696 :   if( FD_UNLIKELY( active!=0 && active!=1 ) ) return 0;
     183        7693 :   return 1;
     184        7696 : }
     185             : 
     186             : static int
     187       64966 : fd_sysvar_validate_epoch_schedule( uchar const * data, ulong data_sz ) {
     188       64966 :   if( FD_UNLIKELY( data_sz < sizeof(fd_epoch_schedule_t) ) ) return 0;
     189       64966 :   fd_epoch_schedule_t es = FD_LOAD( fd_epoch_schedule_t, data );
     190       64966 :   uchar warmup = es.warmup;
     191       64966 :   if( FD_UNLIKELY( warmup!=0 && warmup!=1 ) ) return 0;
     192       64966 :   return 1;
     193       64966 : }
     194             : 
     195             : fd_sysvar_pos_t const fd_sysvar_pos_tbl[ FD_SYSVAR_CACHE_ENTRY_CNT ] = {
     196             :   [FD_SYSVAR_clock_IDX] =
     197             :     { .name="clock",
     198             :       .data_off=offsetof(fd_sysvar_cache_t, bin_clock            ), .data_max=FD_SYSVAR_CLOCK_BINCODE_SZ,
     199             :       .validate=fd_sysvar_validate_clock },
     200             :   [FD_SYSVAR_epoch_rewards_IDX] =
     201             :     { .name="epoch rewards",
     202             :       .data_off=offsetof(fd_sysvar_cache_t, bin_epoch_rewards    ), .data_max=FD_SYSVAR_EPOCH_REWARDS_BINCODE_SZ,
     203             :       .validate=fd_sysvar_validate_epoch_rewards },
     204             :   [FD_SYSVAR_epoch_schedule_IDX] =
     205             :     { .name="epoch schedule",
     206             :       .data_off=offsetof(fd_sysvar_cache_t, bin_epoch_schedule   ), .data_max=FD_SYSVAR_EPOCH_SCHEDULE_BINCODE_SZ,
     207             :       .validate=fd_sysvar_validate_epoch_schedule },
     208             :   [FD_SYSVAR_last_restart_slot_IDX] =
     209             :     { .name="last restart slot",
     210             :       .data_off=offsetof(fd_sysvar_cache_t, bin_last_restart_slot), .data_max=FD_SYSVAR_LAST_RESTART_SLOT_BINCODE_SZ,
     211             :       .validate=fd_sysvar_validate_last_restart_slot },
     212             :   [FD_SYSVAR_recent_hashes_IDX] =
     213             :     { .name="recent blockhashes",
     214             :       .data_off=offsetof(fd_sysvar_cache_t, bin_recent_hashes    ), .data_max=FD_SYSVAR_RECENT_HASHES_BINCODE_SZ,
     215             :       .validate=fd_sysvar_recent_hashes_validate },
     216             :   [FD_SYSVAR_rent_IDX] =
     217             :     { .name="rent",
     218             :       .data_off=offsetof(fd_sysvar_cache_t, bin_rent             ), .data_max=FD_SYSVAR_RENT_BINCODE_SZ,
     219             :       .validate=fd_sysvar_validate_rent },
     220             :   [FD_SYSVAR_slot_hashes_IDX] =
     221             :     { .name="slot hashes",
     222             :       .data_off=offsetof(fd_sysvar_cache_t, bin_slot_hashes      ), .data_max=FD_SYSVAR_SLOT_HASHES_BINCODE_SZ,
     223             :       .validate=fd_sysvar_slot_hashes_validate },
     224             :   [FD_SYSVAR_stake_history_IDX] =
     225             :     { .name="stake history",
     226             :       .data_off=offsetof(fd_sysvar_cache_t, bin_stake_history    ), .data_max=FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ,
     227             :       .validate=fd_sysvar_stake_history_validate },
     228             : };
     229             : 
     230             : fd_slot_hashes_t *
     231             : fd_sysvar_cache_slot_hashes_view( fd_sysvar_cache_t const * cache,
     232        9085 :                                   fd_slot_hashes_t *        view ) {
     233        9085 :   fd_sysvar_desc_t const * desc = &cache->desc[ FD_SYSVAR_slot_hashes_IDX ];
     234        9085 :   if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
     235        9085 :   return fd_sysvar_slot_hashes_view( view, cache->bin_slot_hashes, desc->data_sz );
     236        9085 : }
     237             : 
     238             : fd_stake_history_t *
     239             : fd_sysvar_cache_stake_history_view( fd_sysvar_cache_t const * cache,
     240           0 :                                     fd_stake_history_t *      view ) {
     241           0 :   fd_sysvar_desc_t const * desc = &cache->desc[ FD_SYSVAR_stake_history_IDX ];
     242           0 :   if( FD_UNLIKELY( !( desc->flags & FD_SYSVAR_FLAG_VALID ) ) ) return NULL;
     243           0 :   return fd_sysvar_stake_history_view( view, cache->bin_stake_history, desc->data_sz );
     244           0 : }

Generated by: LCOV version 1.14