LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_epoch_schedule.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 86 109 78.9 %
Date: 2026-01-13 05:01:51 Functions: 6 8 75.0 %

          Line data    Source code
       1             : #include "fd_sysvar_epoch_schedule.h"
       2             : #include "fd_sysvar.h"
       3             : #include "../fd_system_ids.h"
       4             : #include "../../accdb/fd_accdb_sync.h"
       5             : 
       6             : fd_epoch_schedule_t *
       7             : fd_epoch_schedule_derive( fd_epoch_schedule_t * schedule,
       8             :                           ulong                 epoch_len,
       9             :                           ulong                 leader_schedule_slot_offset,
      10       12105 :                           int                   warmup ) {
      11             : 
      12       12105 :   if( FD_UNLIKELY( epoch_len < FD_EPOCH_LEN_MIN ) ) {
      13           3 :     FD_LOG_WARNING(( "epoch_len too small" ));
      14           3 :     return NULL;
      15           3 :   }
      16             : 
      17       12102 :   *schedule = (fd_epoch_schedule_t) {
      18       12102 :     .slots_per_epoch             = epoch_len,
      19       12102 :     .leader_schedule_slot_offset = leader_schedule_slot_offset,
      20       12102 :     .warmup                      = !!warmup
      21       12102 :   };
      22             : 
      23       12102 :   if( warmup ) {
      24        6051 :     ulong ceil_log2_epoch   = (ulong)fd_ulong_find_msb( epoch_len-1UL ) + 1UL;
      25        6051 :     ulong ceil_log2_len_min = (ulong)fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
      26             : 
      27        6051 :     schedule->first_normal_epoch = fd_ulong_sat_sub( ceil_log2_epoch, ceil_log2_len_min );
      28        6051 :     schedule->first_normal_slot  = (1UL << ceil_log2_epoch) - FD_EPOCH_LEN_MIN;
      29        6051 :   }
      30             : 
      31       12102 :   return schedule;
      32       12105 : }
      33             : 
      34             : void
      35             : fd_sysvar_epoch_schedule_write( fd_bank_t *                 bank,
      36             :                                 fd_accdb_user_t *           accdb,
      37             :                                 fd_funk_txn_xid_t const *   xid,
      38             :                                 fd_capture_ctx_t *          capture_ctx,
      39           9 :                                 fd_epoch_schedule_t const * epoch_schedule ) {
      40           9 :   ulong sz = fd_epoch_schedule_size( epoch_schedule );
      41             :   /* TODO remove alloca */
      42           9 :   uchar enc[ sz ];
      43           9 :   memset( enc, 0, sz );
      44           9 :   fd_bincode_encode_ctx_t ctx = {
      45           9 :     .data    = enc,
      46           9 :     .dataend = enc + sz
      47           9 :   };
      48           9 :   if( fd_epoch_schedule_encode( epoch_schedule, &ctx ) ) {
      49           0 :     FD_LOG_ERR(("fd_epoch_schedule_encode failed"));
      50           0 :   }
      51             : 
      52           9 :   fd_sysvar_account_update( bank, accdb, xid, capture_ctx, &fd_sysvar_epoch_schedule_id, enc, sz );
      53           9 : }
      54             : 
      55             : fd_epoch_schedule_t *
      56             : fd_sysvar_epoch_schedule_read( fd_accdb_user_t *         accdb,
      57             :                                fd_funk_txn_xid_t const * xid,
      58           0 :                                fd_epoch_schedule_t *     out ) {
      59           0 :   fd_accdb_ro_t ro[1];
      60           0 :   if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, &fd_sysvar_epoch_schedule_id ) ) ) {
      61           0 :     return NULL;
      62           0 :   }
      63             : 
      64             :   /* This check is needed as a quirk of the fuzzer. If a sysvar account
      65             :      exists in the accounts database, but doesn't have any lamports,
      66             :      this means that the account does not exist. This wouldn't happen
      67             :      in a real execution environment. */
      68           0 :   if( FD_UNLIKELY( fd_accdb_ref_lamports( ro )==0UL ) ) {
      69           0 :     fd_accdb_close_ro( accdb, ro );
      70           0 :     return NULL;
      71           0 :   }
      72             : 
      73           0 :   fd_epoch_schedule_t * rc = fd_bincode_decode_static(
      74           0 :       epoch_schedule, out,
      75           0 :       fd_accdb_ref_data_const( ro ),
      76           0 :       fd_accdb_ref_data_sz   ( ro ),
      77           0 :       NULL );
      78           0 :   fd_accdb_close_ro( accdb, ro );
      79           0 :   return rc;
      80           0 : }
      81             : 
      82             : void
      83             : fd_sysvar_epoch_schedule_init( fd_bank_t *               bank,
      84             :                                fd_accdb_user_t *         accdb,
      85             :                                fd_funk_txn_xid_t const * xid,
      86           0 :                                fd_capture_ctx_t *        capture_ctx ) {
      87           0 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
      88           0 :   fd_sysvar_epoch_schedule_write( bank, accdb, xid, capture_ctx, epoch_schedule );
      89           0 : }
      90             : 
      91             : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L105 */
      92             : 
      93             : ulong
      94             : fd_epoch_slot_cnt( fd_epoch_schedule_t const * schedule,
      95          75 :                    ulong                       epoch ) {
      96             : 
      97          75 :   if( FD_UNLIKELY( epoch < schedule->first_normal_epoch ) ) {
      98          63 :     ulong exp = fd_ulong_sat_add( epoch, (ulong)fd_ulong_find_lsb( FD_EPOCH_LEN_MIN ) );
      99          63 :     return ( exp<64UL ? 1UL<<exp : ULONG_MAX ); // saturating_pow
     100          63 :   }
     101             : 
     102          12 :   return schedule->slots_per_epoch;
     103          75 : }
     104             : 
     105             : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L170 */
     106             : 
     107             : ulong
     108             : fd_epoch_slot0( fd_epoch_schedule_t const * schedule,
     109         228 :                 ulong                       epoch ) {
     110         228 :   if( FD_UNLIKELY( epoch <= schedule->first_normal_epoch ) ) {
     111         207 :     ulong power = fd_ulong_if( epoch<64UL, 1UL<<epoch, ULONG_MAX );
     112         207 :     return fd_ulong_sat_mul( power-1UL, FD_EPOCH_LEN_MIN );
     113         207 :   }
     114             : 
     115          21 :   return fd_ulong_sat_add(
     116          21 :           fd_ulong_sat_mul(
     117          21 :             fd_ulong_sat_sub(
     118          21 :               epoch,
     119          21 :               schedule->first_normal_epoch),
     120          21 :             schedule->slots_per_epoch),
     121          21 :           schedule->first_normal_slot);
     122         228 : }
     123             : 
     124             : /* https://github.com/solana-labs/solana/blob/88aeaa82a856fc807234e7da0b31b89f2dc0e091/sdk/program/src/epoch_schedule.rs#L140 */
     125             : 
     126             : ulong
     127             : fd_slot_to_epoch( fd_epoch_schedule_t const * schedule,
     128             :                   ulong                       slot,
     129     1319916 :                   ulong *                     out_offset_opt ) {
     130             : 
     131     1319916 :   if( FD_UNLIKELY( schedule->slots_per_epoch == 0UL ) ) {
     132           3 :     FD_LOG_WARNING(( "zero slots_per_epoch" ));
     133           3 :     return 0UL;
     134           3 :   }
     135             : 
     136     1319913 :   ulong epoch;
     137     1319913 :   ulong offset;
     138             : 
     139     1319913 :   if( FD_UNLIKELY( slot < schedule->first_normal_slot ) ) {
     140             :     /* step0 = ceil(log2(FD_EPOCH_LEN_MIN))
     141             :        step1 = ceil(log2(FD_EPOCH_LEN_MIN + slot + 1))
     142             :        epoch = step1 - step0 - 1 */
     143             : 
     144       23715 :     ulong s0 = FD_EPOCH_LEN_MIN + slot + 1UL;
     145             :     /* Invariant: s0 > 1UL */
     146             :     /* Invariant: s0 > FD_EPOCH_LEN_MIN */
     147             : 
     148             :     /* Find lowest exp where (2^exp) >= s0
     149             :        (Only valid for s0 > 1UL and FD_EPOCH_LEN_MIN > 1UL) */
     150       23715 :     int   exp       = fd_ulong_find_msb( s0-1UL ) + 1;
     151       23715 :     int   min_exp   = fd_ulong_find_msb( FD_EPOCH_LEN_MIN );
     152       23715 :           epoch     = (ulong)( exp - min_exp - 1 );
     153       23715 :     ulong epoch_len = 1UL<<( epoch + (ulong)fd_uint_find_lsb( FD_EPOCH_LEN_MIN ) );
     154       23715 :           offset    = slot - ( epoch_len - FD_EPOCH_LEN_MIN );
     155     1296198 :   } else {
     156             :     // FD_LOG_WARNING(("First %lu slots per epoch %lu", schedule->first_normal_slot, schedule->slots_per_epoch));
     157     1296198 :     ulong n_slot  = slot - schedule->first_normal_slot;
     158     1296198 :     ulong n_epoch = n_slot / schedule->slots_per_epoch;
     159     1296198 :           epoch   = schedule->first_normal_epoch + n_epoch;
     160     1296198 :           offset  = n_slot % schedule->slots_per_epoch;
     161     1296198 :   }
     162             : 
     163     1319913 :   ulong   dummy_out;
     164     1319913 :   ulong * out_offset = out_offset_opt ? out_offset_opt : &dummy_out;
     165             : 
     166     1319913 :   *out_offset = offset;
     167     1319913 :   return epoch;
     168     1319916 : }
     169             : 
     170             : /* https://docs.rs/solana-epoch-schedule/2.2.1/src/solana_epoch_schedule/lib.rs.html#136-151 */
     171             : 
     172             : ulong
     173             : fd_slot_to_leader_schedule_epoch( fd_epoch_schedule_t const * schedule,
     174          39 :                                   ulong                       slot ) {
     175             : 
     176          39 :   if( FD_UNLIKELY( slot<schedule->first_normal_slot ) ) {
     177           3 :     return fd_slot_to_epoch( schedule, slot, NULL ) + 1UL;
     178           3 :   }
     179             : 
     180          36 :   ulong new_slots_since_first_normal_slot =
     181          36 :       slot - schedule->first_normal_slot;
     182          36 :   ulong new_first_normal_leader_schedule_slot =
     183          36 :       new_slots_since_first_normal_slot + schedule->leader_schedule_slot_offset;
     184          36 :   ulong new_epochs_since_first_normal_leader_schedule =
     185          36 :       new_first_normal_leader_schedule_slot / schedule->slots_per_epoch;
     186             : 
     187          36 :   return schedule->first_normal_epoch + new_epochs_since_first_normal_leader_schedule;
     188          39 : }

Generated by: LCOV version 1.14