LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 822 1172 70.1 %
Date: 2026-08-29 04:30:01 Functions: 41 49 83.7 %

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

Generated by: LCOV version 1.14