LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 136 1082 12.6 %
Date: 2025-10-13 04:42:14 Functions: 2 33 6.1 %

          Line data    Source code
       1             : #include "fd_runtime.h"
       2             : #include "context/fd_capture_ctx.h"
       3             : #include "fd_acc_mgr.h"
       4             : #include "fd_bank.h"
       5             : #include "fd_hashes.h"
       6             : #include "fd_runtime_err.h"
       7             : #include "fd_runtime_init.h"
       8             : 
       9             : #include "fd_executor.h"
      10             : #include "sysvar/fd_sysvar_cache.h"
      11             : #include "sysvar/fd_sysvar_clock.h"
      12             : #include "sysvar/fd_sysvar_epoch_schedule.h"
      13             : #include "sysvar/fd_sysvar_recent_hashes.h"
      14             : #include "sysvar/fd_sysvar_stake_history.h"
      15             : 
      16             : #include "../stakes/fd_stakes.h"
      17             : #include "../rewards/fd_rewards.h"
      18             : 
      19             : #include "context/fd_exec_txn_ctx.h"
      20             : 
      21             : #include "program/fd_stake_program.h"
      22             : #include "program/fd_builtin_programs.h"
      23             : #include "program/fd_vote_program.h"
      24             : #include "program/fd_program_cache.h"
      25             : #include "program/fd_bpf_loader_program.h"
      26             : #include "program/fd_address_lookup_table_program.h"
      27             : 
      28             : #include "sysvar/fd_sysvar_clock.h"
      29             : #include "sysvar/fd_sysvar_last_restart_slot.h"
      30             : #include "sysvar/fd_sysvar_recent_hashes.h"
      31             : #include "sysvar/fd_sysvar_rent.h"
      32             : #include "sysvar/fd_sysvar_slot_hashes.h"
      33             : #include "sysvar/fd_sysvar_slot_history.h"
      34             : 
      35             : #include "tests/fd_dump_pb.h"
      36             : 
      37             : #include "fd_system_ids.h"
      38             : #include "../../disco/pack/fd_pack.h"
      39             : 
      40             : #include <unistd.h>
      41             : #include <sys/stat.h>
      42             : #include <sys/types.h>
      43             : #include <errno.h>
      44             : #include <fcntl.h>
      45             : 
      46             : /******************************************************************************/
      47             : /* Public Runtime Helpers                                                     */
      48             : /******************************************************************************/
      49             : 
      50             : int
      51           0 : fd_runtime_should_use_vote_keyed_leader_schedule( fd_bank_t * bank ) {
      52             :   /* Agave uses an option type for their effective_epoch value. We
      53             :      represent None as ULONG_MAX and Some(value) as the value.
      54             :      https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6149-L6165 */
      55           0 :   if( FD_FEATURE_ACTIVE_BANK( bank, enable_vote_address_leader_schedule ) ) {
      56             :     /* Return the first epoch if activated at genesis
      57             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6153-L6157 */
      58           0 :     ulong activation_slot = fd_bank_features_query( bank )->enable_vote_address_leader_schedule;
      59           0 :     if( activation_slot==0UL ) return 1; /* effective_epoch=0, current_epoch >= effective_epoch always true */
      60             : 
      61             :     /* Calculate the epoch that the feature became activated in
      62             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6159-L6160 */
      63           0 :     fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
      64           0 :     ulong activation_epoch = fd_slot_to_epoch( epoch_schedule, activation_slot, NULL );
      65             : 
      66             :     /* The effective epoch is the epoch immediately after the activation
      67             :        epoch.
      68             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6162-L6164 */
      69           0 :     ulong effective_epoch = activation_epoch + 1UL;
      70           0 :     ulong current_epoch   = fd_bank_epoch_get( bank );
      71             : 
      72             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6167-L6170 */
      73           0 :     return !!( current_epoch >= effective_epoch );
      74           0 :   }
      75             : 
      76             :   /* ...The rest of the logic in this function either returns None or
      77             :      Some(false) so we will just return 0 by default. */
      78           0 :   return 0;
      79           0 : }
      80             : 
      81             : /*
      82             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
      83             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
      84             :  */
      85             : int
      86             : fd_runtime_compute_max_tick_height( ulong   ticks_per_slot,
      87             :                                     ulong   slot,
      88           0 :                                     ulong * out_max_tick_height /* out */ ) {
      89           0 :   ulong max_tick_height = 0UL;
      90           0 :   if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
      91           0 :     ulong next_slot = fd_ulong_sat_add( slot, 1UL );
      92           0 :     if( FD_UNLIKELY( next_slot == slot ) ) {
      93           0 :       FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      94           0 :       return -1;
      95           0 :     }
      96           0 :     if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
      97           0 :       FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      98           0 :       return -1;
      99           0 :     }
     100           0 :     max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
     101           0 :   }
     102           0 :   *out_max_tick_height = max_tick_height;
     103           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     104           0 : }
     105             : 
     106             : void
     107             : fd_runtime_update_slots_per_epoch( fd_bank_t * bank,
     108           0 :                                    ulong       slots_per_epoch ) {
     109           0 :   if( FD_LIKELY( slots_per_epoch == fd_bank_slots_per_epoch_get( bank ) ) ) {
     110           0 :     return;
     111           0 :   }
     112             : 
     113           0 :   fd_bank_slots_per_epoch_set( bank, slots_per_epoch );
     114           0 : }
     115             : 
     116             : void
     117             : fd_runtime_update_leaders( fd_bank_t * bank,
     118           0 :                            fd_spad_t * runtime_spad ) {
     119             : 
     120           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
     121             : 
     122           0 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     123             : 
     124           0 :   ulong epoch    = fd_slot_to_epoch ( epoch_schedule, fd_bank_slot_get( bank ), NULL );
     125           0 :   ulong slot0    = fd_epoch_slot0   ( epoch_schedule, epoch );
     126           0 :   ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
     127             : 
     128           0 :   fd_vote_states_t const * vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_query( bank );
     129           0 :   ulong                    vote_acc_cnt          = fd_vote_states_cnt( vote_states_prev_prev ) ;
     130           0 :   fd_vote_stake_weight_t * epoch_weights         = fd_spad_alloc_check( runtime_spad, alignof(fd_vote_stake_weight_t), vote_acc_cnt * sizeof(fd_vote_stake_weight_t) );
     131           0 :   ulong                    stake_weight_cnt      = fd_stake_weights_by_node( vote_states_prev_prev, epoch_weights );
     132           0 :   fd_bank_vote_states_prev_prev_end_locking_query( bank );
     133             : 
     134             :   /* Derive leader schedule */
     135             : 
     136           0 :   FD_LOG_INFO(( "stake_weight_cnt=%lu slot_cnt=%lu", stake_weight_cnt, slot_cnt ));
     137           0 :   ulong epoch_leaders_footprint = fd_epoch_leaders_footprint( stake_weight_cnt, slot_cnt );
     138           0 :   FD_LOG_INFO(( "epoch_leaders_footprint=%lu", epoch_leaders_footprint ));
     139           0 :   if( FD_LIKELY( epoch_leaders_footprint ) ) {
     140           0 :     if( FD_UNLIKELY( stake_weight_cnt>MAX_PUB_CNT ) ) {
     141           0 :       FD_LOG_ERR(( "Stake weight count exceeded max" ));
     142           0 :     }
     143           0 :     if( FD_UNLIKELY( slot_cnt>MAX_SLOTS_PER_EPOCH ) ) {
     144           0 :       FD_LOG_ERR(( "Slot count exceeeded max" ));
     145           0 :     }
     146             : 
     147           0 :     ulong vote_keyed_lsched = (ulong)fd_runtime_should_use_vote_keyed_leader_schedule( bank );
     148           0 :     void * epoch_leaders_mem = fd_bank_epoch_leaders_locking_modify( bank );
     149           0 :     fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
     150           0 :         epoch_leaders_mem,
     151           0 :         epoch,
     152           0 :         slot0,
     153           0 :         slot_cnt,
     154           0 :         stake_weight_cnt,
     155           0 :         epoch_weights,
     156           0 :         0UL,
     157           0 :         vote_keyed_lsched ) );
     158           0 :     if( FD_UNLIKELY( !leaders ) ) {
     159           0 :       FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
     160           0 :     }
     161           0 :     fd_bank_epoch_leaders_end_locking_modify( bank );
     162           0 :   }
     163           0 :   } FD_SPAD_FRAME_END;
     164           0 : }
     165             : 
     166             : /******************************************************************************/
     167             : /* Various Private Runtime Helpers                                            */
     168             : /******************************************************************************/
     169             : 
     170             : /* fee to be deposited should be > 0
     171             :    Returns 0 if validation succeeds
     172             :    Returns the amount to burn(==fee) on failure */
     173             : static ulong
     174             : fd_runtime_validate_fee_collector( fd_bank_t *              bank,
     175             :                                    fd_txn_account_t const * collector,
     176           0 :                                    ulong                    fee ) {
     177           0 :   if( FD_UNLIKELY( fee<=0UL ) ) {
     178           0 :     FD_LOG_ERR(( "expected fee(%lu) to be >0UL", fee ));
     179           0 :   }
     180             : 
     181           0 :   if( FD_UNLIKELY( memcmp( fd_txn_account_get_owner( collector ), fd_solana_system_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
     182           0 :     FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
     183           0 :     FD_LOG_WARNING(( "cannot pay a non-system-program owned account (%s)", _out_key ));
     184           0 :     return fee;
     185           0 :   }
     186             : 
     187             :   /* https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/bank/fee_distribution.rs#L111
     188             :      https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/accounts/account_rent_state.rs#L39
     189             :      In agave's fee deposit code, rent state transition check logic is as follows:
     190             :      The transition is NOT allowed iff
     191             :      === BEGIN
     192             :      the post deposit account is rent paying AND the pre deposit account is not rent paying
     193             :      OR
     194             :      the post deposit account is rent paying AND the pre deposit account is rent paying AND !(post_data_size == pre_data_size && post_lamports <= pre_lamports)
     195             :      === END
     196             :      post_data_size == pre_data_size is always true during fee deposit.
     197             :      However, post_lamports > pre_lamports because we are paying a >0 amount.
     198             :      So, the above reduces down to
     199             :      === BEGIN
     200             :      the post deposit account is rent paying AND the pre deposit account is not rent paying
     201             :      OR
     202             :      the post deposit account is rent paying AND the pre deposit account is rent paying AND TRUE
     203             :      === END
     204             :      This is equivalent to checking that the post deposit account is rent paying.
     205             :      An account is rent paying if the post deposit balance is >0 AND it's not rent exempt.
     206             :      We already know that the post deposit balance is >0 because we are paying a >0 amount.
     207             :      So TLDR we just check if the account is rent exempt.
     208             :    */
     209           0 :   fd_rent_t const * rent = fd_bank_rent_query( bank );
     210           0 :   ulong minbal = fd_rent_exempt_minimum_balance( rent, fd_txn_account_get_data_len( collector ) );
     211           0 :   if( FD_UNLIKELY( fd_txn_account_get_lamports( collector )+fee<minbal ) ) {
     212           0 :     FD_BASE58_ENCODE_32_BYTES( collector->pubkey->key, _out_key );
     213           0 :     FD_LOG_WARNING(("cannot pay a rent paying account (%s)", _out_key ));
     214           0 :     return fee;
     215           0 :   }
     216             : 
     217           0 :   return 0UL;
     218           0 : }
     219             : 
     220             : static int
     221             : fd_runtime_run_incinerator( fd_bank_t *               bank,
     222             :                             fd_funk_t *               funk,
     223             :                             fd_funk_txn_xid_t const * xid,
     224           0 :                             fd_capture_ctx_t *        capture_ctx ) {
     225           0 :   FD_TXN_ACCOUNT_DECL( rec );
     226           0 :   fd_funk_rec_prepare_t prepare = {0};
     227             : 
     228           0 :   int err = fd_txn_account_init_from_funk_mutable(
     229           0 :       rec,
     230           0 :       &fd_sysvar_incinerator_id,
     231           0 :       funk,
     232           0 :       xid,
     233           0 :       0,
     234           0 :       0UL,
     235           0 :       &prepare );
     236           0 :   if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
     237             :     // TODO: not really an error! This is fine!
     238           0 :     return -1;
     239           0 :   }
     240             : 
     241           0 :   fd_lthash_value_t prev_hash[1];
     242           0 :   fd_hashes_account_lthash( rec->pubkey, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
     243             : 
     244           0 :   ulong new_capitalization = fd_ulong_sat_sub( fd_bank_capitalization_get( bank ), fd_txn_account_get_lamports( rec ) );
     245           0 :   fd_bank_capitalization_set( bank, new_capitalization );
     246             : 
     247           0 :   fd_txn_account_set_lamports( rec, 0UL );
     248           0 :   fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
     249           0 :   fd_txn_account_mutable_fini( rec, funk, &prepare );
     250             : 
     251           0 :   return 0;
     252           0 : }
     253             : 
     254             : static void
     255             : fd_runtime_freeze( fd_bank_t *               bank,
     256             :                    fd_funk_t *               funk,
     257             :                    fd_funk_txn_xid_t const * xid,
     258           0 :                    fd_capture_ctx_t *        capture_ctx ) {
     259             : 
     260           0 :   if( FD_LIKELY( fd_bank_slot_get( bank ) != 0UL ) ) {
     261           0 :     fd_sysvar_recent_hashes_update( bank, funk, xid, capture_ctx );
     262           0 :   }
     263             : 
     264           0 :   fd_sysvar_slot_history_update( bank, funk, xid, capture_ctx );
     265             : 
     266           0 :   ulong execution_fees = fd_bank_execution_fees_get( bank );
     267           0 :   ulong priority_fees  = fd_bank_priority_fees_get( bank );
     268             : 
     269           0 :   ulong burn = execution_fees / 2;
     270           0 :   ulong fees = fd_ulong_sat_add( priority_fees, execution_fees - burn );
     271             : 
     272           0 :   if( FD_LIKELY( fees ) ) {
     273             :     // Look at collect_fees... I think this was where I saw the fee payout..
     274           0 :     FD_TXN_ACCOUNT_DECL( rec );
     275             : 
     276           0 :     do {
     277             :       /* do_create=1 because we might wanna pay fees to a leader
     278             :          account that we've purged due to 0 balance. */
     279             : 
     280           0 :       fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_locking_query( bank );
     281           0 :       if( FD_UNLIKELY( !leaders ) ) {
     282           0 :         FD_LOG_CRIT(( "fd_runtime_freeze: leaders not found" ));
     283           0 :         fd_bank_epoch_leaders_end_locking_query( bank );
     284           0 :         break;
     285           0 :       }
     286             : 
     287           0 :       fd_pubkey_t const * leader = fd_epoch_leaders_get( leaders, fd_bank_slot_get( bank ) );
     288           0 :       if( FD_UNLIKELY( !leader ) ) {
     289           0 :         FD_LOG_CRIT(( "fd_runtime_freeze: leader not found" ));
     290           0 :         fd_bank_epoch_leaders_end_locking_query( bank );
     291           0 :         break;
     292           0 :       }
     293             : 
     294           0 :       fd_funk_rec_prepare_t prepare = {0};
     295           0 :       int err = fd_txn_account_init_from_funk_mutable(
     296           0 :           rec,
     297           0 :           leader,
     298           0 :           funk,
     299           0 :           xid,
     300           0 :           1,
     301           0 :           0UL,
     302           0 :           &prepare );
     303           0 :       if( FD_UNLIKELY( err ) ) {
     304           0 :         FD_LOG_WARNING(("fd_runtime_freeze: fd_txn_account_init_from_funk_mutable for leader (%s) failed (%d)", FD_BASE58_ENC_32_ALLOCA( leader ), err));
     305           0 :         burn = fd_ulong_sat_add( burn, fees );
     306           0 :         fd_bank_epoch_leaders_end_locking_query( bank );
     307           0 :         break;
     308           0 :       }
     309             : 
     310           0 :       fd_lthash_value_t prev_hash[1];
     311           0 :       fd_hashes_account_lthash( leader, fd_txn_account_get_meta( rec ), fd_txn_account_get_data( rec ), prev_hash );
     312             : 
     313           0 :       fd_bank_epoch_leaders_end_locking_query( bank );
     314             : 
     315           0 :       if ( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( bank, validate_fee_collector_account ) ) ) {
     316           0 :         ulong _burn;
     317           0 :         if( FD_UNLIKELY( _burn=fd_runtime_validate_fee_collector( bank, rec, fees ) ) ) {
     318           0 :           if( FD_UNLIKELY( _burn!=fees ) ) {
     319           0 :             FD_LOG_ERR(( "expected _burn(%lu)==fees(%lu)", _burn, fees ));
     320           0 :           }
     321           0 :           burn = fd_ulong_sat_add( burn, fees );
     322           0 :           FD_LOG_WARNING(("fd_runtime_freeze: burned %lu", fees ));
     323           0 :           break;
     324           0 :         }
     325           0 :       }
     326             : 
     327             :       /* TODO: is it ok to not check the overflow error here? */
     328           0 :       fd_txn_account_checked_add_lamports( rec, fees );
     329           0 :       fd_txn_account_set_slot( rec, fd_bank_slot_get( bank ) );
     330             : 
     331           0 :       fd_hashes_update_lthash( rec, prev_hash, bank, capture_ctx );
     332           0 :       fd_txn_account_mutable_fini( rec, funk, &prepare );
     333             : 
     334           0 :     } while(0);
     335             : 
     336           0 :     ulong old = fd_bank_capitalization_get( bank );
     337           0 :     fd_bank_capitalization_set( bank, fd_ulong_sat_sub( old, burn ) );
     338           0 :     FD_LOG_DEBUG(( "fd_runtime_freeze: burn %lu, capitalization %lu->%lu ", burn, old, fd_bank_capitalization_get( bank ) ));
     339             : 
     340           0 :     fd_bank_execution_fees_set( bank, 0UL );
     341             : 
     342           0 :     fd_bank_priority_fees_set( bank, 0UL );
     343           0 :   }
     344             : 
     345           0 :   fd_runtime_run_incinerator( bank, funk, xid, capture_ctx );
     346             : 
     347           0 : }
     348             : 
     349             : /* fd_runtime_collect_rent_from_account performs rent collection duties.
     350             :    Although the Solana runtime prevents the creation of new accounts
     351             :    that are subject to rent, some older accounts are still undergo the
     352             :    rent collection process.  Updates the account's 'rent_epoch' if
     353             :    needed. Returns the amount of rent collected. */
     354             : /* https://github.com/anza-xyz/agave/blob/v2.0.10/svm/src/account_loader.rs#L71-96 */
     355             : ulong
     356             : fd_runtime_collect_rent_from_account( fd_epoch_schedule_t const * schedule,
     357             :                                       fd_rent_t const *           rent,
     358             :                                       double                      slots_per_year,
     359             :                                       fd_txn_account_t *          acc,
     360           0 :                                       ulong                       epoch ) {
     361           0 :   (void)schedule; (void)rent; (void)slots_per_year; (void)acc; (void)epoch;
     362           0 :   return 0UL;
     363           0 : }
     364             : 
     365             : /******************************************************************************/
     366             : /* Block-Level Execution Preparation/Finalization                             */
     367             : /******************************************************************************/
     368             : 
     369             : /*
     370             : https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/sdk/program/src/fee_calculator.rs#L105-L165
     371             : */
     372             : static void
     373             : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
     374           0 :                                           ulong       latest_signatures_per_slot ) {
     375             : 
     376           0 :   fd_fee_rate_governor_t const * base_fee_rate_governor = fd_bank_fee_rate_governor_query( bank );
     377             : 
     378           0 :   ulong old_lamports_per_signature = fd_bank_lamports_per_signature_get( bank );
     379             : 
     380           0 :   fd_fee_rate_governor_t me = {
     381           0 :     .target_signatures_per_slot    = base_fee_rate_governor->target_signatures_per_slot,
     382           0 :     .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
     383           0 :     .max_lamports_per_signature    = base_fee_rate_governor->max_lamports_per_signature,
     384           0 :     .min_lamports_per_signature    = base_fee_rate_governor->min_lamports_per_signature,
     385           0 :     .burn_percent                  = base_fee_rate_governor->burn_percent
     386           0 :   };
     387             : 
     388           0 :   ulong new_lamports_per_signature = 0;
     389           0 :   if( me.target_signatures_per_slot > 0 ) {
     390           0 :     me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
     391           0 :     me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
     392           0 :     ulong desired_lamports_per_signature = fd_ulong_min(
     393           0 :       me.max_lamports_per_signature,
     394           0 :       fd_ulong_max(
     395           0 :         me.min_lamports_per_signature,
     396           0 :         me.target_lamports_per_signature
     397           0 :         * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
     398           0 :         / me.target_signatures_per_slot
     399           0 :       )
     400           0 :     );
     401           0 :     long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
     402           0 :     if ( gap == 0 ) {
     403           0 :       new_lamports_per_signature = desired_lamports_per_signature;
     404           0 :     } else {
     405           0 :       long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
     406           0 :         * (gap != 0)
     407           0 :         * (gap > 0 ? 1 : -1);
     408           0 :       new_lamports_per_signature = fd_ulong_min(
     409           0 :         me.max_lamports_per_signature,
     410           0 :         fd_ulong_max(
     411           0 :           me.min_lamports_per_signature,
     412           0 :           (ulong)((long)old_lamports_per_signature + gap_adjust)
     413           0 :         )
     414           0 :       );
     415           0 :     }
     416           0 :   } else {
     417           0 :     new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
     418           0 :     me.min_lamports_per_signature = me.target_lamports_per_signature;
     419           0 :     me.max_lamports_per_signature = me.target_lamports_per_signature;
     420           0 :   }
     421             : 
     422           0 :   if( FD_UNLIKELY( old_lamports_per_signature==0UL ) ) {
     423           0 :     fd_bank_prev_lamports_per_signature_set( bank, new_lamports_per_signature );
     424           0 :   } else {
     425           0 :     fd_bank_prev_lamports_per_signature_set( bank, old_lamports_per_signature );
     426           0 :   }
     427             : 
     428           0 :   fd_bank_fee_rate_governor_set( bank, me );
     429             : 
     430           0 :   fd_bank_lamports_per_signature_set( bank, new_lamports_per_signature );
     431           0 : }
     432             : 
     433             : static int
     434             : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t *               bank,
     435             :                                             fd_funk_t *               funk,
     436             :                                             fd_funk_txn_xid_t const * xid,
     437             :                                             fd_capture_ctx_t *        capture_ctx,
     438           0 :                                             fd_spad_t *               runtime_spad ) {
     439             :   // let (fee_rate_governor, fee_components_time_us) = measure_us!(
     440             :   //     FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
     441             :   // );
     442             :   /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
     443             : 
     444           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
     445             : 
     446           0 :   fd_runtime_new_fee_rate_governor_derived( bank, fd_bank_parent_signature_cnt_get( bank ) );
     447             : 
     448           0 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     449           0 :   ulong                       parent_epoch   = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
     450           0 :   fd_sysvar_clock_update( bank, funk, xid, capture_ctx, runtime_spad, &parent_epoch );
     451             : 
     452             :   // It has to go into the current txn previous info but is not in slot 0
     453           0 :   if( fd_bank_slot_get( bank ) != 0 ) {
     454           0 :     fd_sysvar_slot_hashes_update( bank, funk, xid, capture_ctx, runtime_spad );
     455           0 :   }
     456           0 :   fd_sysvar_last_restart_slot_update( bank, funk, xid, capture_ctx, fd_bank_last_restart_slot_get( bank ).slot );
     457             : 
     458           0 :   } FD_SPAD_FRAME_END;
     459             : 
     460           0 :   return 0;
     461           0 : }
     462             : 
     463             : int
     464             : fd_runtime_load_txn_address_lookup_tables(
     465             :     fd_txn_t const *          txn,
     466             :     uchar const *             payload,
     467             :     fd_funk_t *               funk,
     468             :     fd_funk_txn_xid_t const * xid,
     469             :     ulong                     slot,
     470             :     fd_slot_hash_t const *    hashes, /* deque */
     471          51 :     fd_acct_addr_t *          out_accts_alt ) {
     472             : 
     473          51 :   if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
     474             : 
     475          12 :   ulong            readonly_lut_accs_cnt = 0UL;
     476          12 :   ulong            writable_lut_accs_cnt = 0UL;
     477          12 :   fd_acct_addr_t * readonly_lut_accs     = out_accts_alt+txn->addr_table_adtl_writable_cnt;
     478          12 :   fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
     479          24 :   for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
     480          12 :     fd_txn_acct_addr_lut_t const * addr_lut  = &addr_luts[i];
     481          12 :     fd_pubkey_t const * addr_lut_acc = (fd_pubkey_t *)(payload + addr_lut->addr_off);
     482             : 
     483             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94 */
     484          12 :     FD_TXN_ACCOUNT_DECL( addr_lut_rec );
     485          12 :     int err = fd_txn_account_init_from_funk_readonly( addr_lut_rec,
     486          12 :                                                       addr_lut_acc,
     487          12 :                                                       funk,
     488          12 :                                                       xid );
     489          12 :     if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
     490           0 :       return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
     491           0 :     }
     492             : 
     493             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L96-L114 */
     494          12 :     if( FD_UNLIKELY( memcmp( fd_txn_account_get_owner( addr_lut_rec ), fd_solana_address_lookup_table_program_id.key, sizeof(fd_pubkey_t) ) ) ) {
     495           0 :       return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_OWNER;
     496           0 :     }
     497             : 
     498             :     /* Realistically impossible case, but need to make sure we don't cause an OOB data access
     499             :        https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L205-L209 */
     500          12 :     if( FD_UNLIKELY( fd_txn_account_get_data_len( addr_lut_rec ) < FD_LOOKUP_TABLE_META_SIZE ) ) {
     501           0 :       return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA;
     502           0 :     }
     503             : 
     504             :     /* https://github.com/anza-xyz/agave/blob/574bae8fefc0ed256b55340b9d87b7689bcdf222/accounts-db/src/accounts.rs#L141-L142 */
     505          12 :     fd_bincode_decode_ctx_t decode_ctx = {
     506          12 :       .data    = fd_txn_account_get_data( addr_lut_rec ),
     507          12 :       .dataend = &fd_txn_account_get_data( addr_lut_rec )[FD_LOOKUP_TABLE_META_SIZE]
     508          12 :     };
     509             : 
     510          12 :     ulong total_sz = 0UL;
     511          12 :     err = fd_address_lookup_table_state_decode_footprint( &decode_ctx, &total_sz );
     512          12 :     if( FD_UNLIKELY( err ) ) {
     513           0 :       return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA;
     514           0 :     }
     515             : 
     516             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L197-L214 */
     517          12 :     fd_address_lookup_table_state_t table[1];
     518          12 :     fd_address_lookup_table_state_t * addr_lookup_table_state = fd_address_lookup_table_state_decode( table, &decode_ctx );
     519             : 
     520             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L200-L203 */
     521          12 :     if( FD_UNLIKELY( addr_lookup_table_state->discriminant != fd_address_lookup_table_state_enum_lookup_table ) ) {
     522           0 :       return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA;
     523           0 :     }
     524             : 
     525             :     /* Again probably an impossible case, but the ALUT data needs to be 32-byte aligned
     526             :        https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L210-L214 */
     527          12 :     if( FD_UNLIKELY( (fd_txn_account_get_data_len( addr_lut_rec ) - FD_LOOKUP_TABLE_META_SIZE) & 0x1fUL ) ) {
     528           0 :       return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA;
     529           0 :     }
     530             : 
     531             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L101-L112 */
     532          12 :     fd_acct_addr_t * lookup_addrs     = (fd_acct_addr_t *)&fd_txn_account_get_data( addr_lut_rec )[FD_LOOKUP_TABLE_META_SIZE];
     533          12 :     ulong            lookup_addrs_cnt = (fd_txn_account_get_data_len( addr_lut_rec ) - FD_LOOKUP_TABLE_META_SIZE) >> 5UL; // = (dlen - 56) / 32
     534             : 
     535             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L175-L176 */
     536          12 :     ulong active_addresses_len;
     537          12 :     err = fd_get_active_addresses_len( &addr_lookup_table_state->inner.lookup_table,
     538          12 :                                        slot,
     539          12 :                                        hashes,
     540          12 :                                        lookup_addrs_cnt,
     541          12 :                                        &active_addresses_len );
     542          12 :     if( FD_UNLIKELY( err ) ) {
     543           0 :       return err;
     544           0 :     }
     545             : 
     546             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L169-L182 */
     547          12 :     uchar * writable_lut_idxs = (uchar *)payload + addr_lut->writable_off;
     548          24 :     for( ulong j=0UL; j<addr_lut->writable_cnt; j++ ) {
     549             :       /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L177-L181 */
     550          12 :       if( writable_lut_idxs[j] >= active_addresses_len ) {
     551           0 :         return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_INDEX;
     552           0 :       }
     553          12 :       out_accts_alt[writable_lut_accs_cnt++] = lookup_addrs[writable_lut_idxs[j]];
     554          12 :     }
     555             : 
     556          12 :     uchar * readonly_lut_idxs = (uchar *)payload + addr_lut->readonly_off;
     557          24 :     for( ulong j=0UL; j<addr_lut->readonly_cnt; j++ ) {
     558             :       /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/sdk/program/src/address_lookup_table/state.rs#L177-L181 */
     559          12 :       if( readonly_lut_idxs[j] >= active_addresses_len ) {
     560           0 :         return FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_INDEX;
     561           0 :       }
     562          12 :       readonly_lut_accs[readonly_lut_accs_cnt++] = lookup_addrs[readonly_lut_idxs[j]];
     563          12 :     }
     564          12 :   }
     565             : 
     566          12 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     567          12 : }
     568             : 
     569             : int
     570             : fd_runtime_microblock_verify_read_write_conflicts( fd_txn_p_t *               txns,
     571             :                                                    ulong                      txn_cnt,
     572             :                                                    fd_conflict_detect_ele_t * acct_map,
     573             :                                                    fd_acct_addr_t *           acct_arr,
     574             :                                                    fd_funk_t *                funk,
     575             :                                                    fd_funk_txn_xid_t const *  xid,
     576             :                                                    ulong                      slot,
     577             :                                                    fd_slot_hash_t *           slot_hashes,
     578             :                                                    fd_features_t *            features,
     579             :                                                    int *                      out_conflict_detected,
     580          24 :                                                    fd_acct_addr_t *           out_conflict_addr_opt ) {
     581          24 :   *out_conflict_detected=FD_RUNTIME_NO_CONFLICT_DETECTED;
     582         246 : #define NO_CONFLICT ( *out_conflict_detected==FD_RUNTIME_NO_CONFLICT_DETECTED )
     583             : 
     584          36 : #define UPDATE_CONFLICT(cond1, cond2, acct) \
     585          36 : if( FD_UNLIKELY( cond1 ) ) { \
     586           9 :   if( FD_LIKELY( out_conflict_addr_opt ) ) *out_conflict_addr_opt = acct; \
     587           9 :   *out_conflict_detected=FD_RUNTIME_WRITE_WRITE_CONFLICT_DETECTED; \
     588          27 : } else if( FD_UNLIKELY( cond2 ) ) { \
     589           6 :   if( FD_LIKELY( out_conflict_addr_opt ) ) *out_conflict_addr_opt = acct; \
     590           6 :   *out_conflict_detected=FD_RUNTIME_READ_WRITE_CONFLICT_DETECTED; \
     591           6 : }
     592             : 
     593          24 :   ulong curr_idx            = 0;
     594          24 :   ulong sentinel_is_read    = 0;
     595          24 :   ulong sentinel_is_written = 0;
     596          24 :   int runtime_err           = FD_RUNTIME_EXECUTE_SUCCESS;
     597          75 :   for( ulong i=0; i<txn_cnt && NO_CONFLICT; i++ ) {
     598          51 :     fd_txn_p_t *           txn = txns+i;
     599          51 :     fd_acct_addr_t * txn_accts = acct_arr+curr_idx;
     600             : 
     601             :     /* Put the immediate & ALT accounts at txn_accts */
     602          51 :     const fd_acct_addr_t * accts_imm = fd_txn_get_acct_addrs( TXN(txn), txn->payload );
     603          51 :     ulong              accts_imm_cnt = fd_txn_account_cnt( TXN(txn), FD_TXN_ACCT_CAT_IMM );
     604          51 :     fd_memcpy( txn_accts, accts_imm, accts_imm_cnt*sizeof(fd_acct_addr_t) );
     605          51 :     runtime_err = fd_runtime_load_txn_address_lookup_tables( TXN(txn),
     606          51 :                                                              txn->payload,
     607          51 :                                                              funk,
     608          51 :                                                              xid,
     609          51 :                                                              slot,
     610          51 :                                                              slot_hashes,
     611          51 :                                                              txn_accts+accts_imm_cnt );
     612          51 :     if( FD_UNLIKELY( runtime_err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) break;
     613             : 
     614          51 :     ulong accounts_cnt   = fd_txn_account_cnt( TXN(txn), FD_TXN_ACCT_CAT_ALL );
     615          51 :     curr_idx            +=accounts_cnt;
     616          51 :     uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( fd_type_pun( txn_accts ), accounts_cnt );
     617             : 
     618             :     /* Iterate all writable accounts and detect W-W/R-W conflicts */
     619          51 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( TXN(txn), FD_TXN_ACCT_CAT_WRITABLE );
     620         162 :          iter!=fd_txn_acct_iter_end() && NO_CONFLICT;
     621         111 :          iter=fd_txn_acct_iter_next( iter ) ) {
     622         111 :       ushort idx                     = (ushort)fd_txn_acct_iter_idx( iter );
     623         111 :       fd_acct_addr_t writable_acc = txn_accts[ idx ];
     624             : 
     625             :       /* Check whether writable_acc is demoted to a read-only account */
     626         111 :       if( FD_UNLIKELY( !fd_exec_txn_account_is_writable_idx_flat( slot,
     627         111 :                                                                   idx,
     628         111 :                                                                   fd_type_pun( &txn_accts[ idx ] ),
     629         111 :                                                                   TXN(txn),
     630         111 :                                                                   features,
     631         111 :                                                                   bpf_upgradeable ) ) ) {
     632           3 :         continue;
     633           3 :       }
     634             : 
     635             :       /* writable_acc is the sentinel (fd_acct_addr_null) */
     636         108 :       if( FD_UNLIKELY( fd_conflict_detect_map_key_inval( writable_acc ) ) ) {
     637           9 :         UPDATE_CONFLICT( sentinel_is_written, sentinel_is_read, writable_acc );
     638           9 :         sentinel_is_written = 1;
     639           9 :         continue;
     640           9 :       }
     641             : 
     642             :       /* writable_acc is not the sentinel (fd_acct_addr_null) */
     643          99 :       fd_conflict_detect_ele_t * found = fd_conflict_detect_map_query( acct_map, writable_acc, NULL );
     644          99 :       if( FD_UNLIKELY( found ) ) {
     645           6 :         UPDATE_CONFLICT( found->writable, !found->writable, writable_acc );
     646          93 :       } else {
     647          93 :         fd_conflict_detect_ele_t * entry = fd_conflict_detect_map_insert( acct_map, writable_acc );
     648          93 :         entry->writable                  = 1;
     649          93 :       }
     650          99 :     }
     651             : 
     652             :     /* Iterate all readonly accounts and detect R-W conflicts */
     653          51 :     for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( TXN(txn), FD_TXN_ACCT_CAT_READONLY );
     654          99 :          iter!=fd_txn_acct_iter_end() && NO_CONFLICT;
     655          51 :          iter=fd_txn_acct_iter_next( iter ) ) {
     656          48 :       fd_acct_addr_t readonly_acc = txn_accts[ fd_txn_acct_iter_idx( iter ) ];
     657             : 
     658             :       /* readonly_acc is the sentinel (fd_acct_addr_null) */
     659          48 :       if( FD_UNLIKELY( fd_conflict_detect_map_key_inval( readonly_acc ) ) ) {
     660           3 :         UPDATE_CONFLICT( 0, sentinel_is_written, readonly_acc );
     661           3 :         sentinel_is_read = 1;
     662           3 :         continue;
     663           3 :       }
     664             : 
     665             :       /* readonly_acc is not the sentinel (fd_acct_addr_null) */
     666          45 :       fd_conflict_detect_ele_t * found = fd_conflict_detect_map_query( acct_map, readonly_acc, NULL );
     667          45 :       if( FD_UNLIKELY( found ) ) {
     668          18 :         UPDATE_CONFLICT( 0, found->writable, readonly_acc );
     669          27 :       } else {
     670          27 :         fd_conflict_detect_ele_t * entry = fd_conflict_detect_map_insert( acct_map, readonly_acc );
     671          27 :         entry->writable                  = 0;
     672          27 :       }
     673          45 :     }
     674          51 :   }
     675             : 
     676             :   /* Clear all the entries inserted into acct_map */
     677         198 :   for( ulong i=0; i<curr_idx; i++ ) {
     678         174 :     if( FD_UNLIKELY( fd_conflict_detect_map_key_inval( acct_arr[i] ) ) ) continue;
     679         162 :     fd_conflict_detect_ele_t * found = fd_conflict_detect_map_query( acct_map, acct_arr[i], NULL );
     680         162 :     if( FD_LIKELY( found ) ) fd_conflict_detect_map_remove( acct_map, found );
     681         162 :   }
     682             : 
     683          24 :   if( FD_UNLIKELY( runtime_err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     684           0 :     return runtime_err;
     685          24 :   } else {
     686             :     /* https://github.com/anza-xyz/agave/blob/v2.2.3/accounts-db/src/account_locks.rs#L31 */
     687             :     /* https://github.com/anza-xyz/agave/blob/v2.2.3/accounts-db/src/account_locks.rs#L34 */
     688          24 :     return NO_CONFLICT? FD_RUNTIME_EXECUTE_SUCCESS : FD_RUNTIME_TXN_ERR_ACCOUNT_IN_USE;
     689          24 :   }
     690          24 : }
     691             : 
     692             : int
     693             : fd_runtime_block_execute_prepare( fd_bank_t *               bank,
     694             :                                   fd_funk_t *               funk,
     695             :                                   fd_funk_txn_xid_t const * xid,
     696             :                                   fd_capture_ctx_t *        capture_ctx,
     697           0 :                                   fd_spad_t *               runtime_spad ) {
     698           0 :   fd_bank_execution_fees_set( bank, 0UL );
     699             : 
     700           0 :   fd_bank_priority_fees_set( bank, 0UL );
     701             : 
     702           0 :   fd_bank_signature_count_set( bank, 0UL );
     703             : 
     704           0 :   fd_bank_total_compute_units_used_set( bank, 0UL );
     705             : 
     706             :   /* Setup cost tracker */
     707           0 :   if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
     708           0 :     fd_cost_tracker_t * cost_tracker = fd_cost_tracker_join( fd_cost_tracker_new(
     709           0 :         fd_bank_cost_tracker_locking_modify( bank ),
     710           0 :         fd_bank_features_query( bank ),
     711           0 :         fd_bank_slot_get( bank ),
     712           0 :         999UL /* TODO: Needs real seed */ ) );
     713           0 :     if( FD_UNLIKELY( !cost_tracker ) ) {
     714           0 :       FD_LOG_CRIT(("Unable to allocate memory for cost tracker" ));
     715           0 :     }
     716           0 :     fd_bank_cost_tracker_end_locking_modify( bank );
     717           0 :   }
     718             : 
     719           0 :   int result = fd_runtime_block_sysvar_update_pre_execute( bank, funk, xid, capture_ctx, runtime_spad );
     720           0 :   if( FD_UNLIKELY( result != 0 ) ) {
     721           0 :     FD_LOG_WARNING(("updating sysvars failed"));
     722           0 :     return result;
     723           0 :   }
     724             : 
     725           0 :   if( FD_UNLIKELY( !fd_sysvar_cache_restore( bank, funk, xid ) ) ) {
     726           0 :     FD_LOG_ERR(( "Failed to restore sysvar cache" ));
     727           0 :   }
     728             : 
     729           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     730           0 : }
     731             : 
     732             : static void
     733             : fd_runtime_update_bank_hash( fd_bank_t *        bank,
     734             :                              fd_capture_ctx_t * capture_ctx,
     735           0 :                              int                silent ) {
     736             :   /* Save the previous bank hash, and the parents signature count */
     737           0 :   fd_hash_t const * prev_bank_hash = NULL;
     738           0 :   if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
     739           0 :     prev_bank_hash = fd_bank_bank_hash_query( bank );
     740           0 :     fd_bank_prev_bank_hash_set( bank, *prev_bank_hash );
     741           0 :   } else {
     742           0 :     prev_bank_hash = fd_bank_prev_bank_hash_query( bank );
     743           0 :   }
     744             : 
     745           0 :   fd_bank_parent_signature_cnt_set( bank, fd_bank_signature_count_get( bank ) );
     746             : 
     747             :   /* Compute the new bank hash */
     748           0 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
     749           0 :   fd_hash_t new_bank_hash[1] = { 0 };
     750           0 :   fd_hashes_hash_bank(
     751           0 :       lthash,
     752           0 :       prev_bank_hash,
     753           0 :       (fd_hash_t *)fd_bank_poh_query( bank )->hash,
     754           0 :       fd_bank_signature_count_get( bank ),
     755           0 :       new_bank_hash );
     756             : 
     757             :   /* Update the bank hash */
     758           0 :   fd_bank_bank_hash_set( bank, *new_bank_hash );
     759             : 
     760           0 :   if( !silent ) {
     761           0 :     FD_LOG_NOTICE(( "\n\n[Runtime]\n"
     762           0 :                     "slot:             %lu\n"
     763           0 :                     "bank hash:        %s\n"
     764           0 :                     "parent bank hash: %s\n"
     765           0 :                     "lthash:           %s\n"
     766           0 :                     "signature_count:  %lu\n"
     767           0 :                     "last_blockhash:   %s\n",
     768           0 :                     fd_bank_slot_get( bank ),
     769           0 :                     FD_BASE58_ENC_32_ALLOCA( new_bank_hash->hash ),
     770           0 :                     FD_BASE58_ENC_32_ALLOCA( fd_bank_prev_bank_hash_query( bank ) ),
     771           0 :                     FD_LTHASH_ENC_32_ALLOCA( lthash->bytes ),
     772           0 :                     fd_bank_signature_count_get( bank ),
     773           0 :                     FD_BASE58_ENC_32_ALLOCA( fd_bank_poh_query( bank )->hash ) ));
     774           0 :   }
     775             : 
     776           0 :   if( capture_ctx != NULL && capture_ctx->capture != NULL &&
     777           0 :     fd_bank_slot_get( bank )>=capture_ctx->solcap_start_slot ) {
     778             : 
     779           0 :     uchar lthash_hash[FD_HASH_FOOTPRINT];
     780           0 :     fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
     781             : 
     782           0 :     fd_solcap_write_bank_preimage(
     783           0 :           capture_ctx->capture,
     784           0 :           new_bank_hash->hash,
     785           0 :           fd_bank_prev_bank_hash_query( bank ),
     786           0 :           NULL,
     787           0 :           lthash_hash,
     788           0 :           fd_bank_poh_query( bank )->hash,
     789           0 :           fd_bank_signature_count_get( bank ) );
     790           0 :   }
     791             : 
     792           0 :   fd_bank_lthash_end_locking_query( bank );
     793           0 : }
     794             : 
     795             : /******************************************************************************/
     796             : /* Transaction Level Execution Management                                     */
     797             : /******************************************************************************/
     798             : 
     799             : /* fd_runtime_pre_execute_check is responsible for conducting many of the
     800             :    transaction sanitization checks. */
     801             : 
     802             : int
     803           0 : fd_runtime_pre_execute_check( fd_exec_txn_ctx_t * txn_ctx ) {
     804             : 
     805           0 :   int err;
     806             : 
     807             :   /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
     808             :      TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
     809             :      be moved in the future. The Agave call heirarchy looks something like this:
     810             :             process_single_slot
     811             :                    v
     812             :             confirm_full_slot
     813             :                    v
     814             :             confirm_slot_entries --------------------------------------------------->
     815             :                    v                               v                                v
     816             :             verify_transaction    ComputeBudget::process_instruction         process_entries
     817             :                    v                                                                v
     818             :             verify_precompiles                                                process_batches
     819             :                                                                                     v
     820             :                                                                                    ...
     821             :                                                                                     v
     822             :                                                                         load_and_execute_transactions
     823             :                                                                                     v
     824             :                                                                                    ...
     825             :                                                                                     v
     826             :                                                                               load_accounts --> load_transaction_accounts
     827             :                                                                                     v
     828             :                                                                        general transaction execution
     829             : 
     830             :   */
     831             : 
     832           0 :   uchar dump_txn = !!( txn_ctx->capture_ctx &&
     833           0 :                        txn_ctx->slot >= txn_ctx->capture_ctx->dump_proto_start_slot &&
     834           0 :                        txn_ctx->capture_ctx->dump_txn_to_pb );
     835           0 :   if( FD_UNLIKELY( dump_txn ) ) {
     836           0 :     fd_dump_txn_to_protobuf( txn_ctx, txn_ctx->spad );
     837           0 :   }
     838             : 
     839             :   /* Verify the transaction. For now, this step only involves processing
     840             :      the compute budget instructions. */
     841           0 :   err = fd_executor_verify_transaction( txn_ctx );
     842           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     843           0 :     txn_ctx->flags = 0U;
     844           0 :     return err;
     845           0 :   }
     846             : 
     847             :   /* Resolve and verify ALUT-referenced account keys, if applicable */
     848           0 :   err = fd_executor_setup_txn_alut_account_keys( txn_ctx );
     849           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     850           0 :     txn_ctx->flags = 0U;
     851           0 :     return err;
     852           0 :   }
     853             : 
     854             :   /* Set up the transaction accounts and other txn ctx metadata */
     855           0 :   fd_executor_setup_accounts_for_txn( txn_ctx );
     856             : 
     857             :   /* Post-sanitization checks. Called from prepare_sanitized_batch()
     858             :      which, for now, only is used to lock the accounts and perform a
     859             :      couple basic validations.
     860             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
     861           0 :   err = fd_executor_validate_account_locks( txn_ctx );
     862           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     863           0 :     txn_ctx->flags = 0U;
     864           0 :     return err;
     865           0 :   }
     866             : 
     867             :   /* load_and_execute_transactions() -> check_transactions()
     868             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
     869           0 :   err = fd_executor_check_transactions( txn_ctx );
     870           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     871           0 :     txn_ctx->flags = 0U;
     872           0 :     return err;
     873           0 :   }
     874             : 
     875             :   /* load_and_execute_sanitized_transactions() -> validate_fees() ->
     876             :      validate_transaction_fee_payer()
     877             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
     878           0 :   err = fd_executor_validate_transaction_fee_payer( txn_ctx );
     879           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     880           0 :     txn_ctx->flags = 0U;
     881           0 :     return err;
     882           0 :   }
     883             : 
     884             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
     885           0 :   err = fd_executor_load_transaction_accounts( txn_ctx );
     886           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     887             :     /* Regardless of whether transaction accounts were loaded successfully, the transaction is
     888             :        included in the block and transaction fees are collected.
     889             :        https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
     890           0 :     txn_ctx->flags |= FD_TXN_P_FLAGS_FEES_ONLY;
     891             : 
     892             :     /* If the transaction fails to load, the "rollback" accounts will include one of the following:
     893             :         1. Nonce account only
     894             :         2. Fee payer only
     895             :         3. Nonce account + fee payer
     896             : 
     897             :         Because the cost tracker uses the loaded account data size in block cost calculations, we need to
     898             :         make sure our calculated loaded accounts data size is conformant with Agave's.
     899             :         https://github.com/anza-xyz/agave/blob/v2.1.14/runtime/src/bank.rs#L4116
     900             : 
     901             :         In any case, we should always add the dlen of the fee payer. */
     902           0 :     txn_ctx->loaded_accounts_data_size = fd_txn_account_get_data_len( &txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX] );
     903             : 
     904             :     /* Special case handling for if a nonce account is present in the transaction. */
     905           0 :     if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
     906             :       /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
     907             :           be double counting the dlen of the fee payer. */
     908           0 :       if( txn_ctx->nonce_account_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
     909           0 :         txn_ctx->loaded_accounts_data_size += fd_txn_account_get_data_len( txn_ctx->rollback_nonce_account );
     910           0 :       }
     911           0 :     }
     912           0 :   }
     913             : 
     914             :   /*
     915             :      The fee payer and the nonce account will be stored and hashed so
     916             :      long as the transaction landed on chain, or, in Agave terminology,
     917             :      the transaction was processed.
     918             :      https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
     919             : 
     920             :      A transaction lands on chain in one of two ways:
     921             :      (1) Passed fee validation and loaded accounts.
     922             :      (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
     923             :          SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
     924             : 
     925             :      So, at this point, the transaction is committable.
     926             :    */
     927             : 
     928           0 :   return err;
     929           0 : }
     930             : 
     931             : /* fd_runtime_finalize_account is a helper used to commit the data from
     932             :    a writable transaction account back into the accountsdb. */
     933             : 
     934             : static void
     935             : fd_runtime_finalize_account( fd_funk_t *               funk,
     936             :                              fd_funk_txn_xid_t const * xid,
     937           0 :                              fd_txn_account_t *        acc ) {
     938           0 :   if( FD_UNLIKELY( !fd_txn_account_is_mutable( acc ) ) ) {
     939           0 :     FD_LOG_CRIT(( "fd_runtime_finalize_account: account is not mutable" ));
     940           0 :   }
     941             : 
     942           0 :   fd_pubkey_t const * key         = acc->pubkey;
     943           0 :   uchar       const * record_data = (uchar *)fd_txn_account_get_meta( acc );
     944           0 :   ulong               record_sz   = fd_account_meta_get_record_sz( acc->meta );
     945             : 
     946           0 :   int err = FD_FUNK_SUCCESS;
     947             : 
     948           0 :   fd_funk_rec_key_t     funk_key = fd_funk_acc_key( key );
     949           0 :   fd_funk_rec_prepare_t prepare[1];
     950           0 :   fd_funk_rec_t *       rec = fd_funk_rec_prepare( funk, xid, &funk_key, prepare, &err );
     951           0 :   if( FD_UNLIKELY( !rec || err!=FD_FUNK_SUCCESS ) ) {
     952           0 :     FD_LOG_ERR(( "fd_runtime_finalize_account: failed to prepare record (%i-%s)", err, fd_funk_strerror( err ) ));
     953           0 :   }
     954             : 
     955           0 :   if( FD_UNLIKELY( !fd_funk_val_truncate(
     956           0 :       rec,
     957           0 :       fd_funk_alloc( funk ),
     958           0 :       fd_funk_wksp( funk ),
     959           0 :       0UL,
     960           0 :       record_sz,
     961           0 :       &err ) ) ) {
     962           0 :     FD_LOG_ERR(( "fd_funk_val_truncate(sz=%lu) for account failed (%i-%s)", record_sz, err, fd_funk_strerror( err ) ));
     963           0 :   }
     964             : 
     965           0 :   fd_memcpy( fd_funk_val( rec, fd_funk_wksp( funk ) ), record_data, record_sz );
     966             : 
     967           0 :   fd_funk_rec_publish( funk, prepare );
     968           0 : }
     969             : 
     970             : /* fd_runtime_buffer_solcap_account_update buffers an account
     971             :    update event message in the capture context, which will be
     972             :    sent to the replay tile via the exec_replay link.
     973             :    This buffering is done to avoid passing stem down into the runtime.
     974             : 
     975             :    TODO: remove this when solcap v2 is here. */
     976             : static void
     977             : fd_runtime_buffer_solcap_account_update( fd_txn_account_t *        account,
     978             :                                          fd_bank_t *               bank,
     979           0 :                                          fd_capture_ctx_t *        capture_ctx ) {
     980             : 
     981             :   /* Check if we should publish the update */
     982           0 :   if( FD_UNLIKELY( !capture_ctx || fd_bank_slot_get( bank )<capture_ctx->solcap_start_slot ) ) {
     983           0 :     return;
     984           0 :   }
     985             : 
     986             :   /* Get account data */
     987           0 :   fd_account_meta_t const * meta = fd_txn_account_get_meta( account );
     988           0 :   void const * data              = fd_txn_account_get_data( account );
     989             : 
     990             :   /* Calculate account hash using lthash */
     991           0 :   fd_lthash_value_t lthash[1];
     992           0 :   fd_hashes_account_lthash( account->pubkey, meta, data, lthash );
     993             : 
     994             :   /* Calculate message size */
     995           0 :   if( FD_UNLIKELY( capture_ctx->account_updates_len > FD_CAPTURE_CTX_MAX_ACCOUNT_UPDATES ) ) {
     996           0 :     FD_LOG_CRIT(( "cannot buffer solcap account update. this should never happen" ));
     997           0 :     return;
     998           0 :   }
     999             : 
    1000             :   /* Write the message to the buffer */
    1001           0 :   fd_capture_ctx_account_update_msg_t * account_update_msg = (fd_capture_ctx_account_update_msg_t *)(capture_ctx->account_updates_buffer_ptr);
    1002           0 :   account_update_msg->pubkey               = *account->pubkey;
    1003           0 :   account_update_msg->info                 = fd_txn_account_get_solana_meta( account );
    1004           0 :   account_update_msg->data_sz              = meta->dlen;
    1005           0 :   account_update_msg->bank_idx             = bank->idx;
    1006           0 :   memcpy( account_update_msg->hash.uc, lthash->bytes, sizeof(fd_hash_t) );
    1007           0 :   capture_ctx->account_updates_buffer_ptr += sizeof(fd_capture_ctx_account_update_msg_t);
    1008             : 
    1009             :   /* Write the account data to the buffer */
    1010           0 :   memcpy( capture_ctx->account_updates_buffer_ptr, data, meta->dlen );
    1011           0 :   capture_ctx->account_updates_buffer_ptr += meta->dlen;
    1012             : 
    1013           0 :   capture_ctx->account_updates_len++;
    1014           0 : }
    1015             : 
    1016             : /* fd_runtime_save_account is a convenience wrapper that looks
    1017             :    up the previous account state from funk before updating the lthash
    1018             :    and saving the new version of the account to funk.
    1019             : 
    1020             :    TODO: We have to make a read request to the DB, so that we can calculate
    1021             :    the previous version of the accounts hash, to mix-out from the accumulated
    1022             :    lthash.  In future we should likely cache the previous version of the account
    1023             :    in transaction setup, so that we don't have to issue a read request here.
    1024             : 
    1025             :    funk is the funk database handle.  funk_txn is the transaction
    1026             :    context to query (NULL for root context).  account is the modified
    1027             :    account.  bank and capture_ctx are passed to fd_hashes_update_lthash.
    1028             : 
    1029             :    This function:
    1030             :    - Queries funk for the previous account version
    1031             :    - Computes the hash of the previous version (or uses zero for new)
    1032             :    - Calls fd_hashes_update_lthash with the computed previous hash
    1033             :    - Saves the new version of the account to Funk
    1034             :    - Notifies the replay tile that an account update has occurred, so it
    1035             :      can write the account to the solcap file.
    1036             : 
    1037             :    The function handles FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT gracefully (uses
    1038             :    zero hash).  On other funk errors, the function will FD_LOG_ERR.
    1039             :    All non-optional pointers must be valid. */
    1040             : 
    1041             : static void
    1042             : fd_runtime_save_account( fd_funk_t *               funk,
    1043             :                          fd_funk_txn_xid_t const * xid,
    1044             :                          fd_txn_account_t *        account,
    1045             :                          fd_bank_t *               bank,
    1046             :                          fd_wksp_t *               acc_data_wksp,
    1047           0 :                          fd_capture_ctx_t *        capture_ctx ) {
    1048             : 
    1049             :   /* Join the transaction account */
    1050           0 :   if( FD_UNLIKELY( !fd_txn_account_join( account, acc_data_wksp ) ) ) {
    1051           0 :     FD_LOG_CRIT(( "fd_runtime_save_account: failed to join account" ));
    1052           0 :   }
    1053             : 
    1054             :   /* Look up the previous version of the account from Funk */
    1055           0 :   FD_TXN_ACCOUNT_DECL( previous_account_version );
    1056           0 :   int err = fd_txn_account_init_from_funk_readonly( previous_account_version, account->pubkey, funk, xid );
    1057           0 :   if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS && err!=FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) {
    1058           0 :     FD_LOG_CRIT(( "Failed to read old account version from Funk" ));
    1059           0 :     return;
    1060           0 :   }
    1061             : 
    1062             :   /* Hash the old version of the account */
    1063           0 :   fd_lthash_value_t prev_hash[1];
    1064           0 :   fd_lthash_zero( prev_hash );
    1065           0 :   if( err != FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) {
    1066           0 :     fd_hashes_account_lthash(
    1067           0 :       account->pubkey,
    1068           0 :       fd_txn_account_get_meta( previous_account_version ),
    1069           0 :       fd_txn_account_get_data( previous_account_version ),
    1070           0 :       prev_hash );
    1071           0 :   }
    1072             : 
    1073             :   /* Mix in the account hash into the bank hash */
    1074           0 :   fd_hashes_update_lthash( account, prev_hash, bank, NULL );
    1075             : 
    1076             :   /* Publish account update to replay tile for solcap writing
    1077             :      TODO: write in the exec tile with solcap v2 */
    1078           0 :   fd_runtime_buffer_solcap_account_update( account, bank, capture_ctx );
    1079             : 
    1080             :   /* Save the new version of the account to Funk */
    1081           0 :   fd_runtime_finalize_account( funk, xid, account );
    1082           0 : }
    1083             : 
    1084             : /* fd_runtime_finalize_txn is a helper used by the non-tpool transaction
    1085             :    executor to finalize borrowed account changes back into funk. It also
    1086             :    handles txncache insertion and updates to the vote/stake cache.
    1087             :    TODO: This function should probably be moved to fd_executor.c. */
    1088             : 
    1089             : void
    1090             : fd_runtime_finalize_txn( fd_funk_t *               funk,
    1091             :                          fd_txncache_t *           txncache,
    1092             :                          fd_funk_txn_xid_t const * xid,
    1093             :                          fd_exec_txn_ctx_t *       txn_ctx,
    1094             :                          fd_bank_t *               bank,
    1095           0 :                          fd_capture_ctx_t *        capture_ctx ) {
    1096             : 
    1097             :   /* Collect fees */
    1098           0 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_txn_count_modify( bank ), 1UL );
    1099           0 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_execution_fees_modify( bank ), txn_ctx->execution_fee );
    1100           0 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_priority_fees_modify( bank ), txn_ctx->priority_fee );
    1101             : 
    1102           0 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_signature_count_modify( bank ), TXN( &txn_ctx->txn )->signature_cnt );
    1103             : 
    1104           0 :   if( FD_UNLIKELY( txn_ctx->exec_err ) ) {
    1105             : 
    1106             :     /* Save the fee_payer. Everything but the fee balance should be reset.
    1107             :        TODO: an optimization here could be to use a dirty flag in the
    1108             :        borrowed account. If the borrowed account data has been changed in
    1109             :        any way, then the full account can be rolled back as it is done now.
    1110             :        However, most of the time the account data is not changed, and only
    1111             :        the lamport balance has to change. */
    1112             : 
    1113             :     /* With nonce account rollbacks, there are three cases:
    1114             :        1. No nonce account in the transaction
    1115             :        2. Nonce account is the fee payer
    1116             :        3. Nonce account is not the fee payer
    1117             : 
    1118             :        We should always rollback the nonce account first. Note that the nonce account may be the fee payer (case 2). */
    1119           0 :     if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
    1120           0 :       fd_runtime_save_account( funk, xid, txn_ctx->rollback_nonce_account, bank, txn_ctx->spad_wksp, capture_ctx );
    1121           0 :     }
    1122             : 
    1123             :     /* Now, we must only save the fee payer if the nonce account was not the fee payer (because that was already saved above) */
    1124           0 :     if( FD_LIKELY( txn_ctx->nonce_account_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
    1125           0 :       fd_runtime_save_account( funk, xid, txn_ctx->rollback_fee_payer_account, bank, txn_ctx->spad_wksp, capture_ctx );
    1126           0 :     }
    1127           0 :   } else {
    1128             : 
    1129           0 :     int dirty_vote_acc  = txn_ctx->dirty_vote_acc;
    1130             : 
    1131           0 :     for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
    1132             :       /* We are only interested in saving writable accounts and the fee
    1133             :          payer account. */
    1134           0 :       if( !fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, i ) && i!=FD_FEE_PAYER_TXN_IDX ) {
    1135           0 :         continue;
    1136           0 :       }
    1137             : 
    1138           0 :       fd_txn_account_t * acc_rec = fd_txn_account_join( &txn_ctx->accounts[i], txn_ctx->spad_wksp );
    1139           0 :       if( FD_UNLIKELY( !acc_rec ) ) {
    1140           0 :         FD_LOG_CRIT(( "fd_runtime_finalize_txn: failed to join account at idx %u", i ));
    1141           0 :       }
    1142             : 
    1143           0 :       if( dirty_vote_acc && 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_vote_program_id, sizeof(fd_pubkey_t) ) ) {
    1144           0 :         fd_vote_store_account( acc_rec, bank );
    1145           0 :       }
    1146             : 
    1147           0 :       if( 0==memcmp( fd_txn_account_get_owner( acc_rec ), &fd_solana_stake_program_id, sizeof(fd_pubkey_t) ) ) {
    1148           0 :         fd_update_stake_delegation( acc_rec, bank );
    1149           0 :       }
    1150             : 
    1151             :       /* Reclaim any accounts that have 0-lamports, now that any related
    1152             :          cache updates have been applied. */
    1153           0 :       fd_executor_reclaim_account( txn_ctx, &txn_ctx->accounts[i] );
    1154             : 
    1155           0 :       fd_runtime_save_account( funk, xid, &txn_ctx->accounts[i], bank, txn_ctx->spad_wksp, capture_ctx );
    1156           0 :     }
    1157             : 
    1158             :     /* We need to queue any existing program accounts that may have
    1159             :        been deployed / upgraded for reverification in the program
    1160             :        cache since their programdata may have changed. ELF / sBPF
    1161             :        metadata will need to be updated. */
    1162           0 :       ulong current_slot = fd_bank_slot_get( bank );
    1163           0 :       for( uchar i=0; i<txn_ctx->programs_to_reverify_cnt; i++ ) {
    1164           0 :         fd_pubkey_t const * program_key = &txn_ctx->programs_to_reverify[i];
    1165           0 :         fd_program_cache_queue_program_for_reverification( funk, xid, program_key, current_slot );
    1166           0 :       }
    1167           0 :   }
    1168             : 
    1169           0 :   int is_vote = fd_txn_is_simple_vote_transaction( TXN( &txn_ctx->txn ), txn_ctx->txn.payload );
    1170           0 :   if( !is_vote ){
    1171           0 :     ulong * nonvote_txn_count = fd_bank_nonvote_txn_count_modify( bank );
    1172           0 :     FD_ATOMIC_FETCH_AND_ADD(nonvote_txn_count, 1);
    1173             : 
    1174           0 :     if( FD_UNLIKELY( txn_ctx->exec_err ) ){
    1175           0 :       ulong * nonvote_failed_txn_count = fd_bank_nonvote_failed_txn_count_modify( bank );
    1176           0 :       FD_ATOMIC_FETCH_AND_ADD( nonvote_failed_txn_count, 1 );
    1177           0 :     }
    1178           0 :   } else {
    1179           0 :     if( FD_UNLIKELY( txn_ctx->exec_err ) ){
    1180           0 :       ulong * failed_txn_count = fd_bank_failed_txn_count_modify( bank );
    1181           0 :       FD_ATOMIC_FETCH_AND_ADD( failed_txn_count, 1 );
    1182           0 :     }
    1183           0 :   }
    1184             : 
    1185           0 :   ulong * total_compute_units_used = fd_bank_total_compute_units_used_modify( bank );
    1186           0 :   FD_ATOMIC_FETCH_AND_ADD( total_compute_units_used, txn_ctx->compute_budget_details.compute_unit_limit - txn_ctx->compute_budget_details.compute_meter );
    1187             : 
    1188             :   /* Update the cost tracker */
    1189           0 :   fd_cost_tracker_t * cost_tracker = fd_cost_tracker_join( fd_bank_cost_tracker_locking_modify( bank ) );
    1190           0 :   int res = fd_cost_tracker_calculate_cost_and_add( cost_tracker, txn_ctx );
    1191           0 :   if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
    1192           0 :     txn_ctx->flags = 0U;
    1193           0 :   }
    1194           0 :   fd_bank_cost_tracker_end_locking_modify( bank );
    1195             : 
    1196           0 :   txn_ctx->loaded_accounts_data_size_cost = fd_cost_tracker_calculate_loaded_accounts_data_size_cost( txn_ctx );
    1197             : 
    1198           0 :   if( FD_LIKELY( txncache && txn_ctx->nonce_account_idx_in_txn==ULONG_MAX ) ) {
    1199             :     /* In Agave, durable nonce transactions are inserted to the status
    1200             :        cache the same as any others, but this is only to serve RPC
    1201             :        requests, they do not need to be in there for correctness as the
    1202             :        nonce mechanism itself prevents double spend.  We skip this logic
    1203             :        entirely to simplify and improve performance of the txn cache. */
    1204             : 
    1205           0 :     fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_ctx->txn.payload + TXN( &txn_ctx->txn )->recent_blockhash_off);
    1206           0 :     fd_txncache_insert( txncache, bank->txncache_fork_id, blockhash->uc, txn_ctx->blake_txn_msg_hash.uc );
    1207           0 :   }
    1208           0 : }
    1209             : 
    1210             : int
    1211             : fd_runtime_prepare_and_execute_txn( fd_banks_t *        banks,
    1212             :                                     ulong               bank_idx,
    1213             :                                     fd_exec_txn_ctx_t * txn_ctx,
    1214             :                                     fd_txn_p_t *        txn,
    1215             :                                     fd_spad_t *         exec_spad,
    1216           0 :                                     fd_capture_ctx_t *  capture_ctx ) {
    1217           0 :   FD_SPAD_FRAME_BEGIN( exec_spad ) {
    1218           0 :   int exec_res = 0;
    1219             : 
    1220           0 :   fd_bank_t * bank = fd_banks_bank_query( banks, bank_idx );
    1221           0 :   if( FD_UNLIKELY( !bank ) ) {
    1222           0 :     FD_LOG_CRIT(( "Could not get bank at pool idx %lu", bank_idx ));
    1223           0 :   }
    1224             : 
    1225           0 :   ulong slot = fd_bank_slot_get( bank );
    1226             : 
    1227             :   /* Setup and execute the transaction. */
    1228           0 :   txn_ctx->bank                  = bank;
    1229           0 :   txn_ctx->slot                  = fd_bank_slot_get( bank );
    1230           0 :   txn_ctx->bank_idx              = bank_idx;
    1231           0 :   txn_ctx->features              = fd_bank_features_get( bank );
    1232           0 :   txn_ctx->enable_exec_recording = !!( bank->flags & FD_BANK_FLAGS_EXEC_RECORDING );
    1233           0 :   txn_ctx->xid[0]                = (fd_funk_txn_xid_t){ .ul = { slot, slot } };
    1234           0 :   txn_ctx->capture_ctx           = capture_ctx;
    1235           0 :   txn_ctx->txn                   = *txn;
    1236             : 
    1237           0 :   txn_ctx->flags = FD_TXN_P_FLAGS_SANITIZE_SUCCESS;
    1238           0 :   fd_exec_txn_ctx_setup_basic( txn_ctx );
    1239             : 
    1240             :   /* Set up the core account keys. These are the account keys directly
    1241             :      passed in via the serialized transaction, represented as an array.
    1242             :      Note that this does not include additional keys referenced in
    1243             :      address lookup tables. */
    1244           0 :   fd_executor_setup_txn_account_keys( txn_ctx );
    1245             : 
    1246             :   /* Pre-execution checks */
    1247           0 :   exec_res = fd_runtime_pre_execute_check( txn_ctx );
    1248           0 :   if( FD_UNLIKELY( !( txn_ctx->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS ) ) ) {
    1249           0 :     return exec_res;
    1250           0 :   }
    1251             : 
    1252             :   /* Execute the transaction. Note that fees-only transactions are still
    1253             :      marked as "executed". */
    1254           0 :   txn_ctx->flags |= FD_TXN_P_FLAGS_EXECUTE_SUCCESS;
    1255           0 :   if( FD_LIKELY( !( txn_ctx->flags & FD_TXN_P_FLAGS_FEES_ONLY ) ) ) {
    1256           0 :     exec_res = fd_execute_txn( txn_ctx );
    1257           0 :   }
    1258             : 
    1259           0 :   return exec_res;
    1260             : 
    1261           0 :   } FD_SPAD_FRAME_END;
    1262           0 : }
    1263             : 
    1264             : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responisble
    1265             :    for the bulk of the pre-transaction execution checks in the runtime.
    1266             :    They aim to preserve the ordering present in the Agave client to match
    1267             :    parity in terms of error codes. Sigverify is kept separate from the rest
    1268             :    of the transaction checks for fuzzing convenience.
    1269             : 
    1270             :    For reference this is the general code path which contains all relevant
    1271             :    pre-transactions checks in the v2.0.x Agave client from upstream
    1272             :    to downstream is as follows:
    1273             : 
    1274             :    confirm_slot_entries() which calls verify_ticks() and
    1275             :    verify_transaction(). verify_transaction() calls verify_and_hash_message()
    1276             :    and verify_precompiles() which parallels fd_executor_txn_verify() and
    1277             :    fd_executor_verify_transaction().
    1278             : 
    1279             :    process_entries() contains a duplicate account check which is part of
    1280             :    agave account lock acquiring. This is checked inline in
    1281             :    fd_runtime_pre_execute_check().
    1282             : 
    1283             :    load_and_execute_transactions() contains the function check_transactions().
    1284             :    This contains check_age() and check_status_cache() which is paralleled by
    1285             :    fd_executor_check_transaction_age_and_compute_budget_limits() and
    1286             :    fd_executor_check_status_cache() respectively.
    1287             : 
    1288             :    load_and_execute_sanitized_transactions() contains validate_fees()
    1289             :    which is responsible for executing the compute budget instructions,
    1290             :    validating the fee payer and collecting the fee. This is mirrored in
    1291             :    firedancer with fd_executor_compute_budget_program_execute_instructions()
    1292             :    and fd_executor_collect_fees(). load_and_execute_sanitized_transactions()
    1293             :    also checks the total data size of the accounts in load_accounts() and
    1294             :    validates the program accounts in load_transaction_accounts(). This
    1295             :    is paralled by fd_executor_load_transaction_accounts(). */
    1296             : 
    1297             : /******************************************************************************/
    1298             : /* Epoch Boundary                                                             */
    1299             : /******************************************************************************/
    1300             : 
    1301             : /* Replace the vote states for T-2 (vote_states_prev_prev) with the vote
    1302             :    states for T-1 (vote_states_prev) */
    1303             : 
    1304             : static void
    1305           0 : fd_update_vote_states_prev_prev( fd_bank_t * bank ) {
    1306             : 
    1307           0 :   fd_vote_states_t *       vote_states_prev_prev = fd_bank_vote_states_prev_prev_locking_modify( bank );
    1308           0 :   fd_vote_states_t const * vote_states_prev      = fd_bank_vote_states_prev_locking_query( bank );
    1309           0 :   fd_memcpy( vote_states_prev_prev, vote_states_prev, fd_bank_vote_states_footprint );
    1310           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( bank );
    1311           0 :   fd_bank_vote_states_prev_end_locking_query( bank );
    1312           0 : }
    1313             : 
    1314             : /* Replace the vote states for T-1 (vote_states_prev) with the vote
    1315             :    states for T-1 (vote_states) */
    1316             : 
    1317             : static void
    1318           0 : fd_update_vote_states_prev( fd_bank_t * bank ) {
    1319           0 :   fd_vote_states_t *       vote_states_prev = fd_bank_vote_states_prev_locking_modify( bank );
    1320           0 :   fd_vote_states_t const * vote_states      = fd_bank_vote_states_locking_query( bank );
    1321           0 :   fd_memcpy( vote_states_prev, vote_states, fd_bank_vote_states_footprint );
    1322           0 :   fd_bank_vote_states_prev_end_locking_modify( bank );
    1323           0 :   fd_bank_vote_states_end_locking_query( bank );
    1324           0 : }
    1325             : 
    1326             : void
    1327             : fd_migrate_builtin_to_core_bpf( fd_bank_t *                            bank,
    1328             :                                 fd_funk_t *                            funk,
    1329             :                                 fd_funk_txn_xid_t const *              xid,
    1330             :                                 fd_core_bpf_migration_config_t const * config,
    1331             :                                 fd_spad_t *                            runtime_spad );
    1332             : 
    1333             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
    1334             : static void
    1335             : fd_apply_builtin_program_feature_transitions( fd_bank_t *               bank,
    1336             :                                               fd_funk_t *               funk,
    1337             :                                               fd_funk_txn_xid_t const * xid,
    1338             :                                               fd_capture_ctx_t *        capture_ctx,
    1339           0 :                                               fd_spad_t *               runtime_spad ) {
    1340             :   /* TODO: Set the upgrade authority properly from the core bpf migration config. Right now it's set to None.
    1341             : 
    1342             :      Migrate any necessary stateless builtins to core BPF. So far,
    1343             :      the only "stateless" builtin is the Feature program. Beginning
    1344             :      checks in the migrate_builtin_to_core_bpf function will fail if the
    1345             :      program has already been migrated to BPF. */
    1346             : 
    1347           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
    1348             : 
    1349           0 :   fd_builtin_program_t const * builtins = fd_builtins();
    1350           0 :   for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
    1351             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6732-L6751 */
    1352           0 :     if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_get( bank ), builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
    1353           0 :       FD_LOG_DEBUG(( "Migrating builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
    1354           0 :       fd_migrate_builtin_to_core_bpf( bank, funk, xid, builtins[i].core_bpf_migration_config, runtime_spad );
    1355           0 :     }
    1356             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6753-L6774 */
    1357           0 :     if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, builtins[i].enable_feature_offset ) ) {
    1358           0 :       FD_LOG_DEBUG(( "Enabling builtin program %s", FD_BASE58_ENC_32_ALLOCA( builtins[i].pubkey->key ) ));
    1359           0 :       fd_write_builtin_account( bank, funk, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
    1360           0 :     }
    1361           0 :   }
    1362             : 
    1363             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
    1364           0 :   fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
    1365           0 :   for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
    1366           0 :     if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_get( bank ), stateless_builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
    1367           0 :       FD_LOG_DEBUG(( "Migrating stateless builtin program %s to core BPF", FD_BASE58_ENC_32_ALLOCA( stateless_builtins[i].pubkey->key ) ));
    1368           0 :       fd_migrate_builtin_to_core_bpf( bank, funk, xid, stateless_builtins[i].core_bpf_migration_config, runtime_spad );
    1369           0 :     }
    1370           0 :   }
    1371             : 
    1372             :   /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
    1373           0 :   fd_precompile_program_t const * precompiles = fd_precompiles();
    1374           0 :   for( ulong i=0UL; i<fd_num_precompiles(); i++ ) {
    1375           0 :     if( precompiles[i].feature_offset != NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles[i].feature_offset ) ) {
    1376           0 :       fd_write_builtin_account( bank, funk, xid, capture_ctx, *precompiles[i].pubkey, "", 0 );
    1377           0 :     }
    1378           0 :   }
    1379             : 
    1380           0 :   } FD_SPAD_FRAME_END;
    1381           0 : }
    1382             : 
    1383             : static void
    1384             : fd_feature_activate( fd_bank_t *               bank,
    1385             :                      fd_funk_t *               funk,
    1386             :                      fd_funk_txn_xid_t const * xid,
    1387             :                      fd_capture_ctx_t *        capture_ctx,
    1388             :                      fd_feature_id_t const *   id,
    1389           0 :                      fd_pubkey_t const *       addr ) {
    1390           0 :   fd_features_t * features = fd_bank_features_modify( bank );
    1391             : 
    1392           0 :   if( id->reverted==1 ) return;
    1393             : 
    1394           0 :   FD_TXN_ACCOUNT_DECL( acct_rec );
    1395           0 :   int err = fd_txn_account_init_from_funk_readonly( acct_rec, addr, funk, xid );
    1396           0 :   if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
    1397           0 :     return;
    1398           0 :   }
    1399             : 
    1400           0 :   FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
    1401           0 :   fd_feature_t feature[1];
    1402           0 :   int decode_err = 0;
    1403           0 :   if( FD_UNLIKELY( !fd_bincode_decode_static(
    1404           0 :       feature, feature,
    1405           0 :       fd_txn_account_get_data( acct_rec ),
    1406           0 :       fd_txn_account_get_data_len( acct_rec ),
    1407           0 :       &decode_err ) ) ) {
    1408           0 :     FD_LOG_WARNING(( "Failed to decode feature account %s (%d)", addr_b58, decode_err ));
    1409           0 :     return;
    1410           0 :   }
    1411             : 
    1412           0 :   if( feature->has_activated_at ) {
    1413           0 :     FD_LOG_DEBUG(( "feature already activated - acc: %s, slot: %lu", addr_b58, feature->activated_at ));
    1414           0 :     fd_features_set( features, id, feature->activated_at);
    1415           0 :   } else {
    1416           0 :     FD_LOG_DEBUG(( "Feature %s not activated at %lu, activating", addr_b58, feature->activated_at ));
    1417             : 
    1418           0 :     FD_TXN_ACCOUNT_DECL( modify_acct_rec );
    1419           0 :     fd_funk_rec_prepare_t modify_acct_prepare = {0};
    1420           0 :     err = fd_txn_account_init_from_funk_mutable( modify_acct_rec, addr, funk, xid, 0, 0UL, &modify_acct_prepare );
    1421           0 :     if( FD_UNLIKELY( err != FD_ACC_MGR_SUCCESS ) ) {
    1422           0 :       return;
    1423           0 :     }
    1424             : 
    1425           0 :     fd_lthash_value_t prev_hash[1];
    1426           0 :     fd_hashes_account_lthash(
    1427           0 :       addr,
    1428           0 :       fd_txn_account_get_meta( modify_acct_rec ),
    1429           0 :       fd_txn_account_get_data( modify_acct_rec ),
    1430           0 :       prev_hash );
    1431             : 
    1432           0 :     feature->has_activated_at = 1;
    1433           0 :     feature->activated_at     = fd_bank_slot_get( bank );
    1434           0 :     fd_bincode_encode_ctx_t encode_ctx = {
    1435           0 :       .data    = fd_txn_account_get_data_mut( modify_acct_rec ),
    1436           0 :       .dataend = fd_txn_account_get_data_mut( modify_acct_rec ) + fd_txn_account_get_data_len( modify_acct_rec ),
    1437           0 :     };
    1438           0 :     int encode_err = fd_feature_encode( feature, &encode_ctx );
    1439           0 :     if( FD_UNLIKELY( encode_err != FD_BINCODE_SUCCESS ) ) {
    1440           0 :       FD_LOG_ERR(( "Failed to encode feature account %s (%d)", addr_b58, decode_err ));
    1441           0 :     }
    1442             : 
    1443           0 :     fd_hashes_update_lthash( modify_acct_rec, prev_hash, bank, capture_ctx );
    1444           0 :     fd_txn_account_mutable_fini( modify_acct_rec, funk, &modify_acct_prepare );
    1445           0 :   }
    1446           0 : }
    1447             : 
    1448             : static void
    1449             : fd_features_activate( fd_bank_t *               bank,
    1450             :                       fd_funk_t *               funk,
    1451             :                       fd_funk_txn_xid_t const * xid,
    1452           0 :                       fd_capture_ctx_t *        capture_ctx ) {
    1453           0 :   for( fd_feature_id_t const * id = fd_feature_iter_init();
    1454           0 :                                    !fd_feature_iter_done( id );
    1455           0 :                                id = fd_feature_iter_next( id ) ) {
    1456           0 :     fd_feature_activate( bank, funk, xid, capture_ctx, id, &id->id );
    1457           0 :   }
    1458           0 : }
    1459             : 
    1460             : /* Starting a new epoch.
    1461             :   New epoch:        T
    1462             :   Just ended epoch: T-1
    1463             :   Epoch before:     T-2
    1464             : 
    1465             :   In this function:
    1466             :   - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
    1467             :   - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
    1468             :   - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
    1469             : 
    1470             :   Invariant during an epoch T:
    1471             :   vote_states_prev holds the stakes at T-1
    1472             :   vote_states_prev_prev holds the stakes at T-2
    1473             :  */
    1474             : /* process for the start of a new epoch */
    1475             : static void
    1476             : fd_runtime_process_new_epoch( fd_banks_t *              banks,
    1477             :                               fd_bank_t *               bank,
    1478             :                               fd_funk_t *               funk,
    1479             :                               fd_funk_txn_xid_t const * xid,
    1480             :                               fd_capture_ctx_t *        capture_ctx,
    1481             :                               ulong                     parent_epoch,
    1482           0 :                               fd_spad_t *               runtime_spad ) {
    1483           0 :   FD_LOG_NOTICE(( "fd_process_new_epoch start, epoch: %lu, slot: %lu", fd_bank_epoch_get( bank ), fd_bank_slot_get( bank ) ));
    1484             : 
    1485           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
    1486             : 
    1487           0 :   fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
    1488           0 :   if( FD_UNLIKELY( !stake_delegations ) ) {
    1489           0 :     FD_LOG_CRIT(( "stake_delegations is NULL" ));
    1490           0 :   }
    1491             : 
    1492           0 :   long start = fd_log_wallclock();
    1493             : 
    1494           0 :   ulong const slot = fd_bank_slot_get( bank );
    1495             : 
    1496             :   /* Activate new features
    1497             :      https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6587-L6598 */
    1498           0 :   fd_features_activate( bank, funk, xid, capture_ctx );
    1499           0 :   fd_features_restore( bank, funk, xid );
    1500             : 
    1501             :   /* Apply builtin program feature transitions
    1502             :      https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
    1503           0 :   fd_apply_builtin_program_feature_transitions( bank, funk, xid, capture_ctx, runtime_spad );
    1504             : 
    1505             :   /* Get the new rate activation epoch */
    1506           0 :   int _err[1];
    1507           0 :   ulong   new_rate_activation_epoch_val = 0UL;
    1508           0 :   ulong * new_rate_activation_epoch     = &new_rate_activation_epoch_val;
    1509           0 :   int is_some = fd_new_warmup_cooldown_rate_epoch(
    1510           0 :       fd_bank_epoch_schedule_query( bank ),
    1511           0 :       fd_bank_features_query( bank ),
    1512           0 :       slot,
    1513           0 :       new_rate_activation_epoch,
    1514           0 :       _err );
    1515           0 :   if( FD_UNLIKELY( !is_some ) ) {
    1516           0 :     new_rate_activation_epoch = NULL;
    1517           0 :   }
    1518             : 
    1519             :   /* Updates stake history sysvar accumulated values. */
    1520           0 :   fd_stakes_activate_epoch( bank, funk, xid, capture_ctx, stake_delegations, new_rate_activation_epoch, runtime_spad );
    1521             : 
    1522             :   /* Refresh vote accounts in stakes cache using updated stake weights, and merges slot bank vote accounts with the epoch bank vote accounts.
    1523             :     https://github.com/anza-xyz/agave/blob/v2.1.6/runtime/src/stakes.rs#L363-L370 */
    1524           0 :   fd_stake_history_t const * history = fd_sysvar_stake_history_read( funk, xid, runtime_spad );
    1525           0 :   if( FD_UNLIKELY( !history ) ) {
    1526           0 :     FD_LOG_ERR(( "StakeHistory sysvar could not be read and decoded" ));
    1527           0 :   }
    1528             : 
    1529             :   /* Now increment the epoch */
    1530             : 
    1531           0 :   fd_bank_epoch_set( bank, fd_bank_epoch_get( bank ) + 1UL );
    1532             : 
    1533           0 :   fd_refresh_vote_accounts( bank,
    1534           0 :                             stake_delegations,
    1535           0 :                             history,
    1536           0 :                             new_rate_activation_epoch );
    1537             : 
    1538             :   /* Distribute rewards */
    1539             : 
    1540           0 :   fd_hash_t parent_blockhash = {0};
    1541           0 :   {
    1542           0 :     fd_blockhashes_t const * bhq = fd_bank_block_hash_queue_query( bank );
    1543           0 :     fd_hash_t const * bhq_last = fd_blockhashes_peek_last( bhq );
    1544           0 :     FD_TEST( bhq_last );
    1545           0 :     parent_blockhash = *bhq_last;
    1546           0 :   }
    1547             : 
    1548           0 :   fd_begin_partitioned_rewards( bank,
    1549           0 :                                 funk,
    1550           0 :                                 xid,
    1551           0 :                                 capture_ctx,
    1552           0 :                                 stake_delegations,
    1553           0 :                                 &parent_blockhash,
    1554           0 :                                 parent_epoch,
    1555           0 :                                 runtime_spad );
    1556             : 
    1557             : 
    1558             :   /* Update vote_states_prev_prev with vote_states_prev */
    1559             : 
    1560           0 :   fd_update_vote_states_prev_prev( bank );
    1561             : 
    1562             :   /* Update vote_states_prev with vote_states */
    1563             : 
    1564           0 :   fd_update_vote_states_prev( bank );
    1565             : 
    1566             :   /* Update current leaders using epoch_stakes (new T-2 stakes) */
    1567             : 
    1568           0 :   fd_runtime_update_leaders( bank, runtime_spad );
    1569             : 
    1570           0 :   FD_LOG_NOTICE(( "fd_process_new_epoch end" ));
    1571             : 
    1572           0 :   long end = fd_log_wallclock();
    1573           0 :   FD_LOG_NOTICE(("fd_process_new_epoch took %ld ns", end - start));
    1574             : 
    1575           0 :   } FD_SPAD_FRAME_END;
    1576           0 : }
    1577             : 
    1578             : /******************************************************************************/
    1579             : /* Block Parsing                                                              */
    1580             : /******************************************************************************/
    1581             : 
    1582             : /* Block iteration and parsing */
    1583             : 
    1584             : /* As a note, all of the logic in this section is used by the full firedancer
    1585             :    client. The store tile uses these APIs to help parse raw (micro)blocks
    1586             :    received from the network. */
    1587             : 
    1588             : /* Helpers */
    1589             : 
    1590             : void
    1591             : fd_runtime_update_program_cache( fd_bank_t *               bank,
    1592             :                                  fd_funk_t *               funk,
    1593             :                                  fd_funk_txn_xid_t const * xid,
    1594             :                                  fd_txn_p_t const *        txn_p,
    1595           0 :                                  fd_spad_t *               runtime_spad ) {
    1596           0 :   fd_txn_t const * txn_descriptor = TXN( txn_p );
    1597             : 
    1598           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
    1599             : 
    1600             :   /* Iterate over account keys referenced directly in the transaction first */
    1601           0 :   fd_acct_addr_t const * acc_addrs = fd_txn_get_acct_addrs( txn_descriptor, txn_p );
    1602           0 :   for( ushort acc_idx=0; acc_idx<txn_descriptor->acct_addr_cnt; acc_idx++ ) {
    1603           0 :     fd_pubkey_t const * account = fd_type_pun_const( &acc_addrs[acc_idx] );
    1604           0 :     fd_program_cache_update_program( bank, funk, xid, account, runtime_spad );
    1605           0 :   }
    1606             : 
    1607           0 :   if( txn_descriptor->transaction_version==FD_TXN_V0 ) {
    1608             : 
    1609             :     /* Iterate over account keys referenced in ALUTs */
    1610           0 :     fd_acct_addr_t alut_accounts[256];
    1611           0 :     fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_slot_hashes_read( funk, xid, runtime_spad );
    1612           0 :     if( FD_UNLIKELY( !slot_hashes_global ) ) {
    1613           0 :       return;
    1614           0 :     }
    1615             : 
    1616           0 :     fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar *)slot_hashes_global + slot_hashes_global->hashes_offset );
    1617             : 
    1618             :     /* TODO: This is done twice, once in the replay tile and once in the
    1619             :        exec tile. We should consolidate the account resolution into a
    1620             :        single place, but also keep in mind from a conformance
    1621             :        perspective that these ALUT resolution checks happen after some
    1622             :        things like compute budget instruction parsing */
    1623           0 :     if( FD_UNLIKELY( fd_runtime_load_txn_address_lookup_tables(
    1624           0 :           txn_descriptor,
    1625           0 :           txn_p->payload,
    1626           0 :           funk,
    1627           0 :           xid,
    1628           0 :           fd_bank_slot_get( bank ),
    1629           0 :           slot_hash,
    1630           0 :           alut_accounts ) ) ) {
    1631           0 :       return;
    1632           0 :     }
    1633             : 
    1634           0 :     for( ushort alut_idx=0; alut_idx<txn_descriptor->addr_table_adtl_cnt; alut_idx++ ) {
    1635           0 :       fd_pubkey_t const * account = fd_type_pun_const( &alut_accounts[alut_idx] );
    1636           0 :       fd_program_cache_update_program( bank, funk, xid, account, runtime_spad );
    1637           0 :     }
    1638           0 :   }
    1639             : 
    1640           0 :   } FD_SPAD_FRAME_END;
    1641           0 : }
    1642             : 
    1643             : /******************************************************************************/
    1644             : /* Genesis                                                                    */
    1645             : /*******************************************************************************/
    1646             : 
    1647             : static void
    1648             : fd_runtime_genesis_init_program( fd_bank_t *               bank,
    1649             :                                  fd_funk_t *               funk,
    1650             :                                  fd_funk_txn_xid_t const * xid,
    1651             :                                  fd_capture_ctx_t *        capture_ctx,
    1652           0 :                                  fd_spad_t *               runtime_spad ) {
    1653             : 
    1654           0 :   fd_sysvar_clock_init( bank, funk, xid, capture_ctx );
    1655           0 :   fd_sysvar_rent_init( bank, funk, xid, capture_ctx );
    1656             : 
    1657           0 :   fd_sysvar_slot_history_init( bank, funk, xid, capture_ctx, runtime_spad );
    1658           0 :   fd_sysvar_epoch_schedule_init( bank, funk, xid, capture_ctx );
    1659           0 :   fd_sysvar_recent_hashes_init( bank, funk, xid, capture_ctx );
    1660           0 :   fd_sysvar_stake_history_init( bank, funk, xid, capture_ctx );
    1661           0 :   fd_sysvar_last_restart_slot_init( bank, funk, xid, capture_ctx );
    1662             : 
    1663           0 :   fd_builtin_programs_init( bank, funk, xid, capture_ctx );
    1664           0 :   fd_stake_program_config_init( funk, xid );
    1665           0 : }
    1666             : 
    1667             : static void
    1668             : fd_runtime_init_bank_from_genesis( fd_banks_t *                       banks,
    1669             :                                    fd_bank_t *                        bank,
    1670             :                                    fd_funk_t *                        funk,
    1671             :                                    fd_funk_txn_xid_t const *          xid,
    1672             :                                    fd_genesis_solana_global_t const * genesis_block,
    1673             :                                    fd_hash_t const *                  genesis_hash,
    1674           0 :                                    fd_spad_t *                        runtime_spad ) {
    1675             : 
    1676           0 :   fd_bank_poh_set( bank, *genesis_hash );
    1677             : 
    1678           0 :   fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
    1679           0 :   memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
    1680             : 
    1681           0 :   fd_poh_config_global_t const * poh = &genesis_block->poh_config;
    1682           0 :   uint128 target_tick_duration = ((uint128)poh->target_tick_duration.seconds * 1000000000UL + (uint128)poh->target_tick_duration.nanoseconds);
    1683             : 
    1684           0 :   fd_bank_epoch_schedule_set( bank, genesis_block->epoch_schedule );
    1685             : 
    1686           0 :   fd_bank_rent_set( bank, genesis_block->rent );
    1687             : 
    1688           0 :   fd_bank_block_height_set( bank, 0UL );
    1689             : 
    1690           0 :   fd_bank_inflation_set( bank, genesis_block->inflation );
    1691             : 
    1692           0 :   {
    1693             :     /* FIXME Why is there a previous blockhash at genesis?  Why is the
    1694             :              last_hash field an option type in Agave, if even the first
    1695             :              real block has a previous blockhash? */
    1696             :     /* TODO: Use a real seed here. */
    1697           0 :     fd_blockhashes_t *    bhq  = fd_blockhashes_init( fd_bank_block_hash_queue_modify( bank ), 0UL );
    1698           0 :     fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
    1699           0 :     info->fee_calculator.lamports_per_signature = 0UL;
    1700           0 :   }
    1701             : 
    1702           0 :   fd_bank_fee_rate_governor_set( bank, genesis_block->fee_rate_governor );
    1703             : 
    1704           0 :   fd_bank_lamports_per_signature_set( bank, 0UL );
    1705             : 
    1706           0 :   fd_bank_prev_lamports_per_signature_set( bank, 0UL );
    1707             : 
    1708           0 :   fd_bank_max_tick_height_set( bank, genesis_block->ticks_per_slot * (fd_bank_slot_get( bank ) + 1) );
    1709             : 
    1710           0 :   fd_bank_hashes_per_tick_set( bank, !!poh->hashes_per_tick ? poh->hashes_per_tick : 0UL );
    1711             : 
    1712           0 :   fd_bank_ns_per_slot_set( bank, target_tick_duration * genesis_block->ticks_per_slot );
    1713             : 
    1714           0 :   fd_bank_ticks_per_slot_set( bank, genesis_block->ticks_per_slot );
    1715             : 
    1716           0 :   fd_bank_genesis_creation_time_set( bank, genesis_block->creation_time );
    1717             : 
    1718           0 :   fd_bank_slots_per_year_set( bank, SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis_block->ticks_per_slot );
    1719             : 
    1720           0 :   fd_bank_signature_count_set( bank, 0UL );
    1721             : 
    1722             :   /* Derive epoch stakes */
    1723             : 
    1724           0 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
    1725           0 :   if( FD_UNLIKELY( !stake_delegations ) ) {
    1726           0 :     FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
    1727           0 :   }
    1728             : 
    1729           0 :   fd_vote_states_t * vote_states = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_locking_modify( bank ), FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
    1730           0 :   if( FD_UNLIKELY( !vote_states ) ) {
    1731           0 :     FD_LOG_CRIT(( "Failed to join and new a vote states" ));
    1732           0 :   }
    1733             : 
    1734           0 :   ulong capitalization = 0UL;
    1735             : 
    1736           0 :   fd_pubkey_account_pair_global_t const * accounts = fd_genesis_solana_accounts_join( genesis_block );
    1737             : 
    1738           0 :   for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
    1739           0 :     fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
    1740           0 :     capitalization = fd_ulong_sat_add( capitalization, acc->account.lamports );
    1741             : 
    1742           0 :     uchar const * acc_data = fd_solana_account_data_join( &acc->account );
    1743             : 
    1744           0 :     if( !memcmp(acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t)) ) {
    1745             :       /* This means that there is a vote account which should be
    1746             :          inserted into the vote states. Even after the vote account is
    1747             :          inserted, we still don't know the total amount of stake that is
    1748             :          delegated to the vote account. This must be calculated later. */
    1749           0 :       fd_vote_states_update_from_account( vote_states, &acc->key, acc_data, acc->account.data_len );
    1750           0 :     } else if( !memcmp( acc->account.owner.key, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
    1751             :       /* If an account is a stake account, then it must be added to the
    1752             :          stake delegations cache. We should only add stake accounts that
    1753             :          have a valid non-zero stake. */
    1754           0 :       fd_stake_state_v2_t stake_state = {0};
    1755           0 :       if( FD_UNLIKELY( !fd_bincode_decode_static(
    1756           0 :           stake_state_v2, &stake_state,
    1757           0 :           acc_data, acc->account.data_len,
    1758           0 :           NULL ) ) ) {
    1759           0 :         FD_BASE58_ENCODE_32_BYTES( acc->key.key, stake_b58 );
    1760           0 :         FD_LOG_ERR(( "Failed to deserialize genesis stake account %s", stake_b58 ));
    1761           0 :       }
    1762           0 :       if( !fd_stake_state_v2_is_stake( &stake_state )     ) continue;
    1763           0 :       if( !stake_state.inner.stake.stake.delegation.stake ) continue;
    1764             : 
    1765           0 :       fd_stake_delegations_update(
    1766           0 :           stake_delegations,
    1767           0 :           (fd_pubkey_t *)acc->key.key,
    1768           0 :           &stake_state.inner.stake.stake.delegation.voter_pubkey,
    1769           0 :           stake_state.inner.stake.stake.delegation.stake,
    1770           0 :           stake_state.inner.stake.stake.delegation.activation_epoch,
    1771           0 :           stake_state.inner.stake.stake.delegation.deactivation_epoch,
    1772           0 :           stake_state.inner.stake.stake.credits_observed,
    1773           0 :           stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
    1774             : 
    1775           0 :     } else if( !memcmp(acc->account.owner.key, fd_solana_feature_program_id.key, sizeof(fd_pubkey_t)) ) {
    1776             :       /* Feature Account */
    1777             : 
    1778             :       /* Scan list of feature IDs to resolve address=>feature offset */
    1779           0 :       fd_feature_id_t const *found = NULL;
    1780           0 :       for( fd_feature_id_t const * id = fd_feature_iter_init();
    1781           0 :            !fd_feature_iter_done( id );
    1782           0 :            id = fd_feature_iter_next( id ) ) {
    1783           0 :         if( !memcmp( acc->key.key, id->id.key, sizeof(fd_pubkey_t) ) ) {
    1784           0 :           found = id;
    1785           0 :           break;
    1786           0 :         }
    1787           0 :       }
    1788             : 
    1789           0 :       if( found ) {
    1790             :         /* Load feature activation */
    1791           0 :         FD_SPAD_FRAME_BEGIN( runtime_spad ) {
    1792           0 :           int err;
    1793           0 :           fd_feature_t * feature = fd_bincode_decode_spad(
    1794           0 :               feature, runtime_spad,
    1795           0 :               acc_data,
    1796           0 :               acc->account.data_len,
    1797           0 :               &err );
    1798           0 :           FD_TEST( err==FD_BINCODE_SUCCESS );
    1799             : 
    1800           0 :           fd_features_t * features = fd_bank_features_modify( bank );
    1801           0 :           if( feature->has_activated_at ) {
    1802           0 :             FD_LOG_DEBUG(( "Feature %s activated at %lu (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ), feature->activated_at ));
    1803           0 :             fd_features_set( features, found, feature->activated_at );
    1804           0 :           } else {
    1805           0 :             FD_LOG_DEBUG(( "Feature %s not activated (genesis)", FD_BASE58_ENC_32_ALLOCA( acc->key.key ) ));
    1806           0 :             fd_features_set( features, found, ULONG_MAX );
    1807           0 :           }
    1808           0 :         } FD_SPAD_FRAME_END;
    1809           0 :       }
    1810           0 :     }
    1811           0 :   }
    1812           0 :   fd_bank_vote_states_end_locking_modify( bank );
    1813             : 
    1814             :   /* fd_refresh_vote_accounts is responsible for updating the vote
    1815             :      states with the total amount of active delegated stake. It does
    1816             :      this by iterating over all active stake delegations and summing up
    1817             :      the amount of stake that is delegated to each vote account. */
    1818             : 
    1819           0 :   ulong new_rate_activation_epoch = 0UL;
    1820           0 :   fd_stake_history_t * stake_history = fd_sysvar_stake_history_read( funk, xid, runtime_spad );
    1821           0 :   fd_refresh_vote_accounts(
    1822           0 :       bank,
    1823           0 :       stake_delegations,
    1824           0 :       stake_history,
    1825           0 :       &new_rate_activation_epoch );
    1826             : 
    1827             :   /* Now that the stake and vote delegations are updated correctly, we
    1828             :      will propagate the vote states to the vote states for the previous
    1829             :      epoch and the epoch before that.
    1830             : 
    1831             :      This is despite the fact we are booting off of genesis which means
    1832             :      that there is no previous or previous-previous epoch. This is done
    1833             :      to simplify edge cases around leader schedule and rewards
    1834             :      calculation.
    1835             : 
    1836             :      TODO: Each of the edge cases around this needs to be documented
    1837             :      much better where each case is clearly enumerated and explained. */
    1838             : 
    1839           0 :   fd_vote_states_t const * vote_states_curr = fd_bank_vote_states_locking_query( bank );
    1840             : 
    1841           0 :   fd_vote_states_t * vote_states_prev_prev = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_prev_prev_locking_modify( bank ), FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
    1842           0 :   fd_vote_states_t * vote_states_prev = fd_vote_states_join( fd_vote_states_new( fd_bank_vote_states_prev_locking_modify( bank ), FD_RUNTIME_MAX_VOTE_ACCOUNTS, 999UL ) );
    1843             : 
    1844           0 :   for( ulong i=0UL; i<genesis_block->accounts_len; i++ ) {
    1845           0 :     fd_pubkey_account_pair_global_t const * acc = &accounts[ i ];
    1846             : 
    1847           0 :     uchar const * acc_data = fd_solana_account_data_join( &acc->account );
    1848             : 
    1849           0 :     if( !memcmp( acc->account.owner.key, fd_solana_vote_program_id.key, sizeof(fd_pubkey_t) ) ) {
    1850           0 :       fd_vote_state_ele_t * vote_state = fd_vote_states_query( vote_states_curr, &acc->key );
    1851           0 :       fd_vote_states_update_from_account( vote_states_prev_prev, &acc->key, acc_data, acc->account.data_len );
    1852           0 :       fd_vote_states_update_from_account( vote_states_prev, &acc->key, acc_data, acc->account.data_len );
    1853           0 :       fd_vote_states_update_stake( vote_states_prev, &acc->key, vote_state->stake );
    1854           0 :       fd_vote_states_update_stake( vote_states_prev_prev, &acc->key, vote_state->stake );
    1855           0 :     }
    1856           0 :   }
    1857             : 
    1858           0 :   fd_bank_vote_states_prev_prev_end_locking_modify( bank );
    1859           0 :   fd_bank_vote_states_prev_end_locking_modify( bank );
    1860           0 :   fd_bank_vote_states_end_locking_query( bank );
    1861             : 
    1862           0 :   fd_bank_epoch_set( bank, 0UL );
    1863             : 
    1864           0 :   fd_bank_capitalization_set( bank, capitalization );
    1865           0 : }
    1866             : 
    1867             : static int
    1868             : fd_runtime_process_genesis_block( fd_bank_t *               bank,
    1869             :                                   fd_funk_t *               funk,
    1870             :                                   fd_funk_txn_xid_t const * xid,
    1871             :                                   fd_capture_ctx_t *        capture_ctx,
    1872           0 :                                   fd_spad_t *               runtime_spad ) {
    1873             : 
    1874           0 :   fd_hash_t * poh = fd_bank_poh_modify( bank );
    1875           0 :   ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
    1876           0 :   while( hashcnt_per_slot-- ) {
    1877           0 :     fd_sha256_hash( poh->hash, sizeof(fd_hash_t), poh->hash );
    1878           0 :   }
    1879             : 
    1880           0 :   fd_bank_execution_fees_set( bank, 0UL );
    1881             : 
    1882           0 :   fd_bank_priority_fees_set( bank, 0UL );
    1883             : 
    1884           0 :   fd_bank_signature_count_set( bank, 0UL );
    1885             : 
    1886           0 :   fd_bank_txn_count_set( bank, 0UL );
    1887             : 
    1888           0 :   fd_bank_failed_txn_count_set( bank, 0UL );
    1889             : 
    1890           0 :   fd_bank_nonvote_failed_txn_count_set( bank, 0UL );
    1891             : 
    1892           0 :   fd_bank_total_compute_units_used_set( bank, 0UL );
    1893             : 
    1894           0 :   fd_runtime_genesis_init_program( bank, funk, xid, capture_ctx, runtime_spad );
    1895             : 
    1896           0 :   fd_sysvar_slot_history_update( bank, funk, xid, capture_ctx );
    1897             : 
    1898           0 :   fd_runtime_update_leaders( bank, runtime_spad );
    1899             : 
    1900           0 :   fd_runtime_freeze( bank, funk, xid, capture_ctx );
    1901             : 
    1902           0 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
    1903             : 
    1904           0 :   fd_hash_t const * prev_bank_hash = fd_bank_bank_hash_query( bank );
    1905             : 
    1906           0 :   fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
    1907           0 :   fd_hashes_hash_bank(
    1908           0 :     lthash,
    1909           0 :     prev_bank_hash,
    1910           0 :     (fd_hash_t *)fd_bank_poh_query( bank )->hash,
    1911           0 :     0UL,
    1912           0 :     bank_hash );
    1913             : 
    1914           0 :   fd_bank_lthash_end_locking_query( bank );
    1915             : 
    1916           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    1917           0 : }
    1918             : 
    1919             : void
    1920             : fd_runtime_read_genesis( fd_banks_t *                       banks,
    1921             :                          fd_bank_t *                        bank,
    1922             :                          fd_funk_t *                        funk,
    1923             :                          fd_funk_txn_xid_t const *          xid,
    1924             :                          fd_capture_ctx_t *                 capture_ctx,
    1925             :                          fd_hash_t const *                  genesis_hash,
    1926             :                          fd_lthash_value_t const *          genesis_lthash,
    1927             :                          fd_genesis_solana_global_t const * genesis_block,
    1928           0 :                          fd_spad_t *                        runtime_spad ) {
    1929           0 :   FD_SPAD_FRAME_BEGIN( runtime_spad ) {
    1930             : 
    1931           0 :   fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
    1932           0 :   *lthash = *genesis_lthash;
    1933           0 :   fd_bank_lthash_end_locking_modify( bank );
    1934             : 
    1935             :   /* Once the accounts have been loaded from the genesis config into
    1936             :      the accounts db, we can initialize the bank state. This involves
    1937             :      setting some fields, and notably setting up the vote and stake
    1938             :      caches which are used for leader scheduling/rewards. */
    1939             : 
    1940           0 :   fd_runtime_init_bank_from_genesis( banks, bank, funk, xid, genesis_block, genesis_hash, runtime_spad );
    1941             : 
    1942             :   /* Write the native programs to the accounts db. */
    1943             : 
    1944           0 :   fd_string_pubkey_pair_global_t * nips = fd_genesis_solana_native_instruction_processors_join( genesis_block );
    1945             : 
    1946           0 :   for( ulong i=0UL; i<genesis_block->native_instruction_processors_len; i++ ) {
    1947           0 :     fd_string_pubkey_pair_global_t const * a = &nips[ i ];
    1948             : 
    1949           0 :     uchar const * string = fd_string_pubkey_pair_string_join( a );
    1950           0 :     fd_write_builtin_account( bank, funk, xid, capture_ctx, a->pubkey, (const char *)string, a->string_len );
    1951           0 :   }
    1952             : 
    1953           0 :   fd_features_restore( bank, funk, xid );
    1954             : 
    1955             :   /* At this point, state related to the bank and the accounts db
    1956             :      have been initialized and we are free to finish executing the
    1957             :      block. In practice, this updates some bank fields (notably the
    1958             :      poh and bank hash). */
    1959             : 
    1960           0 :   int err = fd_runtime_process_genesis_block( bank, funk, xid, capture_ctx, runtime_spad );
    1961           0 :   if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
    1962             : 
    1963           0 :   } FD_SPAD_FRAME_END;
    1964           0 : }
    1965             : 
    1966             : /******************************************************************************/
    1967             : /* Offline Replay                                                             */
    1968             : /******************************************************************************/
    1969             : 
    1970             : /* As a note, currently offline and live replay of transactions has differences
    1971             :    with regards to how the execution environment is setup. These are helpers
    1972             :    used to emulate this behavior */
    1973             : 
    1974             : void
    1975             : fd_runtime_block_pre_execute_process_new_epoch( fd_banks_t *              banks,
    1976             :                                                 fd_bank_t *               bank,
    1977             :                                                 fd_funk_t *               funk,
    1978             :                                                 fd_funk_txn_xid_t const * xid,
    1979             :                                                 fd_capture_ctx_t *        capture_ctx,
    1980             :                                                 fd_spad_t *               runtime_spad,
    1981           0 :                                                 int *                     is_epoch_boundary ) {
    1982             : 
    1983           0 :   ulong const slot = fd_bank_slot_get( bank );
    1984           0 :   if( slot != 0UL ) {
    1985           0 :     fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
    1986             : 
    1987           0 :     ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
    1988           0 :     ulong slot_idx;
    1989           0 :     ulong new_epoch  = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
    1990           0 :     if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
    1991             :       /* The block after genesis has a height of 1. */
    1992           0 :       fd_bank_block_height_set( bank, 1UL );
    1993           0 :     }
    1994             : 
    1995           0 :     if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
    1996           0 :       FD_LOG_DEBUG(( "Epoch boundary starting" ));
    1997           0 :       fd_runtime_process_new_epoch( banks, bank, funk, xid, capture_ctx, prev_epoch, runtime_spad );
    1998           0 :       *is_epoch_boundary = 1;
    1999           0 :     }
    2000           0 :   } else {
    2001           0 :     *is_epoch_boundary = 0;
    2002           0 :   }
    2003             : 
    2004           0 :   if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
    2005           0 :     fd_distribute_partitioned_epoch_rewards( bank, funk, xid, capture_ctx );
    2006           0 :   }
    2007           0 : }
    2008             : 
    2009             : void
    2010             : fd_runtime_block_execute_finalize( fd_bank_t *               bank,
    2011             :                                    fd_funk_t *               funk,
    2012             :                                    fd_funk_txn_xid_t const * xid,
    2013             :                                    fd_capture_ctx_t *        capture_ctx,
    2014           0 :                                    int                       silent ) {
    2015             : 
    2016             :   /* This slot is now "frozen" and can't be changed anymore. */
    2017           0 :   fd_runtime_freeze( bank, funk, xid, capture_ctx );
    2018             : 
    2019           0 :   fd_runtime_update_bank_hash( bank, capture_ctx, silent );
    2020           0 : }

Generated by: LCOV version 1.14