LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_executor.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 8 922 0.9 %
Date: 2025-07-18 05:01:12 Functions: 1 42 2.4 %

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

Generated by: LCOV version 1.14