LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_executor.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 8 965 0.8 %
Date: 2025-07-01 05:00:49 Functions: 1 40 2.5 %

          Line data    Source code
       1             : #include "fd_executor.h"
       2             : #include "fd_acc_mgr.h"
       3             : #include "fd_hashes.h"
       4             : #include "fd_runtime.h"
       5             : #include "fd_runtime_err.h"
       6             : 
       7             : #include "context/fd_exec_slot_ctx.h"
       8             : #include "context/fd_exec_txn_ctx.h"
       9             : #include "context/fd_exec_instr_ctx.h"
      10             : 
      11             : #include "../../util/rng/fd_rng.h"
      12             : #include "fd_system_ids.h"
      13             : #include "program/fd_address_lookup_table_program.h"
      14             : #include "program/fd_bpf_loader_program.h"
      15             : #include "program/fd_loader_v4_program.h"
      16             : #include "program/fd_compute_budget_program.h"
      17             : #include "program/fd_config_program.h"
      18             : #include "program/fd_precompiles.h"
      19             : #include "program/fd_stake_program.h"
      20             : #include "program/fd_system_program.h"
      21             : #include "program/fd_vote_program.h"
      22             : #include "program/fd_zk_elgamal_proof_program.h"
      23             : #include "program/fd_bpf_program_util.h"
      24             : #include "sysvar/fd_sysvar_slot_history.h"
      25             : #include "sysvar/fd_sysvar_epoch_schedule.h"
      26             : #include "sysvar/fd_sysvar_instructions.h"
      27             : #include "sysvar/fd_sysvar_slot_hashes.h"
      28             : #include "sysvar/fd_sysvar_rent.h"
      29             : 
      30             : #include "tests/fd_dump_pb.h"
      31             : 
      32             : #include "../../ballet/base58/fd_base58.h"
      33             : #include "../../disco/pack/fd_pack.h"
      34             : #include "../../disco/pack/fd_pack_cost.h"
      35             : 
      36             : #include "../../util/bits/fd_uwide.h"
      37             : 
      38             : #include <assert.h>
      39             : #include <math.h>
      40             : #include <stdio.h>   /* snprintf(3) */
      41             : #include <fcntl.h>   /* openat(2) */
      42             : #include <unistd.h>  /* write(3) */
      43             : #include <time.h>
      44             : 
      45             : struct fd_native_prog_info {
      46             :   fd_pubkey_t key;
      47             :   fd_exec_instr_fn_t fn;
      48             :   uchar is_bpf_loader;
      49             : };
      50             : typedef struct fd_native_prog_info fd_native_prog_info_t;
      51             : 
      52             : #define MAP_PERFECT_NAME fd_native_program_fn_lookup_tbl
      53             : #define MAP_PERFECT_LG_TBL_SZ 4
      54             : #define MAP_PERFECT_T fd_native_prog_info_t
      55          15 : #define MAP_PERFECT_HASH_C 478U
      56             : #define MAP_PERFECT_KEY key.uc
      57             : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
      58             : #define MAP_PERFECT_ZERO_KEY  (0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0)
      59             : #define MAP_PERFECT_COMPLEX_KEY 1
      60          15 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
      61             : 
      62          15 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>28)&0xFU)
      63             : 
      64             : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
      65             :                              a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
      66             :                                           PERFECT_HASH( (a08 | (a09<<8) | (a10<<16) | (a11<<24)) )
      67          15 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_4( (uchar const *)ptr + 8UL ) )
      68             : 
      69             : #define MAP_PERFECT_0       ( VOTE_PROG_ID            ), .fn = fd_vote_program_execute,                      .is_bpf_loader = 0
      70             : #define MAP_PERFECT_1       ( SYS_PROG_ID             ), .fn = fd_system_program_execute,                    .is_bpf_loader = 0
      71             : #define MAP_PERFECT_2       ( CONFIG_PROG_ID          ), .fn = fd_config_program_execute,                    .is_bpf_loader = 0
      72             : #define MAP_PERFECT_3       ( STAKE_PROG_ID           ), .fn = fd_stake_program_execute,                     .is_bpf_loader = 0
      73             : #define MAP_PERFECT_4       ( COMPUTE_BUDGET_PROG_ID  ), .fn = fd_compute_budget_program_execute,            .is_bpf_loader = 0
      74             : #define MAP_PERFECT_5       ( ADDR_LUT_PROG_ID        ), .fn = fd_address_lookup_table_program_execute,      .is_bpf_loader = 0
      75             : #define MAP_PERFECT_6       ( ZK_EL_GAMAL_PROG_ID     ), .fn = fd_executor_zk_elgamal_proof_program_execute, .is_bpf_loader = 0
      76             : #define MAP_PERFECT_7       ( BPF_LOADER_1_PROG_ID    ), .fn = fd_bpf_loader_program_execute,                .is_bpf_loader = 1
      77             : #define MAP_PERFECT_8       ( BPF_LOADER_2_PROG_ID    ), .fn = fd_bpf_loader_program_execute,                .is_bpf_loader = 1
      78             : #define MAP_PERFECT_9       ( BPF_UPGRADEABLE_PROG_ID ), .fn = fd_bpf_loader_program_execute,                .is_bpf_loader = 1
      79             : #define MAP_PERFECT_10      ( LOADER_V4_PROG_ID       ), .fn = fd_loader_v4_program_execute,                 .is_bpf_loader = 1
      80             : 
      81             : #include "../../util/tmpl/fd_map_perfect.c"
      82             : #undef PERFECT_HASH
      83             : 
      84             : #define MAP_PERFECT_NAME fd_native_precompile_program_fn_lookup_tbl
      85             : #define MAP_PERFECT_LG_TBL_SZ 2
      86             : #define MAP_PERFECT_T fd_native_prog_info_t
      87           0 : #define MAP_PERFECT_HASH_C 63546U
      88             : #define MAP_PERFECT_KEY key.uc
      89             : #define MAP_PERFECT_KEY_T fd_pubkey_t const *
      90             : #define MAP_PERFECT_ZERO_KEY  (0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0)
      91             : #define MAP_PERFECT_COMPLEX_KEY 1
      92           0 : #define MAP_PERFECT_KEYS_EQUAL(k1,k2) (!memcmp( (k1), (k2), 32UL ))
      93             : 
      94           0 : #define PERFECT_HASH( u ) (((MAP_PERFECT_HASH_C*(u))>>30)&0x3U)
      95             : 
      96             : #define MAP_PERFECT_HASH_PP( a00,a01,a02,a03,a04,a05,a06,a07,a08,a09,a10,a11,a12,a13,a14,a15, \
      97             :                              a16,a17,a18,a19,a20,a21,a22,a23,a24,a25,a26,a27,a28,a29,a30,a31) \
      98             :                                           PERFECT_HASH( (a00 | (a01<<8)) )
      99           0 : #define MAP_PERFECT_HASH_R( ptr ) PERFECT_HASH( fd_uint_load_2( (uchar const *)ptr ) )
     100             : 
     101             : #define MAP_PERFECT_0      ( ED25519_SV_PROG_ID      ), .fn = fd_precompile_ed25519_verify
     102             : #define MAP_PERFECT_1      ( KECCAK_SECP_PROG_ID     ), .fn = fd_precompile_secp256k1_verify
     103             : #define MAP_PERFECT_2      ( SECP256R1_PROG_ID       ), .fn = fd_precompile_secp256r1_verify
     104             : 
     105             : #include "../../util/tmpl/fd_map_perfect.c"
     106             : #undef PERFECT_HASH
     107             : 
     108             : fd_exec_instr_fn_t
     109           0 : fd_executor_lookup_native_precompile_program( fd_txn_account_t const * prog_acc ) {
     110           0 :   fd_pubkey_t const * pubkey                = prog_acc->pubkey;
     111           0 :   const fd_native_prog_info_t null_function = {0};
     112           0 :   return fd_native_precompile_program_fn_lookup_tbl_query( pubkey, &null_function )->fn;
     113           0 : }
     114             : 
     115             : uchar
     116          15 : fd_executor_pubkey_is_bpf_loader( fd_pubkey_t const * pubkey ) {
     117          15 :   fd_native_prog_info_t const null_function = {0};
     118          15 :   return fd_native_program_fn_lookup_tbl_query( pubkey, &null_function )->is_bpf_loader;
     119          15 : }
     120             : 
     121             : /* fd_executor_lookup_native_program returns the appropriate instruction processor for the given
     122             :    native program ID. Returns NULL if given ID is not a recognized native program.
     123             :    https://github.com/anza-xyz/agave/blob/v2.2.6/program-runtime/src/invoke_context.rs#L520-L544 */
     124             : static int
     125             : fd_executor_lookup_native_program( fd_txn_account_t const * prog_acc,
     126             :                                    fd_exec_txn_ctx_t *      txn_ctx,
     127             :                                    fd_exec_instr_fn_t *     native_prog_fn,
     128           0 :                                    uchar *                  is_precompile ) {
     129             :   /* First lookup to see if the program key is a precompile */
     130           0 :   *is_precompile = 0;
     131           0 :   *native_prog_fn = fd_executor_lookup_native_precompile_program( prog_acc );
     132           0 :   if( FD_UNLIKELY( *native_prog_fn!=NULL ) ) {
     133           0 :     *is_precompile = 1;
     134           0 :     return 0;
     135           0 :   }
     136             : 
     137           0 :   fd_pubkey_t const * pubkey = prog_acc->pubkey;
     138           0 :   fd_pubkey_t const * owner  = prog_acc->vt->get_owner( prog_acc );
     139             : 
     140             :   /* Native programs should be owned by the native loader...
     141             :      This will not be the case though once core programs are migrated to BPF. */
     142           0 :   int is_native_program = !memcmp( owner, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) );
     143             : 
     144           0 :   if( !is_native_program && FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, remove_accounts_executable_flag_checks ) ) {
     145           0 :     if( FD_UNLIKELY( !fd_executor_pubkey_is_bpf_loader( owner ) ) ) {
     146           0 :       return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
     147           0 :     }
     148           0 :   }
     149             : 
     150           0 :   fd_pubkey_t const *         lookup_pubkey = is_native_program ? pubkey : owner;
     151           0 :   fd_native_prog_info_t const null_function = {0};
     152           0 :   *native_prog_fn                           = fd_native_program_fn_lookup_tbl_query( lookup_pubkey, &null_function )->fn;
     153           0 :   return 0;
     154           0 : }
     155             : 
     156             : static int
     157           0 : fd_executor_is_system_nonce_account( fd_txn_account_t * account, fd_spad_t * exec_spad ) {
     158           0 :   if( memcmp( account->vt->get_owner( account ), fd_solana_system_program_id.uc, sizeof(fd_pubkey_t) ) == 0 ) {
     159           0 :     if( !account->vt->get_data_len( account ) ) {
     160           0 :       return 0;
     161           0 :     } else {
     162           0 :       if( account->vt->get_data_len( account )!=FD_SYSTEM_PROGRAM_NONCE_DLEN ) {
     163           0 :         return -1;
     164           0 :       }
     165             : 
     166           0 :       int err;
     167           0 :       fd_nonce_state_versions_t * versions = fd_bincode_decode_spad(
     168           0 :           nonce_state_versions, exec_spad,
     169           0 :           account->vt->get_data( account ),
     170           0 :           account->vt->get_data_len( account ),
     171           0 :           &err );
     172           0 :       if( FD_UNLIKELY( err ) ) {
     173           0 :         return -1;
     174           0 :       }
     175             : 
     176           0 :       fd_nonce_state_t * state = NULL;
     177           0 :       if( fd_nonce_state_versions_is_current( versions ) ) {
     178           0 :         state = &versions->inner.current;
     179           0 :       } else {
     180           0 :         state = &versions->inner.legacy;
     181           0 :       }
     182             : 
     183           0 :       if( fd_nonce_state_is_initialized( state ) ) {
     184           0 :         return 1;
     185           0 :       }
     186             : 
     187           0 :     }
     188           0 :   }
     189             : 
     190           0 :   return -1;
     191           0 : }
     192             : 
     193             : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L117-L136 */
     194             : static uchar
     195             : fd_executor_rent_transition_allowed( fd_rent_state_t const * pre_rent_state,
     196           0 :                                      fd_rent_state_t const * post_rent_state ) {
     197           0 :   switch( post_rent_state->discriminant ) {
     198           0 :     case fd_rent_state_enum_uninitialized:
     199           0 :     case fd_rent_state_enum_rent_exempt: {
     200           0 :       return 1;
     201           0 :     }
     202           0 :     case fd_rent_state_enum_rent_paying: {
     203           0 :       switch( pre_rent_state->discriminant ) {
     204           0 :         case fd_rent_state_enum_uninitialized:
     205           0 :         case fd_rent_state_enum_rent_exempt: {
     206           0 :           return 0;
     207           0 :         }
     208           0 :         case fd_rent_state_enum_rent_paying: {
     209           0 :           return post_rent_state->inner.rent_paying.data_size==pre_rent_state->inner.rent_paying.data_size &&
     210           0 :                  post_rent_state->inner.rent_paying.lamports<=pre_rent_state->inner.rent_paying.lamports;
     211           0 :         }
     212           0 :         default: {
     213           0 :           __builtin_unreachable();
     214           0 :         }
     215           0 :       }
     216           0 :     }
     217           0 :     default: {
     218           0 :       __builtin_unreachable();
     219           0 :     }
     220           0 :   }
     221           0 : }
     222             : 
     223             : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L61-L77 */
     224             : static int
     225             : fd_executor_check_rent_state_with_account( fd_txn_account_t const * account,
     226             :                                            fd_rent_state_t const *  pre_rent_state,
     227           0 :                                            fd_rent_state_t const *  post_rent_state ) {
     228           0 :   if( FD_UNLIKELY( memcmp( account->pubkey->key, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) &&
     229           0 :                    !fd_executor_rent_transition_allowed( pre_rent_state, post_rent_state ) ) ) {
     230           0 :     return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
     231           0 :   }
     232           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     233           0 : }
     234             : 
     235             : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L87-L101 */
     236             : fd_rent_state_t
     237           0 : fd_executor_get_account_rent_state( fd_txn_account_t const * account, fd_rent_t const * rent ) {
     238             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L88-L89 */
     239           0 :   if( account->vt->get_lamports( account )==0UL ) {
     240           0 :     return (fd_rent_state_t){
     241           0 :       .discriminant = fd_rent_state_enum_uninitialized
     242           0 :     };
     243           0 :   }
     244             : 
     245             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L90-L94 */
     246           0 :   if( account->vt->get_lamports( account )>=fd_rent_exempt_minimum_balance( rent, account->vt->get_data_len( account ) ) ) {
     247           0 :     return (fd_rent_state_t){
     248           0 :       .discriminant = fd_rent_state_enum_rent_exempt
     249           0 :     };
     250           0 :   }
     251             : 
     252             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm-rent-collector/src/svm_rent_collector.rs#L95-L99 */
     253           0 :   return (fd_rent_state_t){
     254           0 :     .discriminant = fd_rent_state_enum_rent_paying,
     255           0 :     .inner = {
     256           0 :       .rent_paying = {
     257           0 :         .lamports = account->vt->get_lamports( account ),
     258           0 :         .data_size = account->vt->get_data_len( account )
     259           0 :       }
     260           0 :     }
     261           0 :   };
     262           0 : }
     263             : 
     264             : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L293-L342 */
     265             : static int
     266             : fd_validate_fee_payer( fd_txn_account_t * account,
     267             :                        fd_rent_t const *  rent,
     268             :                        ulong              fee,
     269           0 :                        fd_spad_t *        exec_spad ) {
     270             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L301-L304 */
     271           0 :   if( FD_UNLIKELY( account->vt->get_lamports( account )==0UL ) ) {
     272           0 :     return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
     273           0 :   }
     274             : 
     275             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L305-L308 */
     276           0 :   int is_nonce = fd_executor_is_system_nonce_account( account, exec_spad );
     277           0 :   if( FD_UNLIKELY( is_nonce<0 ) ) {
     278           0 :     return FD_RUNTIME_TXN_ERR_INVALID_ACCOUNT_FOR_FEE;
     279           0 :   }
     280             : 
     281             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L309-L318 */
     282           0 :   ulong min_balance = 0UL;
     283           0 :   if( is_nonce ) {
     284           0 :     min_balance = fd_rent_exempt_minimum_balance( rent, 80 );
     285           0 :   }
     286             : 
     287             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L320-L327 */
     288           0 :   if( FD_UNLIKELY( min_balance>account->vt->get_lamports( account ) ||
     289           0 :                    fee>account->vt->get_lamports( account )-min_balance ) ) {
     290           0 :     return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
     291           0 :   }
     292             : 
     293             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L329 */
     294           0 :   fd_rent_state_t payer_pre_rent_state = fd_executor_get_account_rent_state( account, rent );
     295             : 
     296             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L330-L332 */
     297           0 :   int err = account->vt->checked_sub_lamports( account, fee );
     298           0 :   if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
     299           0 :     return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_FEE;
     300           0 :   }
     301             : 
     302             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L334 */
     303           0 :   fd_rent_state_t payer_post_rent_state = fd_executor_get_account_rent_state( account, rent );
     304             : 
     305             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/account_loader.rs#L335-L342 */
     306           0 :   return fd_executor_check_rent_state_with_account( account, &payer_pre_rent_state, &payer_post_rent_state );
     307           0 : }
     308             : 
     309             : static int FD_FN_UNUSED
     310           0 : status_check_tower( ulong slot, void * _ctx ) {
     311           0 :   fd_exec_txn_ctx_t * ctx = (fd_exec_txn_ctx_t *)_ctx;
     312           0 :   if( slot==ctx->slot ) {
     313           0 :     return 1;
     314           0 :   }
     315           0 : 
     316           0 :   if( fd_txncache_is_rooted_slot( ctx->status_cache, slot ) ) {
     317           0 :     return 1;
     318           0 :   }
     319           0 : 
     320           0 :   fd_slot_history_global_t * slot_history = fd_sysvar_slot_history_read( ctx->funk,
     321           0 :                                                                          ctx->funk_txn,
     322           0 :                                                                          ctx->spad );
     323           0 :   if( FD_UNLIKELY( !slot_history ) ) {
     324           0 :     FD_LOG_ERR(( "Unable to read and decode slot history sysvar" ));
     325           0 :   }
     326           0 : 
     327           0 :   if( fd_sysvar_slot_history_find_slot( slot_history,
     328           0 :                                         slot,
     329           0 :                                         ctx->runtime_pub_wksp ) == FD_SLOT_HISTORY_SLOT_FOUND ) {
     330           0 :     return 1;
     331           0 :   }
     332           0 : 
     333           0 :   return 0;
     334           0 : }
     335             : 
     336             : static int
     337           0 : fd_executor_check_status_cache( fd_exec_txn_ctx_t * txn_ctx ) {
     338             : 
     339           0 :   if( FD_UNLIKELY( !txn_ctx->status_cache ) ) {
     340           0 :     return FD_RUNTIME_EXECUTE_SUCCESS;
     341           0 :   }
     342             : 
     343           0 :   fd_hash_t * blockhash = (fd_hash_t *)((uchar *)txn_ctx->_txn_raw->raw + txn_ctx->txn_descriptor->recent_blockhash_off);
     344             : 
     345           0 :   fd_txncache_query_t curr_query;
     346           0 :   curr_query.blockhash = blockhash->uc;
     347           0 :   fd_blake3_t b3[1];
     348             : 
     349             :   /* Compute the blake3 hash of the transaction message
     350             :      https://github.com/anza-xyz/agave/blob/v2.1.7/sdk/program/src/message/versions/mod.rs#L159-L167 */
     351           0 :   fd_blake3_init( b3 );
     352           0 :   fd_blake3_append( b3, "solana-tx-message-v1", 20UL );
     353           0 :   fd_blake3_append( b3, ((uchar *)txn_ctx->_txn_raw->raw + txn_ctx->txn_descriptor->message_off),(ulong)( txn_ctx->_txn_raw->txn_sz - txn_ctx->txn_descriptor->message_off ) );
     354           0 :   fd_blake3_fini( b3, &txn_ctx->blake_txn_msg_hash );
     355           0 :   curr_query.txnhash = txn_ctx->blake_txn_msg_hash.uc;
     356             : 
     357             :   // FIXME: Commenting out until txncache is fixed
     358           0 :   (void)curr_query;
     359             :   // int err;
     360             :   // fd_txncache_query_batch( txn_ctx->status_cache,
     361             :   //                          &curr_query,
     362             :   //                          1UL,
     363             :   //                          (void *)txn_ctx,
     364             :   //                          status_check_tower, &err );
     365           0 :   return 0;
     366           0 : }
     367             : 
     368             : /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3596-L3605 */
     369             : int
     370           0 : fd_executor_check_transactions( fd_exec_txn_ctx_t * txn_ctx ) {
     371             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3603 */
     372           0 :   int err = fd_check_transaction_age( txn_ctx );
     373           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     374           0 :     return err;
     375           0 :   }
     376             : 
     377             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/runtime/src/bank.rs#L3604 */
     378           0 :   err = fd_executor_check_status_cache( txn_ctx );
     379           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     380           0 :     return err;
     381           0 :   }
     382             : 
     383           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     384           0 : }
     385             : 
     386             : /* https://github.com/anza-xyz/agave/blob/v2.1.0/runtime/src/verify_precompiles.rs#L11-L34 */
     387             : int
     388           0 : fd_executor_verify_precompiles( fd_exec_txn_ctx_t * txn_ctx ) {
     389           0 :   ushort instr_cnt = txn_ctx->txn_descriptor->instr_cnt;
     390           0 :   int    err       = 0;
     391             : 
     392           0 :   for( ushort i=0; i<instr_cnt; i++ ) {
     393           0 :     fd_instr_info_t const *  instr         = &txn_ctx->instr_infos[i];
     394           0 :     fd_txn_account_t const * program_acc   = &txn_ctx->accounts[ instr->program_id ];
     395           0 :     fd_exec_instr_fn_t       precompile_fn = fd_executor_lookup_native_precompile_program( program_acc );
     396             : 
     397             :     /* We need to handle feature-gated precompiles here as well since they're not accounted for in the precompile lookup table. */
     398           0 :     if( FD_LIKELY( precompile_fn==NULL ||
     399           0 :                    ( !memcmp( program_acc->pubkey->key, &fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t) ) &&
     400           0 :                      !FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, enable_secp256r1_precompile ) ) ) ) {
     401           0 :       continue;
     402           0 :     }
     403             : 
     404             :     /* We can create a mock instr_ctx since we only need the txn_ctx and instr fields */
     405           0 :     fd_exec_instr_ctx_t instr_ctx = {
     406           0 :       .txn_ctx = txn_ctx,
     407           0 :       .instr   = instr,
     408           0 :     };
     409             : 
     410           0 :     err = precompile_fn( &instr_ctx );
     411           0 :     if( FD_UNLIKELY( err ) ) {
     412           0 :       FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, err, i );
     413           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     414           0 :     }
     415           0 :   }
     416             : 
     417           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     418           0 : }
     419             : 
     420             : static void
     421           0 : fd_executor_setup_instr_infos_from_txn_instrs( fd_exec_txn_ctx_t * txn_ctx ) {
     422           0 :   ushort instr_cnt = txn_ctx->txn_descriptor->instr_cnt;
     423             : 
     424             :   /* Set up the instr infos for the transaction */
     425           0 :   for( ushort i=0; i<instr_cnt; i++ ) {
     426           0 :     fd_txn_instr_t const * instr = &txn_ctx->txn_descriptor->instr[i];
     427           0 :     fd_instr_info_init_from_txn_instr( &txn_ctx->instr_infos[i], txn_ctx, instr );
     428           0 :   }
     429             : 
     430           0 :   txn_ctx->instr_info_cnt = instr_cnt;
     431           0 : }
     432             : 
     433             : /* https://github.com/anza-xyz/agave/blob/v2.0.9/svm/src/account_loader.rs#L410-427 */
     434             : static int
     435             : accumulate_and_check_loaded_account_data_size( ulong   acc_size,
     436             :                                                ulong   requested_loaded_accounts_data_size,
     437           0 :                                                ulong * accumulated_account_size ) {
     438           0 :   *accumulated_account_size = fd_ulong_sat_add( *accumulated_account_size, acc_size );
     439           0 :   if( FD_UNLIKELY( *accumulated_account_size>requested_loaded_accounts_data_size ) ) {
     440           0 :     return FD_RUNTIME_TXN_ERR_MAX_LOADED_ACCOUNTS_DATA_SIZE_EXCEEDED;
     441           0 :   }
     442           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     443           0 : }
     444             : 
     445             : /* This function contains special casing for loading and collecting rent from
     446             :    each transaction account. The logic is as follows:
     447             :      1. If the account is the instructions sysvar, then load in the compiled
     448             :         instructions from the transactions into the sysvar's data.
     449             :      2. If the account is a fee payer, then it is already loaded.
     450             :      3. Otherwise load in the account from the accounts DB. If the account is
     451             :         writable and exists, try to collect rent from it.
     452             : 
     453             :    https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L536-L580 */
     454             : static void
     455             : load_transaction_account( fd_exec_txn_ctx_t * txn_ctx,
     456             :                           fd_txn_account_t *  acct,
     457             :                           uchar               is_writable,
     458             :                           ulong               epoch,
     459           0 :                           uchar               unknown_acc ) {
     460             :   /* Handling the sysvar instructions account explictly.
     461             :      https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L544-L551 */
     462           0 :   if( FD_UNLIKELY( !memcmp( acct->pubkey->key, fd_sysvar_instructions_id.key, sizeof(fd_pubkey_t) ) ) ) {
     463           0 :     fd_sysvar_instructions_serialize_account( txn_ctx, (fd_instr_info_t const *)txn_ctx->instr_infos, txn_ctx->txn_descriptor->instr_cnt );
     464           0 :     return;
     465           0 :   }
     466             : 
     467             :   /* This next block calls `load_account()` which loads the account from the accounts db. If the
     468             :      account exists and is writable, collect rent from it.
     469             :      https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L552-L565 */
     470           0 :   if( FD_LIKELY( !unknown_acc ) ) {
     471           0 :     if( is_writable ) {
     472           0 :       txn_ctx->collected_rent += fd_runtime_collect_rent_from_account( fd_bank_epoch_schedule_query( txn_ctx->bank ),
     473           0 :                                                                        fd_bank_rent_query( txn_ctx->bank ),
     474           0 :                                                                        fd_bank_slots_per_year_get( txn_ctx->bank ),
     475           0 :                                                                        acct,
     476           0 :                                                                        epoch );
     477           0 :       acct->starting_lamports = acct->vt->get_lamports( acct ); /* TODO: why do we do this everywhere? */
     478           0 :     }
     479           0 :     return;
     480           0 :   }
     481             : 
     482             :   /* The rest of this function is a no-op for us since we already set up the transaction accounts
     483             :      for unknown accounts within `fd_executor_setup_accounts_for_txn()`.
     484             :      https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L566-L577 */
     485           0 : }
     486             : 
     487             : /* This big function contains a lot of logic and special casing for loading transaction accounts.
     488             :    Because of the `enable_transaction_loading_failure_fees` feature, it is imperative that we
     489             :    are conformant with Agave's logic here and reject / accept transactions here where they do.
     490             : 
     491             :    In the firedancer client only some of these steps are necessary because
     492             :    all of the accounts are loaded in from the accounts db into borrowed
     493             :    accounts already.
     494             : 
     495             :    https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L393-L534 */
     496             : int
     497           0 : fd_executor_load_transaction_accounts( fd_exec_txn_ctx_t * txn_ctx ) {
     498           0 :   ulong                       requested_loaded_accounts_data_size = txn_ctx->loaded_accounts_data_size_limit;
     499           0 :   fd_epoch_schedule_t const * schedule                            = fd_sysvar_epoch_schedule_read( txn_ctx->funk, txn_ctx->funk_txn, txn_ctx->spad );
     500           0 :   if( FD_UNLIKELY( !schedule ) ) {
     501           0 :     FD_LOG_ERR(( "Unable to read and decode epoch schedule sysvar" ));
     502           0 :   }
     503             : 
     504           0 :   ulong epoch = fd_slot_to_epoch( schedule, txn_ctx->slot, NULL );
     505             : 
     506             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L429-L443 */
     507           0 :   for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
     508           0 :     fd_txn_account_t * acct = &txn_ctx->accounts[i];
     509           0 :     uchar unknown_acc = !!(fd_exec_txn_ctx_get_account_at_index( txn_ctx, i, &acct, fd_txn_account_check_exists ) ||
     510           0 :                             acct->vt->get_lamports( acct )==0UL);
     511           0 :     ulong acc_size    = unknown_acc ? 0UL : acct->vt->get_data_len( acct );
     512           0 :     uchar is_writable = !!(fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, i ));
     513             : 
     514             :     /* Collect the fee payer account separately
     515             :        https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L429-L431 */
     516           0 :     if( FD_UNLIKELY( i==FD_FEE_PAYER_TXN_IDX ) ) {
     517             :       /* Note that the dlen for most fee payers is 0, but we want to consider the case where the fee payer
     518             :          is a nonce account. */
     519           0 :       int err = accumulate_and_check_loaded_account_data_size( acc_size,
     520           0 :                                                                requested_loaded_accounts_data_size,
     521           0 :                                                                &txn_ctx->loaded_accounts_data_size );
     522           0 :       if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     523           0 :         return err;
     524           0 :       }
     525           0 :       continue;
     526           0 :     }
     527             : 
     528             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L435-L441 */
     529           0 :     load_transaction_account( txn_ctx, acct, is_writable, epoch, unknown_acc );
     530             : 
     531             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L442 */
     532           0 :     int err = accumulate_and_check_loaded_account_data_size( acc_size,
     533           0 :                                                              requested_loaded_accounts_data_size,
     534           0 :                                                              &txn_ctx->loaded_accounts_data_size );
     535             : 
     536           0 :     if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     537           0 :       return err;
     538           0 :     }
     539           0 :   }
     540             : 
     541             :   /* TODO: Consider using a hash set (if its more performant) */
     542           0 :   ushort      instr_cnt             = txn_ctx->txn_descriptor->instr_cnt;
     543           0 :   fd_pubkey_t validated_loaders[instr_cnt];
     544           0 :   ushort      validated_loaders_cnt = 0;
     545             : 
     546             :   /* The logic below handles special casing with loading instruction accounts.
     547             :      https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L445-L525 */
     548           0 :   for( ushort i=0; i<instr_cnt; i++ ) {
     549           0 :     fd_txn_instr_t const * instr = &txn_ctx->txn_descriptor->instr[i];
     550             : 
     551             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L449-L451 */
     552           0 :     if( FD_UNLIKELY( !memcmp( txn_ctx->account_keys[ instr->program_id ].key, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ) ) {
     553           0 :       continue;
     554           0 :     }
     555             : 
     556             :     /* Mimicking `load_account()` here with 0-lamport check as well.
     557             :        https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L455-L462 */
     558           0 :     fd_txn_account_t * program_account = NULL;
     559           0 :     int err = fd_exec_txn_ctx_get_account_at_index( txn_ctx,
     560           0 :                                                     instr->program_id,
     561           0 :                                                     &program_account,
     562           0 :                                                     fd_txn_account_check_exists );
     563           0 :     if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS || program_account->vt->get_lamports( program_account )==0UL ) ) {
     564           0 :       return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
     565           0 :     }
     566             : 
     567             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L464-L471 */
     568           0 :     if( FD_UNLIKELY( !FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, remove_accounts_executable_flag_checks ) &&
     569           0 :                      !program_account->vt->is_executable( program_account ) ) ) {
     570           0 :       return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
     571           0 :     }
     572             : 
     573             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L474-L477 */
     574           0 :     if( !memcmp( program_account->vt->get_owner( program_account ), fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ) {
     575           0 :       continue;
     576           0 :     }
     577             : 
     578             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L479-L522 */
     579           0 :     uchar loader_seen = 0;
     580           0 :     for( ushort j=0; j<validated_loaders_cnt; j++ ) {
     581           0 :       if( !memcmp( validated_loaders[j].key, program_account->vt->get_owner( program_account ), sizeof(fd_pubkey_t) ) ) {
     582             :         /* If the owner account has already been seen, skip the owner checks
     583             :            and do not acccumulate the account size. */
     584           0 :         loader_seen = 1;
     585           0 :         break;
     586           0 :       }
     587           0 :     }
     588           0 :     if( loader_seen ) continue;
     589             : 
     590             :     /* The agave client does checks on the program account's owners as well.
     591             :        However, it is important to not do these checks multiple times as the
     592             :        total size of accounts and their owners are accumulated: duplicate owners
     593             :        should be avoided.
     594             :        https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L496-L517 */
     595           0 :     FD_TXN_ACCOUNT_DECL( owner_account );
     596           0 :     err = fd_txn_account_init_from_funk_readonly( owner_account,
     597           0 :                                                   program_account->vt->get_owner( program_account ),
     598           0 :                                                   txn_ctx->funk,
     599           0 :                                                   txn_ctx->funk_txn );
     600           0 :     if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
     601             :       /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L520 */
     602           0 :       return FD_RUNTIME_TXN_ERR_PROGRAM_ACCOUNT_NOT_FOUND;
     603           0 :     }
     604             : 
     605             : 
     606             :     /* https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L502-L510 */
     607           0 :     if( FD_UNLIKELY( memcmp( owner_account->vt->get_owner( owner_account ), fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ||
     608           0 :                      ( !FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, remove_accounts_executable_flag_checks ) &&
     609           0 :                        !owner_account->vt->is_executable( owner_account ) ) ) ) {
     610           0 :       return FD_RUNTIME_TXN_ERR_INVALID_PROGRAM_FOR_EXECUTION;
     611           0 :     }
     612             : 
     613             :     /* Count the owner's data in the loaded account size for program accounts.
     614             :        However, it is important to not double count repeated owners.
     615             :        https://github.com/anza-xyz/agave/blob/v2.2.0/svm/src/account_loader.rs#L511-L517 */
     616           0 :     err = accumulate_and_check_loaded_account_data_size( owner_account->vt->get_data_len( owner_account ),
     617           0 :                                                          requested_loaded_accounts_data_size,
     618           0 :                                                          &txn_ctx->loaded_accounts_data_size );
     619           0 :     if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     620           0 :       return err;
     621           0 :     }
     622             : 
     623           0 :     fd_memcpy( validated_loaders[ validated_loaders_cnt++ ].key, owner_account->pubkey, sizeof(fd_pubkey_t) );
     624           0 :   }
     625             : 
     626           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     627           0 : }
     628             : 
     629             : /* https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L118 */
     630             : int
     631           0 : fd_executor_validate_account_locks( fd_exec_txn_ctx_t const * txn_ctx ) {
     632             :   /* Ensure the number of account keys does not exceed the transaction lock limit
     633             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L123 */
     634           0 :   ulong tx_account_lock_limit = get_transaction_account_lock_limit( txn_ctx );
     635           0 :   if( FD_UNLIKELY( txn_ctx->accounts_cnt>tx_account_lock_limit ) ) {
     636           0 :     return FD_RUNTIME_TXN_ERR_TOO_MANY_ACCOUNT_LOCKS;
     637           0 :   }
     638             : 
     639             :   /* Duplicate account check
     640             :      https://github.com/anza-xyz/agave/blob/838c1952595809a31520ff1603a13f2c9123aa51/accounts-db/src/account_locks.rs#L125 */
     641           0 :   for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
     642           0 :     for( ushort j=(ushort)(i+1U); j<txn_ctx->accounts_cnt; j++ ) {
     643           0 :       if( FD_UNLIKELY( !memcmp( &txn_ctx->account_keys[i], &txn_ctx->account_keys[j], sizeof(fd_pubkey_t) ) ) ) {
     644           0 :         return FD_RUNTIME_TXN_ERR_ACCOUNT_LOADED_TWICE;
     645           0 :       }
     646           0 :     }
     647           0 :   }
     648             : 
     649             :   /* https://github.com/anza-xyz/agave/blob/ced98f1ebe73f7e9691308afa757323003ff744f/sdk/src/transaction/sanitized.rs#L286-L288 */
     650           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     651           0 : }
     652             : 
     653             : static void
     654             : compute_priority_fee( fd_exec_txn_ctx_t const * txn_ctx,
     655             :                       ulong *                   fee,
     656           0 :                       ulong *                   priority ) {
     657           0 :   switch( txn_ctx->prioritization_fee_type ) {
     658           0 :   case FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_DEPRECATED: {
     659           0 :     if( !txn_ctx->compute_unit_limit ) {
     660           0 :       *priority = 0UL;
     661           0 :     }
     662           0 :     else {
     663           0 :       uint128 micro_lamport_fee = (uint128)txn_ctx->compute_unit_price * (uint128)MICRO_LAMPORTS_PER_LAMPORT;
     664           0 :       uint128 _priority         = micro_lamport_fee / (uint128)txn_ctx->compute_unit_limit;
     665           0 :       *priority                 = _priority > (uint128)ULONG_MAX ? ULONG_MAX : (ulong)_priority;
     666           0 :     }
     667             : 
     668           0 :     *fee = txn_ctx->compute_unit_price;
     669           0 :     return;
     670             : 
     671           0 :   } case FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_COMPUTE_UNIT_PRICE: {
     672           0 :     uint128 micro_lamport_fee = (uint128)txn_ctx->compute_unit_price * (uint128)txn_ctx->compute_unit_limit;
     673           0 :     *priority                 = txn_ctx->compute_unit_price;
     674           0 :     uint128 _fee              = (micro_lamport_fee + (uint128)(MICRO_LAMPORTS_PER_LAMPORT - 1)) / (uint128)(MICRO_LAMPORTS_PER_LAMPORT);
     675           0 :     *fee                      = _fee > (uint128)ULONG_MAX ? ULONG_MAX : (ulong)_fee;
     676           0 :     return;
     677             : 
     678           0 :   }
     679           0 :   default:
     680           0 :     __builtin_unreachable();
     681           0 :   }
     682           0 : }
     683             : 
     684             : static ulong
     685           0 : fd_executor_lamports_per_signature( fd_fee_rate_governor_t const * fee_rate_governor ) {
     686             :   // https://github.com/solana-labs/solana/blob/8f2c8b8388a495d2728909e30460aa40dcc5d733/sdk/program/src/fee_calculator.rs#L110
     687           0 :   return fee_rate_governor->target_lamports_per_signature / 2;
     688           0 : }
     689             : 
     690             : static void
     691             : fd_executor_calculate_fee( fd_exec_txn_ctx_t *  txn_ctx,
     692             :                           fd_txn_t const *      txn_descriptor,
     693             :                           fd_rawtxn_b_t const * txn_raw,
     694             :                           ulong *               ret_execution_fee,
     695           0 :                           ulong *               ret_priority_fee) {
     696             : 
     697             :   // https://github.com/firedancer-io/solana/blob/08a1ef5d785fe58af442b791df6c4e83fe2e7c74/runtime/src/bank.rs#L4443
     698           0 :   ulong priority     = 0UL;
     699           0 :   ulong priority_fee = 0UL;
     700           0 :   compute_priority_fee( txn_ctx, &priority_fee, &priority );
     701             : 
     702             :   // let signature_fee = Self::get_num_signatures_in_message(message) .saturating_mul(fee_structure.lamports_per_signature);
     703           0 :   ulong num_signatures = txn_descriptor->signature_cnt;
     704           0 :   for (ushort i=0; i<txn_descriptor->instr_cnt; ++i ) {
     705           0 :     fd_txn_instr_t const * txn_instr  = &txn_descriptor->instr[i];
     706           0 :     fd_pubkey_t *          program_id = &txn_ctx->account_keys[txn_instr->program_id];
     707           0 :     if( !memcmp(program_id->uc, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t)) ||
     708           0 :         !memcmp(program_id->uc, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t)) ||
     709           0 :         (!memcmp(program_id->uc, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t)) && FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, enable_secp256r1_precompile )) ) {
     710           0 :       if( !txn_instr->data_sz ) {
     711           0 :         continue;
     712           0 :       }
     713           0 :       uchar * data   = (uchar *)txn_raw->raw + txn_instr->data_off;
     714           0 :       num_signatures = fd_ulong_sat_add(num_signatures, (ulong)(data[0]));
     715           0 :     }
     716           0 :   }
     717             : 
     718           0 :   ulong signature_fee = fd_executor_lamports_per_signature( fd_bank_fee_rate_governor_query( txn_ctx->bank ) ) * num_signatures;
     719             : 
     720             :   // TODO: as far as I can tell, this is always 0
     721             :   //
     722             :   //            let write_lock_fee = Self::get_num_write_locks_in_message(message)
     723             :   //                .saturating_mul(fee_structure.lamports_per_write_lock);
     724           0 :   ulong lamports_per_write_lock = 0UL;
     725           0 :   ulong write_lock_fee          = fd_ulong_sat_mul(fd_txn_account_cnt(txn_descriptor, FD_TXN_ACCT_CAT_WRITABLE), lamports_per_write_lock);
     726             : 
     727             :   // TODO: the fee_structure bin is static and default..
     728             :   //        let loaded_accounts_data_size_cost = if include_loaded_account_data_size_in_fee {
     729             :   //            FeeStructure::calculate_memory_usage_cost(
     730             :   //                budget_limits.loaded_accounts_data_size_limit,
     731             :   //                budget_limits.heap_cost,
     732             :   //            )
     733             :   //        } else {
     734             :   //            0_u64
     735             :   //        };
     736             :   //        let total_compute_units =
     737             :   //            loaded_accounts_data_size_cost.saturating_add(budget_limits.compute_unit_limit);
     738             :   //        let compute_fee = self
     739             :   //            .compute_fee_bins
     740             :   //            .iter()
     741             :   //            .find(|bin| total_compute_units <= bin.limit)
     742             :   //            .map(|bin| bin.fee)
     743             :   //            .unwrap_or_else(|| {
     744             :   //                self.compute_fee_bins
     745             :   //                    .last()
     746             :   //                    .map(|bin| bin.fee)
     747             :   //                    .unwrap_or_default()
     748             :   //            });
     749             : 
     750             :   // https://github.com/anza-xyz/agave/blob/2e6ca8c1f62db62c1db7f19c9962d4db43d0d550/sdk/src/fee.rs#L203-L206
     751           0 :   ulong execution_fee = fd_ulong_sat_add( signature_fee, write_lock_fee );
     752             : 
     753           0 :   if( execution_fee >= ULONG_MAX ) {
     754           0 :     *ret_execution_fee = ULONG_MAX;
     755           0 :   } else {
     756           0 :     *ret_execution_fee = execution_fee;
     757           0 :   }
     758             : 
     759           0 :   if( priority_fee >= ULONG_MAX ) {
     760           0 :     *ret_priority_fee = ULONG_MAX;
     761           0 :   } else {
     762           0 :     *ret_priority_fee = priority_fee;
     763           0 :   }
     764           0 : }
     765             : 
     766             : /* This function creates a rollback account for just the fee payer. Although Agave
     767             :    also sets up rollback accounts for both the fee payer and nonce account here,
     768             :    we already set up the rollback nonce account in earlier sanitization checks. Here
     769             :    we have to capture the entire fee payer record so that if the transaction fails,
     770             :    the fee payer state can be rolled back to it's state pre-transaction, and then debited
     771             :    any transaction fees.
     772             : 
     773             :    Our implementation is slightly different than Agave's in several ways:
     774             :    1. The rollback nonce account has already been set up when checking the transaction age
     775             :    2. When the nonce and fee payer accounts are the same...
     776             :       - Agave copies the data from the rollback nonce account into the rollback fee payer account,
     777             :         and then uses that new fee payer account as the rollback account.
     778             :       - We simply set the rent epoch and lamports of the rollback nonce account (since the other fields
     779             :         of the account do not change)
     780             : 
     781             :    https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/rollback_accounts.rs#L34-L77 */
     782             : static void
     783             : fd_executor_create_rollback_fee_payer_account( fd_exec_txn_ctx_t * txn_ctx,
     784           0 :                                                ulong               total_fee ) {
     785           0 :   fd_txn_account_t * fee_payer_rec = &txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX];
     786           0 :   fd_txn_account_t * rollback_fee_payer_rec;
     787             : 
     788             :   /* When setting the data of the rollback fee payer, there is an edge case where the fee payer is the nonce account.
     789             :      In this case, we can just deduct fees from the nonce account and return, because we save the nonce account in the
     790             :      commit phase anyways. */
     791           0 :   if( FD_UNLIKELY( txn_ctx->nonce_account_idx_in_txn==FD_FEE_PAYER_TXN_IDX ) ) {
     792           0 :     rollback_fee_payer_rec = txn_ctx->rollback_nonce_account;
     793             : 
     794             :     /* We also need to update the rent epoch because technically, Agave copies these fields
     795             :        from the fee payer account (since the rollback account does not reflect these changes yet) */
     796           0 :     rollback_fee_payer_rec->vt->set_rent_epoch( rollback_fee_payer_rec,
     797           0 :                                                 fee_payer_rec->vt->get_rent_epoch( fee_payer_rec ) );
     798           0 :   } else {
     799             : 
     800             :     /* In this case, the fee payer is not equal to the nonce account (whether or not it exists).
     801             :        Load in a copy of the fee payer account from funk */
     802           0 :     rollback_fee_payer_rec = fd_txn_account_init( txn_ctx->rollback_fee_payer_account );
     803           0 :     fd_txn_account_init_from_funk_readonly( rollback_fee_payer_rec, &txn_ctx->account_keys[FD_FEE_PAYER_TXN_IDX], txn_ctx->funk, txn_ctx->funk_txn );
     804           0 :     memcpy( rollback_fee_payer_rec->pubkey->key, &txn_ctx->account_keys[FD_FEE_PAYER_TXN_IDX], sizeof(fd_pubkey_t) );
     805             : 
     806             :     /* This allocation should only ever be 104 bytes (since dlen should be 0). */
     807           0 :     ulong  data_len       = txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX].vt->get_data_len( &txn_ctx->accounts[FD_FEE_PAYER_TXN_IDX] );
     808           0 :     void * fee_payer_data = fd_spad_alloc( txn_ctx->spad, FD_ACCOUNT_REC_ALIGN, sizeof(fd_account_meta_t) + data_len );
     809           0 :     fd_txn_account_make_mutable( rollback_fee_payer_rec, fee_payer_data, txn_ctx->spad_wksp );
     810             : 
     811             :     /* There's another weird edge case where if the transaction contains a nonce account, you also have
     812             :        to save the rent epoch field of the fee payer account.
     813             :        https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/rollback_accounts.rs#L68-L75 */
     814           0 :     if( txn_ctx->nonce_account_idx_in_txn!=ULONG_MAX ) {
     815           0 :       rollback_fee_payer_rec->vt->set_rent_epoch( rollback_fee_payer_rec,
     816           0 :                                                   fee_payer_rec->vt->get_rent_epoch( fee_payer_rec ) );
     817           0 :     }
     818           0 :   }
     819             : 
     820             :   /* Deduct the transaction fees from the rollback account. Because of prior checks, this should never fail. */
     821           0 :   if( FD_UNLIKELY( rollback_fee_payer_rec->vt->checked_sub_lamports( rollback_fee_payer_rec, total_fee ) ) ) {
     822           0 :     FD_LOG_ERR(( "fd_executor_create_rollback_fee_payer_account(): failed to deduct fees from rollback account" ));
     823           0 :   }
     824           0 : }
     825             : 
     826             : /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L557-L634 */
     827             : int
     828           0 : fd_executor_validate_transaction_fee_payer( fd_exec_txn_ctx_t * txn_ctx ) {
     829             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L566-L569 */
     830           0 :   int err = fd_executor_compute_budget_program_execute_instructions( txn_ctx, txn_ctx->_txn_raw );
     831           0 :   if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) {
     832           0 :     return err;
     833           0 :   }
     834             : 
     835             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L574-L580 */
     836           0 :   fd_txn_account_t * fee_payer_rec = NULL;
     837           0 :   err = fd_exec_txn_ctx_get_account_at_index( txn_ctx,
     838           0 :                                               FD_FEE_PAYER_TXN_IDX,
     839           0 :                                               &fee_payer_rec,
     840           0 :                                               fd_txn_account_check_fee_payer_writable );
     841           0 :   if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS ) ) {
     842           0 :     return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
     843           0 :   }
     844             : 
     845             :   /* Collect rent from the fee payer
     846             :      https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L583-L589 */
     847           0 :   ulong epoch              = fd_slot_to_epoch( fd_bank_epoch_schedule_query( txn_ctx->bank ), txn_ctx->slot, NULL );
     848           0 :   txn_ctx->collected_rent += fd_runtime_collect_rent_from_account( fd_bank_epoch_schedule_query( txn_ctx->bank ),
     849           0 :                                                                   fd_bank_rent_query( txn_ctx->bank ),
     850           0 :                                                                   fd_bank_slots_per_year_get( txn_ctx->bank ),
     851           0 :                                                                   fee_payer_rec,
     852           0 :                                                                   epoch );
     853             : 
     854             :   /* Calculate transaction fees
     855             :      https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L597-L606 */
     856           0 :   ulong execution_fee = 0UL;
     857           0 :   ulong priority_fee  = 0UL;
     858             : 
     859           0 :   fd_executor_calculate_fee( txn_ctx, txn_ctx->txn_descriptor, txn_ctx->_txn_raw, &execution_fee, &priority_fee );
     860           0 :   ulong total_fee = fd_ulong_sat_add( execution_fee, priority_fee );
     861             : 
     862           0 :   if( !FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, remove_rounding_in_fee_calculation ) ) {
     863           0 :     total_fee = fd_rust_cast_double_to_ulong( round( (double)total_fee ) );
     864           0 :   }
     865             : 
     866             :   /* https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L609-L616 */
     867           0 :   err = fd_validate_fee_payer( fee_payer_rec, fd_bank_rent_query( txn_ctx->bank ), total_fee, txn_ctx->spad );
     868           0 :   if( FD_UNLIKELY( err ) ) {
     869           0 :     return err;
     870           0 :   }
     871             : 
     872             :   /* Create the rollback fee payer account
     873             :      https://github.com/anza-xyz/agave/blob/v2.2.13/svm/src/transaction_processor.rs#L620-L626 */
     874           0 :   fd_executor_create_rollback_fee_payer_account( txn_ctx, total_fee );
     875             : 
     876             :   /* Set the starting lamports (to avoid unbalanced lamports issues in instruction execution) */
     877           0 :   fee_payer_rec->starting_lamports = fee_payer_rec->vt->get_lamports( fee_payer_rec ); /* TODO: why do we do this everywhere? */
     878             : 
     879           0 :   txn_ctx->execution_fee = execution_fee;
     880           0 :   txn_ctx->priority_fee  = priority_fee;
     881             : 
     882           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     883           0 : }
     884             : 
     885             : int
     886           0 : fd_executor_setup_accessed_accounts_for_txn( fd_exec_txn_ctx_t * txn_ctx ) {
     887             : 
     888           0 :   txn_ctx->accounts_cnt = 0UL;
     889             : 
     890           0 :   fd_pubkey_t * tx_accs = (fd_pubkey_t *)((uchar *)txn_ctx->_txn_raw->raw + txn_ctx->txn_descriptor->acct_addr_off);
     891             : 
     892             :   // Set up accounts in the transaction body and perform checks
     893           0 :   for( ulong i = 0UL; i < txn_ctx->txn_descriptor->acct_addr_cnt; i++ ) {
     894           0 :     txn_ctx->account_keys[i] = tx_accs[i];
     895           0 :   }
     896             : 
     897           0 :   txn_ctx->accounts_cnt += (uchar)txn_ctx->txn_descriptor->acct_addr_cnt;
     898             : 
     899           0 :   if( txn_ctx->txn_descriptor->transaction_version == FD_TXN_V0 ) {
     900             :     /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/runtime/src/bank/address_lookup_table.rs#L44-L48 */
     901           0 :     fd_slot_hashes_global_t const * slot_hashes_global = fd_sysvar_slot_hashes_read( txn_ctx->funk, txn_ctx->funk_txn, txn_ctx->spad );
     902           0 :     if( FD_UNLIKELY( !slot_hashes_global ) ) {
     903           0 :       return FD_RUNTIME_TXN_ERR_ACCOUNT_NOT_FOUND;
     904           0 :     }
     905             : 
     906           0 :     fd_slot_hash_t * slot_hash = deq_fd_slot_hash_t_join( (uchar *)slot_hashes_global + slot_hashes_global->hashes_offset );
     907             : 
     908           0 :     fd_acct_addr_t * accts_alt = (fd_acct_addr_t *) fd_type_pun( &txn_ctx->account_keys[txn_ctx->accounts_cnt] );
     909           0 :     int err = fd_runtime_load_txn_address_lookup_tables( txn_ctx->txn_descriptor,
     910           0 :                                                          txn_ctx->_txn_raw->raw,
     911           0 :                                                          txn_ctx->funk,
     912           0 :                                                          txn_ctx->funk_txn,
     913           0 :                                                          txn_ctx->slot,
     914           0 :                                                          slot_hash,
     915           0 :                                                          accts_alt );
     916           0 :     txn_ctx->accounts_cnt += txn_ctx->txn_descriptor->addr_table_adtl_cnt;
     917           0 :     if( FD_UNLIKELY( err!=FD_RUNTIME_EXECUTE_SUCCESS ) ) return err;
     918             : 
     919           0 :   }
     920           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     921           0 : }
     922             : 
     923             : /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L319-L357 */
     924             : static inline int
     925             : fd_txn_ctx_push( fd_exec_txn_ctx_t * txn_ctx,
     926           0 :                  fd_instr_info_t *   instr ) {
     927             :   /* Earlier checks in the permalink are redundant since Agave maintains instr stack and trace accounts separately
     928             :      https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L327-L328 */
     929           0 :   ulong starting_lamports_h = 0UL;
     930           0 :   ulong starting_lamports_l = 0UL;
     931           0 :   int err = fd_instr_info_sum_account_lamports( instr,
     932           0 :                                                 txn_ctx,
     933           0 :                                                 &starting_lamports_h,
     934           0 :                                                 &starting_lamports_l );
     935           0 :   if( FD_UNLIKELY( err ) ) {
     936           0 :     return err;
     937           0 :   }
     938           0 :   instr->starting_lamports_h = starting_lamports_h;
     939           0 :   instr->starting_lamports_l = starting_lamports_l;
     940             : 
     941             :   /* Check that the caller's lamport sum has not changed.
     942             :      https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L329-L340 */
     943           0 :   if( txn_ctx->instr_stack_sz>0 ) {
     944             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L330 */
     945           0 :     fd_exec_instr_ctx_t const * caller_instruction_context = &txn_ctx->instr_stack[ txn_ctx->instr_stack_sz-1 ];
     946             : 
     947             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L331-L332 */
     948           0 :     ulong original_caller_lamport_sum_h = caller_instruction_context->instr->starting_lamports_h;
     949           0 :     ulong original_caller_lamport_sum_l = caller_instruction_context->instr->starting_lamports_l;
     950             : 
     951             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L333-L334 */
     952           0 :     ulong current_caller_lamport_sum_h = 0UL;
     953           0 :     ulong current_caller_lamport_sum_l = 0UL;
     954           0 :     int err = fd_instr_info_sum_account_lamports( caller_instruction_context->instr,
     955           0 :                                                   caller_instruction_context->txn_ctx,
     956           0 :                                                   &current_caller_lamport_sum_h,
     957           0 :                                                   &current_caller_lamport_sum_l );
     958           0 :     if( FD_UNLIKELY( err ) ) {
     959           0 :       return err;
     960           0 :     }
     961             : 
     962             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L335-L339 */
     963           0 :     if( FD_UNLIKELY( current_caller_lamport_sum_h!=original_caller_lamport_sum_h ||
     964           0 :                      current_caller_lamport_sum_l!=original_caller_lamport_sum_l ) ) {
     965           0 :       return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
     966           0 :     }
     967           0 :   }
     968             : 
     969             :   /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L347-L351 */
     970           0 :   if( FD_UNLIKELY( txn_ctx->instr_trace_length>=FD_MAX_INSTRUCTION_TRACE_LENGTH ) ) {
     971           0 :     return FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED;
     972           0 :   }
     973           0 :   txn_ctx->instr_trace_length++;
     974             : 
     975             :   /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L352-L356 */
     976           0 :   if( FD_UNLIKELY( txn_ctx->instr_stack_sz>=FD_MAX_INSTRUCTION_STACK_DEPTH ) ) {
     977           0 :     return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
     978           0 :   }
     979           0 :   txn_ctx->instr_stack_sz++;
     980             : 
     981             :   /* A beloved refactor moves sysvar instructions updating to the instruction level as of v2.2.12...
     982             :      https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L396-L407 */
     983           0 :   int idx = fd_exec_txn_ctx_find_index_of_account( txn_ctx, &fd_sysvar_instructions_id );
     984           0 :   if( FD_UNLIKELY( idx!=-1 ) ) {
     985             :     /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L397-L400 */
     986           0 :     fd_txn_account_t * sysvar_instructions_account = NULL;
     987           0 :     err = fd_exec_txn_ctx_get_account_at_index( txn_ctx, (ushort)idx, &sysvar_instructions_account, NULL );
     988           0 :     if( FD_UNLIKELY( err ) ) {
     989           0 :       return FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS;
     990           0 :     }
     991             : 
     992             :     /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L401-L402 */
     993           0 :     if( FD_UNLIKELY( !sysvar_instructions_account->vt->try_borrow_mut( sysvar_instructions_account ) ) ) {
     994           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED;
     995           0 :     }
     996             : 
     997             :     /* https://github.com/anza-xyz/agave/blob/v2.2.12/transaction-context/src/lib.rs#L403-L406 */
     998           0 :     fd_sysvar_instructions_update_current_instr_idx( sysvar_instructions_account, (ushort)txn_ctx->current_instr_idx );
     999           0 :     sysvar_instructions_account->vt->drop( sysvar_instructions_account );
    1000           0 :   }
    1001             : 
    1002           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
    1003           0 : }
    1004             : 
    1005             : /* Pushes a new instruction onto the instruction stack and trace. This check loops through all instructions in the current call stack
    1006             :    and checks for reentrancy violations. If successful, simply increments the instruction stack and trace size and returns. It is
    1007             :    the responsibility of the caller to populate the newly pushed instruction fields, which are undefined otherwise.
    1008             : 
    1009             :    https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L246-L290 */
    1010             : int
    1011             : fd_instr_stack_push( fd_exec_txn_ctx_t *     txn_ctx,
    1012           0 :                      fd_instr_info_t *       instr ) {
    1013             :   /* Agave keeps a vector of vectors called program_indices that stores the program_id index for each instruction within the transaction.
    1014             :      https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L347-L402
    1015             :      If and only if the program_id is the native loader, then the vector for respective specific instruction (account_indices) is empty.
    1016             :      https://github.com/anza-xyz/agave/blob/v2.1.7/svm/src/account_loader.rs#L350-L358
    1017             :      While trying to push a new instruction onto the instruction stack, if the vector for the respective instruction is empty, Agave throws UnsupportedProgramId
    1018             :      https://github.com/anza-xyz/agave/blob/v2.1.7/program-runtime/src/invoke_context.rs#L253-L255
    1019             :      The only way for the vector to be empty is if the program_id is the native loader, so we can a program_id check here
    1020             :      */
    1021             : 
    1022             :   /* https://github.com/anza-xyz/agave/blob/v2.2.0/program-runtime/src/invoke_context.rs#L250-L252 */
    1023           0 :   fd_pubkey_t const * program_id_pubkey = NULL;
    1024           0 :   int err = fd_exec_txn_ctx_get_key_of_account_at_index( txn_ctx,
    1025           0 :                                                          instr->program_id,
    1026           0 :                                                          &program_id_pubkey );
    1027           0 :   if( FD_UNLIKELY( err ||
    1028           0 :                    !memcmp( program_id_pubkey->key, fd_solana_native_loader_id.key, sizeof(fd_pubkey_t) ) ) ) {
    1029           0 :     return FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    1030           0 :   }
    1031             : 
    1032             :   /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L256-L286 */
    1033           0 :   if( txn_ctx->instr_stack_sz ) {
    1034             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L261-L285 */
    1035           0 :     uchar contains = 0;
    1036           0 :     uchar is_last  = 0;
    1037             : 
    1038             :     // Checks all previous instructions in the stack for reentrancy
    1039           0 :     for( uchar level=0; level<txn_ctx->instr_stack_sz; level++ ) {
    1040           0 :       fd_exec_instr_ctx_t * instr_ctx = &txn_ctx->instr_stack[level];
    1041             :       // Optimization: compare program id index instead of pubkey since account keys are unique
    1042           0 :       if( instr->program_id == instr_ctx->instr->program_id ) {
    1043             :         // Reentrancy not allowed unless caller is calling itself
    1044           0 :         if( level == txn_ctx->instr_stack_sz-1 ) {
    1045           0 :           is_last = 1;
    1046           0 :         }
    1047           0 :         contains = 1;
    1048           0 :       }
    1049           0 :     }
    1050             :     /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L282-L285 */
    1051           0 :     if( FD_UNLIKELY( contains && !is_last ) ) {
    1052           0 :       return FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED;
    1053           0 :     }
    1054           0 :   }
    1055             :   /* "Push" a new instruction onto the stack by simply incrementing the stack and trace size counters
    1056             :      https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L289 */
    1057           0 :   return fd_txn_ctx_push( txn_ctx, instr );
    1058           0 : }
    1059             : 
    1060             : /* Pops an instruction from the instruction stack. Agave's implementation performs instruction balancing checks every time pop is called,
    1061             :    but error codes returned from `pop` are only used if the program's execution was successful. Therefore, we can optimize our code by only
    1062             :    checking for unbalanced instructions if the program execution was successful within fd_execute_instr.
    1063             : 
    1064             :    https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/program-runtime/src/invoke_context.rs#L293-L298 */
    1065             : int
    1066             : fd_instr_stack_pop( fd_exec_txn_ctx_t *       txn_ctx,
    1067           0 :                     fd_instr_info_t const *   instr ) {
    1068             :   /* https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L362-L364 */
    1069           0 :   if( FD_UNLIKELY( txn_ctx->instr_stack_sz==0 ) ) {
    1070           0 :     return FD_EXECUTOR_INSTR_ERR_CALL_DEPTH;
    1071           0 :   }
    1072           0 :   txn_ctx->instr_stack_sz--;
    1073             : 
    1074             :   /* Verify all executable accounts have no outstanding refs
    1075             :      https://github.com/anza-xyz/agave/blob/v2.1.14/sdk/src/transaction_context.rs#L367-L371 */
    1076           0 :   for( ushort i=0; i<instr->acct_cnt; i++ ) {
    1077           0 :     ushort idx_in_txn = instr->accounts[i].index_in_transaction;
    1078           0 :     fd_txn_account_t * account = &txn_ctx->accounts[ idx_in_txn ];
    1079           0 :     if( FD_UNLIKELY( account->vt->is_executable( account ) &&
    1080           0 :                      account->vt->is_borrowed( account ) ) ) {
    1081           0 :       return FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING;
    1082           0 :     }
    1083           0 :   }
    1084             : 
    1085             :   /* Verify lamports are balanced before and after instruction
    1086             :      https://github.com/anza-xyz/agave/blob/c4b42ab045860d7b13b3912eafb30e6d2f4e593f/sdk/src/transaction_context.rs#L366-L380 */
    1087           0 :   ulong ending_lamports_h = 0UL;
    1088           0 :   ulong ending_lamports_l = 0UL;
    1089           0 :   int err = fd_instr_info_sum_account_lamports( instr,
    1090           0 :                                                 txn_ctx,
    1091           0 :                                                 &ending_lamports_h,
    1092           0 :                                                 &ending_lamports_l );
    1093           0 :   if( FD_UNLIKELY( err ) ) {
    1094           0 :     return err;
    1095           0 :   }
    1096           0 :   if( FD_UNLIKELY( ending_lamports_l != instr->starting_lamports_l || ending_lamports_h != instr->starting_lamports_h ) ) {
    1097           0 :    return FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR;
    1098           0 :   }
    1099             : 
    1100           0 :   return FD_EXECUTOR_INSTR_SUCCESS;;
    1101           0 : }
    1102             : 
    1103             : /* This function mimics Agave's `.and(self.pop())` functionality,
    1104             :    where we always pop the instruction stack no matter what the error code is.
    1105             :    https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L480 */
    1106             : static inline int
    1107             : fd_execute_instr_end( fd_exec_instr_ctx_t * instr_ctx,
    1108             :                       fd_instr_info_t *     instr,
    1109           0 :                       int                   instr_exec_result ) {
    1110           0 :   int stack_pop_err = fd_instr_stack_pop( instr_ctx->txn_ctx, instr );
    1111             : 
    1112             :   /* Only report the stack pop error on success */
    1113           0 :   if( FD_UNLIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS && stack_pop_err ) ) {
    1114           0 :     FD_TXN_PREPARE_ERR_OVERWRITE( instr_ctx->txn_ctx );
    1115           0 :     FD_TXN_ERR_FOR_LOG_INSTR( instr_ctx->txn_ctx, stack_pop_err, instr_ctx->txn_ctx->instr_err_idx );
    1116           0 :     instr_exec_result = stack_pop_err;
    1117           0 :   }
    1118             : 
    1119           0 :   if( FD_UNLIKELY( instr_exec_result && !instr_ctx->txn_ctx->failed_instr ) ) {
    1120           0 :     instr_ctx->txn_ctx->failed_instr = instr_ctx;
    1121           0 :   }
    1122             : 
    1123           0 :   return instr_exec_result;
    1124           0 : }
    1125             : 
    1126             : int
    1127             : fd_execute_instr( fd_exec_txn_ctx_t * txn_ctx,
    1128           0 :                   fd_instr_info_t *   instr ) {
    1129           0 :   FD_RUNTIME_TXN_SPAD_FRAME_BEGIN( txn_ctx->spad, txn_ctx ) {
    1130           0 :     fd_exec_instr_ctx_t * parent = NULL;
    1131           0 :     if( txn_ctx->instr_stack_sz ) {
    1132           0 :       parent = &txn_ctx->instr_stack[ txn_ctx->instr_stack_sz - 1 ];
    1133           0 :     }
    1134             : 
    1135           0 :     int instr_exec_result = fd_instr_stack_push( txn_ctx, instr );
    1136           0 :     if( FD_UNLIKELY( instr_exec_result ) ) {
    1137           0 :       FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx );
    1138           0 :       FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, instr_exec_result, txn_ctx->instr_err_idx );
    1139           0 :       return instr_exec_result;
    1140           0 :     }
    1141             : 
    1142             :     /* `process_executable_chain()`
    1143             :         https://github.com/anza-xyz/agave/blob/v2.2.12/program-runtime/src/invoke_context.rs#L512-L619 */
    1144           0 :     fd_exec_instr_ctx_t * ctx = &txn_ctx->instr_stack[ txn_ctx->instr_stack_sz - 1 ];
    1145           0 :     *ctx = (fd_exec_instr_ctx_t) {
    1146           0 :       .instr     = instr,
    1147           0 :       .txn_ctx   = txn_ctx,
    1148           0 :       .depth     = parent ? (parent->depth+1) : 0,
    1149           0 :     };
    1150           0 :     fd_base58_encode_32( txn_ctx->accounts[ instr->program_id ].pubkey->uc, NULL, ctx->program_id_base58 );
    1151             : 
    1152           0 :     txn_ctx->instr_trace[ txn_ctx->instr_trace_length - 1 ] = (fd_exec_instr_trace_entry_t) {
    1153           0 :       .instr_info = instr,
    1154           0 :       .stack_height = txn_ctx->instr_stack_sz,
    1155           0 :     };
    1156             : 
    1157             :     /* Look up the native program. We check for precompiles within the lookup function as well.
    1158             :        https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/message_processor.rs#L88 */
    1159           0 :     fd_exec_instr_fn_t native_prog_fn;
    1160           0 :     uchar              is_precompile;
    1161           0 :     int                err = fd_executor_lookup_native_program( &txn_ctx->accounts[ instr->program_id ],
    1162           0 :                                                                 txn_ctx,
    1163           0 :                                                                 &native_prog_fn,
    1164           0 :                                                                 &is_precompile );
    1165             : 
    1166           0 :     if( FD_UNLIKELY( err ) ) {
    1167           0 :       FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx );
    1168           0 :       FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, err, txn_ctx->instr_err_idx );
    1169           0 :       return err;
    1170           0 :     }
    1171             : 
    1172           0 :     if( FD_LIKELY( native_prog_fn!=NULL ) ) {
    1173             :       /* If this branch is taken, we've found an entrypoint to execute. */
    1174           0 :       fd_log_collector_program_invoke( ctx );
    1175             : 
    1176             :       /* Only reset the return data when executing a native builtin program (not a precompile)
    1177             :          https://github.com/anza-xyz/agave/blob/v2.1.6/program-runtime/src/invoke_context.rs#L536-L537 */
    1178           0 :       if( FD_LIKELY( !is_precompile ) ) {
    1179           0 :         fd_exec_txn_ctx_reset_return_data( txn_ctx );
    1180           0 :       }
    1181             : 
    1182             :       /* Unconditionally execute the native program if precompile verification has been moved to svm,
    1183             :          or if the native program is not a precompile */
    1184           0 :       if( FD_LIKELY( FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, move_precompile_verification_to_svm ) ||
    1185           0 :                     !is_precompile ) ) {
    1186           0 :         instr_exec_result = native_prog_fn( ctx );
    1187           0 :       } else {
    1188             :         /* The precompile was already executed at the transaction level, return success */
    1189           0 :         instr_exec_result = FD_EXECUTOR_INSTR_SUCCESS;
    1190           0 :       }
    1191           0 :     } else {
    1192             :       /* Unknown program. In this case specifically, we should not log the program id. */
    1193           0 :       instr_exec_result = FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID;
    1194           0 :       FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx );
    1195           0 :       FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, instr_exec_result, txn_ctx->instr_err_idx );
    1196           0 :       return fd_execute_instr_end( ctx, instr, instr_exec_result );
    1197           0 :     }
    1198             : 
    1199           0 :     if( FD_LIKELY( instr_exec_result==FD_EXECUTOR_INSTR_SUCCESS ) ) {
    1200             :       /* Log success */
    1201           0 :       fd_log_collector_program_success( ctx );
    1202           0 :     } else {
    1203           0 :       FD_TXN_PREPARE_ERR_OVERWRITE( txn_ctx );
    1204           0 :       FD_TXN_ERR_FOR_LOG_INSTR( txn_ctx, instr_exec_result, txn_ctx->instr_err_idx );
    1205             : 
    1206             :       /* Log failure cases.
    1207             :          We assume that the correct type of error is stored in ctx.
    1208             :          Syscalls are expected to log when the error is generated, while
    1209             :          native programs will be logged here.
    1210             :          (This is because syscall errors often carry data with them.) */
    1211           0 :       fd_log_collector_program_failure( ctx );
    1212           0 :     }
    1213             : 
    1214           0 :     return fd_execute_instr_end( ctx, instr, instr_exec_result );
    1215           0 :   } FD_RUNTIME_TXN_SPAD_FRAME_END;
    1216           0 : }
    1217             : 
    1218             : void
    1219           0 : fd_txn_reclaim_accounts( fd_exec_txn_ctx_t * txn_ctx ) {
    1220           0 :   for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
    1221           0 :     fd_txn_account_t * acc_rec = &txn_ctx->accounts[i];
    1222             : 
    1223             :     /* An account writable iff it is writable AND it is not being
    1224             :        demoted. If this criteria is not met, the account should not be
    1225             :        marked as touched via updating its most recent slot. */
    1226           0 :     if( !fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, i ) ) {
    1227           0 :       continue;
    1228           0 :     }
    1229             : 
    1230           0 :     acc_rec->vt->set_slot( acc_rec, txn_ctx->slot );
    1231             : 
    1232           0 :     if( !acc_rec->vt->get_lamports( acc_rec ) ) {
    1233           0 :       acc_rec->vt->set_data_len( acc_rec, 0UL );
    1234           0 :       acc_rec->vt->clear_owner( acc_rec );
    1235           0 :     }
    1236           0 :   }
    1237           0 : }
    1238             : 
    1239             : int
    1240             : fd_executor_is_blockhash_valid_for_age( fd_block_hash_queue_global_t const * block_hash_queue,
    1241             :                                         fd_hash_t const *                    blockhash,
    1242           0 :                                         ulong                                max_age ) {
    1243           0 :   fd_hash_hash_age_pair_t_mapnode_t key;
    1244           0 :   fd_memcpy( key.elem.key.uc, blockhash, sizeof(fd_hash_t) );
    1245             : 
    1246           0 :   fd_hash_hash_age_pair_t_mapnode_t * ages_pool = fd_block_hash_queue_ages_pool_join( block_hash_queue );
    1247           0 :   fd_hash_hash_age_pair_t_mapnode_t * ages_root = fd_block_hash_queue_ages_root_join( block_hash_queue );
    1248             : 
    1249           0 :   fd_hash_hash_age_pair_t_mapnode_t * hash_age = fd_hash_hash_age_pair_t_map_find( ages_pool, ages_root, &key );
    1250           0 :   if( hash_age==NULL ) {
    1251           0 :     return 0;
    1252           0 :   }
    1253             : 
    1254           0 :   ulong age = block_hash_queue->last_hash_index-hash_age->elem.val.hash_index;
    1255           0 :   return age<=max_age;
    1256           0 : }
    1257             : 
    1258             : void
    1259             : fd_exec_txn_ctx_from_exec_slot_ctx( fd_exec_slot_ctx_t const * slot_ctx,
    1260             :                                     fd_exec_txn_ctx_t *        ctx,
    1261             :                                     fd_wksp_t const *          funk_wksp,
    1262             :                                     fd_wksp_t const *          runtime_pub_wksp,
    1263             :                                     ulong                      funk_txn_gaddr,
    1264             :                                     ulong                      funk_gaddr,
    1265           0 :                                     fd_bank_hash_cmp_t *       bank_hash_cmp ) {
    1266             : 
    1267           0 :   ctx->runtime_pub_wksp = (fd_wksp_t *)runtime_pub_wksp;
    1268             : 
    1269           0 :   ctx->funk_txn = fd_wksp_laddr( funk_wksp, funk_txn_gaddr );
    1270           0 :   if( FD_UNLIKELY( !ctx->funk_txn ) ) {
    1271           0 :     FD_LOG_ERR(( "Could not find valid funk transaction" ));
    1272           0 :   }
    1273             : 
    1274           0 :   if( FD_UNLIKELY( !fd_funk_join( ctx->funk, fd_wksp_laddr( funk_wksp, funk_gaddr ) ) ) ) {
    1275           0 :     FD_LOG_ERR(( "Could not find valid funk %lu", funk_gaddr ));
    1276           0 :   }
    1277             : 
    1278           0 :   ctx->status_cache = slot_ctx->status_cache;
    1279             : 
    1280           0 :   ctx->bank_hash_cmp = bank_hash_cmp;
    1281             : 
    1282           0 :   ctx->enable_exec_recording = fd_bank_enable_exec_recording_get( slot_ctx->bank );
    1283             : 
    1284           0 :   ctx->bank = slot_ctx->bank;
    1285             : 
    1286           0 :   ctx->slot = slot_ctx->bank->slot;
    1287             : 
    1288           0 :   ctx->features = fd_bank_features_get( ctx->bank );
    1289           0 : }
    1290             : 
    1291             : fd_txn_account_t *
    1292             : fd_executor_setup_txn_account( fd_exec_txn_ctx_t * txn_ctx,
    1293           0 :                                ushort              idx ) {
    1294           0 :   fd_pubkey_t *      acc         = &txn_ctx->account_keys[ idx ];
    1295           0 :   int                err         = fd_txn_account_init_from_funk_readonly( &txn_ctx->accounts[ idx ],
    1296           0 :                                                                            acc,
    1297           0 :                                                                            txn_ctx->funk,
    1298           0 :                                                                            txn_ctx->funk_txn );
    1299           0 :   fd_txn_account_t * txn_account = &txn_ctx->accounts[ idx ];
    1300             : 
    1301           0 :   if( FD_UNLIKELY( err!=FD_ACC_MGR_SUCCESS && err!=FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT ) ) {
    1302           0 :     FD_LOG_ERR(( "fd_txn_account_init_from_funk_readonly err=%d", err ));
    1303           0 :   }
    1304             : 
    1305           0 :   uchar is_unknown_account = err==FD_ACC_MGR_ERR_UNKNOWN_ACCOUNT;
    1306           0 :   memcpy( txn_account->pubkey->key, acc, sizeof(fd_pubkey_t) );
    1307             : 
    1308           0 :   if( fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, idx ) || idx==FD_FEE_PAYER_TXN_IDX ) {
    1309           0 :     void * txn_account_data = fd_spad_alloc( txn_ctx->spad, FD_ACCOUNT_REC_ALIGN, FD_ACC_TOT_SZ_MAX );
    1310             : 
    1311             :     /* promote the account to mutable, which requires a memcpy*/
    1312           0 :     fd_txn_account_make_mutable( txn_account, txn_account_data, txn_ctx->spad_wksp );
    1313             : 
    1314             :     /* All new accounts should have their rent epoch set to ULONG_MAX.
    1315             :        https://github.com/anza-xyz/agave/blob/89050f3cb7e76d9e273f10bea5e8207f2452f79f/svm/src/account_loader.rs#L485-L497 */
    1316           0 :     if( FD_UNLIKELY( is_unknown_account ) ) {
    1317           0 :       txn_account->vt->set_rent_epoch( txn_account, ULONG_MAX );
    1318           0 :     }
    1319           0 :   }
    1320             : 
    1321           0 :   fd_account_meta_t const * meta = txn_account->vt->get_meta( txn_account );
    1322             : 
    1323           0 :   if( meta==NULL ) {
    1324           0 :     fd_txn_account_setup_sentinel_meta_readonly( txn_account, txn_ctx->spad, txn_ctx->spad_wksp );
    1325           0 :     return NULL;
    1326           0 :   }
    1327             : 
    1328           0 :   return txn_account;
    1329           0 : }
    1330             : 
    1331             : void
    1332             : fd_executor_setup_executable_account( fd_exec_txn_ctx_t * txn_ctx,
    1333             :                                       ushort              acc_idx,
    1334           0 :                                       ushort *            executable_idx ) {
    1335           0 :   int err = 0;
    1336           0 :   fd_bpf_upgradeable_loader_state_t * program_loader_state = read_bpf_upgradeable_loader_state_for_program( txn_ctx, acc_idx, &err );
    1337           0 :   if( FD_UNLIKELY( !program_loader_state ) ) {
    1338           0 :     return;
    1339           0 :   }
    1340             : 
    1341           0 :   if( !fd_bpf_upgradeable_loader_state_is_program( program_loader_state ) ) {
    1342           0 :     return;
    1343           0 :   }
    1344             : 
    1345             :   /* Attempt to load the program data account from funk. This prevents any unknown program
    1346             :       data accounts from getting loaded into the executable accounts list. If such a program is
    1347             :       invoked, the call will fail at the instruction execution level since the programdata
    1348             :       account will not exist within the executable accounts list. */
    1349           0 :   fd_pubkey_t * programdata_acc = &program_loader_state->inner.program.programdata_address;
    1350           0 :   if( FD_LIKELY( fd_txn_account_init_from_funk_readonly( &txn_ctx->executable_accounts[ *executable_idx ],
    1351           0 :                                                             programdata_acc,
    1352           0 :                                                             txn_ctx->funk,
    1353           0 :                                                             txn_ctx->funk_txn )==0 ) ) {
    1354           0 :     (*executable_idx)++;
    1355           0 :   }
    1356           0 : }
    1357             : 
    1358             : void
    1359           0 : fd_executor_setup_accounts_for_txn( fd_exec_txn_ctx_t * txn_ctx ) {
    1360           0 :   ushort j = 0UL;
    1361           0 :   fd_memset( txn_ctx->accounts, 0, sizeof(fd_txn_account_t) * txn_ctx->accounts_cnt );
    1362             : 
    1363           0 :   for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
    1364             : 
    1365           0 :     fd_txn_account_t * txn_account = fd_executor_setup_txn_account( txn_ctx, i );
    1366             : 
    1367           0 :     if( FD_UNLIKELY( txn_account &&
    1368           0 :                      memcmp( txn_account->vt->get_owner( txn_account ), fd_solana_bpf_loader_upgradeable_program_id.key, sizeof(fd_pubkey_t) ) == 0 ) ) {
    1369           0 :       fd_executor_setup_executable_account( txn_ctx, i, &j );
    1370           0 :     }
    1371           0 :   }
    1372             : 
    1373             :   /* Dumping ELF files to protobuf, if applicable */
    1374           0 :   int dump_elf_to_pb = txn_ctx->capture_ctx &&
    1375           0 :                        txn_ctx->slot >= txn_ctx->capture_ctx->dump_proto_start_slot &&
    1376           0 :                        txn_ctx->capture_ctx->dump_elf_to_pb;
    1377           0 :   if( FD_UNLIKELY( dump_elf_to_pb ) ) {
    1378           0 :     for( ushort i=0; i<txn_ctx->accounts_cnt; i++ ) {
    1379           0 :       fd_dump_elf_to_protobuf( txn_ctx, &txn_ctx->accounts[i] );
    1380           0 :     }
    1381           0 :   }
    1382             : 
    1383           0 :   txn_ctx->nonce_account_idx_in_txn = ULONG_MAX;
    1384           0 :   txn_ctx->executable_cnt           = j;
    1385             : 
    1386             :   /* Set up instr infos from the txn descriptor. No Agave equivalent to this function. */
    1387           0 :   fd_executor_setup_instr_infos_from_txn_instrs( txn_ctx );
    1388           0 : }
    1389             : 
    1390             : /* Stuff to be done before multithreading can begin */
    1391             : int
    1392             : fd_execute_txn_prepare_start( fd_exec_slot_ctx_t const * slot_ctx,
    1393             :                               fd_exec_txn_ctx_t *        txn_ctx,
    1394             :                               fd_txn_t const *           txn_descriptor,
    1395           0 :                               fd_rawtxn_b_t const *      txn_raw ) {
    1396             : 
    1397           0 :   fd_funk_t * funk               = slot_ctx->funk;
    1398           0 :   fd_wksp_t * funk_wksp          = fd_funk_wksp( funk );
    1399             :   /* FIXME: just pass in the runtime workspace, instead of getting it from fd_wksp_containing */
    1400           0 :   fd_wksp_t * runtime_pub_wksp   = fd_wksp_containing( slot_ctx );
    1401           0 :   ulong       funk_txn_gaddr     = fd_wksp_gaddr( funk_wksp, slot_ctx->funk_txn );
    1402           0 :   ulong       funk_gaddr         = fd_wksp_gaddr( funk_wksp, slot_ctx->funk->shmem );
    1403             : 
    1404             :   /* Init txn ctx */
    1405           0 :   fd_exec_txn_ctx_new( txn_ctx );
    1406           0 :   fd_exec_txn_ctx_from_exec_slot_ctx( slot_ctx,
    1407           0 :                                       txn_ctx,
    1408           0 :                                       funk_wksp,
    1409           0 :                                       runtime_pub_wksp,
    1410           0 :                                       funk_txn_gaddr,
    1411           0 :                                       funk_gaddr,
    1412           0 :                                       NULL );
    1413           0 :   fd_exec_txn_ctx_setup( txn_ctx, txn_descriptor, txn_raw );
    1414             : 
    1415             :   /* Unroll accounts from aluts and place into correct spots */
    1416           0 :   int res = fd_executor_setup_accessed_accounts_for_txn( txn_ctx );
    1417             : 
    1418           0 :   return res;
    1419           0 : }
    1420             : 
    1421             : int
    1422           0 : fd_executor_txn_verify( fd_exec_txn_ctx_t * txn_ctx ) {
    1423           0 :   fd_sha512_t * shas[ FD_TXN_ACTUAL_SIG_MAX ];
    1424           0 :   for ( ulong i=0UL; i<FD_TXN_ACTUAL_SIG_MAX; i++ ) {
    1425           0 :     fd_sha512_t * sha = fd_sha512_join( fd_sha512_new( fd_spad_alloc( txn_ctx->spad, alignof(fd_sha512_t), sizeof(fd_sha512_t) ) ) );
    1426           0 :     if( FD_UNLIKELY( !sha ) ) FD_LOG_ERR(( "fd_sha512_join failed" ));
    1427           0 :     shas[i] = sha;
    1428           0 :   }
    1429             : 
    1430           0 :   uchar  signature_cnt = txn_ctx->txn_descriptor->signature_cnt;
    1431           0 :   ushort signature_off = txn_ctx->txn_descriptor->signature_off;
    1432           0 :   ushort acct_addr_off = txn_ctx->txn_descriptor->acct_addr_off;
    1433           0 :   ushort message_off   = txn_ctx->txn_descriptor->message_off;
    1434             : 
    1435           0 :   uchar const * signatures = (uchar *)txn_ctx->_txn_raw->raw + signature_off;
    1436           0 :   uchar const * pubkeys = (uchar *)txn_ctx->_txn_raw->raw + acct_addr_off;
    1437           0 :   uchar const * msg = (uchar *)txn_ctx->_txn_raw->raw + message_off;
    1438           0 :   ulong msg_sz = (ulong)txn_ctx->_txn_raw->txn_sz - message_off;
    1439             : 
    1440             :   /* Verify signatures */
    1441           0 :   int res = fd_ed25519_verify_batch_single_msg( msg, msg_sz, signatures, pubkeys, shas, signature_cnt );
    1442           0 :   if( FD_UNLIKELY( res != FD_ED25519_SUCCESS ) ) {
    1443           0 :     return -1;
    1444           0 :   }
    1445             : 
    1446           0 :   return 0;
    1447           0 : }
    1448             : 
    1449             : int
    1450           0 : fd_execute_txn( fd_execute_txn_task_info_t * task_info ) {
    1451             :   /* Don't execute transactions that are fee only.
    1452             :      https://github.com/anza-xyz/agave/blob/v2.1.6/svm/src/transaction_processor.rs#L341-L357 */
    1453           0 :   if( FD_UNLIKELY( task_info->txn->flags & FD_TXN_P_FLAGS_FEES_ONLY ) ) {
    1454             :     /* return the existing error */
    1455           0 :     return task_info->exec_res;
    1456           0 :   }
    1457             : 
    1458           0 :   fd_exec_txn_ctx_t * txn_ctx   = task_info->txn_ctx;
    1459           0 :   bool                dump_insn = txn_ctx->capture_ctx && txn_ctx->slot >= txn_ctx->capture_ctx->dump_proto_start_slot && txn_ctx->capture_ctx->dump_instr_to_pb;
    1460             : 
    1461             :   /* Initialize log collection */
    1462           0 :   fd_log_collector_init( &txn_ctx->log_collector, txn_ctx->enable_exec_recording );
    1463             : 
    1464           0 :   for( ushort i = 0; i < txn_ctx->txn_descriptor->instr_cnt; i++ ) {
    1465           0 :     txn_ctx->current_instr_idx = i;
    1466           0 :     if( FD_UNLIKELY( dump_insn ) ) {
    1467             :       // Capture the input and convert it into a Protobuf message
    1468           0 :       fd_dump_instr_to_protobuf( txn_ctx, &txn_ctx->instr_infos[i], i );
    1469           0 :     }
    1470             : 
    1471           0 :     int instr_exec_result = fd_execute_instr( txn_ctx, &txn_ctx->instr_infos[i] );
    1472           0 :     if( FD_UNLIKELY( instr_exec_result!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
    1473           0 :       if ( txn_ctx->instr_err_idx==INT_MAX ) {
    1474           0 :         txn_ctx->instr_err_idx = i;
    1475           0 :       }
    1476           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
    1477           0 :     }
    1478           0 :   }
    1479             : 
    1480             :   /* TODO: This function needs to be split out of fd_execute_txn and be placed
    1481             :       into the replay tile once it is implemented. */
    1482           0 :   int err = fd_executor_txn_check( txn_ctx );
    1483           0 :   if( FD_UNLIKELY( err!=FD_EXECUTOR_INSTR_SUCCESS ) ) {
    1484           0 :     FD_LOG_WARNING(( "fd_executor_txn_check failed (%d)", err ));
    1485           0 :     return err;
    1486           0 :   }
    1487           0 :   return 0;
    1488           0 : }
    1489             : 
    1490             : int
    1491           0 : fd_executor_txn_check( fd_exec_txn_ctx_t * txn_ctx ) {
    1492           0 :   fd_rent_t const * rent = fd_bank_rent_query( txn_ctx->bank );
    1493             : 
    1494           0 :   ulong starting_lamports_l = 0;
    1495           0 :   ulong starting_lamports_h = 0;
    1496             : 
    1497           0 :   ulong ending_lamports_l = 0;
    1498           0 :   ulong ending_lamports_h = 0;
    1499             : 
    1500             :   /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L63 */
    1501           0 :   for( ulong idx = 0; idx < txn_ctx->accounts_cnt; idx++ ) {
    1502           0 :     fd_txn_account_t * b = &txn_ctx->accounts[idx];
    1503             : 
    1504             :     // Was this account written to?
    1505             :     /* TODO: Clean this logic up... lots of redundant checks with our newer account loading model.
    1506             :        We should be using the rent transition checking logic instead, along with a small refactor
    1507             :        to keep check ordering consistent. */
    1508           0 :     if( b->vt->get_meta( b )!=NULL ) {
    1509           0 :       fd_uwide_inc( &ending_lamports_h, &ending_lamports_l, ending_lamports_h, ending_lamports_l, b->vt->get_lamports( b ) );
    1510             : 
    1511             :       /* Rent states are defined as followed:
    1512             :          - lamports == 0                      -> Uninitialized
    1513             :          - 0 < lamports < rent_exempt_minimum -> RentPaying
    1514             :          - lamports >= rent_exempt_minimum    -> RentExempt
    1515             :          In Agave, 'self' refers to our 'after' state. */
    1516           0 :       uchar after_uninitialized  = b->vt->get_lamports( b ) == 0;
    1517           0 :       uchar after_rent_exempt    = b->vt->get_lamports( b ) >= fd_rent_exempt_minimum_balance( rent, b->vt->get_data_len( b ) );
    1518             : 
    1519             :       /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L96 */
    1520           0 :       if( FD_LIKELY( memcmp( b->pubkey->key, fd_sysvar_incinerator_id.key, sizeof(fd_pubkey_t) ) != 0 ) ) {
    1521             :         /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L44 */
    1522           0 :         if( after_uninitialized || after_rent_exempt ) {
    1523             :           // no-op
    1524           0 :         } else {
    1525             :           /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L45-L59 */
    1526           0 :           uchar before_uninitialized = b->starting_dlen == ULONG_MAX || b->starting_lamports == 0;
    1527           0 :           uchar before_rent_exempt   = b->starting_dlen != ULONG_MAX && b->starting_lamports >= fd_rent_exempt_minimum_balance( rent, b->starting_dlen );
    1528             : 
    1529             :           /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L50 */
    1530           0 :           if( before_uninitialized || before_rent_exempt ) {
    1531           0 :             FD_LOG_DEBUG(( "Rent exempt error for %s Curr len %lu Starting len %lu Curr lamports %lu Starting lamports %lu Curr exempt %lu Starting exempt %lu",
    1532           0 :                            FD_BASE58_ENC_32_ALLOCA( b->pubkey->uc ),
    1533           0 :                            b->vt->get_data_len( b ),
    1534           0 :                            b->starting_dlen,
    1535           0 :                            b->vt->get_lamports( b ),
    1536           0 :                            b->starting_lamports,
    1537           0 :                            fd_rent_exempt_minimum_balance( rent, b->vt->get_data_len( b ) ),
    1538           0 :                            fd_rent_exempt_minimum_balance( rent, b->starting_dlen ) ));
    1539             :             /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L104 */
    1540           0 :             return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
    1541             :           /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L56 */
    1542           0 :           } else if( (b->vt->get_data_len( b ) == b->starting_dlen) && b->vt->get_lamports( b ) <= b->starting_lamports ) {
    1543             :             // no-op
    1544           0 :           } else {
    1545           0 :             FD_LOG_DEBUG(( "Rent exempt error for %s Curr len %lu Starting len %lu Curr lamports %lu Starting lamports %lu Curr exempt %lu Starting exempt %lu",
    1546           0 :                            FD_BASE58_ENC_32_ALLOCA( b->pubkey->uc ),
    1547           0 :                            b->vt->get_data_len( b ),
    1548           0 :                            b->starting_dlen,
    1549           0 :                            b->vt->get_lamports( b ),
    1550           0 :                            b->starting_lamports,
    1551           0 :                            fd_rent_exempt_minimum_balance( rent, b->vt->get_data_len( b ) ),
    1552           0 :                            fd_rent_exempt_minimum_balance( rent, b->starting_dlen ) ));
    1553             :             /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/account_rent_state.rs#L104 */
    1554           0 :             return FD_RUNTIME_TXN_ERR_INSUFFICIENT_FUNDS_FOR_RENT;
    1555           0 :           }
    1556           0 :         }
    1557           0 :       }
    1558             : 
    1559           0 :       if( b->starting_lamports != ULONG_MAX ) {
    1560           0 :         fd_uwide_inc( &starting_lamports_h, &starting_lamports_l, starting_lamports_h, starting_lamports_l, b->starting_lamports );
    1561           0 :       }
    1562           0 :     }
    1563           0 :   }
    1564             : 
    1565             :   /* https://github.com/anza-xyz/agave/blob/b2c388d6cbff9b765d574bbb83a4378a1fc8af32/svm/src/transaction_processor.rs#L839-L845 */
    1566           0 :   if( FD_UNLIKELY( ending_lamports_l!=starting_lamports_l || ending_lamports_h!=starting_lamports_h ) ) {
    1567           0 :     FD_LOG_DEBUG(( "Lamport sum mismatch: starting %lx%lx ending %lx%lx", starting_lamports_h, starting_lamports_l, ending_lamports_h, ending_lamports_l ));
    1568           0 :     return FD_RUNTIME_TXN_ERR_UNBALANCED_TRANSACTION;
    1569           0 :   }
    1570             : 
    1571           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
    1572           0 : }
    1573             : 
    1574             : /* fd_executor_instr_strerror() returns the error message corresponding to err,
    1575             :    intended to be logged by log_collector, or an empty string if the error code
    1576             :    should be omitted in logs for whatever reason.  Omitted examples are success,
    1577             :    fatal (placeholder just in firedancer), custom error.
    1578             :    See also fd_log_collector_program_failure(). */
    1579             : FD_FN_CONST char const *
    1580           0 : fd_executor_instr_strerror( int err ) {
    1581             : 
    1582           0 :   switch( err ) {
    1583           0 :   case FD_EXECUTOR_INSTR_SUCCESS                                : return ""; // not used
    1584           0 :   case FD_EXECUTOR_INSTR_ERR_FATAL                              : return ""; // not used
    1585           0 :   case FD_EXECUTOR_INSTR_ERR_GENERIC_ERR                        : return "generic instruction error";
    1586           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_ARG                        : return "invalid program argument";
    1587           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA                 : return "invalid instruction data";
    1588           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_DATA                   : return "invalid account data for instruction";
    1589           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_DATA_TOO_SMALL                 : return "account data too small for instruction";
    1590           0 :   case FD_EXECUTOR_INSTR_ERR_INSUFFICIENT_FUNDS                 : return "insufficient funds for instruction";
    1591           0 :   case FD_EXECUTOR_INSTR_ERR_INCORRECT_PROGRAM_ID               : return "incorrect program id for instruction";
    1592           0 :   case FD_EXECUTOR_INSTR_ERR_MISSING_REQUIRED_SIGNATURE         : return "missing required signature for instruction";
    1593           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_ALREADY_INITIALIZED            : return "instruction requires an uninitialized account";
    1594           0 :   case FD_EXECUTOR_INSTR_ERR_UNINITIALIZED_ACCOUNT              : return "instruction requires an initialized account";
    1595           0 :   case FD_EXECUTOR_INSTR_ERR_UNBALANCED_INSTR                   : return "sum of account balances before and after instruction do not match";
    1596           0 :   case FD_EXECUTOR_INSTR_ERR_MODIFIED_PROGRAM_ID                : return "instruction illegally modified the program id of an account";
    1597           0 :   case FD_EXECUTOR_INSTR_ERR_EXTERNAL_ACCOUNT_LAMPORT_SPEND     : return "instruction spent from the balance of an account it does not own";
    1598           0 :   case FD_EXECUTOR_INSTR_ERR_EXTERNAL_DATA_MODIFIED             : return "instruction modified data of an account it does not own";
    1599           0 :   case FD_EXECUTOR_INSTR_ERR_READONLY_LAMPORT_CHANGE            : return "instruction changed the balance of a read-only account";
    1600           0 :   case FD_EXECUTOR_INSTR_ERR_READONLY_DATA_MODIFIED             : return "instruction modified data of a read-only account";
    1601           0 :   case FD_EXECUTOR_INSTR_ERR_DUPLICATE_ACCOUNT_IDX              : return "instruction contains duplicate accounts";
    1602           0 :   case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_MODIFIED                : return "instruction changed executable bit of an account";
    1603           0 :   case FD_EXECUTOR_INSTR_ERR_RENT_EPOCH_MODIFIED                : return "instruction modified rent epoch of an account";
    1604           0 :   case FD_EXECUTOR_INSTR_ERR_NOT_ENOUGH_ACC_KEYS                : return "insufficient account keys for instruction";
    1605           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_DATA_SIZE_CHANGED              : return "program other than the account's owner changed the size of the account data";
    1606           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_NOT_EXECUTABLE                 : return "instruction expected an executable account";
    1607           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_FAILED                  : return "instruction tries to borrow reference for an account which is already borrowed";
    1608           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_BORROW_OUTSTANDING             : return "instruction left account with an outstanding borrowed reference";
    1609           0 :   case FD_EXECUTOR_INSTR_ERR_DUPLICATE_ACCOUNT_OUT_OF_SYNC      : return "instruction modifications of multiply-passed account differ";
    1610           0 :   case FD_EXECUTOR_INSTR_ERR_CUSTOM_ERR                         : return ""; // custom handling via txn_ctx->custom_err
    1611           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_ERR                        : return "program returned invalid error code";
    1612           0 :   case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_DATA_MODIFIED           : return "instruction changed executable accounts data";
    1613           0 :   case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_LAMPORT_CHANGE          : return "instruction changed the balance of an executable account";
    1614           0 :   case FD_EXECUTOR_INSTR_ERR_EXECUTABLE_ACCOUNT_NOT_RENT_EXEMPT : return "executable accounts must be rent exempt";
    1615           0 :   case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_PROGRAM_ID             : return "Unsupported program id";
    1616           0 :   case FD_EXECUTOR_INSTR_ERR_CALL_DEPTH                         : return "Cross-program invocation call depth too deep";
    1617           0 :   case FD_EXECUTOR_INSTR_ERR_MISSING_ACC                        : return "An account required by the instruction is missing";
    1618           0 :   case FD_EXECUTOR_INSTR_ERR_REENTRANCY_NOT_ALLOWED             : return "Cross-program invocation reentrancy not allowed for this instruction";
    1619           0 :   case FD_EXECUTOR_INSTR_ERR_MAX_SEED_LENGTH_EXCEEDED           : return "Length of the seed is too long for address generation";
    1620           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_SEEDS                      : return "Provided seeds do not result in a valid address";
    1621           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_REALLOC                    : return "Failed to reallocate account data";
    1622           0 :   case FD_EXECUTOR_INSTR_ERR_COMPUTE_BUDGET_EXCEEDED            : return "Computational budget exceeded";
    1623           0 :   case FD_EXECUTOR_INSTR_ERR_PRIVILEGE_ESCALATION               : return "Cross-program invocation with unauthorized signer or writable account";
    1624           0 :   case FD_EXECUTOR_INSTR_ERR_PROGRAM_ENVIRONMENT_SETUP_FAILURE  : return "Failed to create program execution environment";
    1625           0 :   case FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPLETE         : return "Program failed to complete";
    1626           0 :   case FD_EXECUTOR_INSTR_ERR_PROGRAM_FAILED_TO_COMPILE          : return "Program failed to compile";
    1627           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_IMMUTABLE                      : return "Account is immutable";
    1628           0 :   case FD_EXECUTOR_INSTR_ERR_INCORRECT_AUTHORITY                : return "Incorrect authority provided";
    1629           0 :   case FD_EXECUTOR_INSTR_ERR_BORSH_IO_ERROR                     : return "Failed to serialize or deserialize account data"; // truncated
    1630           0 :   case FD_EXECUTOR_INSTR_ERR_ACC_NOT_RENT_EXEMPT                : return "An account does not have enough lamports to be rent-exempt";
    1631           0 :   case FD_EXECUTOR_INSTR_ERR_INVALID_ACC_OWNER                  : return "Invalid account owner";
    1632           0 :   case FD_EXECUTOR_INSTR_ERR_ARITHMETIC_OVERFLOW                : return "Program arithmetic overflowed";
    1633           0 :   case FD_EXECUTOR_INSTR_ERR_UNSUPPORTED_SYSVAR                 : return "Unsupported sysvar";
    1634           0 :   case FD_EXECUTOR_INSTR_ERR_ILLEGAL_OWNER                      : return "Provided owner is not allowed";
    1635           0 :   case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_DATA_ALLOCS_EXCEEDED      : return "Accounts data allocations exceeded the maximum allowed per transaction";
    1636           0 :   case FD_EXECUTOR_INSTR_ERR_MAX_ACCS_EXCEEDED                  : return "Max accounts exceeded";
    1637           0 :   case FD_EXECUTOR_INSTR_ERR_MAX_INSN_TRACE_LENS_EXCEEDED       : return "Max instruction trace length exceeded";
    1638           0 :   case FD_EXECUTOR_INSTR_ERR_BUILTINS_MUST_CONSUME_CUS          : return "Builtin programs must consume compute units";
    1639           0 :   default: break;
    1640           0 :   }
    1641             : 
    1642           0 :   return "";
    1643           0 : }
    1644             : 
    1645             : // This is purely linker magic to force the inclusion of the yaml type walker so that it is
    1646             : // available for debuggers
    1647             : void
    1648           0 : fd_debug_symbology(void) {
    1649           0 :   (void)fd_get_types_yaml();
    1650           0 : }

Generated by: LCOV version 1.14