LCOV - code coverage report
Current view: top level - flamenco/runtime/sysvar - fd_sysvar_clock.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 191 207 92.3 %
Date: 2026-07-25 05:22:36 Functions: 8 8 100.0 %

          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             : #include "../program/vote/fd_vote_state_versioned.h"
       7             : 
       8             : /* Syvar Clock Possible Values:
       9             :   slot:
      10             :   [0, ULONG_MAX]
      11             : 
      12             :   epoch:
      13             :   [0, slot/432000UL]
      14             : 
      15             :   epoch_start_timestamp:
      16             :   [0, ULONG_MAX]
      17             : 
      18             :   unix_timestamp:
      19             :   This value is bounded by the slot distance from the
      20             :   epoch_start_timestamp.
      21             :   The protocol allows for a maximum drift (either fast or slow) from the
      22             :   start of the epoch's timestamp.  The expected time is called the PoH
      23             :   offset.  This offset is calculated by (epoch_start_timestamp + slots
      24             :   since epoch * slot_duration). The drift is then bounded by the
      25             :   max_allowable_drift_{slow,fast}.  The stake weighted offset can be
      26             :   150% more than the PoH offset and 25% less than the PoH offset.
      27             :   So, the bounds for the unix_timestamp can be calculated by:
      28             :   upper bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 2.5
      29             :   lower bound = epoch_start_timestamp + (slots since epoch * slot_duration) * 0.75
      30             : 
      31             :   leader_schedule_epoch:
      32             :   This is the value of the epoch used for the leader schedule.  It is
      33             :   computed based on the values of the epoch schedule (first_normal_slot,
      34             :   leader_schedule_slot_offset, slots_per_epoch).  It is always equal to
      35             :   ((slot - first_normal_slot) + leader_schedule_slot_offset) / schedule->slots_per_epoch
      36             : */
      37             : 
      38             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L14 */
      39        4182 : #define MAX_ALLOWABLE_DRIFT_FAST_PERCENT ( 25U )
      40             : 
      41             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L15 */
      42        4182 : #define MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ( 150U )
      43             : 
      44             : /* Do all intermediate calculations at nanosecond precision, to mirror
      45             :    Solana's behavior. */
      46       16536 : #define NS_IN_S ((long)1e9)
      47             : 
      48             : /* FD_SYSVAR_CLOCK_STAKE_WEIGHTS_MAX specifies the max number of stake
      49             :    weights processed in a clock update. */
      50             : 
      51             : #define FD_SYSVAR_CLOCK_STAKE_WEIGHTS_MAX (10240UL)
      52             : 
      53             : /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2110-L2117 */
      54             : static inline long
      55           6 : unix_timestamp_from_genesis( fd_bank_t * bank ) {
      56             :   /* TODO: genesis_creation_time needs to be a long in the bank. */
      57           6 :   return fd_long_sat_add(
      58           6 :       (long)bank->f.genesis_creation_time,
      59           6 :       (long)( fd_uint128_sat_mul( bank->f.slot, bank->f.slot_params.ns_per_slot ) / NS_IN_S ) );
      60           6 : }
      61             : 
      62             : static void
      63             : fd_sysvar_clock_write( fd_bank_t *                   bank,
      64             :                        fd_accdb_t *                  accdb,
      65             :                        fd_capture_ctx_t *            capture_ctx,
      66        4548 :                        fd_sol_sysvar_clock_t const * clock ) {
      67        4548 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_clock_id, clock, sizeof(fd_sol_sysvar_clock_t) );
      68        4548 : }
      69             : 
      70             : fd_sol_sysvar_clock_t *
      71             : fd_sysvar_clock_read( fd_accdb_t *            accdb,
      72             :                       fd_accdb_fork_id_t      fork_id,
      73        4578 :                       fd_sol_sysvar_clock_t * clock ) {
      74        4578 :   fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, fd_sysvar_clock_id.uc );
      75        4578 :   if( FD_UNLIKELY( !acc.lamports || acc.data_len<sizeof(fd_sol_sysvar_clock_t) ) ) {
      76             :     /* This check is needed as a quirk of the fuzzer. If a sysvar
      77             :        account exists in the accounts database, but doesn't have any
      78             :        lamports, this means that the account does not exist.  This
      79             :        wouldn't happen in a real execution environment. */
      80           0 :     fd_accdb_unread_one( accdb, &acc );
      81           0 :     return NULL;
      82           0 :   }
      83             : 
      84        4578 :   fd_memcpy( clock, acc.data, sizeof(fd_sol_sysvar_clock_t) );
      85        4578 :   fd_accdb_unread_one( accdb, &acc );
      86        4578 :   return clock;
      87        4578 : }
      88             : 
      89             : void
      90             : fd_sysvar_clock_init( fd_bank_t *        bank,
      91             :                       fd_accdb_t *       accdb,
      92           6 :                       fd_capture_ctx_t * capture_ctx ) {
      93           6 :   long timestamp = unix_timestamp_from_genesis( bank );
      94             : 
      95           6 :   fd_sol_sysvar_clock_t clock = {
      96           6 :     .slot                  = bank->f.slot,
      97           6 :     .epoch                 = 0,
      98           6 :     .epoch_start_timestamp = timestamp,
      99           6 :     .leader_schedule_epoch = 1,
     100           6 :     .unix_timestamp        = timestamp,
     101           6 :   };
     102           6 :   fd_sysvar_clock_write( bank, accdb, capture_ctx, &clock );
     103           6 : }
     104             : 
     105             : #define SORT_NAME  sort_stake_ts
     106        1347 : #define SORT_KEY_T ts_est_ele_t
     107         777 : #define SORT_BEFORE(a,b) ( (a).timestamp < (b).timestamp )
     108             : #include "../../../util/tmpl/fd_sort.c"
     109             : 
     110             : static void
     111             : accum_vote_stakes_no_vat( fd_bank_t *               bank,
     112             :                           fd_accdb_t *              accdb,
     113             :                           fd_runtime_stack_t *      runtime_stack,
     114             :                           uint128 *                 total_stake_out,
     115        4332 :                           ulong *                   ts_ele_cnt_out ) {
     116             : 
     117        4332 :   ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
     118        4332 :   ulong ts_ele_cnt = 0UL;
     119             : 
     120        4332 :   uint128 total_stake = 0UL;
     121             : 
     122        4332 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     123        4332 :   ulong                       current_slot   = bank->f.slot;
     124             : 
     125        4332 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     126        4332 :   ushort             fork_idx    = bank->vote_stakes_fork_id;
     127             : 
     128        4332 :   fd_top_votes_t const * top_votes = fd_bank_top_votes_t_2_query( bank );
     129             : 
     130        4332 :   uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
     131        4332 :   for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_fork_iter_init( vote_stakes, fork_idx, iter_mem );
     132        9222 :         !fd_vote_stakes_fork_iter_done( vote_stakes, fork_idx, iter );
     133        4890 :         fd_vote_stakes_fork_iter_next( vote_stakes, fork_idx, iter ) ) {
     134        4890 :     fd_pubkey_t pubkey;
     135        4890 :     ulong       stake_t_2;
     136        4890 :     fd_vote_stakes_fork_iter_ele( vote_stakes, fork_idx, iter, &pubkey, NULL, &stake_t_2, NULL, NULL, NULL, NULL );
     137        4890 :     if( FD_UNLIKELY( !stake_t_2 ) ) continue;
     138             : 
     139        4890 :     ulong last_vote_slot;
     140        4890 :     long  last_vote_timestamp;
     141        4890 :     uchar is_valid = 1;
     142        4890 :     int   found = fd_top_votes_query( top_votes, &pubkey, NULL, NULL, &last_vote_slot, &last_vote_timestamp, NULL, &is_valid );
     143        4890 :     if( FD_UNLIKELY( !found ) ) {
     144         396 :       fd_acc_t acc = fd_accdb_read_one( accdb, bank->accdb_fork_id, pubkey.uc );
     145         396 :       if( FD_UNLIKELY( !acc.lamports || !fd_vsv_is_correct_size_owner_and_init( acc.owner, acc.data, acc.data_len ) ) ) {
     146           0 :         fd_accdb_unread_one( accdb, &acc );
     147           0 :         continue;
     148           0 :       }
     149             : 
     150         396 :       fd_vote_block_timestamp_t last_vote;
     151         396 :       FD_TEST( !fd_vote_account_last_timestamp( acc.data, acc.data_len, &last_vote ) );
     152         396 :       fd_accdb_unread_one( accdb, &acc );
     153         396 :       last_vote_slot      = last_vote.slot;
     154         396 :       last_vote_timestamp = last_vote.timestamp;
     155         396 :     }
     156        4890 :     if( FD_UNLIKELY( !is_valid ) ) continue;
     157             : 
     158             :     /* https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2445 */
     159        4890 :     ulong slot_delta;
     160        4890 :     int err = fd_ulong_checked_sub( current_slot, last_vote_slot, &slot_delta );
     161        4890 :     if( FD_UNLIKELY( err ) ) {
     162             :       /* Don't count vote accounts with a last vote slot that is greater
     163             :           than the current slot. */
     164           0 :       continue;
     165           0 :     }
     166             : 
     167             :     /* Don't count vote accounts that haven't voted in the past 432k
     168             :         slots (length of an epoch).
     169             :         https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2446-L2447 */
     170        4890 :     if( FD_UNLIKELY( slot_delta>epoch_schedule->slots_per_epoch ) ) {
     171         627 :       continue;
     172         627 :     }
     173             : 
     174             :     /* Calculate the timestamp estimate by taking the last vote
     175             :         timestamp and adding the estimated time since the last vote
     176             :         (delta from last vote slot to current slot * slot duration).
     177             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L44-L45 */
     178        4263 :     ulong offset   = fd_slot_params_slot_range_duration_ns( bank, last_vote_slot+1UL, current_slot+1UL );
     179        4263 :     long  estimate = fd_long_sat_add( last_vote_timestamp, (long)(offset / NS_IN_S) );
     180             : 
     181             :     /* For each timestamp, accumulate the stake from E-2.  If the acc
     182             :         for the timestamp doesn't exist yet, insert it.  Otherwise,
     183             :         update the existing acc.
     184             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L46-L53 */
     185        4263 :     ts_eles[ ts_ele_cnt ] = (ts_est_ele_t){
     186        4263 :       .timestamp = estimate,
     187        4263 :       .stake     = { .ud=stake_t_2 },
     188        4263 :     };
     189        4263 :     ts_ele_cnt++;
     190             : 
     191             :     /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L54 */
     192        4263 :     total_stake += stake_t_2;
     193        4263 :   }
     194        4332 :   fd_vote_stakes_fork_iter_fini( vote_stakes );
     195             : 
     196        4332 :   *total_stake_out = total_stake;
     197        4332 :   *ts_ele_cnt_out  = ts_ele_cnt;
     198        4332 : }
     199             : 
     200             : static void
     201             : accum_vote_stakes_vat( fd_bank_t *          bank,
     202             :                        fd_runtime_stack_t * runtime_stack,
     203             :                        uint128 *            total_stake_out,
     204         210 :                        ulong *              ts_ele_cnt_out ) {
     205             : 
     206         210 :   ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
     207         210 :   ulong ts_ele_cnt = 0UL;
     208             : 
     209         210 :   uint128 total_stake = 0UL;
     210             : 
     211         210 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     212         210 :   ulong                       current_slot   = bank->f.slot;
     213             : 
     214         210 :   fd_top_votes_t const * top_votes = fd_bank_top_votes_t_2_query( bank );
     215             : 
     216         210 :   uchar __attribute__((aligned(FD_TOP_VOTES_ITER_ALIGN))) iter_mem[ FD_TOP_VOTES_ITER_FOOTPRINT ];
     217         210 :   for( fd_top_votes_iter_t * iter = fd_top_votes_iter_init( top_votes, iter_mem );
     218         735 :        !fd_top_votes_iter_done( top_votes, iter );
     219         525 :        fd_top_votes_iter_next( top_votes, iter ) ) {
     220         525 :     fd_pubkey_t pubkey;
     221         525 :     ulong       stake_t_2;
     222         525 :     ulong       last_vote_slot;
     223         525 :     long        last_vote_timestamp;
     224         525 :     uchar       is_valid;
     225         525 :     fd_top_votes_iter_ele( top_votes, iter, &pubkey, NULL, &stake_t_2, NULL, &last_vote_slot, &last_vote_timestamp, &is_valid );
     226         525 :     if( FD_UNLIKELY( !is_valid ) ) continue;
     227             : 
     228             :     /* https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2445 */
     229         489 :     ulong slot_delta;
     230         489 :     int err = fd_ulong_checked_sub( current_slot, last_vote_slot, &slot_delta );
     231         489 :     if( FD_UNLIKELY( err ) ) {
     232             :       /* Don't count vote accounts with a last vote slot that is greater
     233             :          than the current slot. */
     234           0 :       continue;
     235           0 :     }
     236             : 
     237             :     /* Don't count vote accounts that haven't voted in the past 432k
     238             :         slots (length of an epoch).
     239             :         https://github.com/anza-xyz/agave/blob/v3.0.0/runtime/src/bank.rs#L2446-L2447 */
     240         489 :     if( FD_UNLIKELY( slot_delta>epoch_schedule->slots_per_epoch ) ) {
     241           0 :       continue;
     242           0 :     }
     243             : 
     244             :     /* Calculate the timestamp estimate by taking the last vote
     245             :         timestamp and adding the estimated time since the last vote
     246             :         (delta from last vote slot to current slot * slot duration).
     247             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L44-L45 */
     248         489 :     ulong offset   = fd_slot_params_slot_range_duration_ns( bank, last_vote_slot+1UL, current_slot+1UL );
     249         489 :     long  estimate = fd_long_sat_add( last_vote_timestamp, (long)(offset / NS_IN_S) );
     250             : 
     251             :     /* For each timestamp, accumulate the stake from E-2.  If the entry
     252             :         for the timestamp doesn't exist yet, insert it.  Otherwise,
     253             :         update the existing entry.
     254             :         https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L46-L53 */
     255         489 :     ts_eles[ ts_ele_cnt ] = (ts_est_ele_t){
     256         489 :       .timestamp = estimate,
     257         489 :       .stake     = { .ud=stake_t_2 },
     258         489 :     };
     259         489 :     ts_ele_cnt++;
     260             : 
     261             :     /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L54 */
     262         489 :     total_stake += stake_t_2;
     263         489 :   }
     264             : 
     265         210 :   *total_stake_out = total_stake;
     266         210 :   *ts_ele_cnt_out  = ts_ele_cnt;
     267         210 : }
     268             : 
     269             : /* get_timestamp_estimate calculates a timestamp estimate.  Does not
     270             :    modify the slot context.  Walks all cached vote accounts (from the
     271             :    "bank") and calculates a unix timestamp estimate. Returns the
     272             :    timestamp estimate.  spad is used for scratch allocations (allocates
     273             :    a treap of size FD_SYSVAR_CLOCK_STAKE_WEIGHTS_MAX). Crashes the
     274             :    process with FD_LOG_ERR on failure (e.g. too many vote accounts).
     275             : 
     276             :   https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2563-L2601 */
     277             : static long
     278             : get_timestamp_estimate( fd_bank_t *             bank,
     279             :                         fd_accdb_t *            accdb,
     280             :                         fd_sol_sysvar_clock_t * clock,
     281             :                         fd_runtime_stack_t *    runtime_stack,
     282             :                         ulong const *           parent_epoch,
     283        4542 :                         int *                   out_estimate_present ) {
     284        4542 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     285        4542 :   ulong                       current_slot   = bank->f.slot;
     286             : 
     287        4542 :   ts_est_ele_t * ts_eles = runtime_stack->clock_ts.staked_ts;
     288             : 
     289             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L41 */
     290        4542 :   ulong  ts_ele_cnt   = 0UL;
     291        4542 :   uint128 total_stake = 0UL;
     292             : 
     293             :   /* A timestamp estimate is calculated at every slot using the most
     294             :      recent vote states of voting validators. This estimated is based on
     295             :      a stake weighted median using the stake as of the end of epoch E-2
     296             :      if we are currently in epoch E. We do not count vote accounts that
     297             :      have not voted in an epoch's worth of slots (432k). */
     298             : 
     299        4542 :   ulong curr_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.slot, NULL );
     300        4542 :   ulong vat_epoch  = fd_slot_to_epoch( epoch_schedule, bank->f.features.validator_admission_ticket, NULL );
     301             : 
     302        4542 :   if( curr_epoch>=vat_epoch+1UL ) {
     303         210 :     accum_vote_stakes_vat( bank, runtime_stack, &total_stake, &ts_ele_cnt );
     304        4332 :   } else {
     305        4332 :     accum_vote_stakes_no_vat( bank, accdb, runtime_stack, &total_stake, &ts_ele_cnt );
     306        4332 :   }
     307             : 
     308             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L56-L58 */
     309        4542 :   if( FD_UNLIKELY( total_stake==0UL ) ) {
     310         360 :     *out_estimate_present = 0;
     311         360 :     return 0L;
     312         360 :   }
     313             : 
     314        4182 :   sort_stake_ts_inplace( ts_eles, ts_ele_cnt );
     315             : 
     316             :   /* Populate estimate with the stake-weighted median timestamp.
     317             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L59-L68 */
     318        4182 :   uint128 stake_accumulator = 0;
     319        4182 :   long    estimate          = 0L;
     320        4752 :   for( ulong i=0UL; i<ts_ele_cnt; i++ ) {
     321        4752 :     stake_accumulator = fd_uint128_sat_add( stake_accumulator, ts_eles[i].stake.ud );
     322        4752 :     if( stake_accumulator>(total_stake/2UL) ) {
     323        4182 :       estimate = ts_eles[ i ].timestamp;
     324        4182 :       break;
     325        4182 :     }
     326        4752 :   }
     327             : 
     328             :   /* Bound estimate by `max_allowable_drift` since the start of the
     329             :      epoch.
     330             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L69-L99 */
     331        4182 :   ulong epoch_for_start_slot  = parent_epoch ? *parent_epoch : curr_epoch;
     332        4182 :   ulong epoch_start_slot      = fd_epoch_slot0( epoch_schedule, epoch_for_start_slot );
     333        4182 :   long  epoch_start_timestamp = clock->epoch_start_timestamp;
     334             : 
     335             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L71-L72 */
     336        4182 :   ulong poh_estimate_offset = fd_slot_params_slot_range_duration_ns( bank, epoch_start_slot+1UL, current_slot+1UL );
     337             : 
     338             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L73-L77 */
     339        4182 :   ulong estimate_offset = fd_ulong_sat_mul( NS_IN_S, fd_ulong_sat_sub( (ulong)estimate, (ulong)epoch_start_timestamp ) );
     340             : 
     341             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L78-L81 */
     342        4182 :   ulong max_allowable_drift_fast = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_FAST_PERCENT ) / 100UL;
     343        4182 :   ulong max_allowable_drift_slow = fd_ulong_sat_mul( poh_estimate_offset, MAX_ALLOWABLE_DRIFT_SLOW_PERCENT ) / 100UL;
     344             : 
     345             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/stake_weighted_timestamp.rs#L82-L98 */
     346        4182 :   if( estimate_offset>poh_estimate_offset && fd_ulong_sat_sub( estimate_offset, poh_estimate_offset )>max_allowable_drift_slow ) {
     347         207 :     estimate = fd_long_sat_add(
     348         207 :         epoch_start_timestamp,
     349         207 :         fd_long_sat_add( (long)poh_estimate_offset / NS_IN_S, (long)max_allowable_drift_slow / NS_IN_S ) );
     350        3975 :   } else if( estimate_offset<poh_estimate_offset && fd_ulong_sat_sub( poh_estimate_offset, estimate_offset )>max_allowable_drift_fast ) {
     351        3591 :     estimate = fd_long_sat_sub(
     352        3591 :         fd_long_sat_add( epoch_start_timestamp, (long)poh_estimate_offset / NS_IN_S ),
     353        3591 :         (long)max_allowable_drift_fast / NS_IN_S );
     354        3591 :   }
     355             : 
     356        4182 :   *out_estimate_present = 1;
     357        4182 :   return estimate;
     358        4542 : }
     359             : 
     360             : /* TODO: This function should be called from genesis bootup as well with
     361             :    parent_epoch = NULL
     362             :    https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2158-L2215 */
     363             : void
     364             : fd_sysvar_clock_update( fd_bank_t *          bank,
     365             :                         fd_accdb_t *         accdb,
     366             :                         fd_capture_ctx_t *   capture_ctx,
     367             :                         fd_runtime_stack_t * runtime_stack,
     368        4542 :                         ulong const *        parent_epoch ) {
     369        4542 :   fd_sol_sysvar_clock_t clock_[1];
     370        4542 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, bank->accdb_fork_id, clock_ );
     371        4542 :   if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
     372             : 
     373        4542 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     374        4542 :   ulong                       current_slot   = bank->f.slot;
     375        4542 :   ulong                       current_epoch  = fd_slot_to_epoch( epoch_schedule, current_slot, NULL );
     376             : 
     377             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2159 */
     378        4542 :   long unix_timestamp = clock->unix_timestamp;
     379             : 
     380             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2175 */
     381        4542 :   long ancestor_timestamp = clock->unix_timestamp;
     382             : 
     383             :   /* TODO: Are we handling slot 0 correctly?
     384             :      https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2176-L2183 */
     385        4542 :   int  estimate_present   = 0;
     386        4542 :   long timestamp_estimate = get_timestamp_estimate( bank, accdb, clock, runtime_stack, parent_epoch, &estimate_present );
     387             : 
     388             :   /* If the timestamp was successfully calculated, use it. Otherwise,
     389             :      keep the old one. */
     390        4542 :   if( FD_LIKELY( estimate_present ) ) {
     391        4182 :     unix_timestamp = timestamp_estimate;
     392             : 
     393             :     /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2180-L2182 */
     394        4182 :     if( timestamp_estimate<ancestor_timestamp ) {
     395           6 :       unix_timestamp = ancestor_timestamp;
     396           6 :     }
     397        4182 :   }
     398             : 
     399             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2191-L2197 */
     400        4542 :   long epoch_start_timestamp = (parent_epoch!=NULL && *parent_epoch!=current_epoch) ?
     401         270 :       unix_timestamp :
     402        4542 :       clock->epoch_start_timestamp;
     403             : 
     404             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2198-L2201 */
     405        4542 :   if( FD_UNLIKELY( current_slot==0UL ) ) {
     406           0 :     long timestamp_from_genesis = unix_timestamp_from_genesis( bank );
     407           0 :     unix_timestamp              = timestamp_from_genesis;
     408           0 :     epoch_start_timestamp       = timestamp_from_genesis;
     409           0 :   }
     410             : 
     411             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2202-L2208 */
     412        4542 :   *clock = (fd_sol_sysvar_clock_t){
     413        4542 :     .slot                  = current_slot,
     414        4542 :     .epoch_start_timestamp = epoch_start_timestamp,
     415        4542 :     .epoch                 = current_epoch,
     416        4542 :     .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, current_slot ),
     417        4542 :     .unix_timestamp        = unix_timestamp,
     418        4542 :   };
     419             : 
     420             :   /* https://github.com/anza-xyz/agave/blob/v2.3.7/runtime/src/bank.rs#L2209-L2214 */
     421        4542 :   fd_sysvar_clock_write( bank, accdb, capture_ctx, clock );
     422        4542 : }

Generated by: LCOV version 1.14