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

Generated by: LCOV version 1.14