LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 463 1013 45.7 %
Date: 2026-03-16 06:26:59 Functions: 32 45 71.1 %

          Line data    Source code
       1             : #include "fd_runtime.h"
       2             : #include "../capture/fd_capture_ctx.h"
       3             : #include "../types/fd_cast.h"
       4             : #include "fd_alut_interp.h"
       5             : #include "fd_bank.h"
       6             : #include "fd_executor_err.h"
       7             : #include "fd_hashes.h"
       8             : #include "fd_runtime_err.h"
       9             : #include "fd_runtime_stack.h"
      10             : #include "fd_acc_pool.h"
      11             : #include "fd_genesis_parse.h"
      12             : #include "fd_executor.h"
      13             : #include "sysvar/fd_sysvar_cache.h"
      14             : #include "sysvar/fd_sysvar_clock.h"
      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             : 
      19             : #include "../stakes/fd_stakes.h"
      20             : #include "../rewards/fd_rewards.h"
      21             : #include "../accdb/fd_accdb_sync.h"
      22             : #include "../progcache/fd_progcache_user.h"
      23             : 
      24             : #include "program/fd_stake_program.h"
      25             : #include "program/fd_builtin_programs.h"
      26             : #include "program/fd_precompiles.h"
      27             : #include "program/fd_program_util.h"
      28             : #include "program/vote/fd_vote_state_versioned.h"
      29             : 
      30             : #include "sysvar/fd_sysvar_clock.h"
      31             : #include "sysvar/fd_sysvar_last_restart_slot.h"
      32             : #include "sysvar/fd_sysvar_recent_hashes.h"
      33             : #include "sysvar/fd_sysvar_rent.h"
      34             : #include "sysvar/fd_sysvar_slot_hashes.h"
      35             : #include "sysvar/fd_sysvar_slot_history.h"
      36             : 
      37             : #include "tests/fd_dump_pb.h"
      38             : 
      39             : #include "fd_system_ids.h"
      40             : 
      41             : #include "../../disco/pack/fd_pack.h"
      42             : #include "../../disco/pack/fd_pack_tip_prog_blacklist.h"
      43             : 
      44             : #include <unistd.h>
      45             : #include <sys/stat.h>
      46             : #include <sys/types.h>
      47             : #include <fcntl.h>
      48             : 
      49             : /******************************************************************************/
      50             : /* Public Runtime Helpers                                                     */
      51             : /******************************************************************************/
      52             : 
      53             : 
      54             : /*
      55             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1254-L1258
      56             :    https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/bank.rs#L1749
      57             :  */
      58             : int
      59             : fd_runtime_compute_max_tick_height( ulong   ticks_per_slot,
      60             :                                     ulong   slot,
      61           0 :                                     ulong * out_max_tick_height /* out */ ) {
      62           0 :   ulong max_tick_height = 0UL;
      63           0 :   if( FD_LIKELY( ticks_per_slot > 0UL ) ) {
      64           0 :     ulong next_slot = fd_ulong_sat_add( slot, 1UL );
      65           0 :     if( FD_UNLIKELY( next_slot == slot ) ) {
      66           0 :       FD_LOG_WARNING(( "max tick height addition overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      67           0 :       return -1;
      68           0 :     }
      69           0 :     if( FD_UNLIKELY( ULONG_MAX / ticks_per_slot < next_slot ) ) {
      70           0 :       FD_LOG_WARNING(( "max tick height multiplication overflowed slot %lu ticks_per_slot %lu", slot, ticks_per_slot ));
      71           0 :       return -1;
      72           0 :     }
      73           0 :     max_tick_height = fd_ulong_sat_mul( next_slot, ticks_per_slot );
      74           0 :   }
      75           0 :   *out_max_tick_height = max_tick_height;
      76           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
      77           0 : }
      78             : 
      79             : /* Returns whether the specified epoch should use the new vote account
      80             :    keyed leader schedule (returns 1) or the old validator identity keyed
      81             :    leader schedule (returns 0). See SIMD-0180.  This is the analogous to
      82             :    Agave's Bank::should_use_vote_keyed_leader_schedule():
      83             :    https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6148 */
      84             : 
      85             : static int
      86           9 : fd_runtime_should_use_vote_keyed_leader_schedule( fd_bank_t * bank ) {
      87             :   /* Agave uses an option type for their effective_epoch value. We
      88             :      represent None as ULONG_MAX and Some(value) as the value.
      89             :      https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6149-L6165 */
      90           9 :   if( FD_FEATURE_ACTIVE_BANK( bank, enable_vote_address_leader_schedule ) ) {
      91             :     /* Return the first epoch if activated at genesis
      92             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6153-L6157 */
      93           0 :     ulong activation_slot = fd_bank_features_query( bank )->enable_vote_address_leader_schedule;
      94           0 :     if( activation_slot==0UL ) return 1; /* effective_epoch=0, current_epoch >= effective_epoch always true */
      95             : 
      96             :     /* Calculate the epoch that the feature became activated in
      97             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6159-L6160 */
      98           0 :     fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
      99           0 :     ulong activation_epoch = fd_slot_to_epoch( epoch_schedule, activation_slot, NULL );
     100             : 
     101             :     /* The effective epoch is the epoch immediately after the activation
     102             :        epoch.
     103             :        https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6162-L6164 */
     104           0 :     ulong effective_epoch = activation_epoch + 1UL;
     105           0 :     ulong current_epoch   = fd_bank_epoch_get( bank );
     106             : 
     107             :     /* https://github.com/anza-xyz/agave/blob/v2.3.1/runtime/src/bank.rs#L6167-L6170 */
     108           0 :     return !!( current_epoch >= effective_epoch );
     109           0 :   }
     110             : 
     111             :   /* ...The rest of the logic in this function either returns None or
     112             :      Some(false) so we will just return 0 by default. */
     113           9 :   return 0;
     114           9 : }
     115             : 
     116             : void
     117             : fd_runtime_update_leaders( fd_bank_t *          bank,
     118           9 :                            fd_runtime_stack_t * runtime_stack ) {
     119             : 
     120           9 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     121             : 
     122           9 :   ulong epoch    = fd_slot_to_epoch ( epoch_schedule, fd_bank_slot_get( bank ), NULL );
     123           9 :   ulong slot0    = fd_epoch_slot0   ( epoch_schedule, epoch );
     124           9 :   ulong slot_cnt = fd_epoch_slot_cnt( epoch_schedule, epoch );
     125             : 
     126           9 :   fd_vote_stakes_t *       vote_stakes      = fd_bank_vote_stakes_locking_modify( bank );
     127           9 :   fd_vote_stake_weight_t * epoch_weights    = runtime_stack->stakes.stake_weights;
     128           9 :   ulong                    stake_weight_cnt = fd_stake_weights_by_node( vote_stakes, bank->data->vote_stakes_fork_id, epoch_weights );
     129           9 :   fd_bank_vote_stakes_end_locking_modify( bank );
     130             : 
     131             :   /* TODO: Can optimize by avoiding recomputing if another fork has
     132             :      already computed them for this epoch. */
     133           9 :   void * epoch_leaders_mem = fd_bank_epoch_leaders_modify( bank );
     134           9 :   fd_epoch_leaders_t * leaders = fd_epoch_leaders_join( fd_epoch_leaders_new(
     135           9 :       epoch_leaders_mem,
     136           9 :       epoch,
     137           9 :       slot0,
     138           9 :       slot_cnt,
     139           9 :       stake_weight_cnt,
     140           9 :       epoch_weights,
     141           9 :       0UL,
     142           9 :       (ulong)fd_runtime_should_use_vote_keyed_leader_schedule( bank ) ) );
     143           9 :   if( FD_UNLIKELY( !leaders ) ) {
     144           0 :     FD_LOG_ERR(( "Unable to init and join fd_epoch_leaders" ));
     145           0 :   }
     146           9 : }
     147             : 
     148             : /******************************************************************************/
     149             : /* Various Private Runtime Helpers                                            */
     150             : /******************************************************************************/
     151             : 
     152             : static int
     153             : fd_runtime_validate_fee_collector( fd_bank_t const *     bank,
     154             :                                    fd_accdb_ro_t const * collector,
     155           0 :                                    ulong                 fee ) {
     156           0 :   if( FD_UNLIKELY( !fee ) ) FD_LOG_CRIT(( "invariant violation: fee>0" ));
     157             : 
     158           0 :   if( FD_UNLIKELY( !fd_pubkey_eq( fd_accdb_ref_owner( collector ), &fd_solana_system_program_id ) ) ) {
     159           0 :     return 0;
     160           0 :   }
     161             : 
     162             :   /* https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/bank/fee_distribution.rs#L111
     163             :      https://github.com/anza-xyz/agave/blob/v1.18.23/runtime/src/accounts/account_rent_state.rs#L39
     164             :      In agave's fee deposit code, rent state transition check logic is as follows:
     165             :      The transition is NOT allowed iff
     166             :      === BEGIN
     167             :      the post deposit account is rent paying AND the pre deposit account is not rent paying
     168             :      OR
     169             :      the post deposit account is rent paying AND the pre deposit account is rent paying AND !(post_data_size == pre_data_size && post_lamports <= pre_lamports)
     170             :      === END
     171             :      post_data_size == pre_data_size is always true during fee deposit.
     172             :      However, post_lamports > pre_lamports because we are paying a >0 amount.
     173             :      So, the above reduces down to
     174             :      === BEGIN
     175             :      the post deposit account is rent paying AND the pre deposit account is not rent paying
     176             :      OR
     177             :      the post deposit account is rent paying AND the pre deposit account is rent paying AND TRUE
     178             :      === END
     179             :      This is equivalent to checking that the post deposit account is rent paying.
     180             :      An account is rent paying if the post deposit balance is >0 AND it's not rent exempt.
     181             :      We already know that the post deposit balance is >0 because we are paying a >0 amount.
     182             :      So TLDR we just check if the account is rent exempt.
     183             :    */
     184           0 :   fd_rent_t const * rent = fd_bank_rent_query( bank );
     185           0 :   ulong minbal  = fd_rent_exempt_minimum_balance( rent, fd_accdb_ref_data_sz( collector ) );
     186           0 :   ulong balance = fd_accdb_ref_lamports( collector );
     187           0 :   if( FD_UNLIKELY( __builtin_uaddl_overflow( balance, fee, &balance ) ) ) {
     188           0 :     FD_BASE58_ENCODE_32_BYTES( fd_accdb_ref_address( collector ), addr_b58 );
     189           0 :     FD_LOG_EMERG(( "integer overflow while crediting %lu fee reward lamports to %s (previous balance %lu)",
     190           0 :                    fee, addr_b58, fd_accdb_ref_lamports( collector ) ));
     191           0 :   }
     192           0 :   if( FD_UNLIKELY( balance<minbal ) ) {
     193             :     /* fee collector not rent exempt after payout */
     194           0 :     return 0;
     195           0 :   }
     196             : 
     197           0 :   return 1;
     198           0 : }
     199             : 
     200             : static void
     201             : fd_runtime_run_incinerator( fd_bank_t *               bank,
     202             :                             fd_accdb_user_t *         accdb,
     203             :                             fd_funk_txn_xid_t const * xid,
     204           0 :                             fd_capture_ctx_t *        capture_ctx ) {
     205           0 :   fd_pubkey_t const * address = &fd_sysvar_incinerator_id;
     206           0 :   fd_accdb_rw_t rw[1];
     207           0 :   if( !fd_accdb_open_rw( accdb, rw, xid, address, 0UL, 0 ) ) {
     208             :     /* Incinerator account does not exist, nothing to do */
     209           0 :     return;
     210           0 :   }
     211             : 
     212           0 :   fd_lthash_value_t prev_hash[1];
     213           0 :   fd_hashes_account_lthash( address, rw->meta, fd_accdb_ref_data_const( rw->ro ), prev_hash );
     214             : 
     215             :   /* Deleting account reduces capitalization */
     216           0 :   ulong new_capitalization = fd_ulong_sat_sub( fd_bank_capitalization_get( bank ), fd_accdb_ref_lamports( rw->ro ) );
     217           0 :   fd_bank_capitalization_set( bank, new_capitalization );
     218             : 
     219             :   /* Delete incinerator account */
     220           0 :   fd_accdb_ref_lamports_set( rw, 0UL );
     221           0 :   fd_hashes_update_lthash( address, rw->meta, prev_hash, bank, capture_ctx );
     222           0 :   fd_accdb_close_rw( accdb, rw );
     223           0 : }
     224             : 
     225             : /* fd_runtime_settle_fees settles transaction fees accumulated during a
     226             :    slot.  A portion is burnt, another portion is credited to the fee
     227             :    collector (typically leader). */
     228             : 
     229             : static void
     230             : fd_runtime_settle_fees( fd_bank_t *               bank,
     231             :                         fd_accdb_user_t *         accdb,
     232             :                         fd_funk_txn_xid_t const * xid,
     233           0 :                         fd_capture_ctx_t *        capture_ctx ) {
     234             : 
     235           0 :   ulong slot           = fd_bank_slot_get( bank );
     236           0 :   ulong execution_fees = fd_bank_execution_fees_get( bank );
     237           0 :   ulong priority_fees  = fd_bank_priority_fees_get( bank );
     238             : 
     239           0 :   ulong burn = execution_fees / 2;
     240           0 :   ulong fees = fd_ulong_sat_add( priority_fees, execution_fees - burn );
     241             : 
     242           0 :   if( FD_UNLIKELY( !fees ) ) return;
     243             : 
     244           0 :   fd_epoch_leaders_t const * leaders = fd_bank_epoch_leaders_query( bank );
     245           0 :   if( FD_UNLIKELY( !leaders ) ) FD_LOG_CRIT(( "fd_bank_epoch_leaders_query returned NULL" ));
     246             : 
     247           0 :   fd_pubkey_t const * leader = fd_epoch_leaders_get( leaders, fd_bank_slot_get( bank ) );
     248           0 :   if( FD_UNLIKELY( !leader ) ) FD_LOG_CRIT(( "fd_epoch_leaders_get(%lu) returned NULL", fd_bank_slot_get( bank ) ));
     249             : 
     250             :   /* Credit fee collector, creating it if necessary */
     251           0 :   fd_accdb_rw_t rw[1];
     252           0 :   fd_accdb_open_rw( accdb, rw, xid, leader, 0UL, FD_ACCDB_FLAG_CREATE );
     253           0 :   fd_lthash_value_t prev_hash[1];
     254           0 :   fd_hashes_account_lthash( leader, rw->meta, fd_accdb_ref_data_const( rw->ro ), prev_hash );
     255             : 
     256           0 :   if( FD_UNLIKELY( !fd_runtime_validate_fee_collector( bank, rw->ro, fees ) ) ) {  /* validation failed */
     257           0 :     burn = fd_ulong_sat_add( burn, fees );
     258           0 :     FD_LOG_INFO(( "slot %lu has an invalid fee collector, burning fee reward (%lu lamports)", fd_bank_slot_get( bank ), fees ));
     259           0 :   } else {
     260             :     /* Guaranteed to not overflow, checked above */
     261           0 :     fd_accdb_ref_lamports_set( rw, fd_accdb_ref_lamports( rw->ro ) + fees );
     262           0 :     fd_hashes_update_lthash( fd_accdb_ref_address( rw->ro ), rw->meta, prev_hash, bank, capture_ctx );
     263           0 :   }
     264             : 
     265           0 :   fd_accdb_close_rw( accdb, rw );
     266             : 
     267           0 :   ulong old = fd_bank_capitalization_get( bank );
     268           0 :   fd_bank_capitalization_set( bank, fd_ulong_sat_sub( old, burn ) );
     269           0 :   FD_LOG_INFO(( "slot %lu: burn %lu, capitalization %lu->%lu",
     270           0 :                 slot, burn, old, fd_bank_capitalization_get( bank ) ));
     271           0 : }
     272             : 
     273             : static void
     274             : fd_runtime_freeze( fd_bank_t *         bank,
     275             :                    fd_accdb_user_t *   accdb,
     276           0 :                    fd_capture_ctx_t *  capture_ctx ) {
     277             : 
     278           0 :   fd_funk_txn_xid_t const xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
     279             : 
     280           0 :   if( FD_LIKELY( fd_bank_slot_get( bank ) != 0UL ) ) {
     281           0 :     fd_sysvar_recent_hashes_update( bank, accdb, &xid, capture_ctx );
     282           0 :   }
     283             : 
     284           0 :   fd_sysvar_slot_history_update( bank, accdb, &xid, capture_ctx );
     285             : 
     286           0 :   fd_runtime_settle_fees( bank, accdb, &xid, capture_ctx );
     287             : 
     288             :   /* jito collects a 3% fee at the end of the block + 3% fee at
     289             :      distribution time. */
     290           0 :   ulong tips_pre_comission = fd_bank_tips_get( bank );
     291           0 :   fd_bank_tips_set( bank, (tips_pre_comission - (tips_pre_comission * 6UL / 100UL)) );
     292             : 
     293           0 :   fd_runtime_run_incinerator( bank, accdb, &xid, capture_ctx );
     294             : 
     295           0 : }
     296             : 
     297             : /******************************************************************************/
     298             : /* Block-Level Execution Preparation/Finalization                             */
     299             : /******************************************************************************/
     300             : void
     301             : fd_runtime_new_fee_rate_governor_derived( fd_bank_t * bank,
     302          57 :                                           ulong       latest_signatures_per_slot ) {
     303             : 
     304          57 :   fd_fee_rate_governor_t const * base_fee_rate_governor = fd_bank_fee_rate_governor_query( bank );
     305             : 
     306          57 :   ulong old_lamports_per_signature = fd_bank_rbh_lamports_per_sig_get( bank );
     307             : 
     308          57 :   fd_fee_rate_governor_t me = {
     309          57 :     .target_signatures_per_slot    = base_fee_rate_governor->target_signatures_per_slot,
     310          57 :     .target_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature,
     311          57 :     .max_lamports_per_signature    = base_fee_rate_governor->max_lamports_per_signature,
     312          57 :     .min_lamports_per_signature    = base_fee_rate_governor->min_lamports_per_signature,
     313          57 :     .burn_percent                  = base_fee_rate_governor->burn_percent
     314          57 :   };
     315             : 
     316          57 :   ulong new_lamports_per_signature = 0;
     317          57 :   if( me.target_signatures_per_slot > 0 ) {
     318           0 :     me.min_lamports_per_signature = fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 2) );
     319           0 :     me.max_lamports_per_signature = me.target_lamports_per_signature * 10;
     320           0 :     ulong desired_lamports_per_signature = fd_ulong_min(
     321           0 :       me.max_lamports_per_signature,
     322           0 :       fd_ulong_max(
     323           0 :         me.min_lamports_per_signature,
     324           0 :         me.target_lamports_per_signature
     325           0 :         * fd_ulong_min(latest_signatures_per_slot, (ulong)UINT_MAX)
     326           0 :         / me.target_signatures_per_slot
     327           0 :       )
     328           0 :     );
     329           0 :     long gap = (long)desired_lamports_per_signature - (long)old_lamports_per_signature;
     330           0 :     if ( gap == 0 ) {
     331           0 :       new_lamports_per_signature = desired_lamports_per_signature;
     332           0 :     } else {
     333           0 :       long gap_adjust = (long)(fd_ulong_max( 1UL, (ulong)(me.target_lamports_per_signature / 20) ))
     334           0 :         * (gap != 0)
     335           0 :         * (gap > 0 ? 1 : -1);
     336           0 :       new_lamports_per_signature = fd_ulong_min(
     337           0 :         me.max_lamports_per_signature,
     338           0 :         fd_ulong_max(
     339           0 :           me.min_lamports_per_signature,
     340           0 :           (ulong)((long)old_lamports_per_signature + gap_adjust)
     341           0 :         )
     342           0 :       );
     343           0 :     }
     344          57 :   } else {
     345          57 :     new_lamports_per_signature = base_fee_rate_governor->target_lamports_per_signature;
     346          57 :     me.min_lamports_per_signature = me.target_lamports_per_signature;
     347          57 :     me.max_lamports_per_signature = me.target_lamports_per_signature;
     348          57 :   }
     349          57 :   fd_bank_fee_rate_governor_set( bank, me );
     350          57 :   fd_bank_rbh_lamports_per_sig_set( bank, new_lamports_per_signature );
     351          57 : }
     352             : 
     353             : /******************************************************************************/
     354             : /* Epoch Boundary                                                             */
     355             : /******************************************************************************/
     356             : 
     357             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6704 */
     358             : static void
     359             : fd_apply_builtin_program_feature_transitions( fd_bank_t *               bank,
     360             :                                               fd_accdb_user_t *         accdb,
     361             :                                               fd_funk_txn_xid_t const * xid,
     362             :                                               fd_runtime_stack_t *      runtime_stack,
     363           9 :                                               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           9 :   fd_builtin_program_t const * builtins = fd_builtins();
     372         117 :   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         108 :     if( builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), 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, xid, 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         108 :     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, xid, capture_ctx, *builtins[i].pubkey, builtins[i].data,strlen(builtins[i].data) );
     384           0 :     }
     385         108 :   }
     386             : 
     387             :   /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6776-L6793 */
     388           9 :   fd_stateless_builtin_program_t const * stateless_builtins = fd_stateless_builtins();
     389          27 :   for( ulong i=0UL; i<fd_num_stateless_builtins(); i++ ) {
     390          18 :     if( stateless_builtins[i].core_bpf_migration_config && FD_FEATURE_ACTIVE_OFFSET( fd_bank_slot_get( bank ), fd_bank_features_query( bank ), 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, xid, runtime_stack, stateless_builtins[i].core_bpf_migration_config, capture_ctx );
     394           0 :     }
     395          18 :   }
     396             : 
     397             :   /* https://github.com/anza-xyz/agave/blob/c1080de464cfb578c301e975f498964b5d5313db/runtime/src/bank.rs#L6795-L6805 */
     398          36 :   for( fd_precompile_program_t const * precompiles = fd_precompiles(); precompiles->verify_fn; precompiles++ ) {
     399          27 :     if( precompiles->feature_offset != NO_ENABLE_FEATURE_ID &&
     400          27 :         FD_FEATURE_JUST_ACTIVATED_OFFSET( bank, precompiles->feature_offset ) ) {
     401           0 :       fd_write_builtin_account( bank, accdb, xid, capture_ctx, *precompiles->pubkey, "", 0 );
     402           0 :     }
     403          27 :   }
     404           9 : }
     405             : 
     406             : static void
     407             : fd_feature_activate( fd_bank_t *               bank,
     408             :                      fd_accdb_user_t *         accdb,
     409             :                      fd_funk_txn_xid_t const * xid,
     410             :                      fd_capture_ctx_t *        capture_ctx,
     411             :                      fd_feature_id_t const *   id,
     412        2376 :                      fd_pubkey_t const *       addr ) {
     413        2376 :   fd_features_t * features = fd_bank_features_modify( bank );
     414             : 
     415        2376 :   if( id->reverted==1 ) return;
     416             : 
     417        2277 :   fd_accdb_ro_t ro[1];
     418        2277 :   if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, ro, xid, addr ) ) ) {
     419        2277 :     return;
     420        2277 :   }
     421             : 
     422           0 :   if( FD_UNLIKELY( !fd_pubkey_eq( fd_accdb_ref_owner( ro ), &fd_solana_feature_program_id ) ) ) {
     423             :     /* Feature account not yet initialized */
     424           0 :     fd_accdb_close_ro( accdb, ro );
     425           0 :     return;
     426           0 :   }
     427             : 
     428           0 :   FD_BASE58_ENCODE_32_BYTES( addr->uc, addr_b58 );
     429             : 
     430           0 :   fd_feature_t feature;
     431           0 :   if( FD_UNLIKELY( !fd_feature_decode( &feature, fd_accdb_ref_data_const( ro ), fd_accdb_ref_data_sz( ro ) ) ) ) {
     432           0 :     FD_LOG_WARNING(( "cannot activate feature %s, corrupt account data", addr_b58 ));
     433           0 :     FD_LOG_HEXDUMP_NOTICE(( "corrupt feature account", fd_accdb_ref_data_const( ro ), fd_accdb_ref_data_sz( ro ) ));
     434           0 :     fd_accdb_close_ro( accdb, ro );
     435           0 :     return;
     436           0 :   }
     437           0 :   fd_accdb_close_ro( accdb, ro );
     438             : 
     439           0 :   if( feature.is_active ) {
     440           0 :     FD_LOG_DEBUG(( "feature %s already activated at slot %lu", addr_b58, feature.activation_slot ));
     441           0 :     fd_features_set( features, id, feature.activation_slot);
     442           0 :   } else {
     443           0 :     FD_LOG_DEBUG(( "feature %s not activated at slot %lu, activating", addr_b58, fd_bank_slot_get( bank ) ));
     444           0 :     fd_accdb_rw_t rw[1];
     445           0 :     if( FD_UNLIKELY( !fd_accdb_open_rw( accdb, rw, xid, addr, 0UL, 0 ) ) ) return;
     446           0 :     fd_lthash_value_t prev_hash[1];
     447           0 :     fd_hashes_account_lthash( addr, rw->meta, fd_accdb_ref_data_const( rw->ro ), prev_hash );
     448           0 :     feature.is_active       = 1;
     449           0 :     feature.activation_slot = fd_bank_slot_get( bank );
     450           0 :     FD_CRIT( fd_accdb_ref_data_sz( rw->ro )>=sizeof(fd_feature_t), "unreachable" );
     451           0 :     FD_STORE( fd_feature_t, fd_accdb_ref_data( rw ), feature );
     452           0 :     fd_hashes_update_lthash( addr, rw->meta, prev_hash, bank, capture_ctx );
     453           0 :     fd_accdb_close_rw( accdb, rw );
     454           0 :   }
     455           0 : }
     456             : 
     457             : static void
     458             : fd_features_activate( fd_bank_t *               bank,
     459             :                       fd_accdb_user_t  *        accdb,
     460             :                       fd_funk_txn_xid_t const * xid,
     461           9 :                       fd_capture_ctx_t *        capture_ctx ) {
     462           9 :   for( fd_feature_id_t const * id = fd_feature_iter_init();
     463        2385 :                                    !fd_feature_iter_done( id );
     464        2376 :                                id = fd_feature_iter_next( id ) ) {
     465        2376 :     fd_feature_activate( bank, accdb, xid, capture_ctx, id, &id->id );
     466        2376 :   }
     467           9 : }
     468             : 
     469             : /* SIMD-0194: deprecate_rent_exemption_threshold
     470             :    https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
     471             : static void
     472             : deprecate_rent_exemption_threshold( fd_bank_t *               bank,
     473             :                                     fd_accdb_user_t *         accdb,
     474             :                                     fd_funk_txn_xid_t const * xid,
     475           3 :                                     fd_capture_ctx_t *        capture_ctx ) {
     476             :   /* We use the bank fields here to mirror Agave - in mainnet, devnet
     477             :      and testnet Agave's bank rent.burn_percent field is different to
     478             :      the value in the sysvar. When this feature is activated in Agave,
     479             :      the sysvar inherits the value from the bank. */
     480           3 :   fd_rent_t rent               = fd_bank_rent_get( bank );
     481           3 :   rent.lamports_per_uint8_year = fd_rust_cast_double_to_ulong(
     482           3 :     (double)rent.lamports_per_uint8_year * rent.exemption_threshold );
     483           3 :   rent.exemption_threshold     = FD_SIMD_0194_NEW_RENT_EXEMPTION_THRESHOLD;
     484             : 
     485             :   /* We don't refresh the sysvar cache here. The cache is refreshed in
     486             :      fd_sysvar_cache_restore, which is called at the start of every
     487             :      block in fd_runtime_block_execute_prepare, after this function. */
     488           3 :   fd_sysvar_rent_write( bank, accdb, xid, capture_ctx, &rent );
     489           3 :   fd_bank_rent_set( bank, rent );
     490           3 : }
     491             : 
     492             : // https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391
     493             : static void
     494             : fd_compute_and_apply_new_feature_activations( fd_bank_t *               bank,
     495             :                                               fd_accdb_user_t *         accdb,
     496             :                                               fd_funk_txn_xid_t const * xid,
     497             :                                               fd_runtime_stack_t *      runtime_stack,
     498           9 :                                               fd_capture_ctx_t *        capture_ctx ) {
     499             :   /* Activate new features
     500             :       https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5296-L5391 */
     501           9 :   fd_features_activate( bank, accdb, xid, capture_ctx );
     502           9 :   fd_features_restore( bank, accdb, xid );
     503             : 
     504             :   /* SIMD-0194: deprecate_rent_exemption_threshold
     505             :       https://github.com/anza-xyz/agave/blob/v3.1.4/runtime/src/bank.rs#L5322-L5329 */
     506           9 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, deprecate_rent_exemption_threshold ) ) ) {
     507           3 :     deprecate_rent_exemption_threshold( bank, accdb, xid, capture_ctx );
     508           3 :   }
     509             : 
     510             :   /* Apply builtin program feature transitions
     511             :       https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/bank.rs#L6621-L6624 */
     512           9 :   fd_apply_builtin_program_feature_transitions( bank, accdb, xid, runtime_stack, capture_ctx );
     513             : 
     514           9 :   if( FD_UNLIKELY( FD_FEATURE_JUST_ACTIVATED_BANK( bank, vote_state_v4 ) ) ) {
     515           0 :     fd_upgrade_core_bpf_program( bank, accdb, xid, runtime_stack, &fd_solana_stake_program_id, &fd_solana_stake_program_vote_state_v4_buffer_address, capture_ctx );
     516           0 :   }
     517           9 : }
     518             : 
     519             : /* Starting a new epoch.
     520             :   New epoch:        T
     521             :   Just ended epoch: T-1
     522             :   Epoch before:     T-2
     523             : 
     524             :   In this function:
     525             :   - stakes in T-2 (vote_states_prev_prev) should be replaced by T-1 (vote_states_prev)
     526             :   - stakes at T-1 (vote_states_prev) should be replaced by updated stakes at T (vote_states)
     527             :   - leader schedule should be calculated using new T-2 stakes (vote_states_prev_prev)
     528             : 
     529             :   Invariant during an epoch T:
     530             :   vote_states_prev holds the stakes at T-1
     531             :   vote_states_prev_prev holds the stakes at T-2
     532             :  */
     533             : /* process for the start of a new epoch */
     534             : static void
     535             : fd_runtime_process_new_epoch( fd_banks_t *              banks,
     536             :                               fd_bank_t *               bank,
     537             :                               fd_accdb_user_t *         accdb,
     538             :                               fd_funk_txn_xid_t const * xid,
     539             :                               fd_capture_ctx_t *        capture_ctx,
     540             :                               ulong                     parent_epoch,
     541           9 :                               fd_runtime_stack_t *      runtime_stack ) {
     542           9 :   long start = fd_log_wallclock();
     543             : 
     544           9 :   fd_stake_delegations_t const * stake_delegations = fd_bank_stake_delegations_frontier_query( banks, bank );
     545           9 :   if( FD_UNLIKELY( !stake_delegations ) ) {
     546           0 :     FD_LOG_CRIT(( "stake_delegations is NULL" ));
     547           0 :   }
     548             : 
     549           9 :   fd_compute_and_apply_new_feature_activations( bank, accdb, xid, runtime_stack, capture_ctx );
     550             : 
     551             :   /* Get the new rate activation epoch */
     552           9 :   int _err[1];
     553           9 :   ulong   new_rate_activation_epoch_val = 0UL;
     554           9 :   ulong * new_rate_activation_epoch     = &new_rate_activation_epoch_val;
     555           9 :   int is_some = fd_new_warmup_cooldown_rate_epoch(
     556           9 :       fd_bank_epoch_schedule_query( bank ),
     557           9 :       fd_bank_features_query( bank ),
     558           9 :       new_rate_activation_epoch,
     559           9 :       _err );
     560           9 :   if( FD_UNLIKELY( !is_some ) ) {
     561           0 :     new_rate_activation_epoch = NULL;
     562           0 :   }
     563             : 
     564             :   /* Updates stake history sysvar accumulated values and recomputes
     565             :      stake delegations for vote accounts. */
     566             : 
     567           9 :   fd_stakes_activate_epoch( bank, runtime_stack, accdb, xid, capture_ctx, stake_delegations, new_rate_activation_epoch );
     568             : 
     569             :   /* Distribute rewards.  This involves calculating the rewards for
     570             :      every vote and stake account. */
     571             : 
     572           9 :   fd_hash_t const * parent_blockhash = fd_blockhashes_peek_last_hash( fd_bank_block_hash_queue_query( bank ) );
     573           9 :   fd_begin_partitioned_rewards( bank,
     574           9 :                                 accdb,
     575           9 :                                 xid,
     576           9 :                                 runtime_stack,
     577           9 :                                 capture_ctx,
     578           9 :                                 stake_delegations,
     579           9 :                                 parent_blockhash,
     580           9 :                                 parent_epoch );
     581             : 
     582             :   /* The Agave client handles updating their stakes cache with a call to
     583             :      update_epoch_stakes() which keys stakes by the leader schedule
     584             :      epochs and retains up to 6 epochs of stakes.  However, to correctly
     585             :      calculate the leader schedule, we just need to maintain the vote
     586             :      states for the current epoch, the previous epoch, and the one
     587             :      before that.
     588             :      https://github.com/anza-xyz/agave/blob/v3.0.4/runtime/src/bank.rs#L2175
     589             :   */
     590             : 
     591             :   /* Now that our stakes caches have been updated, we can calculate the
     592             :      leader schedule for the upcoming epoch epoch using our new
     593             :      vote_states_prev_prev (stakes for T-2). */
     594             : 
     595           9 :   fd_runtime_update_leaders( bank, runtime_stack );
     596             : 
     597           9 :   long end = fd_log_wallclock();
     598           9 :   FD_LOG_NOTICE(( "starting epoch %lu at slot %lu took %.6f seconds", fd_bank_epoch_get( bank ), fd_bank_slot_get( bank ), (double)(end - start) / 1e9 ));
     599           9 : }
     600             : 
     601             : static void
     602             : fd_runtime_block_pre_execute_process_new_epoch( fd_banks_t *              banks,
     603             :                                                 fd_bank_t *               bank,
     604             :                                                 fd_accdb_user_t *         accdb,
     605             :                                                 fd_funk_txn_xid_t const * xid,
     606             :                                                 fd_capture_ctx_t *        capture_ctx,
     607             :                                                 fd_runtime_stack_t *      runtime_stack,
     608          57 :                                                 int *                     is_epoch_boundary ) {
     609             : 
     610          57 :   ulong const slot = fd_bank_slot_get( bank );
     611          57 :   if( FD_LIKELY( slot != 0UL ) ) {
     612          57 :     fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     613             : 
     614          57 :     ulong prev_epoch = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
     615          57 :     ulong slot_idx;
     616          57 :     ulong new_epoch  = fd_slot_to_epoch( epoch_schedule, slot, &slot_idx );
     617          57 :     if( FD_UNLIKELY( slot_idx==1UL && new_epoch==0UL ) ) {
     618             :       /* The block after genesis has a height of 1. */
     619           0 :       fd_bank_block_height_set( bank, 1UL );
     620           0 :     }
     621             : 
     622          57 :     if( FD_UNLIKELY( prev_epoch<new_epoch || !slot_idx ) ) {
     623           9 :       FD_LOG_DEBUG(( "Epoch boundary starting" ));
     624           9 :       fd_runtime_process_new_epoch( banks, bank, accdb, xid, capture_ctx, prev_epoch, runtime_stack );
     625           9 :       *is_epoch_boundary = 1;
     626          48 :     } else {
     627          48 :       *is_epoch_boundary = 0;
     628          48 :     }
     629             : 
     630          57 :     fd_distribute_partitioned_epoch_rewards( bank, accdb, xid, capture_ctx );
     631          57 :   } else {
     632           0 :     *is_epoch_boundary = 0;
     633           0 :   }
     634          57 : }
     635             : 
     636             : 
     637             : static void
     638             : fd_runtime_block_sysvar_update_pre_execute( fd_bank_t *               bank,
     639             :                                             fd_accdb_user_t *         accdb,
     640             :                                             fd_funk_txn_xid_t const * xid,
     641             :                                             fd_runtime_stack_t *      runtime_stack,
     642          57 :                                             fd_capture_ctx_t *        capture_ctx ) {
     643             :   // let (fee_rate_governor, fee_components_time_us) = measure_us!(
     644             :   //     FeeRateGovernor::new_derived(&parent.fee_rate_governor, parent.signature_count())
     645             :   // );
     646             :   /* https://github.com/firedancer-io/solana/blob/dab3da8e7b667d7527565bddbdbecf7ec1fb868e/runtime/src/bank.rs#L1312-L1314 */
     647             : 
     648          57 :   fd_runtime_new_fee_rate_governor_derived( bank, fd_bank_parent_signature_cnt_get( bank ) );
     649             : 
     650          57 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     651          57 :   ulong                       parent_epoch   = fd_slot_to_epoch( epoch_schedule, fd_bank_parent_slot_get( bank ), NULL );
     652          57 :   fd_sysvar_clock_update( bank, accdb, xid, capture_ctx, runtime_stack, &parent_epoch );
     653             : 
     654             :   // It has to go into the current txn previous info but is not in slot 0
     655          57 :   if( fd_bank_slot_get( bank ) != 0 ) {
     656          57 :     fd_sysvar_slot_hashes_update( bank, accdb, xid, capture_ctx );
     657          57 :   }
     658          57 :   fd_sysvar_last_restart_slot_update( bank, accdb, xid, capture_ctx, fd_bank_last_restart_slot_get( bank ).slot );
     659          57 : }
     660             : 
     661             : int
     662             : fd_runtime_load_txn_address_lookup_tables(
     663             :     fd_txn_t const *          txn,
     664             :     uchar const *             payload,
     665             :     fd_accdb_user_t *         accdb,
     666             :     fd_funk_txn_xid_t const * xid,
     667             :     ulong                     slot,
     668             :     fd_slot_hash_t const *    hashes, /* deque */
     669          69 :     fd_acct_addr_t *          out_accts_alt ) {
     670             : 
     671          69 :   if( FD_LIKELY( txn->transaction_version!=FD_TXN_V0 ) ) return FD_RUNTIME_EXECUTE_SUCCESS;
     672             : 
     673          69 :   fd_alut_interp_t interp[1];
     674          69 :   fd_alut_interp_new(
     675          69 :       interp,
     676          69 :       out_accts_alt,
     677          69 :       txn,
     678          69 :       payload,
     679          69 :       hashes,
     680          69 :       slot );
     681             : 
     682          69 :   fd_txn_acct_addr_lut_t const * addr_luts = fd_txn_get_address_tables_const( txn );
     683          69 :   for( ulong i=0UL; i<txn->addr_table_lookup_cnt; i++ ) {
     684           0 :     fd_txn_acct_addr_lut_t const * addr_lut = &addr_luts[i];
     685           0 :     fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, payload+addr_lut->addr_off );
     686             : 
     687             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94 */
     688           0 :     fd_accdb_ro_t alut_ro[1];
     689           0 :     if( FD_UNLIKELY( !fd_accdb_open_ro( accdb, alut_ro, xid, &addr_lut_acc ) ) ) {
     690           0 :       fd_alut_interp_delete( interp );
     691           0 :       return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
     692           0 :     }
     693             : 
     694           0 :     int err = fd_alut_interp_next(
     695           0 :         interp,
     696           0 :         &addr_lut_acc,
     697           0 :         fd_accdb_ref_owner     ( alut_ro ),
     698           0 :         fd_accdb_ref_data_const( alut_ro ),
     699           0 :         fd_accdb_ref_data_sz   ( alut_ro ) );
     700           0 :     fd_accdb_close_ro( accdb, alut_ro );
     701           0 :     if( FD_UNLIKELY( err ) ) {
     702           0 :       fd_alut_interp_delete( interp );
     703           0 :       return err;
     704           0 :     }
     705           0 :   }
     706             : 
     707          69 :   fd_alut_interp_delete( interp );
     708             : 
     709          69 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     710          69 : }
     711             : 
     712             : /* Pre-populate the bank's in-memory feature set with upcoming feature
     713             :    activations.  If the current slot is the last slot before an epoch
     714             :    boundary, scan all known feature accounts. Otherwise, returns early.
     715             : 
     716             :    For any feature that is pending (not yet activated on-chain) but has
     717             :    an account owned by the feature program, set the in-memory activation
     718             :    slot within the bank's featureset to the first slot of the next
     719             :    epoch.  This is needed so that deployment verification (which uses
     720             :    slot+1) can detect features that will activate at the next epoch
     721             :    boundary.
     722             : 
     723             :    In Agave, program deployments use the feature set from the next
     724             :    slot via DELAY_VISIBILITY_SLOT_OFFSET.  The runtime environments
     725             :    for deployment are selected based on epoch_of(slot+1):
     726             :    https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/bank.rs#L3280-L3295
     727             :    https://github.com/anza-xyz/agave/blob/v3.1.8/svm/src/transaction_processor.rs#L339-L345
     728             : 
     729             :    This function does NOT write to feature accounts or update the
     730             :    lthash.  It only modifies the bank's in-memory feature set. */
     731             : static void
     732             : fd_features_prepopulate_upcoming( fd_bank_t *               bank,
     733             :                                   fd_accdb_user_t *         accdb,
     734          57 :                                   fd_funk_txn_xid_t const * xid ) {
     735          57 :   ulong slot = fd_bank_slot_get( bank );
     736          57 :   if( FD_UNLIKELY( !slot ) ) {
     737           0 :     return;
     738           0 :   }
     739             : 
     740          57 :   fd_epoch_schedule_t const * epoch_schedule = fd_bank_epoch_schedule_query( bank );
     741          57 :   ulong curr_epoch = fd_slot_to_epoch( epoch_schedule, slot,     NULL );
     742          57 :   ulong next_epoch = fd_slot_to_epoch( epoch_schedule, slot+1UL, NULL );
     743             : 
     744          57 :   if( FD_LIKELY( curr_epoch==next_epoch ) ) {
     745          48 :     return;
     746          48 :   }
     747             : 
     748           9 :   fd_features_restore( bank, accdb, xid );
     749           9 : }
     750             : 
     751             : void
     752             : fd_runtime_block_execute_prepare( fd_banks_t *         banks,
     753             :                                   fd_bank_t *          bank,
     754             :                                   fd_accdb_user_t  *   accdb,
     755             :                                   fd_runtime_stack_t * runtime_stack,
     756             :                                   fd_capture_ctx_t *   capture_ctx,
     757          57 :                                   int *                is_epoch_boundary ) {
     758             : 
     759          57 :   fd_funk_txn_xid_t const xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
     760             : 
     761          57 :   fd_runtime_block_pre_execute_process_new_epoch( banks, bank, accdb, &xid, capture_ctx, runtime_stack, is_epoch_boundary );
     762             : 
     763          57 :   fd_bank_execution_fees_set( bank, 0UL );
     764          57 :   fd_bank_priority_fees_set( bank, 0UL );
     765          57 :   fd_bank_signature_count_set( bank, 0UL );
     766          57 :   fd_bank_total_compute_units_used_set( bank, 0UL );
     767             : 
     768          57 :   if( FD_LIKELY( fd_bank_slot_get( bank ) ) ) {
     769          57 :     fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
     770          57 :     FD_TEST( cost_tracker );
     771          57 :     fd_cost_tracker_init( cost_tracker, fd_bank_features_query( bank ), fd_bank_slot_get( bank ) );
     772          57 :     fd_bank_cost_tracker_end_locking_modify( bank );
     773          57 :   }
     774             : 
     775             :   /* Update the active feature set with any upcoming features */
     776          57 :   fd_features_prepopulate_upcoming( bank, accdb, &xid );
     777             : 
     778          57 :   fd_runtime_block_sysvar_update_pre_execute( bank, accdb, &xid, runtime_stack, capture_ctx );
     779             : 
     780          57 :   if( FD_UNLIKELY( !fd_sysvar_cache_restore( bank, accdb, &xid ) ) ) {
     781           0 :     FD_LOG_ERR(( "Failed to restore sysvar cache" ));
     782           0 :   }
     783          57 : }
     784             : 
     785             : static void
     786             : fd_runtime_update_bank_hash( fd_bank_t *        bank,
     787           0 :                              fd_capture_ctx_t * capture_ctx ) {
     788             :   /* Save the previous bank hash, and the parents signature count */
     789           0 :   fd_hash_t const * prev_bank_hash = NULL;
     790           0 :   if( FD_LIKELY( fd_bank_slot_get( bank )!=0UL ) ) {
     791           0 :     prev_bank_hash = fd_bank_bank_hash_query( bank );
     792           0 :     fd_bank_prev_bank_hash_set( bank, *prev_bank_hash );
     793           0 :   } else {
     794           0 :     prev_bank_hash = fd_bank_prev_bank_hash_query( bank );
     795           0 :   }
     796             : 
     797           0 :   fd_bank_parent_signature_cnt_set( bank, fd_bank_signature_count_get( bank ) );
     798             : 
     799             :   /* Compute the new bank hash */
     800           0 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
     801           0 :   fd_hash_t new_bank_hash[1] = { 0 };
     802           0 :   fd_hashes_hash_bank(
     803           0 :       lthash,
     804           0 :       prev_bank_hash,
     805           0 :       (fd_hash_t *)fd_bank_poh_query( bank )->hash,
     806           0 :       fd_bank_signature_count_get( bank ),
     807           0 :       new_bank_hash );
     808             : 
     809             :   /* Update the bank hash */
     810           0 :   fd_bank_bank_hash_set( bank, *new_bank_hash );
     811             : 
     812           0 :   if( capture_ctx && capture_ctx->capture_solcap &&
     813           0 :       fd_bank_slot_get( bank )>=capture_ctx->solcap_start_slot ) {
     814             : 
     815           0 :     uchar lthash_hash[FD_HASH_FOOTPRINT];
     816           0 :     fd_blake3_hash(lthash->bytes, FD_LTHASH_LEN_BYTES, lthash_hash );
     817           0 :     fd_capture_link_write_bank_preimage(
     818           0 :       capture_ctx,
     819           0 :       fd_bank_slot_get( bank ),
     820           0 :       (fd_hash_t *)new_bank_hash->hash,
     821           0 :       (fd_hash_t *)fd_bank_prev_bank_hash_query( bank ),
     822           0 :       (fd_hash_t *)lthash_hash,
     823           0 :       (fd_hash_t *)fd_bank_poh_query( bank )->hash,
     824           0 :       fd_bank_signature_count_get( bank ) );
     825           0 :   }
     826             : 
     827           0 :   fd_bank_lthash_end_locking_query( bank );
     828           0 : }
     829             : 
     830             : /******************************************************************************/
     831             : /* Transaction Level Execution Management                                     */
     832             : /******************************************************************************/
     833             : 
     834             : /* fd_runtime_pre_execute_check is responsible for conducting many of the
     835             :    transaction sanitization checks. */
     836             : 
     837             : static inline int
     838             : fd_runtime_pre_execute_check( fd_runtime_t *      runtime,
     839             :                               fd_bank_t *         bank,
     840             :                               fd_txn_in_t const * txn_in,
     841          93 :                               fd_txn_out_t *      txn_out ) {
     842             : 
     843             :   /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/sdk/src/transaction/sanitized.rs#L263-L275
     844             :      TODO: Agave's precompile verification is done at the slot level, before batching and executing transactions. This logic should probably
     845             :      be moved in the future. The Agave call heirarchy looks something like this:
     846             :             process_single_slot
     847             :                    v
     848             :             confirm_full_slot
     849             :                    v
     850             :             confirm_slot_entries --------------------------------------------------->
     851             :                    v                               v                                v
     852             :             verify_transaction    ComputeBudget::process_instruction         process_entries
     853             :                    v                                                                v
     854             :             verify_precompiles                                                process_batches
     855             :                                                                                     v
     856             :                                                                                    ...
     857             :                                                                                     v
     858             :                                                                         load_and_execute_transactions
     859             :                                                                                     v
     860             :                                                                                    ...
     861             :                                                                                     v
     862             :                                                                               load_accounts --> load_transaction_accounts
     863             :                                                                                     v
     864             :                                                                        general transaction execution
     865             : 
     866             :   */
     867             : 
     868             :   /* Verify the transaction. For now, this step only involves processing
     869             :      the compute budget instructions. */
     870          93 :   int err = fd_executor_verify_transaction( bank, txn_in, txn_out );
     871          93 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     872           3 :     txn_out->err.is_committable = 0;
     873           3 :     return err;
     874           3 :   }
     875             : 
     876             :   /* Resolve and verify ALUT-referenced account keys, if applicable */
     877          90 :   err = fd_executor_setup_txn_alut_account_keys( runtime, bank, txn_in, txn_out );
     878          90 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     879           0 :     txn_out->err.is_committable = 0;
     880           0 :     return err;
     881           0 :   }
     882             : 
     883             :   /* Set up the transaction accounts and other txn ctx metadata */
     884          90 :   fd_executor_setup_accounts_for_txn( runtime, bank, txn_in, txn_out );
     885             : 
     886             :   /* Post-sanitization checks. Called from prepare_sanitized_batch()
     887             :      which, for now, only is used to lock the accounts and perform a
     888             :      couple basic validations.
     889             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
     890          90 :   err = fd_executor_validate_account_locks( bank, txn_out );
     891          90 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     892           0 :     txn_out->err.is_committable = 0;
     893           0 :     return err;
     894           0 :   }
     895             : 
     896             :   /* load_and_execute_transactions() -> check_transactions()
     897             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3667-L3672 */
     898          90 :   err = fd_executor_check_transactions( runtime, bank, txn_in, txn_out );
     899          90 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     900           0 :     txn_out->err.is_committable = 0;
     901           0 :     return err;
     902           0 :   }
     903             : 
     904             :   /* load_and_execute_sanitized_transactions() -> validate_fees() ->
     905             :      validate_transaction_fee_payer()
     906             :      https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L236-L249 */
     907          90 :   err = fd_executor_validate_transaction_fee_payer( runtime, bank, txn_in, txn_out );
     908          90 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     909           0 :     txn_out->err.is_committable = 0;
     910           0 :     return err;
     911           0 :   }
     912             : 
     913          90 :   txn_out->details.exec_start_timestamp = fd_tickcount();
     914             : 
     915             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/svm/src/transaction_processor.rs#L284-L296 */
     916          90 :   err = fd_executor_load_transaction_accounts( runtime, bank, txn_in, txn_out );
     917          90 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     918             :     /* Regardless of whether transaction accounts were loaded successfully, the transaction is
     919             :        included in the block and transaction fees are collected.
     920             :        https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
     921           0 :     txn_out->err.is_fees_only = 1;
     922             : 
     923             :     /* If the transaction fails to load, the "rollback" accounts will include one of the following:
     924             :         1. Nonce account only
     925             :         2. Fee payer only
     926             :         3. Nonce account + fee payer
     927             : 
     928             :         Because the cost tracker uses the loaded account data size in block cost calculations, we need to
     929             :         make sure our calculated loaded accounts data size is conformant with Agave's.
     930             :         https://github.com/anza-xyz/agave/blob/v2.1.14/runtime/src/bank.rs#L4116
     931             : 
     932             :         In any case, we should always add the dlen of the fee payer. */
     933           0 :     txn_out->details.loaded_accounts_data_size = fd_accdb_ref_data_sz( txn_out->accounts.account[ FD_FEE_PAYER_TXN_IDX ].ro );
     934             : 
     935             :     /* Special case handling for if a nonce account is present in the transaction. */
     936           0 :     if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
     937             :       /* If the nonce account is not the fee payer, then we separately add the dlen of the nonce account. Otherwise, we would
     938             :           be double counting the dlen of the fee payer. */
     939           0 :       if( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) {
     940           0 :         txn_out->details.loaded_accounts_data_size += txn_out->accounts.rollback_nonce->dlen;
     941           0 :       }
     942           0 :     }
     943           0 :   }
     944             : 
     945             :   /*
     946             :      The fee payer and the nonce account will be stored and hashed so
     947             :      long as the transaction landed on chain, or, in Agave terminology,
     948             :      the transaction was processed.
     949             :      https://github.com/anza-xyz/agave/blob/v2.1.1/runtime/src/account_saver.rs#L72
     950             : 
     951             :      A transaction lands on chain in one of two ways:
     952             :      (1) Passed fee validation and loaded accounts.
     953             :      (2) Passed fee validation and failed to load accounts and the enable_transaction_loading_failure_fees feature is enabled as per
     954             :          SIMD-0082 https://github.com/anza-xyz/feature-gate-tracker/issues/52
     955             : 
     956             :      So, at this point, the transaction is committable.
     957             :    */
     958             : 
     959          90 :   return err;
     960          90 : }
     961             : 
     962             : /* fd_runtime_finalize_account is a helper used to commit the data from
     963             :    a writable transaction account back into the accountsdb. */
     964             : 
     965             : static void
     966             : fd_runtime_finalize_account( fd_accdb_user_t *         accdb,
     967             :                              fd_funk_txn_xid_t const * xid,
     968             :                              fd_pubkey_t const *       pubkey,
     969          33 :                              fd_account_meta_t *       meta ) {
     970             :   /* FIXME if account doesn't change according to LtHash, don't update
     971             :            database record */
     972             : 
     973          33 :   fd_accdb_rw_t rw[1];
     974          33 :   int rw_ok = !!fd_accdb_open_rw(
     975          33 :       accdb,
     976          33 :       rw,
     977          33 :       xid,
     978          33 :       pubkey,
     979          33 :       meta->dlen,
     980          33 :       FD_ACCDB_FLAG_CREATE|FD_ACCDB_FLAG_TRUNCATE );
     981          33 :   if( FD_UNLIKELY( !rw_ok ) ) FD_LOG_CRIT(( "fd_accdb_open_rw failed" ));
     982             : 
     983          33 :   void const * data = fd_account_data( meta );
     984          33 :   fd_accdb_ref_lamports_set(        rw, meta->lamports   );
     985          33 :   fd_accdb_ref_owner_set   (        rw, meta->owner      );
     986          33 :   fd_accdb_ref_exec_bit_set(        rw, meta->executable );
     987          33 :   fd_accdb_ref_data_set    ( accdb, rw, data, meta->dlen );
     988          33 :   fd_accdb_ref_slot_set    (        rw, xid->ul[0]       );
     989             : 
     990          33 :   fd_accdb_close_rw( accdb, rw );
     991          33 : }
     992             : 
     993             : /* fd_runtime_save_account persists a transaction account to the account
     994             :    database and updates the bank lthash.
     995             : 
     996             :    This function:
     997             :    - Loads the previous account revision
     998             :    - Computes the LtHash of the previous revision
     999             :    - Computes the LtHash of the new revision
    1000             :    - Removes/adds the previous/new revision's LtHash
    1001             :    - Saves the new version of the account to funk
    1002             :    - Sends updates to metrics and capture infra
    1003             : 
    1004             :    Returns FD_RUNTIME_SAVE_* */
    1005             : 
    1006             : static int
    1007             : fd_runtime_save_account( fd_accdb_user_t *         accdb,
    1008             :                          fd_funk_txn_xid_t const * xid,
    1009             :                          fd_pubkey_t const *       pubkey,
    1010             :                          fd_account_meta_t *       meta,
    1011             :                          fd_bank_t *               bank,
    1012          42 :                          fd_capture_ctx_t *        capture_ctx ) {
    1013          42 :   fd_lthash_value_t lthash_post[1];
    1014          42 :   fd_lthash_value_t lthash_prev[1];
    1015             : 
    1016             :   /* Update LtHash
    1017             :      - Query old version of account and hash it
    1018             :      - Hash new version of account */
    1019          42 :   fd_accdb_ro_t ro[1];
    1020          42 :   int old_exist = 0;
    1021          42 :   if( fd_accdb_open_ro( accdb, ro, xid, pubkey ) ) {
    1022          39 :     old_exist = fd_accdb_ref_lamports( ro )!=0UL;
    1023          39 :     fd_hashes_account_lthash(
    1024          39 :       pubkey,
    1025          39 :       ro->meta,
    1026          39 :       fd_accdb_ref_data_const( ro ),
    1027          39 :       lthash_prev );
    1028          39 :     fd_accdb_close_ro( accdb, ro );
    1029          39 :   } else {
    1030           3 :     old_exist = 0;
    1031           3 :     fd_lthash_zero( lthash_prev );
    1032           3 :   }
    1033          42 :   int new_exist = meta->lamports!=0UL;
    1034             : 
    1035             :   /* FIXME don't calculate LtHash if (!old_exist && !new_exist)
    1036             :            This change is blocked by solcap v2, which is not smart
    1037             :            enough to understand that (lthash+0==lthash). */
    1038          42 :   fd_hashes_update_lthash1( lthash_post, lthash_prev, pubkey, meta, bank, capture_ctx );
    1039             : 
    1040             :   /* The first 32 bytes of an LtHash with a single input element are
    1041             :      equal to the BLAKE3_256 hash of an account.  Therefore, comparing
    1042             :      the first 32 bytes is a cryptographically secure equality check
    1043             :      for an account. */
    1044          42 :   int changed = 0!=memcmp( lthash_post->bytes, lthash_prev->bytes, 32UL );
    1045             : 
    1046          42 :   if( changed ) {
    1047          33 :     fd_runtime_finalize_account( accdb, xid, pubkey, meta );
    1048          33 :   }
    1049             : 
    1050          42 :   int save_type = (old_exist<<1) | (new_exist);
    1051          42 :   if( save_type==FD_RUNTIME_SAVE_MODIFY && !changed ) {
    1052           6 :     save_type = FD_RUNTIME_SAVE_UNCHANGED;
    1053           6 :   }
    1054          42 :   return save_type;
    1055          42 : }
    1056             : 
    1057             : /* fd_runtime_commit_txn is a helper used by the non-tpool transaction
    1058             :    executor to finalize borrowed account changes back into funk. It also
    1059             :    handles txncache insertion and updates to the vote/stake cache.
    1060             :    TODO: This function should probably be moved to fd_executor.c. */
    1061             : 
    1062             : void
    1063             : fd_runtime_commit_txn( fd_runtime_t * runtime,
    1064             :                        fd_bank_t *    bank,
    1065          24 :                        fd_txn_out_t * txn_out ) {
    1066             : 
    1067          24 :   if( FD_UNLIKELY( !txn_out->err.is_committable ) ) {
    1068           0 :     FD_LOG_CRIT(( "fd_runtime_commit_txn: transaction is not committable" ));
    1069           0 :   }
    1070             : 
    1071          24 :   txn_out->details.commit_start_timestamp = fd_tickcount();
    1072             : 
    1073             :   /* Release executable accounts */
    1074             : 
    1075          24 :   for( ulong i=0UL; i<runtime->accounts.executable_cnt; i++ ) {
    1076           0 :     fd_accdb_close_ro( runtime->accdb, &runtime->accounts.executable[i] );
    1077           0 :   }
    1078          24 :   runtime->accounts.executable_cnt = 0UL;
    1079             : 
    1080             :   /* Release read-only accounts */
    1081             : 
    1082          72 :   for( ulong i=0UL; i<txn_out->accounts.cnt; i++ ) {
    1083          48 :     if( !txn_out->accounts.is_writable[i] ) {
    1084           6 :       fd_accdb_close_ro( runtime->accdb, txn_out->accounts.account[i].ro );
    1085           6 :     }
    1086          48 :   }
    1087             : 
    1088          24 :   fd_funk_txn_xid_t xid = { .ul = { fd_bank_slot_get( bank ), bank->data->idx } };
    1089             : 
    1090          24 :   if( FD_UNLIKELY( txn_out->err.txn_err ) ) {
    1091             : 
    1092             :     /* Save the fee_payer. Everything but the fee balance should be reset.
    1093             :        TODO: an optimization here could be to use a dirty flag in the
    1094             :        borrowed account. If the borrowed account data has been changed in
    1095             :        any way, then the full account can be rolled back as it is done now.
    1096             :        However, most of the time the account data is not changed, and only
    1097             :        the lamport balance has to change. */
    1098             : 
    1099             :     /* With nonce account rollbacks, there are three cases:
    1100             :        1. No nonce account in the transaction
    1101             :        2. Nonce account is the fee payer
    1102             :        3. Nonce account is not the fee payer
    1103             : 
    1104             :        We should always rollback the nonce account first. Note that the nonce account may be the fee payer (case 2). */
    1105           0 :     if( txn_out->accounts.nonce_idx_in_txn!=ULONG_MAX ) {
    1106           0 :       int save_type =
    1107           0 :         fd_runtime_save_account(
    1108           0 :             runtime->accdb,
    1109           0 :             &xid,
    1110           0 :             &txn_out->accounts.keys[txn_out->accounts.nonce_idx_in_txn],
    1111           0 :             txn_out->accounts.rollback_nonce,
    1112           0 :             bank,
    1113           0 :             runtime->log.capture_ctx );
    1114           0 :       runtime->metrics.txn_account_save[ save_type ]++;
    1115           0 :     }
    1116             :     /* Now, we must only save the fee payer if the nonce account was not the fee payer (because that was already saved above) */
    1117           0 :     if( FD_LIKELY( txn_out->accounts.nonce_idx_in_txn!=FD_FEE_PAYER_TXN_IDX ) ) {
    1118           0 :       int save_type =
    1119           0 :         fd_runtime_save_account(
    1120           0 :             runtime->accdb,
    1121           0 :             &xid,
    1122           0 :             &txn_out->accounts.keys[FD_FEE_PAYER_TXN_IDX],
    1123           0 :             txn_out->accounts.rollback_fee_payer,
    1124           0 :             bank,
    1125           0 :             runtime->log.capture_ctx );
    1126           0 :       runtime->metrics.txn_account_save[ save_type ]++;
    1127           0 :     }
    1128          24 :   } else {
    1129             : 
    1130             : 
    1131          24 :     fd_top_votes_t * top_votes = fd_bank_top_votes_modify( bank );
    1132          72 :     for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
    1133             :       /* We are only interested in saving writable accounts and the fee
    1134             :          payer account. */
    1135          48 :       if( !txn_out->accounts.is_writable[i] ) {
    1136           6 :         continue;
    1137           6 :       }
    1138             : 
    1139          42 :       fd_pubkey_t const * pubkey  = &txn_out->accounts.keys[i];
    1140          42 :       fd_accdb_rw_t *     account = &txn_out->accounts.account[i];
    1141             : 
    1142             :       /* Tips for bundles are collected in the bank: a user submitting a
    1143             :          bundle must include a instruction that transfers lamports to
    1144             :          a specific tip account.  Tips accumulated through the slot. */
    1145          42 :       if( fd_pack_tip_is_tip_account( fd_type_pun_const( pubkey->uc ) ) ) {
    1146           0 :        txn_out->details.tips += fd_ulong_sat_sub( fd_accdb_ref_lamports( account->ro ), runtime->accounts.starting_lamports[i] );
    1147           0 :       }
    1148             : 
    1149          42 :       if( txn_out->accounts.stake_update[i] ) {
    1150           0 :         fd_stakes_update_stake_delegation( pubkey, account->meta, bank );
    1151           0 :       }
    1152          42 :       if( fd_pubkey_eq( fd_accdb_ref_owner( account->ro ), &fd_solana_vote_program_id ) ) {
    1153           0 :         if( FD_UNLIKELY( fd_accdb_ref_lamports( account->ro )==0UL || !fd_vsv_is_correct_size_and_initialized( account->meta ) ) ) {
    1154           0 :           fd_top_votes_invalidate( top_votes, pubkey );
    1155           0 :         } else {
    1156           0 :           fd_vote_block_timestamp_t last_vote = fd_vsv_get_vote_block_timestamp( fd_account_data( account->meta ), account->meta->dlen );
    1157           0 :           fd_top_votes_update( top_votes, pubkey, last_vote.slot, last_vote.timestamp );
    1158           0 :         }
    1159           0 :       }
    1160             : 
    1161             :       /* TODO: vote update is currently unused */
    1162             : 
    1163          42 :       int save_type = fd_runtime_save_account( runtime->accdb, &xid, pubkey, account->meta, bank, runtime->log.capture_ctx );
    1164          42 :       runtime->metrics.txn_account_save[ save_type ]++;
    1165          42 :     }
    1166             : 
    1167             :     /* Atomically add all accumulated tips to the bank once after processing all accounts */
    1168          24 :     if( txn_out->details.tips>0UL )
    1169           0 :       FD_ATOMIC_FETCH_AND_ADD( fd_bank_tips_modify( bank ), txn_out->details.tips );
    1170             : 
    1171             :     /* We need to queue any existing program accounts that may have
    1172             :        been deployed / upgraded for reverification in the program
    1173             :        cache since their programdata may have changed. ELF / sBPF
    1174             :        metadata will need to be updated. */
    1175          24 :     ulong current_slot = fd_bank_slot_get( bank );
    1176          24 :     for( uchar i=0; i<txn_out->details.programs_to_reverify_cnt; i++ ) {
    1177           0 :       fd_pubkey_t const * program_key = &txn_out->details.programs_to_reverify[i];
    1178           0 :       fd_progcache_invalidate( runtime->progcache, &xid, program_key, current_slot );
    1179           0 :     }
    1180          24 :   }
    1181             : 
    1182             :   /* Accumulate block-level information to the bank. */
    1183             : 
    1184          24 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_txn_count_modify( bank ),       1UL );
    1185          24 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_execution_fees_modify( bank ),  txn_out->details.execution_fee );
    1186          24 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_priority_fees_modify( bank ),   txn_out->details.priority_fee );
    1187          24 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_signature_count_modify( bank ), txn_out->details.signature_count );
    1188             : 
    1189          24 :   if( !txn_out->details.is_simple_vote ) {
    1190          24 :     FD_ATOMIC_FETCH_AND_ADD( fd_bank_nonvote_txn_count_modify( bank ), 1 );
    1191          24 :     if( FD_UNLIKELY( txn_out->err.exec_err ) ) {
    1192           0 :       FD_ATOMIC_FETCH_AND_ADD( fd_bank_nonvote_failed_txn_count_modify( bank ), 1 );
    1193           0 :     }
    1194          24 :   }
    1195             : 
    1196          24 :   if( FD_UNLIKELY( txn_out->err.exec_err ) ) {
    1197           0 :     FD_ATOMIC_FETCH_AND_ADD( fd_bank_failed_txn_count_modify( bank ), 1 );
    1198           0 :   }
    1199             : 
    1200          24 :   FD_ATOMIC_FETCH_AND_ADD( fd_bank_total_compute_units_used_modify( bank ), txn_out->details.compute_budget.compute_unit_limit - txn_out->details.compute_budget.compute_meter );
    1201             : 
    1202             :   /* Update the cost tracker. */
    1203             : 
    1204          24 :   fd_cost_tracker_t * cost_tracker = fd_bank_cost_tracker_locking_modify( bank );
    1205          24 :   int res = fd_cost_tracker_try_add_cost( cost_tracker, txn_out );
    1206          24 :   if( FD_UNLIKELY( res!=FD_COST_TRACKER_SUCCESS ) ) {
    1207           0 :     FD_LOG_DEBUG(( "fd_runtime_commit_txn: transaction failed to fit into block %d", res ));
    1208           0 :     txn_out->err.is_committable = 0;
    1209           0 :     txn_out->err.txn_err        = fd_cost_tracker_err_to_runtime_err( res );
    1210           0 :   }
    1211          24 :   fd_bank_cost_tracker_end_locking_modify( bank );
    1212             : 
    1213             :   /* Finally, update the status cache. */
    1214             : 
    1215          24 :   if( FD_LIKELY( runtime->status_cache && txn_out->accounts.nonce_idx_in_txn==ULONG_MAX ) ) {
    1216             :     /* In Agave, durable nonce transactions are inserted to the status
    1217             :        cache the same as any others, but this is only to serve RPC
    1218             :        requests, they do not need to be in there for correctness as the
    1219             :        nonce mechanism itself prevents double spend.  We skip this logic
    1220             :        entirely to simplify and improve performance of the txn cache. */
    1221             : 
    1222           0 :     fd_txncache_insert( runtime->status_cache, bank->data->txncache_fork_id, txn_out->details.blockhash.uc, txn_out->details.blake_txn_msg_hash.uc );
    1223           0 :   }
    1224             : 
    1225          72 :   for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
    1226          48 :     if( txn_out->accounts.is_writable[i] ) {
    1227          42 :       fd_acc_pool_release( runtime->acc_pool, fd_type_pun( txn_out->accounts.account[i].meta ) );
    1228          42 :     }
    1229          48 :   }
    1230             : 
    1231          24 :   fd_acc_pool_release( runtime->acc_pool, txn_out->accounts.rollback_nonce_mem );
    1232          24 :   fd_acc_pool_release( runtime->acc_pool, txn_out->accounts.rollback_fee_payer_mem );
    1233          24 : }
    1234             : 
    1235             : void
    1236             : fd_runtime_cancel_txn( fd_runtime_t * runtime,
    1237          57 :                        fd_txn_out_t * txn_out ) {
    1238             : 
    1239          57 :   if( FD_UNLIKELY( txn_out->err.is_committable ) ) {
    1240           0 :     FD_LOG_CRIT(( "fd_runtime_cancel_txn: transaction is committable" ));
    1241           0 :   }
    1242             : 
    1243          57 :   if( !txn_out->accounts.is_setup ) {
    1244           0 :     return;
    1245           0 :   }
    1246             : 
    1247          57 :   for( ulong i=0UL; i<runtime->accounts.executable_cnt; i++ ) {
    1248           0 :     fd_accdb_close_ro( runtime->accdb, &runtime->accounts.executable[i] );
    1249           0 :   }
    1250          57 :   runtime->accounts.executable_cnt = 0UL;
    1251             : 
    1252         270 :   for( ushort i=0; i<txn_out->accounts.cnt; i++ ) {
    1253         213 :     if( txn_out->accounts.is_writable[i] ) {
    1254         126 :       fd_acc_pool_release( runtime->acc_pool, fd_type_pun( txn_out->accounts.account[i].meta ) );
    1255         126 :     } else {
    1256          87 :       fd_accdb_close_ro( runtime->accdb, txn_out->accounts.account[i].ro );
    1257          87 :     }
    1258         213 :   }
    1259             : 
    1260          57 :   fd_acc_pool_release( runtime->acc_pool, txn_out->accounts.rollback_nonce_mem );
    1261          57 :   fd_acc_pool_release( runtime->acc_pool, txn_out->accounts.rollback_fee_payer_mem );
    1262          57 : }
    1263             : 
    1264             : static inline void
    1265          93 : fd_runtime_reset_runtime( fd_runtime_t * runtime ) {
    1266          93 :   runtime->instr.stack_sz     = 0;
    1267          93 :   runtime->instr.trace_length = 0UL;
    1268             :   /* The only condition where the executable count of the current
    1269             :      runtime is not 0 is when a bundle of transaction is being executed.
    1270             :      In this case, close any outstanding executable accounts. */
    1271          93 :   for( ulong i=0UL; i<runtime->accounts.executable_cnt; i++ ) {
    1272           0 :     fd_accdb_close_ro( runtime->accdb, &runtime->accounts.executable[i] );
    1273           0 :   }
    1274          93 :   runtime->accounts.executable_cnt = 0UL;
    1275             : 
    1276          93 : }
    1277             : 
    1278             : static inline void
    1279             : fd_runtime_new_txn_out( fd_txn_in_t const * txn_in,
    1280          93 :                         fd_txn_out_t *      txn_out ) {
    1281          93 :   txn_out->details.prep_start_timestamp   = fd_tickcount();
    1282          93 :   txn_out->details.load_start_timestamp   = LONG_MAX;
    1283          93 :   txn_out->details.exec_start_timestamp   = LONG_MAX;
    1284          93 :   txn_out->details.commit_start_timestamp = LONG_MAX;
    1285             : 
    1286          93 :   fd_compute_budget_details_new( &txn_out->details.compute_budget );
    1287             : 
    1288          93 :   txn_out->details.loaded_accounts_data_size = 0UL;
    1289          93 :   txn_out->details.accounts_resize_delta     = 0L;
    1290             : 
    1291          93 :   txn_out->details.return_data.len = 0UL;
    1292          93 :   memset( txn_out->details.return_data.program_id.key, 0, sizeof(fd_pubkey_t) );
    1293             : 
    1294          93 :   txn_out->details.tips            = 0UL;
    1295          93 :   txn_out->details.execution_fee   = 0UL;
    1296          93 :   txn_out->details.priority_fee    = 0UL;
    1297          93 :   txn_out->details.signature_count = 0UL;
    1298             : 
    1299          93 :   txn_out->details.programs_to_reverify_cnt = 0UL;
    1300             : 
    1301          93 :   txn_out->details.signature_count = TXN( txn_in->txn )->signature_cnt;
    1302          93 :   txn_out->details.is_simple_vote  = fd_txn_is_simple_vote_transaction( TXN( txn_in->txn ), txn_in->txn->payload );
    1303             : 
    1304          93 :   fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_in->txn->payload + TXN( txn_in->txn )->recent_blockhash_off);
    1305          93 :   memcpy( txn_out->details.blockhash.uc, blockhash->hash, sizeof(fd_hash_t) );
    1306             : 
    1307          93 :   txn_out->accounts.is_setup           = 0;
    1308          93 :   txn_out->accounts.cnt                = 0UL;
    1309          93 :   txn_out->accounts.rollback_nonce     = NULL;
    1310          93 :   txn_out->accounts.rollback_fee_payer = NULL;
    1311          93 :   memset( txn_out->accounts.stake_update, 0, sizeof(txn_out->accounts.stake_update) );
    1312          93 :   memset( txn_out->accounts.vote_update, 0, sizeof(txn_out->accounts.vote_update) );
    1313             : 
    1314          93 :   txn_out->err.is_committable = 1;
    1315          93 :   txn_out->err.is_fees_only   = 0;
    1316          93 :   txn_out->err.txn_err        = FD_RUNTIME_EXECUTE_SUCCESS;
    1317          93 :   txn_out->err.exec_err       = FD_EXECUTOR_INSTR_SUCCESS;
    1318          93 :   txn_out->err.exec_err_kind  = FD_EXECUTOR_ERR_KIND_NONE;
    1319          93 :   txn_out->err.exec_err_idx   = INT_MAX;
    1320          93 :   txn_out->err.custom_err     = 0;
    1321          93 : }
    1322             : 
    1323             : void
    1324             : fd_runtime_prepare_and_execute_txn( fd_runtime_t *       runtime,
    1325             :                                     fd_bank_t *          bank,
    1326             :                                     fd_txn_in_t const *  txn_in,
    1327          93 :                                     fd_txn_out_t *       txn_out ) {
    1328             : 
    1329          93 :   fd_runtime_reset_runtime( runtime );
    1330             : 
    1331          93 :   fd_runtime_new_txn_out( txn_in, txn_out );
    1332             : 
    1333             :   /* Set up the core account keys before any pre-execution checks.
    1334             :      This is needed both for execution and for protobuf context
    1335             :      dumping. */
    1336          93 :   fd_executor_setup_txn_account_keys( txn_in, txn_out );
    1337             : 
    1338          93 : # if FD_HAS_FLATCC
    1339          93 :   uchar dump_txn = !!( runtime->log.dump_proto_ctx &&
    1340          93 :                        fd_bank_slot_get( bank ) >= runtime->log.dump_proto_ctx->dump_proto_start_slot &&
    1341          93 :                        runtime->log.dump_proto_ctx->dump_txn_to_pb );
    1342             : 
    1343             :   /* Phase 1: Capture TxnContext before execution. */
    1344          93 :   if( FD_UNLIKELY( dump_txn ) ) {
    1345           0 :     if( runtime->log.txn_dump_ctx ) {
    1346           0 :       fd_dump_txn_context_to_protobuf( runtime->log.txn_dump_ctx, runtime, bank, txn_in, txn_out );
    1347           0 :     } else {
    1348           0 :       fd_dump_txn_to_protobuf( runtime, bank, txn_in, txn_out );
    1349           0 :     }
    1350           0 :   }
    1351          93 : # endif
    1352             : 
    1353             :   /* Transaction sanitization.  If a transaction can't be commited or is
    1354             :      fees-only, we return early. */
    1355          93 :   txn_out->err.txn_err = fd_runtime_pre_execute_check( runtime, bank, txn_in, txn_out );
    1356          93 :   ulong cu_before = txn_out->details.compute_budget.compute_meter;
    1357             : 
    1358             :   /* Execute the transaction if eligible to do so. */
    1359          93 :   if( FD_LIKELY( txn_out->err.is_committable ) ) {
    1360          90 :     if( FD_LIKELY( !txn_out->err.is_fees_only ) ) {
    1361          90 :       txn_out->err.txn_err = fd_execute_txn( runtime, bank, txn_in, txn_out );
    1362          90 :     }
    1363          90 :     fd_cost_tracker_calculate_cost( bank, txn_in, txn_out );
    1364          90 :   }
    1365          93 :   ulong cu_after = txn_out->details.compute_budget.compute_meter;
    1366          93 :   runtime->metrics.cu_cum += fd_ulong_sat_sub( cu_before, cu_after );
    1367             : 
    1368          93 : # if FD_HAS_FLATCC
    1369             :   /* Phase 2: Capture TxnResult after execution and write to disk. */
    1370          93 :   if( FD_UNLIKELY( dump_txn && runtime->log.txn_dump_ctx ) ) {
    1371           0 :     fd_dump_txn_result_to_protobuf( runtime->log.txn_dump_ctx, txn_in, txn_out, bank, txn_out->err.txn_err );
    1372           0 :     fd_dump_txn_fixture_to_file( runtime->log.txn_dump_ctx, runtime->log.dump_proto_ctx, txn_in );
    1373           0 :   }
    1374          93 : # endif
    1375          93 : }
    1376             : 
    1377             : /* fd_executor_txn_verify and fd_runtime_pre_execute_check are responisble
    1378             :    for the bulk of the pre-transaction execution checks in the runtime.
    1379             :    They aim to preserve the ordering present in the Agave client to match
    1380             :    parity in terms of error codes. Sigverify is kept separate from the rest
    1381             :    of the transaction checks for fuzzing convenience.
    1382             : 
    1383             :    For reference this is the general code path which contains all relevant
    1384             :    pre-transactions checks in the v2.0.x Agave client from upstream
    1385             :    to downstream is as follows:
    1386             : 
    1387             :    confirm_slot_entries() which calls verify_ticks() and
    1388             :    verify_transaction(). verify_transaction() calls verify_and_hash_message()
    1389             :    and verify_precompiles() which parallels fd_executor_txn_verify() and
    1390             :    fd_executor_verify_transaction().
    1391             : 
    1392             :    process_entries() contains a duplicate account check which is part of
    1393             :    agave account lock acquiring. This is checked inline in
    1394             :    fd_runtime_pre_execute_check().
    1395             : 
    1396             :    load_and_execute_transactions() contains the function check_transactions().
    1397             :    This contains check_age() and check_status_cache() which is paralleled by
    1398             :    fd_executor_check_transaction_age_and_compute_budget_limits() and
    1399             :    fd_executor_check_status_cache() respectively.
    1400             : 
    1401             :    load_and_execute_sanitized_transactions() contains validate_fees()
    1402             :    which is responsible for executing the compute budget instructions,
    1403             :    validating the fee payer and collecting the fee. This is mirrored in
    1404             :    firedancer with fd_executor_compute_budget_program_execute_instructions()
    1405             :    and fd_executor_collect_fees(). load_and_execute_sanitized_transactions()
    1406             :    also checks the total data size of the accounts in load_accounts() and
    1407             :    validates the program accounts in load_transaction_accounts(). This
    1408             :    is paralled by fd_executor_load_transaction_accounts(). */
    1409             : 
    1410             : 
    1411             : /******************************************************************************/
    1412             : /* Genesis                                                                    */
    1413             : /*******************************************************************************/
    1414             : 
    1415             : static void
    1416             : fd_runtime_genesis_init_program( fd_bank_t *               bank,
    1417             :                                  fd_accdb_user_t *         accdb,
    1418             :                                  fd_funk_txn_xid_t const * xid,
    1419           0 :                                  fd_capture_ctx_t *        capture_ctx ) {
    1420             : 
    1421           0 :   fd_sysvar_clock_init( bank, accdb, xid, capture_ctx );
    1422           0 :   fd_sysvar_rent_init( bank, accdb, xid, capture_ctx );
    1423             : 
    1424           0 :   fd_sysvar_slot_history_init( bank, accdb, xid, capture_ctx );
    1425           0 :   fd_sysvar_epoch_schedule_init( bank, accdb, xid, capture_ctx );
    1426           0 :   fd_sysvar_recent_hashes_init( bank, accdb, xid, capture_ctx );
    1427           0 :   fd_sysvar_stake_history_init( bank, accdb, xid, capture_ctx );
    1428           0 :   fd_sysvar_last_restart_slot_init( bank, accdb, xid, capture_ctx );
    1429             : 
    1430           0 :   fd_builtin_programs_init( bank, accdb, xid, capture_ctx );
    1431           0 :   fd_stake_program_config_init( accdb, xid );
    1432           0 : }
    1433             : 
    1434             : static void
    1435             : fd_runtime_init_bank_from_genesis( fd_banks_t *              banks,
    1436             :                                    fd_bank_t *               bank,
    1437             :                                    fd_runtime_stack_t *      runtime_stack,
    1438             :                                    fd_accdb_user_t *         accdb,
    1439             :                                    fd_funk_txn_xid_t const * xid,
    1440             :                                    fd_genesis_t const *      genesis,
    1441             :                                    uchar const *             genesis_blob,
    1442           0 :                                    fd_hash_t const *         genesis_hash ) {
    1443             : 
    1444           0 :   fd_bank_parent_slot_set( bank, ULONG_MAX );
    1445           0 :   fd_bank_poh_set( bank, *genesis_hash );
    1446             : 
    1447           0 :   fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
    1448           0 :   memset( bank_hash->hash, 0, FD_SHA256_HASH_SZ );
    1449             : 
    1450           0 :   uint128 target_tick_duration = (uint128)genesis->poh.tick_duration_secs * 1000000000UL + (uint128)genesis->poh.tick_duration_ns;
    1451             : 
    1452           0 :   fd_epoch_schedule_t * epoch_schedule = fd_bank_epoch_schedule_modify( bank );
    1453           0 :   epoch_schedule->leader_schedule_slot_offset = genesis->epoch_schedule.leader_schedule_slot_offset;
    1454           0 :   epoch_schedule->warmup                      = genesis->epoch_schedule.warmup;
    1455           0 :   epoch_schedule->first_normal_epoch          = genesis->epoch_schedule.first_normal_epoch;
    1456           0 :   epoch_schedule->first_normal_slot           = genesis->epoch_schedule.first_normal_slot;
    1457           0 :   epoch_schedule->slots_per_epoch             = genesis->epoch_schedule.slots_per_epoch;
    1458             : 
    1459           0 :   fd_rent_t * rent = fd_bank_rent_modify( bank );
    1460           0 :   rent->lamports_per_uint8_year = genesis->rent.lamports_per_uint8_year;
    1461           0 :   rent->exemption_threshold     = genesis->rent.exemption_threshold;
    1462           0 :   rent->burn_percent            = genesis->rent.burn_percent;
    1463             : 
    1464           0 :   fd_inflation_t * inflation = fd_bank_inflation_modify( bank );
    1465           0 :   inflation->initial         = genesis->inflation.initial;
    1466           0 :   inflation->terminal        = genesis->inflation.terminal;
    1467           0 :   inflation->taper           = genesis->inflation.taper;
    1468           0 :   inflation->foundation      = genesis->inflation.foundation;
    1469           0 :   inflation->foundation_term = genesis->inflation.foundation_term;
    1470           0 :   inflation->unused          = 0.0;
    1471             : 
    1472           0 :   fd_bank_block_height_set( bank, 0UL );
    1473             : 
    1474           0 :   {
    1475             :     /* FIXME Why is there a previous blockhash at genesis?  Why is the
    1476             :              last_hash field an option type in Agave, if even the first
    1477             :              real block has a previous blockhash? */
    1478           0 :     fd_blockhashes_t *    bhq  = fd_blockhashes_init( fd_bank_block_hash_queue_modify( bank ), 0UL );
    1479           0 :     fd_blockhash_info_t * info = fd_blockhashes_push_new( bhq, genesis_hash );
    1480           0 :     info->fee_calculator.lamports_per_signature = 0UL;
    1481           0 :   }
    1482             : 
    1483           0 :   fd_fee_rate_governor_t * fee_rate_governor = fd_bank_fee_rate_governor_modify( bank );
    1484           0 :   fee_rate_governor->target_lamports_per_signature = genesis->fee_rate_governor.target_lamports_per_signature;
    1485           0 :   fee_rate_governor->target_signatures_per_slot    = genesis->fee_rate_governor.target_signatures_per_slot;
    1486           0 :   fee_rate_governor->min_lamports_per_signature    = genesis->fee_rate_governor.min_lamports_per_signature;
    1487           0 :   fee_rate_governor->max_lamports_per_signature    = genesis->fee_rate_governor.max_lamports_per_signature;
    1488           0 :   fee_rate_governor->burn_percent                  = genesis->fee_rate_governor.burn_percent;
    1489             : 
    1490           0 :   fd_bank_max_tick_height_set( bank, genesis->poh.ticks_per_slot * (fd_bank_slot_get( bank ) + 1) );
    1491             : 
    1492           0 :   fd_bank_hashes_per_tick_set( bank, genesis->poh.hashes_per_tick );
    1493             : 
    1494           0 :   fd_bank_ns_per_slot_set( bank, (fd_w_u128_t) { .ud=target_tick_duration * genesis->poh.ticks_per_slot } );
    1495             : 
    1496           0 :   fd_bank_ticks_per_slot_set( bank, genesis->poh.ticks_per_slot );
    1497             : 
    1498           0 :   fd_bank_genesis_creation_time_set( bank, genesis->creation_time );
    1499             : 
    1500           0 :   fd_bank_slots_per_year_set( bank, SECONDS_PER_YEAR * (1000000000.0 / (double)target_tick_duration) / (double)genesis->poh.ticks_per_slot );
    1501             : 
    1502           0 :   fd_bank_signature_count_set( bank, 0UL );
    1503             : 
    1504             :   /* Derive epoch stakes */
    1505             : 
    1506           0 :   fd_stake_delegations_t * stake_delegations = fd_banks_stake_delegations_root_query( banks );
    1507           0 :   if( FD_UNLIKELY( !stake_delegations ) ) {
    1508           0 :     FD_LOG_CRIT(( "Failed to join and new a stake delegations" ));
    1509           0 :   }
    1510             : 
    1511           0 :   ulong capitalization = 0UL;
    1512             : 
    1513           0 :   for( ulong i=0UL; i<genesis->account_cnt; i++ ) {
    1514           0 :     fd_genesis_account_t account[1];
    1515           0 :     fd_genesis_account( genesis, genesis_blob, account, i );
    1516             : 
    1517           0 :     capitalization = fd_ulong_sat_add( capitalization, account->meta.lamports );
    1518             : 
    1519           0 :     uchar const * acc_data = account->data;
    1520             : 
    1521           0 :     if( !memcmp( account->meta.owner, fd_solana_stake_program_id.key, sizeof(fd_pubkey_t) ) ) {
    1522             :       /* If an account is a stake account, then it must be added to the
    1523             :          stake delegations cache. We should only add stake accounts that
    1524             :          have a valid non-zero stake. */
    1525           0 :       fd_stake_state_v2_t stake_state = {0};
    1526           0 :       if( FD_UNLIKELY( !fd_bincode_decode_static(
    1527           0 :           stake_state_v2, &stake_state,
    1528           0 :           acc_data, account->meta.dlen ) ) ) {
    1529           0 :         FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, stake_b58 );
    1530           0 :         FD_LOG_ERR(( "Failed to deserialize genesis stake account %s", stake_b58 ));
    1531           0 :       }
    1532           0 :       if( !fd_stake_state_v2_is_stake( &stake_state )     ) continue;
    1533           0 :       if( !stake_state.inner.stake.stake.delegation.stake ) continue;
    1534             : 
    1535           0 :       if( FD_UNLIKELY( stake_state.inner.stake.stake.delegation.warmup_cooldown_rate!=0.25 &&
    1536           0 :                        stake_state.inner.stake.stake.delegation.warmup_cooldown_rate!=0.09 ) ) {
    1537           0 :         FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, stake_b58 );
    1538           0 :         FD_LOG_ERR(( "Invalid warmup cooldown rate %f for stake account %s", stake_state.inner.stake.stake.delegation.warmup_cooldown_rate, stake_b58 ));
    1539           0 :       }
    1540             : 
    1541           0 :       fd_stake_delegations_update(
    1542           0 :           stake_delegations,
    1543           0 :           &account->pubkey,
    1544           0 :           &stake_state.inner.stake.stake.delegation.voter_pubkey,
    1545           0 :           stake_state.inner.stake.stake.delegation.stake,
    1546           0 :           stake_state.inner.stake.stake.delegation.activation_epoch,
    1547           0 :           stake_state.inner.stake.stake.delegation.deactivation_epoch,
    1548           0 :           stake_state.inner.stake.stake.credits_observed,
    1549           0 :           stake_state.inner.stake.stake.delegation.warmup_cooldown_rate );
    1550             : 
    1551           0 :     } else if( !memcmp( account->meta.owner, fd_solana_feature_program_id.key, sizeof(fd_pubkey_t) ) ) {
    1552             :       /* Feature Account */
    1553             : 
    1554             :       /* Scan list of feature IDs to resolve address=>feature offset */
    1555           0 :       fd_feature_id_t const *found = NULL;
    1556           0 :       for( fd_feature_id_t const * id = fd_feature_iter_init();
    1557           0 :            !fd_feature_iter_done( id );
    1558           0 :            id = fd_feature_iter_next( id ) ) {
    1559           0 :         if( fd_pubkey_eq( &account->pubkey, &id->id ) ) {
    1560           0 :           found = id;
    1561           0 :           break;
    1562           0 :         }
    1563           0 :       }
    1564             : 
    1565           0 :       if( found ) {
    1566             :         /* Load feature activation */
    1567           0 :         fd_feature_t feature[1];
    1568           0 :         if( FD_UNLIKELY( !fd_feature_decode( feature, acc_data, account->meta.dlen ) ) ) {
    1569           0 :           FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, addr_b58 );
    1570           0 :           FD_LOG_WARNING(( "genesis contains corrupt feature account %s", addr_b58 ));
    1571           0 :           FD_LOG_HEXDUMP_ERR(( "data", acc_data, account->meta.dlen ));
    1572           0 :         }
    1573           0 :         fd_features_t * features = fd_bank_features_modify( bank );
    1574           0 :         if( feature->is_active ) {
    1575           0 :           FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, pubkey_b58 );
    1576           0 :           FD_LOG_DEBUG(( "feature %s activated at slot %lu (genesis)", pubkey_b58, feature->activation_slot ));
    1577           0 :           fd_features_set( features, found, feature->activation_slot );
    1578           0 :         } else {
    1579           0 :           FD_BASE58_ENCODE_32_BYTES( account->pubkey.uc, pubkey_b58 );
    1580           0 :           FD_LOG_DEBUG(( "feature %s not activated (genesis)", pubkey_b58 ));
    1581           0 :           fd_features_set( features, found, ULONG_MAX );
    1582           0 :         }
    1583           0 :       }
    1584           0 :     }
    1585           0 :   }
    1586             : 
    1587             :   /* fd_refresh_vote_accounts is responsible for updating the vote
    1588             :      states with the total amount of active delegated stake. It does
    1589             :      this by iterating over all active stake delegations and summing up
    1590             :      the amount of stake that is delegated to each vote account. */
    1591             : 
    1592           0 :   ulong new_rate_activation_epoch = 0UL;
    1593             : 
    1594           0 :   fd_stake_history_t stake_history[1];
    1595           0 :   fd_sysvar_stake_history_read( accdb, xid, stake_history );
    1596             : 
    1597           0 :   fd_refresh_vote_accounts(
    1598           0 :       bank,
    1599           0 :       accdb,
    1600           0 :       xid,
    1601           0 :       runtime_stack,
    1602           0 :       stake_delegations,
    1603           0 :       stake_history,
    1604           0 :       &new_rate_activation_epoch );
    1605             : 
    1606           0 :   fd_vote_stakes_t * vote_stakes = fd_bank_vote_stakes_locking_modify( bank );
    1607           0 :   fd_vote_stakes_genesis_fini( vote_stakes );
    1608           0 :   fd_bank_vote_stakes_end_locking_modify( bank );
    1609             : 
    1610           0 :   fd_bank_epoch_set( bank, 0UL );
    1611             : 
    1612           0 :   fd_bank_capitalization_set( bank, capitalization );
    1613           0 : }
    1614             : 
    1615             : static int
    1616             : fd_runtime_process_genesis_block( fd_bank_t *               bank,
    1617             :                                   fd_accdb_user_t *         accdb,
    1618             :                                   fd_funk_txn_xid_t const * xid,
    1619             :                                   fd_capture_ctx_t *        capture_ctx,
    1620           0 :                                   fd_runtime_stack_t *      runtime_stack ) {
    1621             : 
    1622           0 :   fd_hash_t * poh = fd_bank_poh_modify( bank );
    1623           0 :   ulong hashcnt_per_slot = fd_bank_hashes_per_tick_get( bank ) * fd_bank_ticks_per_slot_get( bank );
    1624           0 :   while( hashcnt_per_slot-- ) {
    1625           0 :     fd_sha256_hash( poh->hash, sizeof(fd_hash_t), poh->hash );
    1626           0 :   }
    1627             : 
    1628           0 :   fd_bank_execution_fees_set( bank, 0UL );
    1629             : 
    1630           0 :   fd_bank_priority_fees_set( bank, 0UL );
    1631             : 
    1632           0 :   fd_bank_signature_count_set( bank, 0UL );
    1633             : 
    1634           0 :   fd_bank_txn_count_set( bank, 0UL );
    1635             : 
    1636           0 :   fd_bank_failed_txn_count_set( bank, 0UL );
    1637             : 
    1638           0 :   fd_bank_nonvote_failed_txn_count_set( bank, 0UL );
    1639             : 
    1640           0 :   fd_bank_total_compute_units_used_set( bank, 0UL );
    1641             : 
    1642           0 :   fd_runtime_genesis_init_program( bank, accdb, xid, capture_ctx );
    1643             : 
    1644           0 :   fd_sysvar_slot_history_update( bank, accdb, xid, capture_ctx );
    1645             : 
    1646           0 :   fd_runtime_update_leaders( bank, runtime_stack );
    1647             : 
    1648           0 :   fd_runtime_freeze( bank, accdb, capture_ctx );
    1649             : 
    1650           0 :   fd_lthash_value_t const * lthash = fd_bank_lthash_locking_query( bank );
    1651             : 
    1652           0 :   fd_hash_t const * prev_bank_hash = fd_bank_bank_hash_query( bank );
    1653             : 
    1654           0 :   fd_hash_t * bank_hash = fd_bank_bank_hash_modify( bank );
    1655           0 :   fd_hashes_hash_bank(
    1656           0 :     lthash,
    1657           0 :     prev_bank_hash,
    1658           0 :     (fd_hash_t *)fd_bank_poh_query( bank )->hash,
    1659           0 :     0UL,
    1660           0 :     bank_hash );
    1661             : 
    1662           0 :   fd_bank_lthash_end_locking_query( bank );
    1663             : 
    1664           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    1665           0 : }
    1666             : 
    1667             : void
    1668             : fd_runtime_read_genesis( fd_banks_t *              banks,
    1669             :                          fd_bank_t *               bank,
    1670             :                          fd_accdb_user_t *         accdb,
    1671             :                          fd_funk_txn_xid_t const * xid,
    1672             :                          fd_capture_ctx_t *        capture_ctx,
    1673             :                          fd_hash_t const *         genesis_hash,
    1674             :                          fd_lthash_value_t const * genesis_lthash,
    1675             :                          fd_genesis_t const *      genesis,
    1676             :                          uchar const *             genesis_blob,
    1677           0 :                          fd_runtime_stack_t *      runtime_stack ) {
    1678             : 
    1679           0 :   fd_lthash_value_t * lthash = fd_bank_lthash_locking_modify( bank );
    1680           0 :   *lthash = *genesis_lthash;
    1681           0 :   fd_bank_lthash_end_locking_modify( bank );
    1682             : 
    1683             :   /* Once the accounts have been loaded from the genesis config into
    1684             :      the accounts db, we can initialize the bank state. This involves
    1685             :      setting some fields, and notably setting up the vote and stake
    1686             :      caches which are used for leader scheduling/rewards. */
    1687             : 
    1688           0 :   fd_runtime_init_bank_from_genesis( banks, bank, runtime_stack, accdb, xid, genesis, genesis_blob, genesis_hash );
    1689             : 
    1690             :   /* Write the native programs to the accounts db. */
    1691             : 
    1692           0 :   for( ulong i=0UL; i<genesis->builtin_cnt; i++ ) {
    1693           0 :     fd_genesis_builtin_t builtin[1];
    1694           0 :     fd_genesis_builtin( genesis, genesis_blob, builtin, i );
    1695           0 :     fd_write_builtin_account( bank, accdb, xid, capture_ctx, builtin->pubkey, builtin->data, builtin->dlen );
    1696           0 :   }
    1697             : 
    1698           0 :   fd_features_restore( bank, accdb, xid );
    1699             : 
    1700             :   /* At this point, state related to the bank and the accounts db
    1701             :      have been initialized and we are free to finish executing the
    1702             :      block. In practice, this updates some bank fields (notably the
    1703             :      poh and bank hash). */
    1704             : 
    1705           0 :   int err = fd_runtime_process_genesis_block( bank, accdb, xid, capture_ctx, runtime_stack );
    1706           0 :   if( FD_UNLIKELY( err ) ) FD_LOG_CRIT(( "genesis slot 0 execute failed with error %d", err ));
    1707           0 : }
    1708             : 
    1709             : void
    1710             : fd_runtime_block_execute_finalize( fd_bank_t *        bank,
    1711             :                                    fd_accdb_user_t *  accdb,
    1712           0 :                                    fd_capture_ctx_t * capture_ctx ) {
    1713             : 
    1714             :   /* This slot is now "frozen" and can't be changed anymore. */
    1715           0 :   fd_runtime_freeze( bank, accdb, capture_ctx );
    1716             : 
    1717           0 :   fd_runtime_update_bank_hash( bank, capture_ctx );
    1718           0 : }
    1719             : 
    1720             : 
    1721             : /* Mirrors Agave function solana_sdk::transaction_context::find_index_of_account
    1722             : 
    1723             :    Backward scan over transaction accounts.
    1724             :    Returns -1 if not found.
    1725             : 
    1726             :    https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L233-L238 */
    1727             : 
    1728             : int
    1729             : fd_runtime_find_index_of_account( fd_txn_out_t const * txn_out,
    1730         213 :                                   fd_pubkey_t const *  pubkey ) {
    1731         738 :   for( ulong i=txn_out->accounts.cnt; i>0UL; i-- ) {
    1732         651 :     if( 0==memcmp( pubkey, &txn_out->accounts.keys[ i-1UL ], sizeof(fd_pubkey_t) ) ) {
    1733         126 :       return (int)(i-1UL);
    1734         126 :     }
    1735         651 :   }
    1736          87 :   return -1;
    1737         213 : }
    1738             : 
    1739             : int
    1740             : fd_runtime_get_account_at_index( fd_txn_in_t const *             txn_in,
    1741             :                                  fd_txn_out_t *                  txn_out,
    1742             :                                  ushort                          idx,
    1743        1275 :                                  fd_txn_account_condition_fn_t * condition ) {
    1744        1275 :   if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) {
    1745           0 :     return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1746           0 :   }
    1747             : 
    1748        1275 :   if( FD_LIKELY( condition != NULL ) ) {
    1749         429 :     if( FD_UNLIKELY( !condition( txn_in, txn_out, idx ) ) ) {
    1750          15 :       return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1751          15 :     }
    1752         429 :   }
    1753             : 
    1754        1260 :   return FD_ACC_MGR_SUCCESS;
    1755        1275 : }
    1756             : 
    1757             : int
    1758             : fd_runtime_get_account_with_key( fd_txn_in_t const *             txn_in,
    1759             :                                  fd_txn_out_t *                  txn_out,
    1760             :                                  fd_pubkey_t const *             pubkey,
    1761             :                                  int *                           index_out,
    1762           0 :                                  fd_txn_account_condition_fn_t * condition ) {
    1763           0 :   int index = fd_runtime_find_index_of_account( txn_out, pubkey );
    1764           0 :   if( FD_UNLIKELY( index==-1 ) ) {
    1765           0 :     return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1766           0 :   }
    1767             : 
    1768           0 :   *index_out = index;
    1769             : 
    1770           0 :   return fd_runtime_get_account_at_index( txn_in,
    1771           0 :                                           txn_out,
    1772           0 :                                           (uchar)index,
    1773           0 :                                           condition );
    1774           0 : }
    1775             : 
    1776             : int
    1777             : fd_runtime_get_executable_account( fd_runtime_t *              runtime,
    1778             :                                    fd_txn_in_t const *         txn_in,
    1779             :                                    fd_txn_out_t *              txn_out,
    1780             :                                    fd_pubkey_t const *         pubkey,
    1781           0 :                                    fd_account_meta_t const * * meta ) {
    1782             :   /* First try to fetch the executable account from the existing
    1783             :      borrowed accounts.  If the pubkey is in the account keys, then we
    1784             :      want to re-use that borrowed account since it reflects changes from
    1785             :      prior instructions.  Referencing the read-only executable accounts
    1786             :      list is incorrect behavior when the program data account is written
    1787             :      to in a prior instruction (e.g. program upgrade + invoke within the
    1788             :      same txn) */
    1789             : 
    1790           0 :   fd_txn_account_condition_fn_t * condition = fd_runtime_account_check_exists;
    1791             : 
    1792           0 :   int index;
    1793           0 :   int err = fd_runtime_get_account_with_key( txn_in,
    1794           0 :                                              txn_out,
    1795           0 :                                              pubkey,
    1796           0 :                                              &index,
    1797           0 :                                              condition );
    1798           0 :   if( FD_UNLIKELY( err==FD_ACC_MGR_SUCCESS ) ) {
    1799           0 :     *meta = txn_out->accounts.account[index].meta;
    1800           0 :     return FD_ACC_MGR_SUCCESS;
    1801           0 :   }
    1802             : 
    1803           0 :   for( ushort i=0; i<runtime->accounts.executable_cnt; i++ ) {
    1804           0 :     if( fd_pubkey_eq( pubkey, fd_accdb_ref_address( &runtime->accounts.executable[i] ) ) ) {
    1805           0 :       *meta = runtime->accounts.executable[i].meta;
    1806           0 :       if( FD_UNLIKELY( !fd_account_meta_exists( *meta ) ) ) {
    1807           0 :         return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1808           0 :       }
    1809           0 :       return FD_ACC_MGR_SUCCESS;
    1810           0 :     }
    1811           0 :   }
    1812             : 
    1813           0 :   return FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1814           0 : }
    1815             : 
    1816             : int
    1817             : fd_runtime_get_key_of_account_at_index( fd_txn_out_t *        txn_out,
    1818             :                                         ushort                idx,
    1819         747 :                                         fd_pubkey_t const * * key ) {
    1820             :   /* Return a MissingAccount error if idx is out of bounds.
    1821             :      https://github.com/anza-xyz/agave/blob/v3.1.4/transaction-context/src/lib.rs#L187 */
    1822         747 :   if( FD_UNLIKELY( idx>=txn_out->accounts.cnt ) ) {
    1823           0 :     return FD_EXECUTOR_INSTR_ERR_MISSING_ACC;
    1824           0 :   }
    1825             : 
    1826         747 :   *key = &txn_out->accounts.keys[ idx ];
    1827         747 :   return FD_EXECUTOR_INSTR_SUCCESS;
    1828         747 : }
    1829             : 
    1830             : /* https://github.com/anza-xyz/agave/blob/v2.1.1/sdk/program/src/message/versions/v0/loaded.rs#L162 */
    1831             : int
    1832             : fd_txn_account_is_demotion( const int        idx,
    1833             :                             const fd_txn_t * txn_descriptor,
    1834        5778 :                             const uint       bpf_upgradeable_in_txn ) {
    1835        5778 :   uint is_program = 0U;
    1836       11616 :   for( ulong j=0UL; j<txn_descriptor->instr_cnt; j++ ) {
    1837        5838 :     if( txn_descriptor->instr[j].program_id == idx ) {
    1838           0 :       is_program = 1U;
    1839           0 :       break;
    1840           0 :     }
    1841        5838 :   }
    1842             : 
    1843        5778 :   return (is_program && !bpf_upgradeable_in_txn);
    1844        5778 : }
    1845             : 
    1846             : uint
    1847             : fd_txn_account_has_bpf_loader_upgradeable( const fd_pubkey_t * account_keys,
    1848        5916 :                                            const ulong         accounts_cnt ) {
    1849       18846 :   for( ulong j=0; j<accounts_cnt; j++ ) {
    1850       12930 :     const fd_pubkey_t * acc = &account_keys[j];
    1851       12930 :     if ( memcmp( acc->uc, fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) {
    1852           0 :       return 1U;
    1853           0 :     }
    1854       12930 :   }
    1855        5916 :   return 0U;
    1856        5916 : }
    1857             : 
    1858             : static inline int
    1859             : fd_runtime_account_is_writable_idx_flat( const ulong           slot,
    1860             :                                          const ushort          idx,
    1861             :                                          const fd_pubkey_t *   addr_at_idx,
    1862             :                                          const fd_txn_t *      txn_descriptor,
    1863             :                                          const fd_features_t * features,
    1864        5916 :                                          const uint            bpf_upgradeable_in_txn ) {
    1865             :   /* https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L43 */
    1866        5916 :   if( !fd_txn_is_writable( txn_descriptor, idx ) ) {
    1867         129 :     return 0;
    1868         129 :   }
    1869             : 
    1870             :   /* See comments in fd_system_ids.h.
    1871             :      https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L44 */
    1872        5787 :   if( fd_pubkey_is_active_reserved_key( addr_at_idx ) ||
    1873        5787 :       fd_pubkey_is_pending_reserved_key( addr_at_idx ) ||
    1874        5787 :       ( FD_FEATURE_ACTIVE( slot, features, enable_secp256r1_precompile ) &&
    1875        5778 :                            fd_pubkey_is_secp256r1_key( addr_at_idx ) ) ) {
    1876             : 
    1877           9 :     return 0;
    1878           9 :   }
    1879             : 
    1880        5778 :   if( fd_txn_account_is_demotion( idx, txn_descriptor, bpf_upgradeable_in_txn ) ) {
    1881           0 :     return 0;
    1882           0 :   }
    1883             : 
    1884        5778 :   return 1;
    1885        5778 : }
    1886             : 
    1887             : 
    1888             : /* This function aims to mimic the writable accounts check to populate the writable accounts cache, used
    1889             :    to determine if accounts are writable or not.
    1890             : 
    1891             :    https://github.com/anza-xyz/agave/blob/v2.1.11/sdk/program/src/message/sanitized.rs#L38-L47 */
    1892             : int
    1893             : fd_runtime_account_is_writable_idx( fd_txn_in_t const *  txn_in,
    1894             :                                     fd_txn_out_t const * txn_out,
    1895             :                                     fd_bank_t *          bank,
    1896        5916 :                                     ushort               idx ) {
    1897        5916 :   uint bpf_upgradeable = fd_txn_account_has_bpf_loader_upgradeable( txn_out->accounts.keys, txn_out->accounts.cnt );
    1898        5916 :   return fd_runtime_account_is_writable_idx_flat( fd_bank_slot_get( bank ),
    1899        5916 :                                                    idx,
    1900        5916 :                                                    &txn_out->accounts.keys[idx],
    1901        5916 :                                                    TXN( txn_in->txn ),
    1902        5916 :                                                    fd_bank_features_query( bank ),
    1903        5916 :                                                    bpf_upgradeable );
    1904        5916 : }
    1905             : 
    1906             : /* Account pre-condition filtering functions */
    1907             : 
    1908             : int
    1909             : fd_runtime_account_check_exists( fd_txn_in_t const * txn_in,
    1910             :                                  fd_txn_out_t *      txn_out,
    1911         339 :                                  ushort              idx ) {
    1912         339 :   (void) txn_in;
    1913         339 :   return fd_account_meta_exists( txn_out->accounts.account[idx].meta );
    1914         339 : }
    1915             : 
    1916             : int
    1917             : fd_runtime_account_check_fee_payer_writable( fd_txn_in_t const * txn_in,
    1918             :                                              fd_txn_out_t *      txn_out,
    1919          90 :                                              ushort              idx ) {
    1920          90 :   (void) txn_out;
    1921          90 :   return fd_txn_is_writable( TXN( txn_in->txn ), idx );
    1922          90 : }
    1923             : 
    1924             : 
    1925             : int
    1926         180 : fd_account_meta_checked_sub_lamports( fd_account_meta_t * meta, ulong lamports ) {
    1927         180 :   ulong balance_post = 0UL;
    1928         180 :   int err = fd_ulong_checked_sub( meta->lamports,
    1929         180 :                                   lamports,
    1930         180 :                                   &balance_post );
    1931         180 :   if( FD_UNLIKELY( err ) ) {
    1932           0 :     return FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW;
    1933           0 :   }
    1934             : 
    1935         180 :   meta->lamports = balance_post;
    1936         180 :   return FD_EXECUTOR_INSTR_SUCCESS;
    1937         180 : }

Generated by: LCOV version 1.14