LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 950 1272 74.7 %
Date: 2026-10-31 04:25:24 Functions: 49 56 87.5 %

          Line data    Source code
       1             : #include "fd_runtime.h"
       2             : #include "fd_bank.h"
       3             : #include "../events/fd_event_runtime.h"
       4             : 
       5             : #include "../types/fd_cast.h"
       6             : #include "fd_alut.h"
       7             : #include "fd_executor.h"
       8             : #include "fd_hashes.h"
       9             : #include "fd_runtime_stack.h"
      10             : #include "fd_accdb_svm.h"
      11             : #include "../genesis/fd_genesis_parse.h"
      12             : #include "fd_txncache.h"
      13             : #include "fd_compute_budget_details.h"
      14             : #include "tests/fd_dump_pb.h"
      15             : 
      16             : #include "fd_system_ids.h"
      17             : #include "sysvar/fd_sysvar.h"
      18             : #include "sysvar/fd_sysvar_clock.h"
      19             : #include "sysvar/fd_sysvar_epoch_schedule.h"
      20             : #include "sysvar/fd_sysvar_recent_hashes.h"
      21             : #include "sysvar/fd_sysvar_stake_history.h"
      22             : #include "sysvar/fd_sysvar_last_restart_slot.h"
      23             : #include "sysvar/fd_sysvar_slot_hashes.h"
      24             : #include "sysvar/fd_sysvar_slot_history.h"
      25             : 
      26             : #include "../stakes/fd_stakes.h"
      27             : #include "../rewards/fd_rewards.h"
      28             : 
      29             : #include "program/fd_precompiles.h"
      30             : #include "program/vote/fd_vote_state_versioned.h"
      31             : 
      32             : FD_STATIC_ASSERT( MAX_STAKE_WEIGHTS==FD_RUNTIME_MAX_VAT_VOTE_ACCOUNTS, vat_stake_weights );
      33             : 
      34             : /*
      35             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
      36             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
      37             :  */
      38             : int
      39             : fd_runtime_compute_max_tick_height( ulong   ticks_per_slot,
      40             :                                     ulong   slot,
      41           0 :                                     ulong * out_max_tick_height /* out */ ) {
      42           0 :   ulong max_tick_height = 0UL;
      43           0 :   if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
      44           0 :     ulong next_slot = fd_ulong_sat_add( slot, 1UL );
      45           0 :     if( FD_UNLIKELY( next_slot == slot ) ) {
      46           0 :       FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      47           0 :       return -1;
      48           0 :     }
      49           0 :     if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
      50           0 :       FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      51           0 :       return -1;
      52           0 :     }
      53           0 :     max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
      54           0 :   }
      55           0 :   *out_max_tick_height = max_tick_height;
      56           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
      57           0 : }
      58             : 
      59             : void
      60             : fd_runtime_update_next_leaders( fd_bank_t *          bank,
      61           0 :                                 fd_runtime_stack_t * runtime_stack ) {
      62             : 
      63           0 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
      64             : 
      65           0 :   ulong epoch    = fd_slot_to_epoch ( epoch_schedule, bank->f.slot, NULL ) + 1UL;
      66           0 :   ulong slot0    = fd_epoch_slot0   ( epoch_schedule, epoch );
      67           0 :   ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
      68           0 :   if( FD_UNLIKELY( slot_cnt>FD_RUNTIME_SLOTS_PER_EPOCH ) ) {
      69           0 :     FD_LOG_ERR(( "epoch %lu has %lu slots, but the maximum supported is %lu", epoch, slot_cnt, FD_RUNTIME_SLOTS_PER_EPOCH ));
      70           0 :   }
      71             : 
      72           0 :   fd_vote_stakes_t *       vote_stakes      = fd_bank_vote_stakes( bank );
      73           0 :   fd_vote_stake_weight_t * epoch_weights    = runtime_stack->stakes.stake_weights;
      74           0 :   ulong                    stake_weight_cnt = fd_stake_weights_by_node( vote_stakes, bank->vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_1, epoch_weights );
      75           0 :   FD_TEST( stake_weight_cnt<=MAX_STAKE_WEIGHTS );
      76             : 
      77           0 :   void * epoch_leaders_mem = fd_bank_epoch_leaders_modify( bank, epoch );
      78           0 :   fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
      79           0 :       epoch_leaders_mem,
      80           0 :       epoch,
      81           0 :       slot0,
      82           0 :       slot_cnt,
      83           0 :       stake_weight_cnt,
      84           0 :       epoch_weights,
      85           0 :       fd_epoch_leaders_use_geo_aware( &bank->f.features, &bank->f.epoch_schedule, epoch ) ) );
      86           0 :   if( FD_UNLIKELY( !leaders ) ) {
      87           0 :     FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
      88           0 :   }
      89             : 
      90           0 :   memcpy( runtime_stack->epoch_weights.next_stake_weights, epoch_weights, stake_weight_cnt*sizeof(fd_vote_stake_weight_t) );
      91           0 :   runtime_stack->epoch_weights.next_stake_weights_cnt = stake_weight_cnt;
      92             : 
      93           0 :   ulong staked_cnt = compute_id_weights_from_vote_weights( runtime_stack->stakes.id_weights, epoch_weights, stake_weight_cnt );
      94           0 :   FD_TEST( staked_cnt<=MAX_STAKE_WEIGHTS );
      95           0 :   memcpy( runtime_stack->epoch_weights.next_id_weights, runtime_stack->stakes.id_weights, staked_cnt * sizeof(fd_stake_weight_t) );
      96           0 :   runtime_stack->epoch_weights.next_id_weights_cnt = staked_cnt;
      97           0 : }
      98             : 
      99             : void
     100             : fd_runtime_update_leaders( fd_bank_t *          bank,
     101         318 :                            fd_runtime_stack_t * runtime_stack ) {
     102             : 
     103         318 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     104             : 
     105         318 :   ulong epoch    = fd_slot_to_epoch ( epoch_schedule, bank->f.slot, NULL );
     106         318 :   ulong slot0    = fd_epoch_slot0   ( epoch_schedule, epoch );
     107         318 :   ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
     108         318 :   if( FD_UNLIKELY( slot_cnt>FD_RUNTIME_SLOTS_PER_EPOCH ) ) {
     109           0 :     FD_LOG_ERR(( "epoch %lu has %lu slots, but the maximum supported is %lu", epoch, slot_cnt, FD_RUNTIME_SLOTS_PER_EPOCH ));
     110           0 :   }
     111             : 
     112         318 :   fd_vote_stakes_t *       vote_stakes      = fd_bank_vote_stakes( bank );
     113         318 :   fd_vote_stake_weight_t * epoch_weights    = runtime_stack->stakes.stake_weights;
     114         318 :   ulong                    stake_weight_cnt = fd_stake_weights_by_node( vote_stakes, bank->vote_stakes_fork_id, FD_VOTE_STAKES_ITER_T_2, epoch_weights );
     115         318 :   FD_TEST( stake_weight_cnt<=MAX_STAKE_WEIGHTS );
     116             : 
     117             :   /* TODO: Can optimize by avoiding recomputing if another fork has
     118             :      already computed them for this epoch. */
     119         318 :   void * epoch_leaders_mem = fd_bank_epoch_leaders_modify( bank, epoch );
     120         318 :   fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
     121         318 :       epoch_leaders_mem,
     122         318 :       epoch,
     123         318 :       slot0,
     124         318 :       slot_cnt,
     125         318 :       stake_weight_cnt,
     126         318 :       epoch_weights,
     127         318 :       fd_epoch_leaders_use_geo_aware( &bank->f.features, &bank->f.epoch_schedule, epoch ) ) );
     128         318 :   if( FD_UNLIKELY( !leaders ) ) {
     129           0 :     FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
     130           0 :   }
     131             : 
     132         318 :   memcpy( runtime_stack->epoch_weights.stake_weights, epoch_weights, stake_weight_cnt*sizeof(fd_vote_stake_weight_t) );
     133         318 :   runtime_stack->epoch_weights.stake_weights_cnt = stake_weight_cnt;
     134             : 
     135         318 :   ulong staked_cnt = compute_id_weights_from_vote_weights( runtime_stack->stakes.id_weights, epoch_weights, stake_weight_cnt );
     136         318 :   FD_TEST( staked_cnt<=MAX_STAKE_WEIGHTS );
     137         318 :   memcpy( runtime_stack->epoch_weights.id_weights, runtime_stack->stakes.id_weights, staked_cnt * sizeof(fd_stake_weight_t) );
     138         318 :   runtime_stack->epoch_weights.id_weights_cnt = staked_cnt;
     139         318 : }
     140             : 
     141             : /******************************************************************************/
     142             : /* Various Private Runtime Helpers                                            */
     143             : /******************************************************************************/
     144             : 
     145             : /* Validates the fee collector account before depositing fees.  Returns
     146             :    0 on success.  Returns 1 (fee is burned) if the fee collector is not
     147             :    owned by the system program, the deposit overflows the collector's
     148             :    balance, or the rent state transition is invalid.
     149             : 
     150             :    https://github.com/anza-xyz/agave/blob/v4.2.0-beta.2/runtime/src/bank/fee_distribution.rs#L205-L229 */
     151             : static int
     152             : fd_runtime_validate_fee_collector( fd_bank_t const * bank,
     153             :                                    fd_acc_t const *  collector,
     154          27 :                                    ulong             fee ) {
     155          27 :   FD_TEST( fee );
     156             : 
     157             :   /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.0/runtime/src/bank/fee_distribution.rs#L206-L208 */
     158          27 :   if( FD_UNLIKELY( memcmp( collector->owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) ) ) return 1;
     159             : 
     160             :   /* Lamport overflow burns the fee.
     161             :      https://github.com/anza-xyz/agave/blob/v4.2.0-beta.2/runtime/src/bank/fee_distribution.rs#L210-L214 */
     162          24 :   ulong pre_balance = collector->lamports;
     163          24 :   ulong post_balance;
     164          24 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( pre_balance, fee, &post_balance ) ) ) return 1;
     165             : 
     166          21 :   return !!fd_executor_check_static_account_rent_state_transition(
     167          21 :     pre_balance,
     168          21 :     post_balance,
     169          21 :     collector->data_len,
     170          21 :     &bank->f.rent,
     171          21 :     FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check )
     172          21 :   );
     173          24 : }
     174             : 
     175             : /* Validates an external SIMD-0232 block revenue collector after the
     176             :    fee reward has been added.  Returns 0 to deposit, 1 to burn.  The
     177             :    vote account itself is always valid and must not be passed here.
     178             :    https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank/fee_distribution.rs#L231-L270 */
     179             : 
     180             : static int
     181             : fd_runtime_validate_block_revenue_collector( fd_bank_t const *   bank,
     182             :                                              fd_pubkey_t const * collector_id,
     183             :                                              ulong               pre_lamports,
     184         153 :                                              fd_acc_t const *    collector ) {
     185             :   /* Must be a system program owned account. */
     186         153 :   if( FD_UNLIKELY( memcmp( collector->owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) ) ) return 1;
     187             : 
     188             :   /* Must not be a reserved account. */
     189          87 :   if( FD_UNLIKELY( fd_pubkey_is_active_reserved_key( collector_id ) ||
     190          87 :                    fd_pubkey_is_pending_reserved_key( collector_id ) ) ) return 1;
     191             : 
     192             :   /* The incinerator is exempt from the rent check so the deposit
     193             :      always works.  Incinerator funds are burned at the end of the
     194             :      block. */
     195          54 :   if( FD_UNLIKELY( fd_pubkey_eq( collector_id, &fd_sysvar_incinerator_id ) ) ) return 0;
     196             : 
     197             :   /* Must be rent-exempt after the deposit.  With
     198             :      relax_post_exec_min_balance_check (SIMD-0392) a pre-existing
     199             :      account may stay rent-paying. */
     200          51 :   int is_rent_exempt = collector->lamports>=fd_rent_exempt_minimum_balance( &bank->f.rent, collector->data_len );
     201          51 :   return !is_rent_exempt &&
     202          51 :          ( !FD_FEATURE_ACTIVE_BANK( bank, relax_post_exec_min_balance_check ) || !pre_lamports );
     203          54 : }
     204             : 
     205             : /* Deposits fees into the collector account.
     206             :    Returns 0 on deposit, 1 to burn.
     207             :    https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L248 */
     208             : 
     209             : static int
     210             : fd_runtime_deposit_fees( fd_bank_t *         bank,
     211             :                          fd_accdb_t *        accdb,
     212             :                          fd_capture_ctx_t *  capture_ctx,
     213             :                          fd_pubkey_t const * collector_id,
     214             :                          fd_pubkey_t const * leader_vote,
     215         192 :                          ulong               fees ) {
     216         192 :   uchar                      stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
     217         192 :   fd_stake_history_t         stake_history_[1];
     218         192 :   fd_stake_history_t const * stake_history = fd_sysvar_stake_history_read( accdb, bank->accdb_fork_id, stake_history_data, stake_history_ );
     219             : 
     220         192 :   fd_accdb_svm_update_t update[1];
     221         192 :   fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, collector_id, 1 );
     222         192 :   int burn;
     223         192 :   if( FD_FEATURE_ACTIVE_BANK( bank, custom_commission_collector ) ) {
     224             :     /* https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L254-L267 */
     225         165 :     ulong pre_lamports = acc.lamports;
     226         165 :     burn = __builtin_uaddl_overflow( pre_lamports, fees, &acc.lamports );
     227             :     /* The vote account itself is always a valid collector. */
     228         165 :     if( !burn && !fd_pubkey_eq( collector_id, leader_vote ) ) {
     229         153 :       burn = fd_runtime_validate_block_revenue_collector( bank, collector_id, pre_lamports, &acc );
     230         153 :     }
     231         165 :     if( FD_UNLIKELY( burn ) ) acc.lamports = pre_lamports;
     232         165 :   } else {
     233             :     /* https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L269-L291 */
     234          27 :     burn = fd_runtime_validate_fee_collector( bank, &acc, fees );
     235          27 :     if( FD_LIKELY( !burn ) ) {
     236           9 :       acc.lamports += fees; /* guaranteed to not overflow, checked above */
     237           9 :     }
     238          27 :   }
     239         192 :   if( FD_LIKELY( !burn ) ) fd_stakes_update_stake_delegation( collector_id, &acc, bank, stake_history, NULL );
     240         192 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
     241         192 :   return burn;
     242         192 : }
     243             : 
     244             : /* Deposits delegator fees into the leader's vote account and increments
     245             :    pending delegator rewards (SIMD-0123).
     246             :    Returns 0 on deposit, 1 to burn.
     247             :    https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L303 */
     248             : 
     249             : static int
     250             : fd_runtime_deposit_delegator_fees( fd_bank_t *         bank,
     251             :                                    fd_accdb_t *        accdb,
     252             :                                    fd_capture_ctx_t *  capture_ctx,
     253             :                                    fd_pubkey_t const * vote_address,
     254          27 :                                    ulong               fees ) {
     255          27 :   fd_accdb_svm_update_t update[1];
     256          27 :   fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, vote_address, 0 );
     257          27 :   if( FD_UNLIKELY( !acc.lamports ) ) return 1; /* open_rw released the handle */
     258             : 
     259          27 :   int burn = 0;
     260             : 
     261             :   /* Must be a vote account.
     262             :      https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L312-L314 */
     263          27 :   if( memcmp( acc.owner, fd_solana_vote_program_id.uc, sizeof(fd_pubkey_t) ) ) burn = 1;
     264             : 
     265             :   /* Lamports must not overflow.  Checked before the field update: the
     266             :      account is written back as is when the deposit burns.
     267             :      https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L324-L326 */
     268          27 :   ulong pre_lamports = acc.lamports;
     269          27 :   if( !burn && __builtin_uaddl_overflow( pre_lamports, fees, &acc.lamports ) ) burn = 1;
     270             : 
     271             :   /* Must be a v4 state and pending_delegator_rewards must not overflow.
     272             :      https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L316-L322 */
     273          27 :   if( !burn && fd_vote_account_add_pending_delegator_rewards( acc.data, acc.data_len, fees ) ) burn = 1;
     274             : 
     275          27 :   if( burn ) acc.lamports = pre_lamports;
     276          27 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
     277          27 :   return burn;
     278          27 : }
     279             : 
     280             : /* Deposits the fee reward with the leader's collector and, under
     281             :    SIMD-0123, the delegator share with the leader's vote account.
     282             :    Returns the lamports to burn.
     283             :    https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L146 */
     284             : 
     285             : static ulong
     286             : fd_runtime_deposit_or_burn_fee( fd_bank_t *        bank,
     287             :                                 fd_accdb_t *       accdb,
     288             :                                 fd_capture_ctx_t * capture_ctx,
     289         402 :                                 ulong              deposit ) {
     290         402 :   if( FD_UNLIKELY( !deposit ) ) return 0UL;
     291             : 
     292         195 :   fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_query( bank, bank->f.epoch );
     293         195 :   fd_pubkey_t const *        leader  = fd_epoch_leaders_get( leaders, bank->f.slot );
     294         195 :   if( FD_UNLIKELY( !leader ) ) FD_LOG_CRIT(( "fd_epoch_leaders_get(%lu) returned NULL", bank->f.slot ));
     295             : 
     296             :   /* Per SIMD-0232, the fee reward goes to the leader's block revenue
     297             :      collector from the vote account state the leader schedule was
     298             :      derived from (the t-2 vote stakes set); default is the leader
     299             :      identity.
     300             :      https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L151-L181 */
     301         195 :   int custom_commission_collector = FD_FEATURE_ACTIVE_BANK( bank, custom_commission_collector );
     302             : 
     303         195 :   fd_pubkey_t const *  collector_id   = leader;
     304         195 :   fd_pubkey_t const *  leader_vote    = NULL;
     305         195 :   ushort               commission_bps = FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS;
     306         195 :   fd_vote_stakes_ele_t leader_ele[1];
     307         195 :   if( custom_commission_collector ) {
     308         168 :     leader_vote = fd_epoch_leaders_get_vote( leaders, bank->f.slot );
     309         168 :     if( FD_UNLIKELY( !leader_vote ) ) FD_LOG_CRIT(( "fd_epoch_leaders_get_vote(%lu) returned NULL", bank->f.slot ));
     310         168 :     if( FD_UNLIKELY( !fd_vote_stakes_query_t_2( fd_bank_vote_stakes( bank ), bank->vote_stakes_fork_id, leader_vote, leader_ele ) ) ) {
     311           0 :       FD_BASE58_ENCODE_32_BYTES( leader_vote->uc, leader_vote_b58 );
     312             :       /* https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L167 */
     313           0 :       FD_LOG_CRIT(( "leader vote account %s is not in the epoch stakes at slot %lu", leader_vote_b58, bank->f.slot ));
     314           0 :     }
     315         168 :     commission_bps = leader_ele->block_revenue_commission_bps;
     316         168 :     collector_id   = &leader_ele->block_collector;
     317         168 :   }
     318             : 
     319             :   /* https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L183-L199 */
     320         195 :   ulong validator_fee = deposit;
     321         195 :   ulong delegator_fee = 0UL;
     322         195 :   if( FD_FEATURE_ACTIVE_BANK( bank, block_revenue_sharing ) ) {
     323          33 :     ulong clamped_commission_bps = fd_ulong_min( commission_bps, FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS );
     324          33 :     validator_fee = (ulong)( (uint128)deposit * (uint128)clamped_commission_bps / (uint128)FD_VOTE_DEFAULT_BLOCK_REVENUE_COMMISSION_BPS );
     325          33 :     delegator_fee = deposit - validator_fee;
     326          33 :   }
     327             : 
     328             :   /* https://github.com/anza-xyz/agave/blob/v4.4.0-alpha.5/runtime/src/bank/fee_distribution.rs#L201-L212 */
     329         195 :   ulong validator_fee_to_burn = 0UL;
     330         195 :   if( validator_fee && fd_runtime_deposit_fees( bank, accdb, capture_ctx, collector_id, leader_vote, validator_fee ) ) {
     331         129 :     FD_LOG_INFO(( "slot %lu has an invalid fee collector, burning fee reward (%lu lamports)", bank->f.slot, validator_fee ));
     332         129 :     validator_fee_to_burn = validator_fee;
     333         129 :   }
     334         195 :   ulong delegator_fee_to_burn = 0UL;
     335         195 :   if( delegator_fee && fd_runtime_deposit_delegator_fees( bank, accdb, capture_ctx, leader_vote, delegator_fee ) ) {
     336           9 :     FD_LOG_INFO(( "slot %lu has an invalid leader vote account, burning delegator fee reward (%lu lamports)", bank->f.slot, delegator_fee ));
     337           9 :     delegator_fee_to_burn = delegator_fee;
     338           9 :   }
     339         195 :   return validator_fee_to_burn + delegator_fee_to_burn;
     340         195 : }
     341             : 
     342             : /* fd_runtime_settle_fees settles transaction fees accumulated during a
     343             :    slot.  A portion is burnt, another portion is credited to the fee
     344             :    collector (typically leader). */
     345             : 
     346             : void
     347             : fd_runtime_fee_split( ulong   execution_fees,
     348             :                       ulong   priority_fees,
     349             :                       ulong * burn,
     350         402 :                       ulong * reward ) {
     351         402 :   *burn   = execution_fees/2UL;
     352         402 :   *reward = fd_ulong_sat_add( priority_fees, execution_fees-*burn );
     353         402 : }
     354             : 
     355             : static void
     356             : fd_runtime_settle_fees( fd_bank_t *        bank,
     357             :                         fd_accdb_t *       accdb,
     358         402 :                         fd_capture_ctx_t * capture_ctx ) {
     359         402 :   ulong slot           = bank->f.slot;
     360         402 :   ulong execution_fees = bank->f.execution_fees;
     361         402 :   ulong priority_fees  = bank->f.priority_fees;
     362         402 :   ulong total_fees;
     363         402 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( execution_fees, priority_fees, &total_fees ) ) ) {
     364           0 :     FD_LOG_EMERG(( "fee overflow detected (slot=%lu execution_fees=%lu priority_fees=%lu)",
     365           0 :                    slot, execution_fees, priority_fees ));
     366           0 :   }
     367             : 
     368         402 :   ulong fee_burn, fee_reward;
     369         402 :   fd_runtime_fee_split( execution_fees, priority_fees, &fee_burn, &fee_reward );
     370             : 
     371             :   /* Remove fee balance from bank (decreasing capitalization).
     372             :      Allow underflow (wrap) to match Agave's silent fetch_sub behavior. */
     373         402 :   bank->f.capitalization -= total_fees;
     374         402 :   bank->f.execution_fees  = 0;
     375         402 :   bank->f.priority_fees   = 0;
     376             : 
     377         402 :   ulong fee_reward_burn = fd_runtime_deposit_or_burn_fee( bank, accdb, capture_ctx, fee_reward );
     378             : 
     379         402 :   FD_LOG_INFO(( "slot=%lu priority_fees=%lu execution_fees=%lu fee_burn=%lu fee_rewards=%lu fee_reward_burn=%lu",
     380         402 :                 slot,
     381         402 :                 priority_fees, execution_fees, fee_burn, fee_reward, fee_reward_burn ));
     382         402 : }
     383             : 
     384             : static void
     385             : fd_runtime_freeze( fd_bank_t *        bank,
     386             :                    fd_accdb_t *       accdb,
     387         402 :                    fd_capture_ctx_t * capture_ctx ) {
     388         402 :   if( FD_LIKELY( bank->f.slot ) ) fd_sysvar_recent_hashes_update( bank, accdb, capture_ctx );
     389         402 :   fd_sysvar_slot_history_update( bank, accdb, capture_ctx );
     390         402 :   fd_runtime_settle_fees( bank, accdb, capture_ctx );
     391             : 
     392             :   /* jito collects a 3% fee at the end of the block + 3% fee at
     393             :      distribution time. */
     394         402 :   ulong tips_pre_comission = bank->f.tips;
     395         402 :   bank->f.tips = (tips_pre_comission - (tips_pre_comission * 6UL / 100UL));
     396             : 
     397         402 :   fd_accdb_svm_remove( bank, accdb, capture_ctx, &fd_sysvar_incinerator_id );
     398         402 : }
     399             : 
     400             : /******************************************************************************/
     401             : /* Block-Level Execution Preparation/Finalization                             */
     402             : /******************************************************************************/
     403             : void
     404             : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
     405        7476 :                                           ulong       latest_signatures_per_slot ) {
     406             : 
     407        7476 :   fd_fee_rate_governor_t const * base_fee_rate_governor = &bank->f.fee_rate_governor;
     408             : 
     409        7476 :   ulong old_lamports_per_signature = bank->f.rbh_lamports_per_sig;
     410             : 
     411        7476 :   fd_fee_rate_governor_t me = {
     412        7476 :     .target_signatures_per_slot    = base_fee_rate_governor->target_signatures_per_slot,
     413        7476 :     .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
     414        7476 :     .max_lamports_per_signature    = base_fee_rate_governor->max_lamports_per_signature,
     415        7476 :     .min_lamports_per_signature    = base_fee_rate_governor->min_lamports_per_signature,
     416        7476 :     .burn_percent                  = base_fee_rate_governor->burn_percent
     417        7476 :   };
     418             : 
     419        7476 :   ulong new_lamports_per_signature = 0;
     420        7476 :   if( me.target_signatures_per_slot > 0 ) {
     421           6 :     me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
     422           6 :     me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
     423           6 :     ulong desired_lamports_per_signature = fd_ulong_min(
     424           6 :       me.max_lamports_per_signature,
     425           6 :       fd_ulong_max(
     426           6 :         me.min_lamports_per_signature,
     427           6 :         me.target_lamports_per_signature
     428           6 :         * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
     429           6 :         / me.target_signatures_per_slot
     430           6 :       )
     431           6 :     );
     432           6 :     long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
     433           6 :     if ( gap == 0 ) {
     434           0 :       new_lamports_per_signature = desired_lamports_per_signature;
     435           6 :     } else {
     436           6 :       long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
     437           6 :         * (gap != 0)
     438           6 :         * (gap > 0 ? 1 : -1);
     439           6 :       new_lamports_per_signature = fd_ulong_min(
     440           6 :         me.max_lamports_per_signature,
     441           6 :         fd_ulong_max(
     442           6 :           me.min_lamports_per_signature,
     443           6 :           (ulong)((long)old_lamports_per_signature + gap_adjust)
     444           6 :         )
     445           6 :       );
     446           6 :     }
     447        7470 :   } else {
     448        7470 :     new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
     449        7470 :     me.min_lamports_per_signature = me.target_lamports_per_signature;
     450        7470 :     me.max_lamports_per_signature = me.target_lamports_per_signature;
     451        7470 :   }
     452        7476 :   bank->f.fee_rate_governor = me;
     453        7476 :   bank->f.rbh_lamports_per_sig = new_lamports_per_signature;
     454        7476 : }
     455             : 
     456             : /******************************************************************************/
     457             : /* Epoch Boundary                                                             */
     458             : /******************************************************************************/
     459             : 
     460             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
     461             : static void
     462             : fd_apply_builtin_program_feature_transitions( fd_bank_t *          bank,
     463             :                                               fd_accdb_t *         accdb,
     464             :                                               fd_runtime_stack_t * runtime_stack,
     465         318 :                                               fd_capture_ctx_t *   capture_ctx ) {
     466             :   /* TODO: Set the upgrade authority properly from the core bpf migration config. Right now it's set to None.
     467             : 
     468             :      Migrate any necessary stateless builtins to core BPF. So far,
     469             :      the only "stateless" builtin is the Feature program. Beginning
     470             :      checks in the migrate_builtin_to_core_bpf function will fail if the
     471             :      program has already been migrated to BPF. */
     472             : 
     473         318 :   fd_builtin_program_t const * builtins = fd_builtins();
     474        3180 :   for( ulong i=0UL; i<fd_num_builtins(); i++ ) {
     475             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6732-L6751 */
     476        2862 :     if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( bank->f.slot, &bank->f.features, builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
     477           0 :       FD_BASE58_ENCODE_32_BYTES( builtins[i].pubkey->key, pubkey_b58 );
     478           0 :       FD_LOG_DEBUG(( "Migrating builtin program %s to core BPF", pubkey_b58 ));
     479           0 :       fd_migrate_builtin_to_core_bpf( bank, accdb, runtime_stack, builtins[i].core_bpf_migration_config, capture_ctx );
     480           0 :     }
     481             :     /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6753-L6774 */
     482        2862 :     if( builtins[i].enable_feature_offset!=NO_ENABLE_FEATURE_ID && FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, builtins[i].enable_feature_offset ) ) {
     483           0 :       FD_BASE58_ENCODE_32_BYTES( builtins[i].pubkey->key, pubkey_b58 );
     484           0 :       FD_LOG_DEBUG(( "Enabling builtin program %s", pubkey_b58 ));
     485           0 :       fd_write_builtin_account( bank, accdb, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
     486           0 :     }
     487        2862 :   }
     488             : 
     489             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
     490         318 :   fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
     491         954 :   for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
     492         636 :     if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( bank->f.slot, &bank->f.features, stateless_builtins[i].core_bpf_migration_config->enable_feature_offset ) ) {
     493           0 :       FD_BASE58_ENCODE_32_BYTES( stateless_builtins[i].pubkey->key, pubkey_b58 );
     494           0 :       FD_LOG_DEBUG(( "Migrating stateless builtin program %s to core BPF", pubkey_b58 ));
     495           0 :       fd_migrate_builtin_to_core_bpf( bank, accdb, runtime_stack, stateless_builtins[i].core_bpf_migration_config, capture_ctx );
     496           0 :     }
     497         636 :   }
     498             : 
     499             :   /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
     500        1272 :   for( fd_precompile_program_t const * precompiles = fd_precompiles(); precompiles->verify_fn; precompiles++ ) {
     501         954 :     if( precompiles->feature_offset != NO_ENABLE_FEATURE_ID &&
     502         954 :         FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles->feature_offset ) ) {
     503           0 :       fd_write_builtin_account( bank, accdb, capture_ctx, *precompiles->pubkey, "", 0 );
     504           0 :     }
     505         954 :   }
     506         318 : }
     507             : 
     508             : static void
     509             : fd_feature_activate( fd_bank_t *             bank,
     510             :                      fd_accdb_t *            accdb,
     511             :                      fd_capture_ctx_t *      capture_ctx,
     512             :                      fd_feature_id_t const * id,
     513       96990 :                      fd_pubkey_t const *     addr ) {
     514       96990 :   fd_features_set( &bank->f.features, id, FD_FEATURE_DISABLED );
     515             : 
     516       96990 :   if( FD_UNLIKELY( id->reverted==1 ) ) return;
     517             : 
     518       91266 :   fd_acc_t acc = fd_accdb_read_one( accdb, bank->accdb_fork_id, addr->uc );
     519       91266 :   if( FD_UNLIKELY( !acc.lamports || memcmp( acc.owner, fd_solana_feature_program_id.uc, 32UL ) ) ) {
     520       90972 :     fd_accdb_unread_one( accdb, &acc ); /* Feature account not yet initialized */
     521       90972 :     return;
     522       90972 :   }
     523             : 
     524         294 :   fd_feature_t feature;
     525         294 :   if( FD_UNLIKELY( !fd_feature_decode( &feature, acc.data, acc.data_len ) ) ) {
     526           3 :     FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
     527           3 :     FD_LOG_WARNING(( "cannot activate feature %s, corrupt account data", addr_b58 ));
     528           3 :     FD_LOG_HEXDUMP_NOTICE(( "corrupt feature account", acc.data, acc.data_len ));
     529           3 :     fd_accdb_unread_one( accdb, &acc );
     530           3 :     return;
     531           3 :   }
     532         291 :   fd_accdb_unread_one( accdb, &acc );
     533             : 
     534         291 :   FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
     535         291 :   if( FD_UNLIKELY( feature.is_active ) ) {
     536         246 :     FD_LOG_DEBUG(( "feature %s already activated at slot %lu", addr_b58, feature.activation_slot ));
     537         246 :     fd_features_set( &bank->f.features, id, feature.activation_slot);
     538         246 :   } else {
     539          45 :     FD_LOG_DEBUG(( "feature %s not activated at slot %lu, activating", addr_b58, bank->f.slot ));
     540          45 :     fd_accdb_svm_update_t update[1];
     541          45 :     fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, addr, 0 );
     542          45 :     if( FD_UNLIKELY( !acc.lamports ) ) return;
     543          45 :     FD_TEST( acc.data_len>=sizeof(fd_feature_t) );
     544             : 
     545          45 :     feature.is_active       = 1;
     546          45 :     feature.activation_slot = bank->f.slot;
     547          45 :     FD_STORE( fd_feature_t, acc.data, feature );
     548          45 :     fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
     549          45 :     if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_feature( addr->uc );
     550          45 :   }
     551         291 : }
     552             : 
     553             : static void
     554             : fd_features_activate( fd_bank_t *        bank,
     555             :                       fd_accdb_t  *      accdb,
     556         318 :                       fd_capture_ctx_t * capture_ctx ) {
     557         318 :   for( fd_feature_id_t const * id = fd_feature_iter_init();
     558       97308 :                                    !fd_feature_iter_done( id );
     559       96990 :                                id = fd_feature_iter_next( id ) ) {
     560       96990 :     fd_feature_activate( bank, accdb, capture_ctx, id, &id->id );
     561       96990 :   }
     562         318 : }
     563             : 
     564             : /* SIMD-0194: deprecate_rent_exemption_threshold
     565             :    https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
     566             : static void
     567             : deprecate_rent_exemption_threshold( fd_bank_t *        bank,
     568             :                                     fd_accdb_t *       accdb,
     569           0 :                                     fd_capture_ctx_t * capture_ctx ) {
     570             :   /* We use the bank fields here to mirror Agave - in mainnet, devnet
     571             :      and testnet Agave's bank rent.burn_percent field is different to
     572             :      the value in the sysvar. When this feature is activated in Agave,
     573             :      the sysvar inherits the value from the bank. */
     574           0 :   fd_rent_t rent               = bank->f.rent;
     575           0 :   rent.lamports_per_uint8_year = fd_rust_cast_double_to_ulong(
     576           0 :     (double)rent.lamports_per_uint8_year * rent.exemption_threshold );
     577           0 :   rent.exemption_threshold     = FD_SIMD_0194_NEW_RENT_EXEMPTION_THRESHOLD;
     578           0 :   rent.burn_percent            = FD_SIMD_0194_NEW_BURN_PERCENT;
     579             : 
     580           0 :   bank->f.rent = rent;
     581           0 :   fd_sysvar_rent_write( bank, accdb, capture_ctx, &rent );
     582           0 : }
     583             : 
     584             : static void
     585             : set_lamports_per_byte( fd_bank_t *        bank,
     586             :                        fd_accdb_t *       accdb,
     587             :                        fd_capture_ctx_t * capture_ctx,
     588          36 :                        ulong              lamports_per_byte ) {
     589          36 :   fd_rent_t rent = bank->f.rent;
     590          36 :   rent.lamports_per_uint8_year = lamports_per_byte;
     591             : 
     592          36 :   bank->f.rent = rent;
     593          36 :   fd_sysvar_rent_write( bank, accdb, capture_ctx, &rent );
     594          36 : }
     595             : 
     596             : // https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391
     597             : static void
     598             : fd_compute_and_apply_new_feature_activations( fd_bank_t *          bank,
     599             :                                               fd_accdb_t *         accdb,
     600             :                                               fd_runtime_stack_t * runtime_stack,
     601         318 :                                               fd_capture_ctx_t *   capture_ctx ) {
     602             :   /* Activate new features
     603             :       https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391 */
     604         318 :   fd_features_activate( bank, accdb, capture_ctx );
     605         318 :   fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, bank->f.slot, &bank->f.epoch_schedule );
     606             : 
     607             :   /* SIMD-0194: deprecate_rent_exemption_threshold
     608             :       https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
     609         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, deprecate_rent_exemption_threshold ) ) ) {
     610           0 :     deprecate_rent_exemption_threshold( bank, accdb, capture_ctx );
     611           0 :   }
     612             : 
     613             :   /* SIMD-0437 rent reduction gates.
     614             :      https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/runtime/src/bank.rs#L5612-L5639 */
     615         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_6333 ) ) ) {
     616           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 6333UL );
     617           6 :   }
     618         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_5080 ) ) ) {
     619           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 5080UL );
     620           6 :   }
     621         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_2575 ) ) ) {
     622           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 2575UL );
     623           6 :   }
     624         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_1322 ) ) ) {
     625           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 1322UL );
     626           6 :   }
     627         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_696 ) ) ) {
     628           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 696UL );
     629           6 :   }
     630             : 
     631             :   /* SIMD-0438 resets rent to the legacy value (in case something goes
     632             :      wrong with the above).
     633             :      https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/runtime/src/bank.rs#L5641-L5644 */
     634         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, set_lamports_per_byte_to_6960 ) ) ) {
     635           6 :     set_lamports_per_byte( bank, accdb, capture_ctx, 6960UL );
     636           6 :   }
     637             : 
     638             :   /* SIMD-0550: double_disinflation_rate
     639             :      https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L6232-L6234 */
     640         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, double_disinflation_rate ) ) ) {
     641           0 :     fd_apply_double_disinflation_rate( bank );
     642           0 :   }
     643             : 
     644             :   /* Apply builtin program feature transitions
     645             :       https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
     646         318 :   fd_apply_builtin_program_feature_transitions( bank, accdb, runtime_stack, capture_ctx );
     647             : 
     648         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, vote_state_v4 ) ) ) {
     649           0 :     fd_upgrade_core_bpf_program( bank, accdb, runtime_stack, &fd_solana_stake_program_id, &fd_solana_stake_program_vote_state_v4_buffer_address, capture_ctx );
     650           0 :   }
     651             : 
     652         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, validator_location_registration ) ) ) {
     653             :     /* TODO: upgrade the stake program to support vote state v5 */
     654           0 :   }
     655             : 
     656             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.2/runtime/src/bank.rs#L5703-L5716 */
     657         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, replace_spl_token_with_p_token ) ) ) {
     658           0 :     fd_upgrade_loader_v2_program_with_loader_v3_program(
     659           0 :       bank,
     660           0 :       accdb,
     661           0 :       runtime_stack,
     662           0 :       &fd_solana_spl_token_id,
     663           0 :       &fd_solana_ptoken_program_buffer_address,
     664           0 :       FD_FEATURE_ACTIVE_BANK( bank, relax_programdata_account_check_migration ),
     665           0 :       capture_ctx );
     666           0 :   }
     667             : 
     668             :   /* https://github.com/anza-xyz/agave/blob/v4.0.0-beta.4/runtime/src/bank.rs#L5736-L5744 */
     669         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, upgrade_bpf_stake_program_to_v5 ) ) ) {
     670           0 :     fd_upgrade_core_bpf_program(
     671           0 :       bank,
     672           0 :       accdb,
     673           0 :       runtime_stack,
     674           0 :       &fd_solana_stake_program_id,
     675           0 :       &fd_solana_stake_program_v5_buffer_address,
     676           0 :       capture_ctx );
     677           0 :   }
     678             : 
     679             :   /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/bank.rs#L6182-L6190 */
     680         318 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) ) ) {
     681           0 :     fd_upgrade_core_bpf_program(
     682           0 :       bank,
     683           0 :       accdb,
     684           0 :       runtime_stack,
     685           0 :       &fd_solana_stake_program_id,
     686           0 :       &fd_solana_stake_program_v5_1_buffer_address,
     687           0 :       capture_ctx );
     688           0 :   }
     689         318 : }
     690             : 
     691             : /* Starting a new epoch.
     692             :   New epoch:        T
     693             :   Just ended epoch: T-1
     694             :   Epoch before:     T-2
     695             : 
     696             :   In this function:
     697             :   - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
     698             :   - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
     699             :   - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
     700             : 
     701             :   Invariant during an epoch T:
     702             :   vote_states_prev holds the stakes at T-1
     703             :   vote_states_prev_prev holds the stakes at T-2
     704             :  */
     705             : /* process for the start of a new epoch
     706             :    https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L1811-L1899 */
     707             : static void
     708             : fd_runtime_process_new_epoch( fd_bank_t *          bank,
     709             :                               fd_accdb_t *         accdb,
     710             :                               fd_capture_ctx_t *   capture_ctx,
     711             :                               ulong                parent_epoch,
     712         318 :                               fd_runtime_stack_t * runtime_stack ) {
     713         318 :   long  start                      = fd_log_wallclock();
     714         318 :   ulong epoch_start_capitalization = bank->f.capitalization;
     715             : 
     716         318 :   fd_compute_and_apply_new_feature_activations( bank, accdb, runtime_stack, capture_ctx );
     717             : 
     718         318 :   bank->f.slot_params = fd_slot_params_at_slot( bank, bank->f.slot );
     719             : 
     720             :   /* Update the cached warmup/cooldown rate epoch now that features may
     721             :      have changed (reduce_stake_warmup_cooldown may have just activated). */
     722         318 :   bank->f.warmup_cooldown_rate_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule,
     723         318 :                                                          bank->f.features.reduce_stake_warmup_cooldown,
     724         318 :                                                          NULL );
     725             : 
     726             :   /* Updates stake history sysvar accumulated values and recomputes
     727             :      stake delegations for vote accounts. */
     728             : 
     729         318 :   uchar                stake_delegations_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
     730         318 :   fd_stake_history_t   stake_delegations_history_[1];
     731         318 :   fd_stake_history_t * stake_delegations_history = fd_sysvar_stake_history_read( accdb, bank->accdb_fork_id, stake_delegations_history_data, stake_delegations_history_ );
     732             : 
     733         318 :   fd_stake_delegations_t * stake_delegations = fd_bank_stake_delegations_modify( bank );
     734         318 :   if( FD_UNLIKELY( !stake_delegations ) ) {
     735           0 :     FD_LOG_CRIT(( "stake_delegations is NULL" ));
     736           0 :   }
     737         318 :   fd_stake_delegations_view_begin( stake_delegations,
     738         318 :                                    bank->f.epoch,
     739         318 :                                    stake_delegations_history,
     740         318 :                                    &bank->f.warmup_cooldown_rate_epoch,
     741         318 :                                    FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ),
     742         318 :                                    bank->stake_delegations_fork_id );
     743             : 
     744             :   /* Wipe WARMED tags awarded under the old floating point math when the
     745             :      fixed point math activates.  This will force all the effective
     746             :      stakes to be re-computed using the post-activation math.
     747             :      Unfortunately, one wipe at the activation boundary is not enough.
     748             :      The consensus root lags the replay frontier.  So
     749             :      post-activation/post-boundary, when the unrooted pre-activation
     750             :      blocks root and their deltas are applied into the root, their
     751             :      pre-activation tags will also leak into the root pool.  So the
     752             :      award sites record whether any live WARMED tag might have been
     753             :      computed with the pre-activation floating point math, and we wipe
     754             :      whenever that is the case.  We expect the invalidation to fire
     755             :      exactly twice: at the activation boundary and at the first boundary
     756             :      after it, unless some extremely sparse epochs happen after
     757             :      activation.  Tags are only used at boundaries for now, and this
     758             :      runs before any use. */
     759         318 :   if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) ) ) {
     760           0 :     fd_stake_delegations_invalidate_warmed( stake_delegations );
     761           0 :   }
     762             : 
     763         318 :   fd_stakes_activate_epoch( bank, runtime_stack, accdb, capture_ctx, stake_delegations,
     764         318 :                             &bank->f.warmup_cooldown_rate_epoch );
     765             : 
     766             :   /* Distribute rewards.  This involves calculating the rewards for
     767             :      every vote and stake account. */
     768             : 
     769         318 :   fd_hash_t const * parent_blockhash = fd_blockhashes_peek_last_hash( &bank->f.block_hash_queue );
     770         318 :   fd_begin_partitioned_rewards( bank,
     771         318 :                                 accdb,
     772         318 :                                 runtime_stack,
     773         318 :                                 capture_ctx,
     774         318 :                                 stake_delegations,
     775         318 :                                 parent_blockhash,
     776         318 :                                 parent_epoch,
     777         318 :                                 epoch_start_capitalization );
     778             : 
     779             :   /* Update stake history balance and capitalization only after epoch
     780             :      reward partitions have been calculated. */
     781         318 :   fd_stake_history_ensure_rent_exempt( bank, accdb, capture_ctx );
     782             : 
     783         318 :   fd_stake_delegations_view_end( stake_delegations,
     784         318 :                                  stake_delegations_history,
     785         318 :                                  &bank->f.warmup_cooldown_rate_epoch,
     786         318 :                                  FD_FEATURE_ACTIVE_BANK( bank, upgrade_bpf_stake_program_to_v5_1 ) );
     787             : 
     788             :   /* The Agave client handles updating their stakes cache with a call to
     789             :      update_epoch_stakes() which keys stakes by the leader schedule
     790             :      epochs and retains up to 6 epochs of stakes.  However, to correctly
     791             :      calculate the leader schedule, we just need to maintain the vote
     792             :      states for the current epoch, the previous epoch, and the one
     793             :      before that.
     794             :      https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank.rs#L2175
     795             :   */
     796             : 
     797             :   /* Now that our stakes caches have been updated, we can calculate the
     798             :      leader schedule for the upcoming epoch epoch using our new
     799             :      vote_states_prev_prev (stakes for T-2). */
     800             : 
     801         318 :   fd_runtime_update_leaders( bank, runtime_stack );
     802             : 
     803         318 :   if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_emit( bank );
     804             : 
     805         318 :   long end = fd_log_wallclock();
     806         318 :   FD_LOG_NOTICE(( "starting epoch %s%lu%s at slot %lu %s(took %.6f seconds)%s", fd_log_style_bold(), bank->f.epoch, fd_log_style_normal(), bank->f.slot, fd_log_style_dim(), (double)(end - start) / 1e9, fd_log_style_normal() ));
     807         318 : }
     808             : 
     809             : static void
     810             : fd_runtime_block_pre_execute_process_new_epoch( fd_bank_t *          bank,
     811             :                                                 fd_accdb_t *         accdb,
     812             :                                                 fd_capture_ctx_t *   capture_ctx,
     813             :                                                 fd_runtime_stack_t * runtime_stack,
     814        7476 :                                                 int *                is_epoch_boundary ) {
     815             : 
     816        7476 :   ulong const slot = bank->f.slot;
     817        7476 :   if( FD_LIKELY( slot != 0UL ) ) {
     818        7476 :     fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     819             : 
     820        7476 :     ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
     821        7476 :     ulong slot_idx;
     822        7476 :     ulong new_epoch  = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
     823        7476 :     if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
     824             :       /* The block after genesis has a height of 1. */
     825           0 :       bank->f.block_height = 1UL;
     826           0 :     }
     827             : 
     828        7476 :     if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
     829         318 :       FD_LOG_DEBUG(( "Epoch boundary starting" ));
     830         318 :       fd_runtime_process_new_epoch( bank, accdb, capture_ctx, prev_epoch, runtime_stack );
     831         318 :       *is_epoch_boundary = 1;
     832        7158 :     } else {
     833        7158 :       *is_epoch_boundary = 0;
     834        7158 :     }
     835             : 
     836        7476 :     fd_distribute_partitioned_epoch_rewards( bank, accdb, runtime_stack, capture_ctx );
     837        7476 :   } else {
     838           0 :     *is_epoch_boundary = 0;
     839           0 :   }
     840        7476 : }
     841             : 
     842             : 
     843             : static void
     844             : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t *        bank,
     845             :                                             fd_accdb_t *       accdb,
     846        7476 :                                             fd_capture_ctx_t * capture_ctx ) {
     847             :   // let (fee_rate_governor, fee_components_time_us) = measure_us!(
     848             :   //     FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
     849             :   // );
     850             :   /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
     851             : 
     852        7476 :   fd_runtime_new_fee_rate_governor_derived( bank, bank->f.parent_signature_cnt );
     853             : 
     854        7476 :   fd_sysvar_clock_update_slot( bank, accdb, capture_ctx );
     855             : 
     856             :   // It has to go into the current txn previous info but is not in slot 0
     857        7476 :   if( bank->f.slot != 0 ) {
     858        7476 :     fd_sysvar_slot_hashes_update( bank, accdb, capture_ctx );
     859        7476 :   }
     860        7476 :   fd_sysvar_last_restart_slot_update( bank, accdb, capture_ctx );
     861        7476 : }
     862             : 
     863             : int
     864             : fd_runtime_load_txn_address_lookup_tables( fd_txn_t const *         txn,
     865             :                                            uchar const *            payload,
     866             :                                            fd_accdb_t *             accdb,
     867             :                                            fd_accdb_fork_id_t       fork_id,
     868             :                                            ulong                    slot,
     869             :                                            fd_slot_hashes_t const * hashes,
     870         366 :                                            fd_acct_addr_t *         out_accts_alt ) {
     871             : 
     872         366 :   if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
     873             : 
     874         363 :   fd_alut_interp_t interp[1];
     875         363 :   fd_alut_interp_new( interp, out_accts_alt, txn, payload, hashes, slot );
     876             : 
     877         363 :   fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
     878         453 :   for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
     879         129 :     fd_txn_acct_addr_lut_t const * addr_lut = &addr_luts[i];
     880         129 :     fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, payload+addr_lut->addr_off );
     881             : 
     882         129 :     fd_acc_t acc = fd_accdb_read_one( accdb, fork_id, addr_lut_acc.uc );
     883         129 :     if( FD_UNLIKELY( !acc.lamports ) ) {
     884           3 :       fd_accdb_unread_one( accdb, &acc );
     885           3 :       return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
     886           3 :     }
     887         126 :     int err = fd_alut_interp_next( interp, &addr_lut_acc, acc.owner, acc.data, acc.data_len );
     888         126 :     fd_accdb_unread_one( accdb, &acc );
     889         126 :     if( FD_UNLIKELY( err ) ) return err;
     890         126 :   }
     891             : 
     892         324 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     893         363 : }
     894             : 
     895             : /* Pre-populate the bank's in-memory feature set with upcoming feature
     896             :    activations.  If the current slot is the last slot before an epoch
     897             :    boundary, scan all known feature accounts. Otherwise, returns early.
     898             : 
     899             :    For any feature that is pending (not yet activated on-chain) but has
     900             :    an account owned by the feature program, set the in-memory activation
     901             :    slot within the bank's featureset to the first slot of the next
     902             :    epoch.  This is needed so that deployment verification (which uses
     903             :    slot+1) can detect features that will activate at the next epoch
     904             :    boundary.
     905             : 
     906             :    In Agave, program deployments use the feature set from the next
     907             :    slot via DELAY_VISIBILITY_SLOT_OFFSET.  The runtime environments
     908             :    for deployment are selected based on epoch_of(slot+1):
     909             :    https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank.rs#L3280-L3295
     910             :    https://github.com/anza-xyz/agave/blob/v3.1.8/svm/src/transaction_processor.rs#L339-L345
     911             : 
     912             :    This function does NOT write to feature accounts or update the
     913             :    lthash.  It only modifies the bank's in-memory feature set. */
     914             : static void
     915             : fd_features_prepopulate_upcoming( fd_bank_t *  bank,
     916        7476 :                                   fd_accdb_t * accdb ) {
     917        7476 :   ulong slot = bank->f.slot;
     918        7476 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
     919        7476 :   ulong curr_epoch = fd_slot_to_epoch( epoch_schedule, slot,     NULL );
     920        7476 :   ulong next_epoch = fd_slot_to_epoch( epoch_schedule, slot+1UL, NULL );
     921        7476 :   if( FD_LIKELY( curr_epoch==next_epoch ) ) return;
     922             : 
     923         279 :   fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, slot, epoch_schedule );
     924         279 : }
     925             : 
     926             : void
     927             : fd_runtime_block_execute_prepare( fd_banks_t *         banks,
     928             :                                   fd_bank_t *          bank,
     929             :                                   fd_accdb_t *         accdb,
     930             :                                   fd_runtime_stack_t * runtime_stack,
     931             :                                   fd_capture_ctx_t *   capture_ctx,
     932        7476 :                                   int *                is_epoch_boundary ) {
     933             :   /* Cache the Alpenglow migration slot inside the bank so that we can
     934             :      read it inside transaction execution. */
     935        7476 :   bank->f.alpenglow_migration_slot = fd_alpenglow_migration_slot( bank, accdb );
     936             : 
     937        7476 :   if( FD_LIKELY( bank->f.slot ) ) {
     938        7476 :     fd_bank_t * parent = fd_banks_bank_query( banks, bank->parent_idx );
     939        7476 :     FD_TEST( parent );
     940        7476 :     ulong child_epoch = fd_slot_to_epoch( &bank->f.epoch_schedule, bank->f.slot, NULL );
     941        7476 :     fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
     942        7476 :     if( FD_UNLIKELY( child_epoch!=fd_vote_stakes_fork_epoch( vote_stakes, bank->vote_stakes_fork_id ) ) ) {
     943         525 :       fd_vote_stakes_purge_fork( vote_stakes, bank->vote_stakes_fork_id );
     944         525 :       bank->vote_stakes_fork_id = fd_vote_stakes_new_fork( vote_stakes, parent->vote_stakes_fork_id, child_epoch );
     945         525 :     }
     946        7476 :   }
     947             : 
     948        7476 :   fd_runtime_block_pre_execute_process_new_epoch( bank, accdb, capture_ctx, runtime_stack, is_epoch_boundary );
     949             : 
     950        7476 :   if( FD_LIKELY( bank->f.slot ) ) {
     951        7476 :     fd_cost_tracker_store_t * cost_tracker_store = fd_bank_cost_tracker( bank );
     952        7476 :     fd_cost_tracker_t *       cost_tracker       = fd_cost_tracker_store_pin( cost_tracker_store, bank->cost_tracker_fork_id );
     953        7476 :     fd_cost_tracker_init( cost_tracker, &bank->f.features, &bank->f.slot_params, bank->f.slot );
     954        7476 :     fd_cost_tracker_store_unpin( cost_tracker_store, bank->cost_tracker_fork_id );
     955        7476 :   }
     956             : 
     957        7476 :   fd_features_prepopulate_upcoming( bank, accdb );
     958        7476 :   fd_runtime_block_sysvar_update_pre_execute( bank, accdb, capture_ctx );
     959        7476 :   FD_TEST( fd_sysvar_cache_validate( bank, accdb ) );
     960        7476 : }
     961             : 
     962             : static void
     963             : fd_runtime_update_bank_hash( fd_bank_t *        bank,
     964         402 :                              fd_capture_ctx_t * capture_ctx ) {
     965             :   /* Compute the new bank hash */
     966         402 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
     967         402 :   fd_hash_t new_bank_hash[1] = { 0 };
     968         402 :   fd_hashes_hash_bank(
     969         402 :       lthash,
     970         402 :       &bank->f.prev_bank_hash,
     971         402 :       (fd_hash_t *)bank->f.poh.hash,
     972         402 :       bank->f.signature_count,
     973         402 :       new_bank_hash );
     974             : 
     975         402 :   fd_hashes_apply_hard_forks(
     976         402 :     new_bank_hash,
     977         402 :     bank->f.slot,
     978         402 :     bank->f.parent_slot,
     979         402 :     bank->f.hard_forks,
     980         402 :     bank->f.hard_fork_cnt );
     981             : 
     982             :   /* Update the bank hash */
     983         402 :   bank->f.bank_hash = *new_bank_hash;
     984             : 
     985         402 :   if( capture_ctx && capture_ctx->capture_solcap &&
     986         402 :       bank->f.slot>=capture_ctx->solcap_start_slot ) {
     987             : 
     988           0 :     uchar lthash_hash[FD_HASH_FOOTPRINT];
     989           0 :     fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
     990           0 :     fd_capture_link_write_bank_preimage(
     991           0 :       capture_ctx,
     992           0 :       bank->f.slot,
     993           0 :       (fd_hash_t *)new_bank_hash->hash,
     994           0 :       (fd_hash_t *)&bank->f.prev_bank_hash,
     995           0 :       (fd_hash_t *)lthash_hash,
     996           0 :       (fd_hash_t *)bank->f.poh.hash,
     997           0 :       bank->f.signature_count );
     998           0 :   }
     999             : 
    1000         402 :   fd_bank_lthash_end_locking_query( bank );
    1001         402 : }
    1002             : 
    1003             : /******************************************************************************/
    1004             : /* Transaction Level Execution Management                                     */
    1005             : /******************************************************************************/
    1006             : 
    1007             : /* fd_runtime_pre_execute_check is responsible for conducting many of
    1008             :    the transaction sanitization checks.  This is a combination of some
    1009             :    of the work done in Agave's load_and_execute_transactions(), and some
    1010             :    of the work done in Agave's transaction ingestion stage, before the
    1011             :    transaction even hits the scheduler.  We do some of the checks also
    1012             :    in our transaction ingestion stage.  For example, the duplicate
    1013             :    account check is performed in both the leader and the replay
    1014             :    scheduler.  As a result, the duplicate account check below is
    1015             :    essentially redundant, except that our fuzzing harness expects a
    1016             :    single entry point to cover all of these checks.  So we keep all of
    1017             :    the checks below for fuzzing purposes.  We could in theory hoist some
    1018             :    of the pre-scheduler checks into a public function that is only
    1019             :    invoked by the fuzzer to avoid duplication in the leader and the
    1020             :    replay pipeline.  But all the duplicate checks are pretty cheap, and
    1021             :    the order and placement of the checks are also in motion on Agave's
    1022             :    side, and performing all the checks faithfully would require access
    1023             :    to the bank in the scheduler which is kind of gross.  So that's all
    1024             :    probably more hassle than worth. */
    1025             : 
    1026             : static inline int
    1027             : fd_runtime_pre_execute_check( fd_runtime_t *      runtime,
    1028             :                               fd_bank_t *         bank,
    1029             :                               fd_txn_in_t const * txn_in,
    1030         717 :                               fd_txn_out_t *      txn_out ) {
    1031             : 
    1032             :   /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
    1033             :      TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
    1034             :      be moved in the future. The Agave call hierarchy looks something like this:
    1035             :             process_single_slot
    1036             :                    v
    1037             :             confirm_full_slot
    1038             :                    v
    1039             :             confirm_slot_entries --------------------------------------------------->
    1040             :                    v                               v                                v
    1041             :             verify_transaction    ComputeBudget::process_instruction         process_entries
    1042             :                    v                                                                v
    1043             :             verify_precompiles                                                process_batches
    1044             :                                                                                     v
    1045             :                                                                                    ...
    1046             :                                                                                     v
    1047             :                                                                         load_and_execute_transactions
    1048             :                                                                                     v
    1049             :                                                                                    ...
    1050             :                                                                                     v
    1051             :                                                                               load_accounts --> load_transaction_accounts
    1052             :                                                                                     v
    1053             :                                                                        general transaction execution
    1054             : 
    1055             :   */
    1056             : 
    1057             :   /* Verify the transaction. For now, this step only involves processing
    1058             :      the compute budget instructions. */
    1059         717 :   int err = fd_executor_verify_transaction( bank, txn_in, txn_out );
    1060         717 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    1061           6 :     txn_out->err.is_committable = 0;
    1062           6 :     return err;
    1063           6 :   }
    1064             : 
    1065             :   /* Set up the transaction accounts and other txn ctx metadata. This
    1066             :      also resolves ALUT-referenced account keys and validates account
    1067             :      locks before accounts are acquired.  Bundle transactions bind to
    1068             :      the pool acquired and validated once for the whole bundle by
    1069             :      fd_runtime_prepare_bundle_accounts() and never acquire on a
    1070             :      per-transaction basis.  However, the implicit programdata state
    1071             :      must be re-derived here per-transaction, and not once up front.  An
    1072             :      earlier bundle transaction can deploy a program, which changes
    1073             :      whether a pool account parses as a program account, and therefore
    1074             :      which PD account is being implicitly accounted for. */
    1075         711 :   if( FD_UNLIKELY( txn_in->bundle.is_bundle ) ) {
    1076         159 :     fd_executor_setup_accounts_for_txn_bundle( runtime, txn_in, txn_out );
    1077         159 :     err = fd_runtime_setup_bundle_executables( runtime, bank, txn_out );
    1078         552 :   } else {
    1079         552 :     err = fd_executor_setup_accounts_for_txn( runtime, bank, txn_in, txn_out );
    1080         552 :   }
    1081         711 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    1082           0 :     txn_out->err.is_committable = 0;
    1083           0 :     return err;
    1084           0 :   }
    1085             : 
    1086         711 :   txn_out->details.check_start_ticks = fd_tickcount();
    1087             : 
    1088             :   /* load_and_execute_transactions() -> check_transactions()
    1089             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
    1090         711 :   err = fd_executor_check_transactions( runtime, bank, txn_in, txn_out );
    1091         711 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    1092          12 :     txn_out->err.is_committable = 0;
    1093          12 :     return err;
    1094          12 :   }
    1095             : 
    1096             :   /* load_and_execute_sanitized_transactions() -> validate_fees() ->
    1097             :      validate_transaction_fee_payer()
    1098             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
    1099         699 :   err = fd_executor_validate_transaction_fee_payer( bank, txn_in, txn_out );
    1100         699 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    1101             :     /* As of the relax_fee_payer_constraint feature, a transaction with
    1102             :        an invalid fee payer can be included in the block. The
    1103             :        transaction has no effect on account state and charges no fees.
    1104             :        These are called "no-op" transactions. They consume block space
    1105             :        (charging the requested CUs) and their loaded accounts data size
    1106             :        is charged at their loaded accounts data size limit.
    1107             : 
    1108             :        Firedancer does not produce blocks containing no-op transactions
    1109             :        but we need to be able to replay blocks containing these.
    1110             : 
    1111             :        The exception is durable nonce transactions - durable nonce
    1112             :        transactions with invalid fee payers are not allowed in a block,
    1113             :        and if a block contains any such transactions it is invalid.
    1114             : 
    1115             :        https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processor.rs#L741-L770 */
    1116         111 :     if( FD_FEATURE_ACTIVE_BANK( bank, relax_fee_payer_constraint ) &&
    1117         111 :         txn_out->accounts.nonce_idx_in_txn==ULONG_MAX ) {
    1118           6 :       txn_out->err.is_committable = 1;
    1119           6 :       txn_out->err.is_noop        = 1;
    1120           6 :       txn_out->err.is_fees_only   = 0;
    1121         105 :     } else {
    1122         105 :       txn_out->err.is_committable = 0;
    1123         105 :       txn_out->err.is_noop        = 0;
    1124         105 :     }
    1125         111 :     return err;
    1126         111 :   }
    1127             : 
    1128             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
    1129         588 :   err = fd_executor_load_transaction_accounts( bank, txn_in, txn_out );
    1130         588 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    1131             :     /* Regardless of whether transaction accounts were loaded successfully, the transaction is
    1132             :        included in the block and transaction fees are collected.
    1133             :        https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
    1134          27 :     txn_out->err.is_fees_only = 1;
    1135             : 
    1136             :     /* If the transaction fails to load, the "rollback" accounts will include one of the following:
    1137             :         1. Nonce account only
    1138             :         2. Fee payer only
    1139             :         3. Nonce account + fee payer
    1140             : 
    1141             :         Because the cost tracker uses the loaded account data size in block cost calculations, we need to
    1142             :         make sure our calculated loaded accounts data size is conformant with Agave's.
    1143             :         https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/svm/src/account_loader.rs#L437-L449
    1144             : 
    1145             :         These are different depending on if define_ltds_fee_only_semantics is enabled or not.
    1146             : 
    1147             :         If define_ltds_fee_only_semantics is enabled, we use the accumulated load size so far,
    1148             :         clamped to the compute budget requested loaded_accounts_data_size_limit. */
    1149          27 :     if( FD_FEATURE_ACTIVE_BANK( bank, define_ltds_fee_only_semantics ) ) {
    1150             :       /* https://github.com/anza-xyz/agave/blob/v4.1.0-beta.1/svm/src/account_loader.rs#L495-L501 */
    1151           9 :       txn_out->details.loaded_accounts_data_size = fd_ulong_min(
    1152           9 :         txn_out->details.loaded_accounts_data_size,
    1153           9 :         txn_out->details.compute_budget.loaded_accounts_data_size_limit );
    1154          18 :     } else {
    1155             :       /* If define_ltds_fee_only_semantics is not enabled, initialize
    1156             :          loaded_accounts_data_size with the dlen of the fee payer. */
    1157          18 :       txn_out->details.loaded_accounts_data_size = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ]->data_len;
    1158             : 
    1159             :       /* Special case handling for if a nonce account is present in the transaction. */
    1160          18 :       if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
    1161             :         /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
    1162             :             be double counting the dlen of the fee payer. */
    1163           6 :         if( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
    1164           3 :           txn_out->details.loaded_accounts_data_size += txn_out->accounts.account[ txn_out->accounts.nonce_idx_in_txn ]->data_len;
    1165           3 :         }
    1166           6 :       }
    1167          18 :     }
    1168          27 :   }
    1169             : 
    1170             :   /*
    1171             :      The fee payer and the nonce account will be stored and hashed so
    1172             :      long as the transaction landed on chain, or, in Agave terminology,
    1173             :      the transaction was processed.
    1174             :      https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
    1175             : 
    1176             :      A transaction lands on chain in one of two ways:
    1177             :      (1) Passed fee validation and loaded accounts.
    1178             :      (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
    1179             :          SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
    1180             : 
    1181             :      So, at this point, the transaction is committable.
    1182             :    */
    1183             : 
    1184         588 :   return err;
    1185         699 : }
    1186             : 
    1187             : /* fd_runtime_lthash_account updates the running lthash of the bank
    1188             :    given an account that might have been updated. */
    1189             : 
    1190             : static void
    1191             : fd_runtime_lthash_account( fd_runtime_t *      runtime,
    1192             :                            fd_bank_t *         bank,
    1193             :                            fd_pubkey_t const * pubkey,
    1194             :                            fd_acc_t *          acc,
    1195         567 :                            fd_capture_ctx_t *  capture_ctx ) {
    1196         567 :   if( FD_UNLIKELY( !acc->lamports ) ) {
    1197          24 :     acc->data_len   = 0UL;
    1198          24 :     acc->executable = 0;
    1199          24 :     memset( acc->owner, 0, sizeof(acc->owner) );
    1200          24 :   }
    1201             : 
    1202         567 :   if( FD_UNLIKELY( acc->prior_data &&
    1203         567 :                    acc->lamports==acc->prior_lamports &&
    1204         567 :                    acc->data_len==acc->prior_data_len &&
    1205         567 :                    (!!acc->executable)==(!!acc->prior_executable) &&
    1206         567 :                    !memcmp( acc->owner, acc->prior_owner, sizeof(acc->owner) ) &&
    1207         567 :                    !memcmp( acc->data,  acc->prior_data,  acc->data_len ) ) ) {
    1208           6 :     runtime->metrics.lthash_unchanged_cnt++;
    1209           6 :     if( FD_LIKELY( acc->lamports ) ) fd_hashes_capture_account( pubkey->uc, acc->owner, acc->lamports, acc->executable, acc->data, acc->data_len, bank, capture_ctx );
    1210           6 :     return;
    1211           6 :   }
    1212             : 
    1213         561 :   if( FD_LIKELY( acc->prior_lamports || acc->lamports ) ) {
    1214         561 :     ulong prior_lamports = acc->prior_data ? acc->prior_lamports : 0UL;
    1215         561 :     fd_hashes_update_pair( pubkey->uc, acc->prior_owner, prior_lamports, acc->prior_executable, acc->prior_data, acc->prior_data_len,
    1216         561 :                                        acc->owner,       acc->lamports,  acc->executable,       acc->data,       acc->data_len,
    1217         561 :                            bank, capture_ctx );
    1218         561 :   }
    1219         561 : }
    1220             : 
    1221             : /* fd_runtime_commit_txn is a helper used by the transaction executor to
    1222             :    finalize account changes back into the database.  It also handles
    1223             :    txncache insertion and updates to the vote/stake cache.  TODO: This
    1224             :    function should probably be moved to fd_executor.c. */
    1225             : 
    1226             : void
    1227             : fd_runtime_commit_txn( fd_runtime_t *      runtime,
    1228             :                        fd_bank_t *         bank,
    1229             :                        fd_txn_in_t const * txn_in,
    1230         411 :                        fd_txn_out_t *      txn_out ) {
    1231         411 :   FD_TEST( txn_out->err.is_committable );
    1232             : 
    1233         411 :   txn_out->details.commit_start_ticks = fd_tickcount();
    1234             : 
    1235         411 :   if( FD_UNLIKELY( !txn_out->err.txn_err ) ) {
    1236         231 :     fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
    1237        1506 :     for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
    1238             :       /* We are only interested in saving writable accounts and the fee
    1239             :          payer account. */
    1240        1275 :       if( FD_UNLIKELY( !txn_out->accounts.is_writable[ i ] ) ) continue;
    1241             : 
    1242             :       /* https://github.com/anza-xyz/agave/blob/v4.2.0-beta.1/runtime/src/account_saver.rs#L120-L122 */
    1243         507 :       if( FD_UNLIKELY( !txn_out->accounts.touched[ i ] ) ) continue;
    1244             : 
    1245         450 :       fd_pubkey_t const * pubkey = &txn_out->accounts.keys[ i ];
    1246             : 
    1247             :       /* Only the txn that owns the accdb reference commits the account
    1248             :          state.  In a bundle, an account is owned by its last writable
    1249             :          user (account_acquired==1); every other txn referencing it has
    1250             :          account_acquired==0 and skips here, so the final shared state is
    1251             :          lthashed exactly once and not double-counted.  stake_update/
    1252             :          vote_update are recomputed from the final state and were moved
    1253             :          onto this owner, so they also fire here exactly once. */
    1254         450 :       if( FD_UNLIKELY( !txn_out->accounts.account_acquired[ i ] ) ) continue;
    1255             : 
    1256         387 :       fd_acc_t * account = txn_out->accounts.account[ i ];
    1257         387 :       account->commit = 1;
    1258             : 
    1259         387 :       if( FD_UNLIKELY( txn_out->accounts.stake_update[ i ] ) ) {
    1260           6 :         fd_stake_history_t stake_history[1];
    1261           6 :         fd_stakes_update_stake_delegation( pubkey, account, bank,
    1262           6 :             fd_sysvar_cache_stake_history_view( runtime->sysvar_cache, stake_history ), txn_in );
    1263           6 :       }
    1264             : 
    1265         387 :       if( txn_out->accounts.vote_update[i] ) {
    1266          93 :         int is_valid = account->lamports &&
    1267          93 :                        fd_vsv_is_correct_size_owner_and_init( account->owner, account->data, account->data_len );
    1268          93 :         fd_vote_stakes_update_state( vote_stakes, bank->vote_stakes_fork_id, pubkey, is_valid );
    1269          93 :       }
    1270             : 
    1271         387 :       fd_runtime_lthash_account( runtime, bank, pubkey, account, runtime->log.capture_ctx );
    1272         387 :     }
    1273             : 
    1274             :     /* Atomically add all accumulated tips to the bank once after
    1275             :        processing all accounts. */
    1276         231 :     if( FD_UNLIKELY( txn_out->details.tips ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.tips, txn_out->details.tips );
    1277         231 :   }
    1278             : 
    1279         411 :   txn_out->details.commit_index_in_slot = FD_ATOMIC_FETCH_AND_ADD( &bank->f.txn_count, 1UL );
    1280         411 :   FD_ATOMIC_FETCH_AND_ADD( &bank->f.execution_fees,  txn_out->details.execution_fee );
    1281         411 :   FD_ATOMIC_FETCH_AND_ADD( &bank->f.priority_fees,   txn_out->details.priority_fee );
    1282         411 :   FD_ATOMIC_FETCH_AND_ADD( &bank->f.signature_count, txn_out->details.signature_count );
    1283         411 :   FD_ATOMIC_FETCH_AND_ADD( &bank->f.nonvote_txn_count, 1 );
    1284         411 :   if( FD_UNLIKELY( txn_out->err.txn_err ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.nonvote_failed_txn_count, 1 );
    1285             : 
    1286         411 :   if( FD_UNLIKELY( txn_out->err.txn_err ) ) FD_ATOMIC_FETCH_AND_ADD( &bank->f.failed_txn_count, 1 ); /* fees-only failures too, not just instruction errors */
    1287         411 :   FD_ATOMIC_FETCH_AND_ADD( &bank->f.total_compute_units_used, txn_out->details.compute_budget.compute_unit_limit-txn_out->details.compute_budget.compute_meter );
    1288             : 
    1289         411 :   fd_cost_tracker_t * cost_tracker = fd_cost_tracker_store_peek( fd_bank_cost_tracker( bank ), bank->cost_tracker_fork_id );
    1290         411 :   int res = fd_cost_tracker_try_add_cost( cost_tracker, txn_out );
    1291         411 :   if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
    1292           3 :     FD_LOG_DEBUG(( "fd_runtime_commit_txn: transaction failed to fit into block %d", res ));
    1293           3 :     txn_out->err.is_committable = 0;
    1294           3 :     txn_out->err.txn_err        = fd_cost_tracker_err_to_runtime_err( res );
    1295           3 :   }
    1296             : 
    1297         411 :   if( FD_LIKELY( runtime->status_cache && txn_out->accounts.nonce_idx_in_txn==ULONG_MAX && txn_out->err.is_committable ) ) {
    1298             :     /* In Agave, durable nonce transactions are inserted to the status
    1299             :        cache the same as any others, but this is only to serve RPC
    1300             :        requests, they do not need to be in there for correctness as the
    1301             :        nonce mechanism itself prevents double spend.  We skip this logic
    1302             :        entirely to simplify and improve performance of the txn cache.
    1303             : 
    1304             :        Cost tracker rejected transactions are also skipped: the block
    1305             :        is already dead, and skipping keeps the per slot insert count
    1306             :        bounded by the cost model, which sizes the txn cache. */
    1307         402 :     fd_txncache_insert( runtime->status_cache, bank->txncache_fork_id, txn_out->details.blockhash.uc, txn_out->details.blake_txn_msg_hash.uc );
    1308         402 :   }
    1309             : 
    1310             :   /* No-op transactions have no effect on the state so we should skip
    1311             :      these rollbacks. */
    1312         411 :   if( FD_UNLIKELY( txn_out->err.txn_err && !txn_out->err.is_noop ) ) {
    1313             :     /* With nonce account rollbacks, there are three cases:
    1314             : 
    1315             :        1. No nonce account in the transaction
    1316             :        2. Nonce account is the fee payer
    1317             :        3. Nonce account is not the fee payer
    1318             : 
    1319             :        We should always rollback the nonce account first.  Note that the
    1320             :        nonce account may be the fee payer (case 2). */
    1321         180 :     if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) ) {
    1322           0 :       fd_acc_t * nonce_account = txn_out->accounts.account[ txn_out->accounts.nonce_idx_in_txn ];
    1323           0 :       fd_memcpy( nonce_account->data, txn_out->accounts.nonce_rollback_data, txn_out->accounts.nonce_rollback_data_len );
    1324           0 :       nonce_account->data_len = txn_out->accounts.nonce_rollback_data_len;
    1325           0 :       fd_memcpy( nonce_account->owner, nonce_account->prior_owner, 32UL );
    1326           0 :       if( FD_UNLIKELY( txn_out->accounts.nonce_idx_in_txn==FD_FEE_PAYER_TXN_IDX ) ) {
    1327           0 :         nonce_account->lamports = txn_out->accounts.fee_payer_rollback_lamports;
    1328           0 :       } else {
    1329           0 :         nonce_account->lamports = nonce_account->prior_lamports;
    1330           0 :       }
    1331           0 :       nonce_account->executable = nonce_account->prior_executable;
    1332           0 :       nonce_account->commit = 1;
    1333           0 :       fd_runtime_lthash_account( runtime, bank, &txn_out->accounts.keys[ txn_out->accounts.nonce_idx_in_txn ], nonce_account, runtime->log.capture_ctx );
    1334           0 :     }
    1335             : 
    1336             :     /* Now, we must only save the fee payer if the nonce account was not
    1337             :        the fee payer (because that was already saved above). */
    1338         180 :     if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
    1339         180 :       fd_acc_t * fee_payer_account = txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ];
    1340         180 :       fd_memcpy( fee_payer_account->data, fee_payer_account->prior_data, fee_payer_account->prior_data_len );
    1341         180 :       fee_payer_account->data_len = fee_payer_account->prior_data_len;
    1342         180 :       fd_memcpy( fee_payer_account->owner, fee_payer_account->prior_owner, 32UL );
    1343         180 :       fee_payer_account->lamports = txn_out->accounts.fee_payer_rollback_lamports;
    1344         180 :       fee_payer_account->executable = fee_payer_account->prior_executable;
    1345             : 
    1346         180 :       fee_payer_account->commit = 1;
    1347         180 :       fd_runtime_lthash_account( runtime, bank, &txn_out->accounts.keys[ FD_FEE_PAYER_TXN_IDX ], fee_payer_account, runtime->log.capture_ctx );
    1348         180 :     }
    1349         180 :   }
    1350             : 
    1351         411 :   if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_txn_emit( txn_in, txn_out, bank );
    1352             : 
    1353         411 :   if( FD_LIKELY( !txn_out->accounts.is_bundle ) ) {
    1354         315 :     fd_accdb_release_ab( runtime->accdb,
    1355         315 :                          txn_out->accounts.cnt, runtime->accounts.account,
    1356         315 :                          runtime->accounts.executable_cnt, runtime->accounts.executable );
    1357         315 :     runtime->accounts.executable_cnt = 0UL;
    1358         315 :   }
    1359         411 : }
    1360             : 
    1361             : void
    1362             : fd_runtime_cancel_txn( fd_runtime_t *      runtime,
    1363             :                        fd_bank_t *         bank,
    1364             :                        fd_txn_in_t const * txn_in,
    1365         123 :                        fd_txn_out_t *      txn_out ) {
    1366         123 :   FD_TEST( !txn_out->err.is_committable );
    1367             : 
    1368         123 :   if( FD_UNLIKELY( bank && fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_txn_emit( txn_in, txn_out, bank );
    1369             : 
    1370         123 :   if( FD_UNLIKELY( !txn_out->accounts.is_setup ) ) return;
    1371             : 
    1372         120 :   fd_accdb_release_ab( runtime->accdb,
    1373         120 :                        txn_out->accounts.cnt, runtime->accounts.account,
    1374         120 :                        runtime->accounts.executable_cnt, runtime->accounts.executable );
    1375         120 :   runtime->accounts.executable_cnt = 0UL;
    1376         120 : }
    1377             : 
    1378             : void
    1379          84 : fd_runtime_fini_bundle( fd_runtime_t * runtime ) {
    1380          84 :   fd_accdb_release_ab( runtime->accdb,
    1381          84 :     runtime->accounts.account_cnt, runtime->accounts.account,
    1382          84 :     runtime->accounts.executable_cnt, runtime->accounts.executable );
    1383          84 :   runtime->accounts.account_cnt    = 0UL;
    1384          84 :   runtime->accounts.executable_cnt = 0UL;
    1385          84 : }
    1386             : 
    1387             : static inline void
    1388         717 : fd_runtime_reset_runtime( fd_runtime_t * runtime ) {
    1389         717 :   runtime->instr.stack_sz     = 0;
    1390         717 :   runtime->instr.trace_length = 0UL;
    1391         717 : }
    1392             : 
    1393             : static inline void
    1394             : fd_runtime_new_txn_out( fd_txn_in_t const * txn_in,
    1395         717 :                         fd_txn_out_t *      txn_out ) {
    1396         717 :   txn_out->details.load_start_ticks   = fd_tickcount();
    1397         717 :   txn_out->details.check_start_ticks  = LONG_MAX;
    1398         717 :   txn_out->details.exec_start_ticks   = LONG_MAX;
    1399         717 :   txn_out->details.commit_start_ticks = LONG_MAX;
    1400             : 
    1401         717 :   fd_compute_budget_details_new( &txn_out->details.compute_budget );
    1402         717 :   fd_memset( &txn_out->details.txn_cost, 0, sizeof(txn_out->details.txn_cost) );
    1403             : 
    1404         717 :   txn_out->details.loaded_accounts_data_size = 0UL;
    1405         717 :   txn_out->details.accounts_resize_delta     = 0L;
    1406             : 
    1407         717 :   txn_out->details.return_data.len = 0UL;
    1408         717 :   memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
    1409             : 
    1410         717 :   txn_out->details.tips            = 0UL;
    1411         717 :   txn_out->details.execution_fee   = 0UL;
    1412         717 :   txn_out->details.priority_fee    = 0UL;
    1413         717 :   txn_out->details.signature_count = TXN( txn_in->txn )->signature_cnt;
    1414         717 :   if( FD_LIKELY( txn_out->details.signature_count ) ) {
    1415         714 :     fd_memcpy( txn_out->details.signature.uc,
    1416         714 :                (uchar const *)txn_in->txn->payload + TXN( txn_in->txn )->signature_off,
    1417         714 :                sizeof(fd_signature_t) );
    1418         714 :   } else {
    1419           3 :     fd_memset( txn_out->details.signature.uc, 0, sizeof(fd_signature_t) );
    1420           3 :   }
    1421             : 
    1422         717 :   fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
    1423         717 :   memcpy( txn_out->details.blockhash.uc, blockhash->hash, sizeof(fd_hash_t) );
    1424         717 :   memset( txn_out->details.blake_txn_msg_hash.uc, 0, sizeof(fd_hash_t) );
    1425             : 
    1426         717 :   txn_out->accounts.is_setup           = 0;
    1427         717 :   txn_out->accounts.is_bundle          = txn_in->bundle.is_bundle;
    1428         717 :   if( FD_LIKELY( !txn_in->bundle.is_bundle ) ) txn_out->accounts.cnt= 0UL;
    1429             : 
    1430         717 :   FD_STATIC_ASSERT( offsetof(fd_txn_out_t, accounts.touched)-offsetof(fd_txn_out_t, accounts.stake_update)==4UL*MAX_TX_ACCOUNT_LOCKS, txn_out_flags_contiguous );
    1431         717 :   memset( txn_out->accounts.is_writable,  0, sizeof(txn_out->accounts.is_writable) );
    1432         717 :   memset( txn_out->accounts.stake_update, 0, 5UL*MAX_TX_ACCOUNT_LOCKS );
    1433         717 :   txn_out->accounts.nonce_idx_in_txn            = ULONG_MAX;
    1434             : 
    1435             :   /* For bundle transactions the resolved key list is bound once up
    1436             :      front by fd_runtime_prepare_bundle_accounts() before this runs per
    1437             :      transaction, so preserve it here.  For a non-bundle transaction the
    1438             :      executable list is built in fd_executor_setup_accounts_for_txn(),
    1439             :      so reset it here. */
    1440         717 :   if( FD_LIKELY( !txn_in->bundle.is_bundle ) ) {
    1441         558 :     txn_out->accounts.executable_cnt         = 0UL;
    1442         558 :     txn_out->accounts.executable_skipped_cnt = 0;
    1443         558 :   }
    1444         717 :   txn_out->accounts.nonce_rollback_data_len     = 0UL;
    1445         717 :   txn_out->accounts.fee_payer_rollback_lamports = 0UL;
    1446             : 
    1447         717 :   txn_out->err.is_committable = 1;
    1448         717 :   txn_out->err.is_fees_only   = 0;
    1449         717 :   txn_out->err.is_noop        = 0;
    1450         717 :   txn_out->err.txn_err        = FD_RUNTIME_EXECUTE_SUCCESS;
    1451         717 :   txn_out->err.exec_err       = FD_EXECUTOR_INSTR_SUCCESS;
    1452         717 :   txn_out->err.exec_err_kind  = FD_EXECUTOR_ERR_KIND_NONE;
    1453         717 :   txn_out->err.exec_err_idx   = UINT_MAX;
    1454         717 :   txn_out->err.custom_err     = 0;
    1455         717 : }
    1456             : 
    1457             : void
    1458             : fd_runtime_sysvar_cache_refresh( fd_runtime_t *    runtime,
    1459         861 :                                  fd_bank_t const * bank ) {
    1460         861 :   if( FD_LIKELY( runtime->sysvar_bank_seq==bank->bank_seq ) ) return;
    1461         408 :   fd_sysvar_cache_restore( runtime->sysvar_cache, runtime->accdb, bank->accdb_fork_id );
    1462         408 :   runtime->sysvar_bank_seq = bank->bank_seq;
    1463         408 :   runtime->metrics.bank_switch_cnt++;
    1464         408 : }
    1465             : 
    1466             : void
    1467             : fd_runtime_prepare_and_execute_txn( fd_runtime_t *      runtime,
    1468             :                                     fd_bank_t *         bank,
    1469             :                                     fd_txn_in_t const * txn_in,
    1470         717 :                                     fd_txn_out_t *      txn_out ) {
    1471         717 :   fd_runtime_reset_runtime( runtime );
    1472         717 :   fd_runtime_sysvar_cache_refresh( runtime, bank );
    1473             : 
    1474         717 :   fd_runtime_new_txn_out( txn_in, txn_out );
    1475             : 
    1476         717 :   uchar dump_txn = !!(runtime->log.dump_proto_ctx &&
    1477         717 :                       bank->f.slot >= runtime->log.dump_proto_ctx->dump_proto_start_slot &&
    1478         717 :                       runtime->log.dump_proto_ctx->dump_txn_to_pb);
    1479             : 
    1480             :   /* Phase 1: Capture TxnContext before execution. */
    1481         717 :   if( FD_UNLIKELY( dump_txn ) ) {
    1482           0 :     if( runtime->log.txn_dump_ctx ) {
    1483           0 :       fd_dump_txn_context_to_protobuf( runtime->log.txn_dump_ctx, runtime, bank, txn_in, txn_out );
    1484           0 :     } else {
    1485           0 :       fd_dump_txn_to_protobuf( runtime, bank, txn_in, txn_out );
    1486           0 :     }
    1487           0 :   }
    1488             : 
    1489             :   /* Transaction sanitization.  If a transaction can't be committed, is
    1490             :      fees-only, or a no-op, we return early. */
    1491         717 :   txn_out->err.txn_err = fd_runtime_pre_execute_check( runtime, bank, txn_in, txn_out );
    1492         717 :   ulong cu_before = txn_out->details.compute_budget.compute_meter;
    1493             : 
    1494             :   /* Execute the transaction if eligible to do so. */
    1495         717 :   if( FD_LIKELY( txn_out->err.is_committable ) ) {
    1496         594 :     if( FD_UNLIKELY( txn_out->err.is_noop ) ) {
    1497             :       /* No-op transactions charge the requested CUs and loaded
    1498             :          account data size limit.
    1499             :          https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processor.rs#L757-L767
    1500             :          https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/svm/src/transaction_processing_result.rs#L92-L106 */
    1501           6 :       txn_out->details.compute_budget.compute_meter = 0UL;
    1502           6 :       txn_out->details.loaded_accounts_data_size    = txn_out->details.compute_budget.loaded_accounts_data_size_limit;
    1503         588 :     } else if( FD_LIKELY( !txn_out->err.is_fees_only ) ) {
    1504         561 :       txn_out->details.exec_start_ticks = fd_tickcount();
    1505         561 :       txn_out->err.txn_err = fd_execute_txn( runtime, bank, txn_in, txn_out );
    1506         561 :     }
    1507         594 :     fd_cost_tracker_calculate_cost( bank, txn_in, txn_out );
    1508         594 :   }
    1509         717 :   ulong cu_after = txn_out->details.compute_budget.compute_meter;
    1510         717 :   runtime->metrics.cu_cum += fd_ulong_sat_sub( cu_before, cu_after );
    1511             : 
    1512             :   /* Phase 2: Capture TxnResult after execution and write to disk. */
    1513         717 :   if( FD_UNLIKELY( dump_txn && runtime->log.txn_dump_ctx ) ) {
    1514           0 :     fd_dump_txn_result_to_protobuf( runtime->log.txn_dump_ctx, txn_in, txn_out, txn_out->err.txn_err );
    1515           0 :     fd_dump_txn_fixture_to_file( runtime->log.txn_dump_ctx, runtime->log.dump_proto_ctx, txn_in );
    1516           0 :   }
    1517         717 : }
    1518             : 
    1519             : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responsible
    1520             :    for the bulk of the pre-transaction execution checks in the runtime.
    1521             :    They aim to preserve the ordering present in the Agave client to match
    1522             :    parity in terms of error codes. Sigverify is kept separate from the rest
    1523             :    of the transaction checks for fuzzing convenience.
    1524             : 
    1525             :    fd_runtime_pre_execute_check processes compute-budget instructions
    1526             :    (or v1 configuration), sets up accounts, checks transaction age and
    1527             :    status, validates the fee payer, then loads transaction accounts.
    1528             :    See its calls to fd_executor_verify_transaction,
    1529             :    fd_executor_check_transactions, fd_executor_validate_transaction_fee_payer
    1530             :    and fd_executor_load_transaction_accounts. */
    1531             : 
    1532             : 
    1533             : /******************************************************************************/
    1534             : /* Genesis                                                                    */
    1535             : /*******************************************************************************/
    1536             : 
    1537             : static void
    1538             : fd_runtime_genesis_init_program( fd_bank_t *        bank,
    1539             :                                  fd_accdb_t *       accdb,
    1540           0 :                                  fd_capture_ctx_t * capture_ctx ) {
    1541             : 
    1542           0 :   fd_sysvar_clock_init( bank, accdb, capture_ctx );
    1543           0 :   fd_sysvar_rent_init( bank, accdb, capture_ctx );
    1544             : 
    1545           0 :   fd_sysvar_slot_history_init( bank, accdb, capture_ctx );
    1546           0 :   fd_sysvar_epoch_schedule_init( bank, accdb, capture_ctx );
    1547           0 :   fd_sysvar_recent_hashes_init( bank, accdb, capture_ctx );
    1548           0 :   fd_sysvar_stake_history_init( bank, accdb, capture_ctx );
    1549           0 :   fd_sysvar_last_restart_slot_init( bank, accdb, capture_ctx );
    1550             : 
    1551           0 :   fd_builtin_programs_init( bank, accdb, capture_ctx );
    1552           0 : }
    1553             : 
    1554             : static void
    1555             : fd_runtime_init_bank_from_genesis( fd_banks_t *         banks,
    1556             :                                    fd_bank_t *          bank,
    1557             :                                    fd_runtime_stack_t * runtime_stack,
    1558             :                                    fd_accdb_t *         accdb,
    1559             :                                    fd_genesis_t const * genesis,
    1560             :                                    uchar const *        genesis_blob,
    1561           0 :                                    fd_hash_t const *    genesis_hash ) {
    1562             : 
    1563           0 :   bank->f.parent_slot = ULONG_MAX;
    1564           0 :   bank->f.poh = *genesis_hash;
    1565             : 
    1566           0 :   fd_hash_t * bank_hash = &bank->f.bank_hash;
    1567           0 :   memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
    1568             : 
    1569           0 :   uint128 target_tick_duration = (uint128)genesis->poh.tick_duration_secs * 1000000000UL + (uint128)genesis->poh.tick_duration_ns;
    1570             : 
    1571           0 :   fd_epoch_schedule_t * epoch_schedule = &bank->f.epoch_schedule;
    1572           0 :   epoch_schedule->leader_schedule_slot_offset = genesis->epoch_schedule.leader_schedule_slot_offset;
    1573           0 :   epoch_schedule->warmup                      = genesis->epoch_schedule.warmup;
    1574           0 :   epoch_schedule->first_normal_epoch          = genesis->epoch_schedule.first_normal_epoch;
    1575           0 :   epoch_schedule->first_normal_slot           = genesis->epoch_schedule.first_normal_slot;
    1576           0 :   epoch_schedule->slots_per_epoch             = genesis->epoch_schedule.slots_per_epoch;
    1577             : 
    1578           0 :   fd_rent_t * rent = &bank->f.rent;
    1579           0 :   rent->lamports_per_uint8_year = genesis->rent.lamports_per_uint8_year;
    1580           0 :   rent->exemption_threshold     = genesis->rent.exemption_threshold;
    1581           0 :   rent->burn_percent            = genesis->rent.burn_percent;
    1582             : 
    1583           0 :   fd_inflation_t * inflation = &bank->f.inflation;
    1584           0 :   inflation->initial         = genesis->inflation.initial;
    1585           0 :   inflation->terminal        = genesis->inflation.terminal;
    1586           0 :   inflation->taper           = genesis->inflation.taper;
    1587           0 :   inflation->foundation      = genesis->inflation.foundation;
    1588           0 :   inflation->foundation_term = genesis->inflation.foundation_term;
    1589           0 :   inflation->unused          = 0.0;
    1590             : 
    1591           0 :   bank->f.block_height = 0UL;
    1592             : 
    1593           0 :   {
    1594             :     /* FIXME Why is there a previous blockhash at genesis?  Why is the
    1595             :              last_hash field an option type in Agave, if even the first
    1596             :              real block has a previous blockhash? */
    1597           0 :     fd_blockhashes_t *    bhq  = fd_blockhashes_init( &bank->f.block_hash_queue, 0UL );
    1598           0 :     fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
    1599           0 :     info->lamports_per_signature = 0UL;
    1600           0 :   }
    1601             : 
    1602           0 :   fd_fee_rate_governor_t * fee_rate_governor = &bank->f.fee_rate_governor;
    1603           0 :   fee_rate_governor->target_lamports_per_signature = genesis->fee_rate_governor.target_lamports_per_signature;
    1604           0 :   fee_rate_governor->target_signatures_per_slot    = genesis->fee_rate_governor.target_signatures_per_slot;
    1605           0 :   fee_rate_governor->min_lamports_per_signature    = genesis->fee_rate_governor.min_lamports_per_signature;
    1606           0 :   fee_rate_governor->max_lamports_per_signature    = genesis->fee_rate_governor.max_lamports_per_signature;
    1607           0 :   fee_rate_governor->burn_percent                  = genesis->fee_rate_governor.burn_percent;
    1608             : 
    1609           0 :   bank->f.max_tick_height                  = genesis->poh.ticks_per_slot * (bank->f.slot + 1);
    1610           0 :   bank->f.slot_params                      = FD_SLOT_PARAMS_400MS;
    1611           0 :   bank->f.slot_params.ns_per_slot          = (ulong)( target_tick_duration * genesis->poh.ticks_per_slot );
    1612           0 :   bank->f.slot_params.ns_per_slot_adjusted = fd_ulong_sat_sub( bank->f.slot_params.ns_per_slot, FD_TARGET_SLOT_ADJUSTMENT_NS );
    1613           0 :   bank->f.slot_params.slots_per_year       = SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis->poh.ticks_per_slot;
    1614           0 :   bank->f.slot_params.hashes_per_tick      = genesis->poh.hashes_per_tick;
    1615           0 :   bank->f.slot_params_default              = bank->f.slot_params;
    1616             : 
    1617           0 :   bank->f.ticks_per_slot = genesis->poh.ticks_per_slot;
    1618           0 :   bank->f.genesis_creation_time = genesis->creation_time;
    1619             : 
    1620           0 :   bank->f.signature_count = 0UL;
    1621             : 
    1622             :   /* Derive epoch stakes */
    1623             : 
    1624           0 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
    1625           0 :   if( FD_UNLIKELY( !stake_delegations ) ) {
    1626           0 :     FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
    1627           0 :   }
    1628             : 
    1629           0 :   ulong capitalization = 0UL;
    1630             : 
    1631           0 :   for( ulong i=0UL; i<genesis->account_cnt; i++ ) {
    1632           0 :     fd_genesis_account_t account[1];
    1633           0 :     fd_genesis_account( genesis, genesis_blob, account, i );
    1634             : 
    1635           0 :     capitalization = fd_ulong_sat_add( capitalization, account->lamports );
    1636             : 
    1637           0 :     if( !memcmp( account->owner.uc, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
    1638             :       /* If an account is a stake account, then it must be added to the
    1639             :          stake delegations cache.  Like Agave, membership is decided by
    1640             :          the variant alone: a delegation of zero is still a delegation. */
    1641           0 :       fd_stake_state_t const * stake_state = fd_stake_state_view( account->data, account->data_len );
    1642           0 :       if( FD_UNLIKELY( !stake_state ) ) { FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, stake_b58 ); FD_LOG_ERR(( "invalid stake account %s", stake_b58 )); }
    1643           0 :       if( stake_state->stake_type!=FD_STAKE_STATE_STAKE ) continue;
    1644             : 
    1645           0 :       fd_stake_delegations_root_update(
    1646           0 :           stake_delegations,
    1647           0 :           &account->pubkey,
    1648           0 :           &stake_state->stake.stake.delegation.voter_pubkey,
    1649           0 :           stake_state->stake.stake.delegation.stake,
    1650           0 :           stake_state->stake.stake.delegation.activation_epoch,
    1651           0 :           stake_state->stake.stake.delegation.deactivation_epoch,
    1652           0 :           stake_state->stake.stake.credits_observed,
    1653           0 :           account->lamports,
    1654           0 :           (uint)account->data_len );
    1655           0 :     }
    1656           0 :   }
    1657             : 
    1658           0 :   fd_features_restore( &bank->f.features, accdb, bank->accdb_fork_id, bank->f.slot, &bank->f.epoch_schedule );
    1659             : 
    1660             :   /* https://github.com/anza-xyz/agave/blob/v4.3.0-beta.0/runtime/src/bank.rs#L6101-L6109 */
    1661           0 :   if( FD_UNLIKELY( FD_FEATURE_ACTIVE_BANK( bank, double_disinflation_rate ) ) ) {
    1662           0 :     fd_apply_double_disinflation_rate( bank );
    1663           0 :   }
    1664             : 
    1665             :   /* fd_refresh_vote_accounts is responsible for updating the vote
    1666             :      states with the total amount of active delegated stake.  It does
    1667             :      this by iterating over all active stake delegations and summing up
    1668             :      the amount of stake that is delegated to each vote account. */
    1669           0 :   ulong new_rate_activation_epoch = 0UL;
    1670             : 
    1671           0 :   {
    1672             :     /* Snapshot the stake history sysvar into a local buffer and release
    1673             :        the accdb bracket before calling fd_refresh_vote_accounts, which
    1674             :        performs its own accdb acquires.  fd_sysvar_stake_history_view
    1675             :        aliases the source bytes, so the bracket cannot be held open
    1676             :        across an inner acquire. */
    1677           0 :     uchar                stake_history_data[ FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ ];
    1678           0 :     fd_stake_history_t   stake_history_[1];
    1679           0 :     fd_stake_history_t * stake_history = NULL;
    1680           0 :     fd_acc_t ro = fd_accdb_read_one( accdb, bank->accdb_fork_id, fd_sysvar_stake_history_id.uc );
    1681           0 :     if( FD_LIKELY( ro.lamports ) ) {
    1682           0 :       ulong copy_sz = fd_ulong_min( ro.data_len, FD_SYSVAR_STAKE_HISTORY_BINCODE_SZ );
    1683           0 :       fd_memcpy( stake_history_data, ro.data, copy_sz );
    1684           0 :       fd_accdb_unread_one( accdb, &ro );
    1685           0 :       stake_history = fd_sysvar_stake_history_view( stake_history_, stake_history_data, copy_sz );
    1686           0 :     } else {
    1687           0 :       fd_accdb_unread_one( accdb, &ro );
    1688           0 :     }
    1689             : 
    1690           0 :     fd_refresh_vote_accounts( bank, accdb, runtime_stack, stake_delegations, stake_history, ULONG_MAX, &new_rate_activation_epoch );
    1691           0 :   }
    1692             : 
    1693             :   /* At genesis (epoch 0) there is no previous epoch, so the t-2 set
    1694             :      should equal the genesis staked set. */
    1695             : 
    1696           0 :   {
    1697           0 :     fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes( bank );
    1698           0 :     ushort             fork_id     = bank->vote_stakes_fork_id;
    1699           0 :     uchar __attribute__((aligned(FD_VOTE_STAKES_ITER_ALIGN))) iter_mem[ FD_VOTE_STAKES_ITER_FOOTPRINT ];
    1700           0 :     for( fd_vote_stakes_iter_t * iter = fd_vote_stakes_iter_init( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter_mem );
    1701           0 :          !fd_vote_stakes_iter_done( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter );
    1702           0 :          fd_vote_stakes_iter_next( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter ) ) {
    1703           0 :       fd_vote_stakes_ele_t ele[1];
    1704           0 :       fd_vote_stakes_iter_ele( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_1, iter, ele );
    1705           0 :       fd_vote_stakes_snap_insert_t_2( vote_stakes, fork_id, &ele->pubkey, &ele->node_account, ele->stake, ele->commission, ele->bls_key,
    1706           0 :                                       ele->block_revenue_commission_bps, ele->pending_delegator_rewards, &ele->inflation_collector, &ele->block_collector, &ele->location );
    1707           0 :     }
    1708           0 :     fd_vote_stakes_finalize( vote_stakes, fork_id, FD_VOTE_STAKES_ITER_T_2 );
    1709           0 :     fd_vote_stakes_refresh( vote_stakes, fork_id, accdb, bank->accdb_fork_id );
    1710           0 :   }
    1711             : 
    1712           0 :   bank->f.epoch = 0UL;
    1713           0 :   bank->f.capitalization = capitalization;
    1714           0 : }
    1715             : 
    1716             : static int
    1717             : fd_runtime_process_genesis_block( fd_bank_t *          bank,
    1718             :                                   fd_accdb_t *         accdb,
    1719             :                                   fd_capture_ctx_t *   capture_ctx,
    1720           0 :                                   fd_runtime_stack_t * runtime_stack ) {
    1721           0 :   bank->f.execution_fees = 0UL;
    1722           0 :   bank->f.priority_fees = 0UL;
    1723           0 :   bank->f.signature_count = 0UL;
    1724           0 :   bank->f.txn_count = 0UL;
    1725           0 :   bank->f.failed_txn_count = 0UL;
    1726           0 :   bank->f.nonvote_failed_txn_count = 0UL;
    1727           0 :   bank->f.total_compute_units_used = 0UL;
    1728             : 
    1729           0 :   fd_runtime_genesis_init_program( bank, accdb, capture_ctx );
    1730           0 :   fd_sysvar_slot_history_update( bank, accdb, capture_ctx );
    1731           0 :   fd_runtime_update_leaders( bank, runtime_stack );
    1732           0 :   fd_runtime_freeze( bank, accdb, capture_ctx );
    1733             : 
    1734           0 :   fd_hash_t const * prev_bank_hash = &bank->f.bank_hash;
    1735             : 
    1736           0 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
    1737             : 
    1738           0 :   fd_hash_t * bank_hash = &bank->f.bank_hash;
    1739           0 :   fd_hashes_hash_bank( lthash, prev_bank_hash, (fd_hash_t *)bank->f.poh.hash, 0UL, bank_hash );
    1740             : 
    1741           0 :   fd_bank_lthash_end_locking_query( bank );
    1742             : 
    1743           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    1744           0 : }
    1745             : 
    1746             : void
    1747             : fd_runtime_read_genesis( fd_banks_t *              banks,
    1748             :                          fd_bank_t *               bank,
    1749             :                          fd_accdb_t *              accdb,
    1750             :                          fd_capture_ctx_t *        capture_ctx,
    1751             :                          fd_hash_t const *         genesis_hash,
    1752             :                          fd_lthash_value_t const * genesis_lthash,
    1753             :                          fd_genesis_t const *      genesis,
    1754             :                          uchar const *             genesis_blob,
    1755           0 :                          fd_runtime_stack_t *      runtime_stack ) {
    1756           0 :   if( FD_UNLIKELY( genesis->poh.hashes_per_tick ) ) {
    1757           0 :     FD_LOG_ERR(( "unsupported genesis: Firedancer only supports Alpenglow consensus" ));
    1758           0 :   }
    1759             : 
    1760           0 :   fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
    1761           0 :   *lthash = *genesis_lthash;
    1762           0 :   fd_bank_lthash_end_locking_modify( bank );
    1763             : 
    1764             :   /* Once the accounts have been loaded from the genesis config into
    1765             :      the accounts db, we can initialize the bank state. This involves
    1766             :      setting some fields, and notably setting up the vote and stake
    1767             :      caches which are used for leader scheduling/rewards. */
    1768             : 
    1769           0 :   fd_runtime_init_bank_from_genesis( banks, bank, runtime_stack, accdb, genesis, genesis_blob, genesis_hash );
    1770             : 
    1771             :   /* Write the native programs to the accounts db. */
    1772             : 
    1773           0 :   for( ulong i=0UL; i<genesis->builtin_cnt; i++ ) {
    1774           0 :     fd_genesis_builtin_t builtin[1];
    1775           0 :     fd_genesis_builtin( genesis, genesis_blob, builtin, i );
    1776           0 :     fd_write_builtin_account( bank, accdb, capture_ctx, builtin->pubkey, builtin->data, builtin->data_len );
    1777           0 :   }
    1778             : 
    1779             :   /* At this point, state related to the bank and the accounts db
    1780             :      have been initialized and we are free to finish executing the
    1781             :      block. In practice, this updates some bank fields (notably the
    1782             :      poh and bank hash). */
    1783             : 
    1784           0 :   int err = fd_runtime_process_genesis_block( bank, accdb, capture_ctx, runtime_stack );
    1785           0 :   if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
    1786             : 
    1787             :   /* Genesis init ran the epoch-boundary refresh paths above; discard
    1788             :      the accumulated summary so the first real boundary's runtime_epoch
    1789             :      event only reflects itself. */
    1790           0 :   if( FD_UNLIKELY( fd_bank_report_runtime_diffs( bank ) ) ) fd_event_runtime_epoch_reset();
    1791           0 : }
    1792             : 
    1793             : /* valid_producer_time returns 1 iff an Alpenglow block footer's
    1794             :    producer_time_nanos is within valid bounds relative to the parent's
    1795             :    timestamp and can be safely represented as a long, and 0 if not. */
    1796             : 
    1797             : static inline int
    1798             : valid_producer_time( fd_bank_t const * bank,
    1799             :                      fd_accdb_t *      accdb,
    1800           3 :                      ulong             producer_time_nanos ) {
    1801           3 :   long lo, hi;
    1802           3 :   fd_alpenglow_producer_time_bounds( bank, accdb, &lo, &hi );
    1803           3 :   return producer_time_nanos<=(ulong)LONG_MAX && (long)producer_time_nanos>=lo && (long)producer_time_nanos<=hi;
    1804           3 : }
    1805             : 
    1806             : static int
    1807             : apply_footer( fd_bank_t *               bank,
    1808             :               fd_accdb_t *              accdb,
    1809             :               fd_capture_ctx_t *        capture_ctx,
    1810             :               fd_block_footer_t const * footer,
    1811             :               int                       verify,
    1812             :               ushort                    shred_version,
    1813           6 :               fd_footer_cache_t *       cache ) {
    1814             : 
    1815             :   /* leader bank's certs were verified by votor */
    1816           6 :   if( FD_UNLIKELY( verify && !bank->is_leader && fd_alpenglow_footer_verify( bank, footer, shred_version, cache ) ) ) return -1;
    1817             : 
    1818             :   /* Rewrite the clock sysvar and the alpenclock account from the
    1819             :      footer's producer timestamp (Agave Bank::update_clock_from_footer).
    1820             :      The clock must be applied before the reward certs. */
    1821             : 
    1822           3 :   ulong producer_time_nanos = footer->block_producer_time_nanos;
    1823           3 :   long  unix_timestamp = (long)(producer_time_nanos/1000000000UL);
    1824             : 
    1825           3 :   fd_sol_sysvar_clock_t clock_[1];
    1826           3 :   fd_sol_sysvar_clock_t * clock = fd_sysvar_clock_read( accdb, bank->accdb_fork_id, clock_ );
    1827           3 :   if( FD_UNLIKELY( !clock ) ) FD_LOG_ERR(( "fd_sysvar_clock_read failed" ));
    1828             : 
    1829           3 :   if( FD_UNLIKELY( !valid_producer_time( bank, accdb, producer_time_nanos ) ) ) return -1;
    1830             : 
    1831           3 :   fd_epoch_schedule_t const * epoch_schedule = &bank->f.epoch_schedule;
    1832           3 :   ulong current_epoch = fd_slot_to_epoch( epoch_schedule, bank->f.slot,        NULL );
    1833           3 :   ulong parent_epoch  = fd_slot_to_epoch( epoch_schedule, bank->f.parent_slot, NULL );
    1834             : 
    1835           3 :   long epoch_start_timestamp = clock->epoch_start_timestamp;
    1836           3 :   if( FD_UNLIKELY( bank->f.slot && parent_epoch!=current_epoch ) ) epoch_start_timestamp = unix_timestamp;
    1837             : 
    1838           3 :   fd_sol_sysvar_clock_t new_clock = {
    1839           3 :     .slot                  = bank->f.slot,
    1840           3 :     .epoch_start_timestamp = epoch_start_timestamp,
    1841           3 :     .epoch                 = current_epoch,
    1842           3 :     .leader_schedule_epoch = fd_slot_to_leader_schedule_epoch( epoch_schedule, bank->f.slot ),
    1843           3 :     .unix_timestamp        = unix_timestamp,
    1844           3 :   };
    1845           3 :   fd_sysvar_account_update( bank, accdb, capture_ctx, &fd_sysvar_clock_id, &new_clock, sizeof(fd_sol_sysvar_clock_t) );
    1846             : 
    1847             :   /* size the alpenclock balance with exactly the default rent, any
    1848             :      excess lamports must be burned */
    1849           3 :   fd_pubkey_t alpenclock_addr;
    1850           3 :   fd_alpenglow_pda( "alpenclock", &alpenclock_addr );
    1851           3 :   uchar data[ 8UL ];
    1852           3 :   FD_STORE( ulong, data, producer_time_nanos );
    1853           3 :   fd_accdb_svm_update_t update[1];
    1854           3 :   fd_acc_t acc = fd_accdb_svm_open_rw( bank, accdb, update, &alpenclock_addr, 1 );
    1855           3 :   acc.lamports = fd_rent_exempt_minimum_balance( &FD_RENT_DEFAULT_PARAMS, sizeof(data) );
    1856           3 :   fd_memcpy( acc.owner, fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) );
    1857           3 :   acc.executable = 0;
    1858           3 :   acc.data_len   = sizeof(data);
    1859           3 :   fd_memcpy( acc.data, data, sizeof(data) );
    1860           3 :   fd_accdb_svm_close_rw( bank, accdb, capture_ctx, &acc, update );
    1861             : 
    1862           3 :   return fd_alpenglow_rewards_apply( bank, accdb, capture_ctx, cache, footer );
    1863           3 : }
    1864             : 
    1865             : int
    1866             : fd_runtime_block_execute_finalize( fd_bank_t *               bank,
    1867             :                                    fd_accdb_t *              accdb,
    1868             :                                    fd_capture_ctx_t *        capture_ctx,
    1869             :                                    fd_block_footer_t const * footer,
    1870             :                                    ushort                    shred_version,
    1871         402 :                                    fd_footer_cache_t *       cache ) {
    1872         402 :   if( FD_UNLIKELY( footer && apply_footer( bank, accdb, capture_ctx, footer, 1, shred_version, cache ) ) ) return -1;
    1873         399 :   fd_runtime_freeze( bank, accdb, capture_ctx );
    1874         399 :   fd_runtime_update_bank_hash( bank, capture_ctx );
    1875         399 :   return 0;
    1876         402 : }
    1877             : 
    1878             : int
    1879             : fd_runtime_block_execute_finalize_unverified( fd_bank_t *               bank,
    1880             :                                               fd_accdb_t *              accdb,
    1881             :                                               fd_block_footer_t const * footer,
    1882           3 :                                               fd_footer_cache_t *       cache ) {
    1883           3 :   FD_TEST( footer && !bank->is_leader && !fd_bank_report_runtime_diffs( bank ) );
    1884           3 :   if( FD_UNLIKELY( apply_footer( bank, accdb, NULL, footer, 0, 0, cache ) ) ) return -1;
    1885           3 :   fd_runtime_freeze( bank, accdb, NULL );
    1886           3 :   fd_runtime_update_bank_hash( bank, NULL );
    1887           3 :   return 0;
    1888           3 : }
    1889             : 
    1890             : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
    1891             : 
    1892             :    Backward scan over transaction accounts. Returns ULONG_MAX if not found.
    1893             : 
    1894             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
    1895             : 
    1896             : ulong
    1897             : fd_runtime_find_index_of_account( fd_txn_out_t const * txn_out,
    1898       13899 :                                   fd_pubkey_t const *  pubkey ) {
    1899       34158 :   for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
    1900       31062 :     if( FD_UNLIKELY( !memcmp( pubkey, &txn_out->accounts.keys[ txn_out->accounts.cnt-1UL-i ], sizeof(fd_pubkey_t) ) ) ) return txn_out->accounts.cnt-1UL-i;
    1901       31062 :   }
    1902        3096 :   return ULONG_MAX;
    1903       13899 : }
    1904             : 
    1905             : fd_acc_t *
    1906             : fd_runtime_get_account_at_index( fd_txn_in_t const *             txn_in,
    1907             :                                  fd_txn_out_t *                  txn_out,
    1908             :                                  ushort                          idx,
    1909       33339 :                                  fd_txn_account_condition_fn_t * condition ) {
    1910       33339 :   if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) return NULL;
    1911       33339 :   if( FD_LIKELY( condition && !condition( txn_in, txn_out, idx ) ) ) return NULL;
    1912       33183 :   return txn_out->accounts.account[ idx ];
    1913       33339 : }
    1914             : 
    1915             : fd_acc_t *
    1916             : fd_runtime_get_executable_account( fd_txn_out_t *      txn_out,
    1917             :                                    fd_pubkey_t const * pubkey,
    1918             :                                    int *               from_parent_copy,
    1919          72 :                                    int *               pd_write_this_slot ) {
    1920             :   /* First try to fetch the executable account from the existing
    1921             :      borrowed accounts.  If the pubkey is in the account keys, then we
    1922             :      want to re-use that borrowed account since it reflects changes from
    1923             :      prior instructions.  Referencing the read-only executable accounts
    1924             :      list is incorrect behavior when the program data account is written
    1925             :      to in a prior instruction (e.g. program upgrade + invoke within the
    1926             :      same txn) */
    1927             : 
    1928          72 :   if( FD_UNLIKELY( from_parent_copy   ) ) *from_parent_copy   = 0;
    1929          72 :   if( FD_UNLIKELY( pd_write_this_slot ) ) *pd_write_this_slot = 0;
    1930             : 
    1931          72 :   ulong account_idx = fd_runtime_find_index_of_account( txn_out, pubkey );
    1932          72 :   if( FD_LIKELY( account_idx!=ULONG_MAX && txn_out->accounts.account[ account_idx ]->lamports ) ) return txn_out->accounts.account[ account_idx ];
    1933             : 
    1934          69 :   for( ushort i=0; i<txn_out->accounts.executable_cnt; i++ ) {
    1935          36 :     fd_acc_t * ro = txn_out->accounts.executable[ i ];
    1936          36 :     if( FD_UNLIKELY( !memcmp( pubkey->uc, ro->pubkey, 32UL ) ) ) {
    1937          36 :       if( FD_UNLIKELY( !ro->lamports ) ) return NULL;
    1938          33 :       if( FD_UNLIKELY( from_parent_copy   ) ) *from_parent_copy   = txn_out->accounts.executable_from_parent[ i ];
    1939          33 :       if( FD_UNLIKELY( pd_write_this_slot ) ) *pd_write_this_slot = txn_out->accounts.executable_pd_write[ i ];
    1940          33 :       return ro;
    1941          36 :     }
    1942          36 :   }
    1943             : 
    1944          33 :   return NULL;
    1945          69 : }
    1946             : 
    1947             : int
    1948             : fd_runtime_get_key_of_account_at_index( fd_txn_out_t *        txn_out,
    1949             :                                         ushort                idx,
    1950       26391 :                                         fd_pubkey_t const * * key ) {
    1951             :   /* Return a MissingAccount error if idx is out of bounds.
    1952             :      https://github.com/anza-xyz/agave/blob/v3.1.4/transaction-context/src/lib.rs#L187 */
    1953       26391 :   if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) {
    1954           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
    1955           0 :   }
    1956             : 
    1957       26391 :   *key = &txn_out->accounts.keys[ idx ];
    1958       26391 :   return FD_EXECUTOR_INSTR_SUCCESS;
    1959       26391 : }
    1960             : 
    1961             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
    1962             : int
    1963             : fd_txn_account_is_demotion( const int        idx,
    1964             :                             const fd_txn_t * txn_descriptor,
    1965        2709 :                             const uint       bpf_upgradeable_in_txn ) {
    1966        2709 :   uint is_program = 0U;
    1967        5094 :   for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
    1968        2391 :     if( txn_descriptor->instr[j].program_id == idx ) {
    1969           6 :       is_program = 1U;
    1970           6 :       break;
    1971           6 :     }
    1972        2391 :   }
    1973             : 
    1974        2709 :   return (is_program && !bpf_upgradeable_in_txn);
    1975        2709 : }
    1976             : 
    1977             : uint
    1978             : fd_txn_account_has_bpf_loader_upgradeable( fd_pubkey_t const * account_keys,
    1979        4200 :                                            ulong               accounts_cnt ) {
    1980       25893 :   for( ulong j=0; j<accounts_cnt; j++ ) {
    1981       22062 :     const fd_pubkey_t * acc = &account_keys[j];
    1982       22062 :     if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
    1983         369 :       return 1U;
    1984         369 :     }
    1985       22062 :   }
    1986        3831 :   return 0U;
    1987        4200 : }
    1988             : 
    1989             : static inline int
    1990             : fd_runtime_account_is_writable_idx_flat( const ushort        idx,
    1991             :                                          const fd_pubkey_t * addr_at_idx,
    1992             :                                          const fd_txn_t *    txn_descriptor,
    1993        5070 :                                          const uint          bpf_upgradeable_in_txn ) {
    1994             :   /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
    1995        5070 :   if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
    1996        2352 :     return 0;
    1997        2352 :   }
    1998             : 
    1999             :   /* See comments in fd_system_ids.h.
    2000             :      https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
    2001        2718 :   if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
    2002        2718 :       fd_pubkey_is_pending_reserved_key( addr_at_idx ) ) {
    2003             : 
    2004           9 :     return 0;
    2005           9 :   }
    2006             : 
    2007        2709 :   if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
    2008           6 :     return 0;
    2009           6 :   }
    2010             : 
    2011        2703 :   return 1;
    2012        2709 : }
    2013             : 
    2014             : 
    2015             : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
    2016             :    to determine if accounts are writable or not.
    2017             : 
    2018             :    https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
    2019             : int
    2020             : fd_runtime_account_is_writable_idx( fd_txn_in_t const *  txn_in,
    2021             :                                     fd_txn_out_t const * txn_out,
    2022        4008 :                                     ushort               idx ) {
    2023        4008 :   uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_out->accounts.keys, txn_out->accounts.cnt );
    2024        4008 :   return fd_runtime_account_is_writable_idx_flat( idx,
    2025        4008 :                                                    &txn_out->accounts.keys[idx],
    2026        4008 :                                                    TXN( txn_in->txn ),
    2027        4008 :                                                    bpf_upgradeable );
    2028        4008 : }
    2029             : 
    2030             : /* Account pre-condition filtering functions */
    2031             : 
    2032             : int
    2033             : fd_runtime_account_check_exists( fd_txn_in_t const * txn_in,
    2034             :                                  fd_txn_out_t *      txn_out,
    2035        3177 :                                  ushort              idx ) {
    2036        3177 :   (void) txn_in;
    2037        3177 :   return txn_out->accounts.account[ idx ]->lamports!=0UL;
    2038        3177 : }
    2039             : 
    2040             : int
    2041             : fd_runtime_account_check_fee_payer_writable( fd_txn_in_t const * txn_in,
    2042             :                                              fd_txn_out_t *      txn_out,
    2043         699 :                                              ushort              idx ) {
    2044         699 :   (void) txn_out;
    2045         699 :   return fd_txn_is_writable( TXN( txn_in->txn ), idx );
    2046         699 : }
    2047             : 
    2048             : 
    2049             : int
    2050             : fd_account_meta_checked_sub_lamports( fd_acc_t * acc,
    2051         588 :                                       ulong      lamports ) {
    2052         588 :   ulong balance_post = 0UL;
    2053         588 :   int err = fd_ulong_checked_sub( acc->lamports, lamports, &balance_post );
    2054         588 :   if( FD_UNLIKELY( err ) ) return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
    2055             : 
    2056         588 :   acc->lamports = balance_post;
    2057         588 :   return FD_EXECUTOR_INSTR_SUCCESS;
    2058         588 : }
    2059             : 
    2060             : /* fd_executor_reuse_bundle_executable scans the runtime's deduplicated
    2061             :    account pools for a programdata account matching programdata_key that
    2062             :    was already opened by an earlier transaction in the bundle (either as
    2063             :    a regular transaction account or as a programdata account).  Returns
    2064             :    the existing fd_acc_t so it can be reused instead of re-acquiring it,
    2065             :    or NULL if none is found.  Must only be called for bundle txns. */
    2066             : 
    2067             : static fd_acc_t *
    2068             : reuse_bundle_executable( fd_runtime_t *      runtime,
    2069          39 :                          fd_pubkey_t const * programdata_key ) {
    2070         114 :   for( ulong j=0UL; j<runtime->accounts.account_cnt; j++ ) {
    2071          96 :     if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &runtime->accounts.account[ j ].pubkey ), programdata_key ) ) ) continue;
    2072          21 :     return &runtime->accounts.account[ j ];
    2073          96 :   }
    2074          18 :   for( ulong j=0UL; j<runtime->accounts.executable_cnt; j++ ) {
    2075           6 :     if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &runtime->accounts.executable[ j ].pubkey ), programdata_key ) ) ) continue;
    2076           6 :     return &runtime->accounts.executable[ j ];
    2077           6 :   }
    2078          12 :   return NULL;
    2079          18 : }
    2080             : 
    2081             : int
    2082             : fd_runtime_prepare_bundle_accounts( fd_runtime_t *      runtime,
    2083             :                                     fd_bank_t *         bank,
    2084             :                                     fd_txn_in_t const * txn_ins,
    2085             :                                     fd_txn_out_t *      txn_outs,
    2086          96 :                                     ulong               txn_cnt ) {
    2087             : 
    2088          96 : # define FD_BUNDLE_ACCT_MAX (FD_PACK_MAX_TXN_PER_BUNDLE*MAX_TX_ACCOUNT_LOCKS)
    2089             : 
    2090          96 :   runtime->accounts.account_cnt    = 0UL;
    2091          96 :   runtime->accounts.executable_cnt = 0UL;
    2092             : 
    2093          96 :   fd_runtime_sysvar_cache_refresh( runtime, bank );
    2094             : 
    2095          96 :   uchar const * acquire_pubkeys[ FD_BUNDLE_ACCT_MAX ];
    2096          96 :   int           acquire_writable[ FD_BUNDLE_ACCT_MAX ];
    2097          96 :   ulong         acquire_cnt = 0UL;
    2098             : 
    2099             :   /* First resolve a deduped set of account keys for all txns in the
    2100             :     bundle.  This includes static account keys as well as ALUT resolved
    2101             :     addresses. */
    2102             : 
    2103         288 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
    2104         204 :     fd_txn_in_t const * txn_in  = &txn_ins [ i ];
    2105         204 :     fd_txn_out_t *      txn_out = &txn_outs[ i ];
    2106             : 
    2107         204 :     txn_out->accounts.cnt = (uchar)TXN( txn_in->txn )->acct_addr_cnt;
    2108         204 :     fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->acct_addr_off);
    2109        1296 :     for( ulong j=0UL; j<TXN( txn_in->txn )->acct_addr_cnt; j++ ) {
    2110        1092 :       txn_out->accounts.keys[ j ]             = tx_accs[ j ];
    2111        1092 :       txn_out->accounts.account[ j ]          = NULL;
    2112        1092 :       txn_out->accounts.account_acquired[ j ] = 0U;
    2113        1092 :     }
    2114             : 
    2115         204 :     int err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
    2116         204 :     for( ulong j=TXN( txn_in->txn )->acct_addr_cnt; j<txn_out->accounts.cnt; j++ ) {
    2117           0 :       txn_out->accounts.account[ j ]          = NULL;
    2118           0 :       txn_out->accounts.account_acquired[ j ] = 0U;
    2119           0 :     }
    2120             :     /* Validate account locks before the union acquire below, bounding
    2121             :        the deduped set within the accdb acquire limit. */
    2122         204 :     if( FD_LIKELY( err==FD_RUNTIME_EXECUTE_SUCCESS ) ) err = fd_executor_validate_account_locks( txn_out );
    2123         204 :     if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
    2124          12 :       txn_out->err.txn_err = err;
    2125          12 :       return err;
    2126          12 :     }
    2127             : 
    2128         192 :     uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_out->accounts.keys, txn_out->accounts.cnt );
    2129        1254 :     for( ushort j=0; j<txn_out->accounts.cnt; j++ ) {
    2130        1062 :       fd_pubkey_t const * key = &txn_out->accounts.keys[ j ];
    2131        1062 :       int writable = fd_runtime_account_is_writable_idx_flat( j, key, TXN( txn_in->txn ), bpf_upgradeable );
    2132        1062 :       int dup = 0;
    2133        4725 :       for( ulong k=0UL; k<acquire_cnt; k++ ) if( FD_UNLIKELY( !memcmp( acquire_pubkeys[ k ], key->uc, 32UL ) ) ) { acquire_writable[ k ] |= writable; dup = 1; break; }
    2134        1062 :       if( FD_UNLIKELY( dup ) ) continue;
    2135         546 :       FD_TEST( acquire_cnt<FD_BUNDLE_ACCT_MAX );
    2136         546 :       acquire_pubkeys [ acquire_cnt ] = key->uc;
    2137             :       /* Acquire writable if any bundle member can write this account. */
    2138         546 :       acquire_writable[ acquire_cnt ] = writable;
    2139         546 :       acquire_cnt++;
    2140         546 :     }
    2141         192 :   }
    2142             : 
    2143          84 :   if( FD_LIKELY( acquire_cnt ) ) {
    2144          84 :     fd_accdb_acquire_a( runtime->accdb, bank->accdb_fork_id, acquire_cnt, acquire_pubkeys, acquire_writable, runtime->accounts.account );
    2145          84 :     runtime->accounts.account_cnt = acquire_cnt;
    2146          84 :   }
    2147             : 
    2148         264 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
    2149         180 :     fd_txn_out_t * txn_out = &txn_outs[ i ];
    2150        1206 :     for( ushort j=0; j<txn_out->accounts.cnt; j++ ) {
    2151        1026 :       txn_out->accounts.account[ j ] = NULL;
    2152        4647 :       for( ulong k=0UL; k<runtime->accounts.account_cnt; k++ ) {
    2153        4647 :         fd_acc_t * acc = &runtime->accounts.account[ k ];
    2154        4647 :         if( FD_LIKELY( !fd_pubkey_eq( fd_type_pun_const( &acc->pubkey ), &txn_out->accounts.keys[ j ] ) ) ) continue;
    2155        1026 :         txn_out->accounts.account[ j ]           = acc;
    2156        1026 :         txn_out->accounts.starting_lamports[ j ] = acc->prior_lamports;
    2157        1026 :         txn_out->accounts.starting_data_len[ j ] = acc->prior_data_len;
    2158        1026 :         memcpy( &txn_out->accounts.starting_owner[ j ], acc->prior_owner, sizeof(fd_pubkey_t) );
    2159        1026 :         break;
    2160        4647 :       }
    2161        1026 :     }
    2162         180 :   }
    2163             : 
    2164             :   /* Do the same for executable accounts (programdata accounts).  Dedup
    2165             :     against all other executable-only accounts as well as ones that
    2166             :     were already included. */
    2167             : 
    2168          84 :   fd_pubkey_t   programdata_keys[ FD_BUNDLE_ACCT_MAX ];
    2169          84 :   uchar const * pd_pubkeys      [ FD_BUNDLE_ACCT_MAX ];
    2170          84 :   ulong         pd_cnt = 0UL;
    2171             : 
    2172          84 :   FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
    2173             : 
    2174         600 :   for( ulong i=0UL; i<runtime->accounts.account_cnt; i++ ) {
    2175         516 :     fd_acc_t * acc = &runtime->accounts.account[ i ];
    2176         516 :     if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
    2177          39 :     fd_bpf_state_t program_loader_state[1];
    2178          39 :     if( FD_UNLIKELY( fd_bpf_loader_program_get_state( acc, program_loader_state )!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
    2179          39 :     if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
    2180             : 
    2181          24 :     fd_pubkey_t const * programdata_key = &program_loader_state->inner.program.programdata_address;
    2182             :     /* PD is not live as of the parent, so there is nothing to acquire
    2183             :        from the parent.  We will still need its latest length for loaded
    2184             :        accounts data size, but that is taken care of per transaction by
    2185             :        fd_runtime_setup_bundle_executables() without acquiring. */
    2186          24 :     if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) continue;
    2187             : 
    2188             :     /* Already part of the transaction account pool, or already queued. */
    2189          12 :     if( reuse_bundle_executable( runtime, programdata_key ) ) continue;
    2190           6 :     int dup = 0;
    2191           6 :     for( ulong u=0UL; u<pd_cnt; u++ ) if( FD_UNLIKELY( !memcmp( programdata_keys[ u ].uc, programdata_key->uc, 32UL ) ) ) { dup = 1; break; }
    2192           6 :     if( dup ) continue;
    2193             : 
    2194           6 :     FD_TEST( pd_cnt<FD_BUNDLE_ACCT_MAX );
    2195           6 :     programdata_keys[ pd_cnt ] = *programdata_key;
    2196           6 :     pd_pubkeys[ pd_cnt ]       = programdata_keys[ pd_cnt ].uc;
    2197           6 :     pd_cnt++;
    2198           6 :   }
    2199             : 
    2200             :   /* acquire_b refunds the per-class reservations acquire_a made for the
    2201             :     union (reserved_cnt==acquire_cnt) that did not turn out to be
    2202             :     programdata.  Skip it entirely for an empty bundle (nothing was
    2203             :     reserved and nothing is executable). */
    2204          84 :   if( FD_LIKELY( acquire_cnt || pd_cnt ) ) {
    2205          84 :     fd_accdb_acquire_b( runtime->accdb, bank->parent_accdb_fork_id, acquire_cnt, pd_cnt, pd_pubkeys, runtime->accounts.executable );
    2206          84 :   }
    2207          84 :   runtime->accounts.executable_cnt = pd_cnt;
    2208             : 
    2209          84 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    2210             : 
    2211          84 : # undef FD_BUNDLE_ACCT_MAX
    2212          84 : }
    2213             : 
    2214             : int
    2215             : fd_runtime_setup_bundle_executables( fd_runtime_t * runtime,
    2216             :                                      fd_bank_t *    bank,
    2217         159 :                                      fd_txn_out_t * txn_out ) {
    2218         159 :   FD_TEST( bank->parent_accdb_fork_id.val!=USHORT_MAX );
    2219             : 
    2220         159 :   ushort exe_cnt = 0;
    2221         159 :   txn_out->accounts.executable_skipped_cnt = 0;
    2222        1128 :   for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
    2223             :     /* The checks below mimic the ones in the non-bundle path.  The
    2224             :        difference is only the source of state as of this transaction.
    2225             :        Here it's from the live pool when possible, and in the non-bundle
    2226             :        path it's from the accdb. */
    2227         969 :     fd_acc_t * acc = txn_out->accounts.account[ i ]; /* This has gone through zero-lamport reset by fd_executor_setup_accounts_for_txn_bundle(). */
    2228         969 :     if( FD_LIKELY( memcmp( acc->owner, fd_solana_bpf_loader_upgradeable_program_id.key, 32UL ) ) ) continue;
    2229          51 :     fd_bpf_state_t program_loader_state[1];
    2230          51 :     if( FD_UNLIKELY( fd_bpf_loader_program_get_state( acc, program_loader_state )!=FD_EXECUTOR_INSTR_SUCCESS ) ) continue;
    2231          51 :     if( FD_UNLIKELY( program_loader_state->discriminant!=FD_BPF_STATE_PROGRAM ) ) continue;
    2232             : 
    2233          36 :     fd_pubkey_t const * programdata_key = &program_loader_state->inner.program.programdata_address;
    2234             : 
    2235             :     /* Declared PD is charged as a plain top-level transaction account
    2236             :        and is preferred by fd_runtime_get_executable_account(). */
    2237          36 :     if( FD_UNLIKELY( fd_runtime_find_index_of_account( txn_out, programdata_key )!=ULONG_MAX ) ) continue;
    2238             : 
    2239          27 :     fd_acc_t * programdata = reuse_bundle_executable( runtime, programdata_key );
    2240             : 
    2241          27 :     if( FD_UNLIKELY( !fd_accdb_exists( runtime->accdb, bank->parent_accdb_fork_id, programdata_key->uc ) ) ) {
    2242             :       /* PD is not live as of the parent.  Length only, and no
    2243             :          executable[] entry, so the loader reports "Program is not
    2244             :          deployed" exactly as the non-bundle path does.  The non-bundle
    2245             :          path takes the length from the latest accdb view committed on
    2246             :          this fork.  The equivalent here is the live pool copy, which an
    2247             :          earlier bundle transaction may have created, resized or
    2248             :          drained.  Only a LIVE PD is recorded.  Membership in the
    2249             :          skipped list is the liveness answer, so a dead PD is left out
    2250             :          entirely rather than recorded with a zero length: a live PD may
    2251             :          itself have zero-length data, and Agave still charges the
    2252             :          64-byte base for that. */
    2253          15 :       ulong skip_len;
    2254          15 :       if( FD_LIKELY( programdata ) ) {
    2255             :         /* A dead PD must contribute nothing to loaded accounts data
    2256             :            size.  Like every other consumer, the lamports need to be
    2257             :            checked first.  The pool copy that programdata points to has
    2258             :            an up to date lamports, but it is not necessarily otherwise
    2259             :            normalized: zero length, non-executable, and system-owned.
    2260             :            The reset in setup_accounts_for_txn_bundle() only covers the
    2261             :            declared accounts of the transaction.  If PD were declared,
    2262             :            it would have short circuited above.  So an in-bundle Close
    2263             :            leaves PD here at zero lamports with length at
    2264             :            SIZE_OF_UNINITIALIZED. */
    2265           9 :         if( FD_UNLIKELY( !programdata->lamports ) ) continue;
    2266           6 :         skip_len = programdata->data_len;
    2267           6 :       } else {
    2268             :         /* At this point, this PD
    2269             :            - has no pool copy, so no bundle transaction declares PD, and
    2270             :            - it is not live on the parent.
    2271             : 
    2272             :            So PD was created this slot by something outside the bundle,
    2273             :            and only the latest accdb view has the length we need for
    2274             :            accounting.  It may also have been created and closed within
    2275             :            this slot, in which case it is dead and contributes nothing. */
    2276           6 :         int   probe_pd  = 0;
    2277           6 :         ulong probe_len = 0UL;
    2278           6 :         ulong probe_lamports = 0UL;
    2279           6 :         if( FD_UNLIKELY( !fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, programdata_key->uc, &probe_pd, &probe_len, &probe_lamports ) ) ) continue;
    2280           6 :         if( FD_UNLIKELY( !probe_lamports ) ) continue;
    2281           3 :         skip_len = probe_len;
    2282           3 :       }
    2283           9 :       ushort s = txn_out->accounts.executable_skipped_cnt++;
    2284           9 :       txn_out->accounts.executable_skipped_key[ s ] = *programdata_key;
    2285           9 :       txn_out->accounts.executable_skipped_len[ s ] = skip_len;
    2286           9 :       continue;
    2287          15 :     }
    2288             : 
    2289             :     /* At this point, PD exists in the parent.  So prepare() must have
    2290             :        acquired PD, either in accounts[] if a bundle transaction
    2291             :        declares it, or in executable[] aka implied load from the parent.
    2292             :        A miss would imply that a pool account only began parsing as
    2293             :        Program{PD} after prepare() computed the acquire set, naming a PD
    2294             :        no bundle transaction declares.  This should not be possible,
    2295             :        because only the DeployWithMaxDataLen instruction writes
    2296             :        Program{PD}.  The instruction declares PD, and its CreateAccount
    2297             :        CPI cannot succeed against a PD that is already live on the
    2298             :        parent.  We cannot acquire the PD at this point, since a
    2299             :        mid-bundle acquire of an account the bundle might be holding as
    2300             :        writable would violate the accdb acquire contract.  So fail the
    2301             :        bundle. */
    2302          12 :     if( FD_UNLIKELY( !programdata ) ) {
    2303           0 :       FD_BASE58_ENCODE_32_BYTES( programdata_key->uc, pd_str );
    2304           0 :       FD_BASE58_ENCODE_64_BYTES( txn_out->details.signature.uc, sig_str );
    2305           0 :       FD_LOG_INFO(( "dropping bundle: bundle txn %s in slot %lu implies unacquired programdata %s", sig_str, bank->f.slot, pd_str ));
    2306           0 :       return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
    2307           0 :     }
    2308             : 
    2309          12 :     int from_parent = ( programdata>=runtime->accounts.executable ) &&
    2310          12 :                       ( programdata< runtime->accounts.executable+runtime->accounts.executable_cnt ) &&
    2311          12 :                       ( bank->parent_accdb_fork_id.val!=bank->accdb_fork_id.val );
    2312          12 :     txn_out->accounts.executable[ exe_cnt ]             = programdata;
    2313          12 :     txn_out->accounts.executable_from_parent[ exe_cnt ] = from_parent;
    2314          12 :     if( from_parent ) {
    2315             :       /* A from_parent copy is one no bundle transaction declared, hence
    2316             :          one no bundle transaction could have modified, because mutating
    2317             :          PD requires declaring it writable.  So the probe from accdb
    2318             :          reports the latest state that we need. */
    2319           6 :       int   pd  = 0;
    2320           6 :       ulong len = ULONG_MAX;
    2321           6 :       ulong lamports = 0UL;
    2322           6 :       fd_accdb_probe_pd_this_fork( runtime->accdb, bank->accdb_fork_id, programdata_key->uc, &pd, &len, &lamports );
    2323           6 :       txn_out->accounts.executable_pd_write    [ exe_cnt ] = pd;
    2324           6 :       txn_out->accounts.executable_cur_len     [ exe_cnt ] = len;
    2325           6 :       txn_out->accounts.executable_cur_lamports[ exe_cnt ] = lamports;
    2326           6 :     } else {
    2327           6 :       txn_out->accounts.executable_pd_write    [ exe_cnt ] = 0;
    2328           6 :       txn_out->accounts.executable_cur_len     [ exe_cnt ] = ULONG_MAX;
    2329           6 :       txn_out->accounts.executable_cur_lamports[ exe_cnt ] = 0UL;
    2330           6 :     }
    2331          12 :     exe_cnt++;
    2332          12 :   }
    2333         159 :   txn_out->accounts.executable_cnt = exe_cnt;
    2334         159 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    2335         159 : }

Generated by: LCOV version 1.14