LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_clock.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 151 0.0 %
Date: 2025-10-27 04:40:00 Functions: 0 6 0.0 %

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

Generated by: LCOV version 1.14