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

Generated by: LCOV version 1.14