LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_clock.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 120 163 73.6 %
Date: 2026-10-22 18:10:43 Functions: 5 8 62.5 %

          Line data    Source code
       1             : #include "fd_sysvar_clock.h"
       2             : #include "fd_sysvar_epoch_schedule.h"
       3             : #include "../fd_runtime_stack.h"
       4             : #include "../fd_system_ids.h"
       5             : #include "../sysvar/fd_sysvar.h"
       6             : 
       7             : /* Syvar Clock Possible Values:
       8             :   slot:
       9             :   [0, ULONG_MAX]
      10             : 
      11             :   epoch:
      12             :   [0, slot/432000UL]
      13             : 
      14             :   epoch_start_timestamp:
      15             :   [0, ULONG_MAX]
      16             : 
      17             :   unix_timestamp:
      18             :   This value is bounded by the slot distance from the
      19             :   epoch_start_timestamp.
      20             :   The protocol allows for a maximum drift (either fast or slow) from the
      21             :   start of the epoch's timestamp.  The expected time is called the PoH
      22             :   offset.  This offset is calculated by (epoch_start_timestamp + slots
      23             :   since epoch * slot_duration). The drift is then bounded by the
      24             :   max_allowable_drift_{slow,fast}.  The stake weighted offset can be
      25             :   150% more than the PoH offset and 25% less than the PoH offset.
      26             :   So, the bounds for the unix_timestamp can be calculated by:
      27             :   upper bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 2.5
      28             :   lower bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 0.75
      29             : 
      30             :   leader_schedule_epoch:
      31             :   This is the value of the epoch used for the leader schedule.  It is
      32             :   computed based on the values of the epoch schedule (first_normal_slot,
      33             :   leader_schedule_slot_offset, slots_per_epoch).  It is always equal to
      34             :   ((slot - first_normal_slot) + leader_schedule_slot_offset) / schedule->slots_per_epoch
      35             : */
      36             : 
      37             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L14 */
      38         414 : #define MAX_ALLOWABLE_DRIFT_FAST_PERCENT ( 25U )
      39             : 
      40             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L15 */
      41         414 : #define MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ( 150U )
      42             : 
      43             : /* Do all intermediate calculations at nanosecond precision, to mirror
      44             :    Solana's behavior. */
      45        1134 : #define NS_IN_S ((long)1e9)
      46             : 
      47             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2110-L2117 */
      48             : static inline long
      49           0 : unix_timestamp_from_genesis( fd_bank_t * bank ) {
      50             :   /* TODO: genesis_creation_time needs to be a long in the bank. */
      51           0 :   return fd_long_sat_add(
      52           0 :       (long)bank->f.genesis_creation_time,
      53           0 :       (long)( fd_uint128_sat_mul( bank->f.slot, bank->f.slot_params.ns_per_slot ) / NS_IN_S ) );
      54           0 : }
      55             : 
      56             : static void
      57             : fd_sysvar_clock_write( fd_bank_t *                   bank,
      58             :                        fd_accdb_t *                  accdb,
      59             :                        fd_capture_ctx_t *            capture_ctx,
      60         615 :                        fd_sol_sysvar_clock_t const * clock ) {
      61         615 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_clock_id, clock, sizeof(fd_sol_sysvar_clock_t) );
      62         615 : }
      63             : 
      64             : fd_sol_sysvar_clock_t *
      65             : fd_sysvar_clock_read( fd_accdb_t *            accdb,
      66             :                       fd_accdb_fork_id_t      fork_id,
      67         614 :                       fd_sol_sysvar_clock_t * clock ) {
      68         614 :   fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, fd_sysvar_clock_id.uc );
      69         614 :   if( FD_UNLIKELY( !acc.lamports || acc.data_len<sizeof(fd_sol_sysvar_clock_t) ) ) {
      70             :     /* This check is needed as a quirk of the fuzzer. If a sysvar
      71             :        account exists in the accounts database, but doesn't have any
      72             :        lamports, this means that the account does not exist.  This
      73             :        wouldn't happen in a real execution environment. */
      74           0 :     fd_accdb_unread_one( accdb, &acc );
      75           0 :     return NULL;
      76           0 :   }
      77             : 
      78         614 :   fd_memcpy( clock, acc.data, sizeof(fd_sol_sysvar_clock_t) );
      79         614 :   fd_accdb_unread_one( accdb, &acc );
      80         614 :   return clock;
      81         614 : }
      82             : 
      83             : void
      84             : fd_sysvar_clock_init( fd_bank_t *        bank,
      85             :                       fd_accdb_t *       accdb,
      86           0 :                       fd_capture_ctx_t * capture_ctx ) {
      87           0 :   long timestamp = unix_timestamp_from_genesis( bank );
      88             : 
      89           0 :   fd_sol_sysvar_clock_t clock = {
      90           0 :     .slot                  = bank->f.slot,
      91           0 :     .epoch                 = 0,
      92           0 :     .epoch_start_timestamp = timestamp,
      93           0 :     .leader_schedule_epoch = 1,
      94           0 :     .unix_timestamp        = timestamp,
      95           0 :   };
      96           0 :   fd_sysvar_clock_write( bank, accdb, capture_ctx, &clock );
      97           0 : }
      98             : 
      99             : #define SORT_NAME  sort_stake_ts
     100         121 : #define SORT_KEY_T ts_est_ele_t
     101          69 : #define SORT_BEFORE(a,b) ( (a).timestamp < (b).timestamp )
     102             : #include "../../../util/tmpl/fd_sort.c"
     103             : 
     104             : static void
     105             : accum_vote_stakes( fd_bank_t *          bank,
     106             :                    fd_runtime_stack_t * runtime_stack,
     107             :                    uint128 *            total_stake_out,
     108         614 :                    ulong *              ts_ele_cnt_out ) {
     109             : 
     110         614 :   ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
     111         614 :   ulong ts_ele_cnt = 0UL;
     112             : 
     113         614 :   uint128 total_stake = 0UL;
     114             : 
     115         614 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     116         614 :   ulong                       current_slot   = bank->f.slot;
     117             : 
     118         614 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     119         614 :   ulong              fork_id     = bank->vote_stakes_fork_id;
     120             : 
     121         614 :   ulong stake         [ FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ];
     122         614 :   ulong last_vote_slots[ FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ];
     123         614 :   long  last_vote_tss  [ FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS ];
     124         614 :   ulong vote_cnt = fd_vote_stakes_t_2_votes( vote_stakes, fork_id, stake, last_vote_slots, last_vote_tss );
     125        1658 :   for( ulong i=0UL; i<vote_cnt; i++ ) {
     126        1044 :     ulong last_vote_slot      = last_vote_slots[ i ];
     127        1044 :     long  last_vote_timestamp = last_vote_tss[ i ];
     128             : 
     129             :     /* https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2445 */
     130        1044 :     if( FD_UNLIKELY( current_slot<last_vote_slot ) ) {
     131             :       /* Don't count vote accounts with a last vote slot that is greater
     132             :          than the current slot. */
     133         503 :       continue;
     134         503 :     }
     135         541 :     ulong slot_delta = current_slot-last_vote_slot;
     136             : 
     137             :     /* Don't count vote accounts that haven't voted in the past 432k
     138             :         slots (length of an epoch).
     139             :         https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2446-L2447 */
     140         541 :     if( FD_UNLIKELY( slot_delta>epoch_schedule->slots_per_epoch ) ) {
     141          75 :       continue;
     142          75 :     }
     143             : 
     144             :     /* Calculate the timestamp estimate by taking the last vote
     145             :         timestamp and adding the estimated time since the last vote
     146             :         (delta from last vote slot to current slot * slot duration).
     147             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L44-L45 */
     148         466 :     ulong offset   = fd_slot_params_slot_range_duration_ns( bank, last_vote_slot+1UL, current_slot+1UL );
     149         466 :     long  estimate = fd_long_sat_add( last_vote_timestamp, (long)(offset / NS_IN_S) );
     150             : 
     151             :     /* For each timestamp, accumulate the stake from E-2.  If the entry
     152             :         for the timestamp doesn't exist yet, insert it.  Otherwise,
     153             :         update the existing entry.
     154             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L46-L53 */
     155         466 :     ts_eles[ ts_ele_cnt ] = (ts_est_ele_t){
     156         466 :       .timestamp = estimate,
     157         466 :       .stake     = { .ud=stake[ i ] },
     158         466 :     };
     159         466 :     ts_ele_cnt++;
     160             : 
     161             :     /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L54 */
     162         466 :     total_stake += stake[ i ];
     163         466 :   }
     164             : 
     165         614 :   *total_stake_out = total_stake;
     166         614 :   *ts_ele_cnt_out  = ts_ele_cnt;
     167         614 : }
     168             : 
     169             : /* get_timestamp_estimate calculates a timestamp estimate.  Does not
     170             :    modify the slot context.  Walks all cached vote accounts (from the
     171             :    "bank") and calculates a unix timestamp estimate. Returns the
     172             :    timestamp estimate.  Crashes the process with FD_LOG_ERR on failure
     173             :    (e.g. too many vote accounts).
     174             : 
     175             :   https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2563-L2601 */
     176             : static long
     177             : get_timestamp_estimate( fd_bank_t *             bank,
     178             :                         fd_sol_sysvar_clock_t * clock,
     179             :                         fd_runtime_stack_t *    runtime_stack,
     180             :                         ulong const *           parent_epoch,
     181         614 :                         int *                   out_estimate_present ) {
     182         614 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     183         614 :   ulong                       current_slot   = bank->f.slot;
     184             : 
     185         614 :   ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
     186             : 
     187             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L41 */
     188         614 :   ulong  ts_ele_cnt   = 0UL;
     189         614 :   uint128 total_stake = 0UL;
     190             : 
     191             :   /* A timestamp estimate is calculated at every slot using the most
     192             :      recent vote states of voting validators. This estimated is based on
     193             :      a stake weighted median using the stake as of the end of epoch E-2
     194             :      if we are currently in epoch E. We do not count vote accounts that
     195             :      have not voted in an epoch's worth of slots (432k). */
     196             : 
     197         614 :   ulong curr_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.slot, NULL );
     198             : 
     199         614 :   accum_vote_stakes( bank, runtime_stack, &total_stake, &ts_ele_cnt );
     200             : 
     201             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L56-L58 */
     202         614 :   if( FD_UNLIKELY( total_stake==0UL ) ) {
     203         200 :     *out_estimate_present = 0;
     204         200 :     return 0L;
     205         200 :   }
     206             : 
     207         414 :   sort_stake_ts_inplace( ts_eles, ts_ele_cnt );
     208             : 
     209             :   /* Populate estimate with the stake-weighted median timestamp.
     210             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L59-L68 */
     211         414 :   uint128 stake_accumulator = 0;
     212         414 :   long    estimate          = 0L;
     213         435 :   for( ulong i=0UL; i<ts_ele_cnt; i++ ) {
     214         435 :     stake_accumulator = fd_uint128_sat_add( stake_accumulator, ts_eles[i].stake.ud );
     215         435 :     if( stake_accumulator>(total_stake/2UL) ) {
     216         415 :       estimate = ts_eles[ i ].timestamp;
     217         415 :       break;
     218         415 :     }
     219         435 :   }
     220             : 
     221             :   /* Bound estimate by `max_allowable_drift` since the start of the
     222             :      epoch.
     223             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L69-L99 */
     224 >1844*10^16 :   ulong epoch_for_start_slot  = parent_epoch ? *parent_epoch : curr_epoch;
     225         414 :   ulong epoch_start_slot      = fd_epoch_slot0( epoch_schedule, epoch_for_start_slot );
     226         414 :   long  epoch_start_timestamp = clock->epoch_start_timestamp;
     227             : 
     228             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L71-L72 */
     229         414 :   ulong poh_estimate_offset = fd_slot_params_slot_range_duration_ns( bank, epoch_start_slot+1UL, current_slot+1UL );
     230             : 
     231             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L73-L77 */
     232         414 :   ulong estimate_offset = fd_ulong_sat_mul( NS_IN_S, fd_ulong_sat_sub( (ulong)estimate, (ulong)epoch_start_timestamp ) );
     233             : 
     234             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L78-L81 */
     235         414 :   ulong max_allowable_drift_fast = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_FAST_PERCENT ) / 100UL;
     236         414 :   ulong max_allowable_drift_slow = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ) / 100UL;
     237             : 
     238             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L82-L98 */
     239         414 :   if( estimate_offset>poh_estimate_offset && fd_ulong_sat_sub( estimate_offset, poh_estimate_offset )>max_allowable_drift_slow ) {
     240          92 :     estimate = fd_long_sat_add(
     241          92 :         epoch_start_timestamp,
     242          92 :         fd_long_sat_add( (long)poh_estimate_offset / NS_IN_S, (long)max_allowable_drift_slow / NS_IN_S ) );
     243         322 :   } else if( estimate_offset<poh_estimate_offset && fd_ulong_sat_sub( poh_estimate_offset, estimate_offset )>max_allowable_drift_fast ) {
     244          35 :     estimate = fd_long_sat_sub(
     245          35 :         fd_long_sat_add( epoch_start_timestamp, (long)poh_estimate_offset / NS_IN_S ),
     246          35 :         (long)max_allowable_drift_fast / NS_IN_S );
     247          35 :   }
     248             : 
     249         414 :   *out_estimate_present = 1;
     250         414 :   return estimate;
     251         614 : }
     252             : 
     253             : /* TODO: This function should be called from genesis bootup as well with
     254             :    parent_epoch = NULL
     255             :    https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2158-L2215 */
     256             : void
     257             : fd_sysvar_clock_update( fd_bank_t *          bank,
     258             :                         fd_accdb_t *         accdb,
     259             :                         fd_capture_ctx_t *   capture_ctx,
     260             :                         fd_runtime_stack_t * runtime_stack,
     261         614 :                         ulong const *        parent_epoch ) {
     262         614 :   fd_sol_sysvar_clock_t clock_[1];
     263         614 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, bank->accdb_fork_id, clock_ );
     264         614 :   if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
     265             : 
     266         614 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     267         614 :   ulong                       current_slot   = bank->f.slot;
     268         614 :   ulong                       current_epoch  = fd_slot_to_epoch( epoch_schedule, current_slot, NULL );
     269             : 
     270             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2159 */
     271         614 :   long unix_timestamp = clock->unix_timestamp;
     272             : 
     273             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2175 */
     274         614 :   long ancestor_timestamp = clock->unix_timestamp;
     275             : 
     276             :   /* TODO: Are we handling slot 0 correctly?
     277             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2176-L2183 */
     278         614 :   int  estimate_present   = 0;
     279         614 :   long timestamp_estimate = get_timestamp_estimate( bank, clock, runtime_stack, parent_epoch, &estimate_present );
     280             : 
     281             :   /* If the timestamp was successfully calculated, use it. Otherwise,
     282             :      keep the old one. */
     283         614 :   if( FD_LIKELY( estimate_present ) ) {
     284         415 :     unix_timestamp = timestamp_estimate;
     285             : 
     286             :     /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2180-L2182 */
     287         415 :     if( timestamp_estimate<ancestor_timestamp ) {
     288          13 :       unix_timestamp = ancestor_timestamp;
     289          13 :     }
     290         415 :   }
     291             : 
     292             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2191-L2197 */
     293         615 :   long epoch_start_timestamp = (parent_epoch!=NULL && *parent_epoch!=current_epoch) ?
     294          32 :       unix_timestamp :
     295         614 :       clock->epoch_start_timestamp;
     296             : 
     297             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2198-L2201 */
     298         614 :   if( FD_UNLIKELY( current_slot==0UL ) ) {
     299           0 :     long timestamp_from_genesis = unix_timestamp_from_genesis( bank );
     300           0 :     unix_timestamp              = timestamp_from_genesis;
     301           0 :     epoch_start_timestamp       = timestamp_from_genesis;
     302           0 :   }
     303             : 
     304             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2202-L2208 */
     305         614 :   *clock = (fd_sol_sysvar_clock_t){
     306         614 :     .slot                  = current_slot,
     307         614 :     .epoch_start_timestamp = epoch_start_timestamp,
     308         614 :     .epoch                 = current_epoch,
     309         614 :     .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, current_slot ),
     310         614 :     .unix_timestamp        = unix_timestamp,
     311         614 :   };
     312             : 
     313             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2209-L2214 */
     314         614 :   fd_sysvar_clock_write( bank, accdb, capture_ctx, clock );
     315         614 : }
     316             : 
     317             : void
     318             : fd_sysvar_clock_update_slot_alpenglow( fd_bank_t *        bank,
     319             :                                        fd_accdb_t *       accdb,
     320           0 :                                        fd_capture_ctx_t * capture_ctx ) {
     321           0 :   fd_sol_sysvar_clock_t clock_[1];
     322           0 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, bank->accdb_fork_id, clock_ );
     323           0 :   if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
     324             : 
     325           0 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     326           0 :   ulong current_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.slot,        NULL );
     327           0 :   ulong parent_epoch  = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
     328             : 
     329           0 :   long epoch_start_timestamp;
     330           0 :   if(      FD_UNLIKELY( !bank->f.slot               ) ) epoch_start_timestamp = unix_timestamp_from_genesis( bank );
     331           0 :   else if( FD_UNLIKELY( parent_epoch!=current_epoch ) ) epoch_start_timestamp = clock->unix_timestamp;
     332           0 :   else                                                  epoch_start_timestamp = clock->epoch_start_timestamp;
     333             : 
     334           0 :   *clock = (fd_sol_sysvar_clock_t){
     335           0 :     .slot                  = bank->f.slot,
     336           0 :     .epoch_start_timestamp = epoch_start_timestamp,
     337           0 :     .epoch                 = current_epoch,
     338           0 :     .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, bank->f.slot ),
     339           0 :     .unix_timestamp        = clock->unix_timestamp, /* parent's footer timestamp */
     340           0 :   };
     341           0 :   fd_sysvar_clock_write( bank, accdb, capture_ctx, clock );
     342           0 : }

Generated by: LCOV version 1.14