LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 795 1144 69.5 %
Date: 2026-07-18 05:25:05 Functions: 39 46 84.8 %

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

Generated by: LCOV version 1.14