LCOV - code coverage report
Current view: top level - flamenco/runtime/program - fd_compute_budget_program.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 136 0.0 %
Date: 2025-07-10 04:52:38 Functions: 0 6 0.0 %

          Line data    Source code
       1             : #include "fd_compute_budget_program.h"
       2             : 
       3             : #include "../fd_runtime_err.h"
       4             : #include "../fd_system_ids.h"
       5             : #include "../fd_executor.h"
       6             : #include "../context/fd_exec_instr_ctx.h"
       7             : #include "../context/fd_exec_txn_ctx.h"
       8             : #include "../context/fd_exec_slot_ctx.h"
       9             : #include "../../vm/fd_vm.h"
      10             : #include "fd_builtin_programs.h"
      11             : 
      12           0 : #define DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT    (200000UL)
      13             : #define DEFAULT_COMPUTE_UNITS                     (150UL)
      14             : 
      15             : /* https://github.com/anza-xyz/agave/blob/v2.1.13/compute-budget/src/compute_budget_limits.rs#L11-L13 */
      16           0 : #define MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT (3000UL)
      17             : 
      18             : FD_FN_PURE static inline uchar
      19             : get_program_kind( fd_exec_txn_ctx_t const * txn_ctx,
      20           0 :                   fd_txn_instr_t const *    instr ) {
      21           0 :   fd_acct_addr_t const * txn_accs       = fd_txn_get_acct_addrs( txn_ctx->txn_descriptor, txn_ctx->_txn_raw->raw );
      22           0 :   fd_pubkey_t const *    program_pubkey = fd_type_pun_const( &txn_accs[ instr->program_id ] );
      23             : 
      24             :   /* The program is a standard, non-migrating builtin (e.g. system program) */
      25           0 :   if( fd_is_non_migrating_builtin_program( program_pubkey ) ) {
      26           0 :     return FD_PROGRAM_KIND_BUILTIN;
      27           0 :   }
      28             : 
      29           0 :   uchar migrated_yet;
      30           0 :   uchar is_migrating_program = fd_is_migrating_builtin_program( txn_ctx, program_pubkey, &migrated_yet );
      31             : 
      32             :   /* The program has a BPF migration config but has not been migrated yet, so it's still a builtin program */
      33           0 :   if( is_migrating_program && !migrated_yet ) {
      34           0 :     return FD_PROGRAM_KIND_BUILTIN;
      35           0 :   }
      36             : 
      37             :   /* The program has a BPF migration config AND has been migrated */
      38           0 :   if( is_migrating_program && migrated_yet ) {
      39           0 :     return FD_PROGRAM_KIND_MIGRATING_BUILTIN;
      40           0 :   }
      41             : 
      42             :   /* The program is some other program kind, i.e. not a builtin */
      43           0 :   return FD_PROGRAM_KIND_NOT_BUILTIN;
      44           0 : }
      45             : 
      46             : FD_FN_PURE static inline int
      47             : is_compute_budget_instruction( fd_txn_t const *       txn,
      48             :                                fd_rawtxn_b_t const *  txn_raw,
      49           0 :                                fd_txn_instr_t const * instr ) {
      50           0 :   fd_acct_addr_t const * txn_accs       = fd_txn_get_acct_addrs( txn, txn_raw->raw );
      51           0 :   fd_acct_addr_t const * program_pubkey = &txn_accs[ instr->program_id ];
      52           0 :   return !memcmp(program_pubkey, fd_solana_compute_budget_program_id.key, sizeof(fd_pubkey_t));
      53           0 : }
      54             : 
      55             : /* In our implementation of this function, our parameters map to Agave's as follows:
      56             :   - `num_builtin_instrs` -> `num_non_migratable_builtin_instructions` + `num_not_migrated`
      57             :   - `num_non_builtin_instrs` -> `num_non_builtin_instructions` + `num_migrated`
      58             : 
      59             :    https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L211-L239 */
      60             : FD_FN_PURE static inline ulong
      61             : calculate_default_compute_unit_limit( ulong num_builtin_instrs,
      62           0 :                                       ulong num_non_builtin_instrs ) {
      63             :   /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L227-L234 */
      64           0 :   return fd_ulong_sat_add( fd_ulong_sat_mul( num_builtin_instrs, MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT ),
      65           0 :                            fd_ulong_sat_mul( num_non_builtin_instrs, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT ) );
      66             : 
      67           0 : }
      68             : 
      69             : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/compute-budget/src/compute_budget_processor.rs#L150-L153 */
      70             : FD_FN_PURE static inline int
      71           0 : sanitize_requested_heap_size( ulong bytes ) {
      72           0 :   return !(bytes>FD_MAX_HEAP_FRAME_BYTES || bytes<FD_MIN_HEAP_FRAME_BYTES || bytes%FD_HEAP_FRAME_BYTES_GRANULARITY);
      73           0 : }
      74             : 
      75             : /* NOTE: At this point, the transaction context has NOT been fully initialized (namely, the accounts). The accounts are NOT safe to access.
      76             : 
      77             :    https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/compute-budget/src/compute_budget_processor.rs#L69-L148 */
      78             : int
      79           0 : fd_executor_compute_budget_program_execute_instructions( fd_exec_txn_ctx_t * ctx ) {
      80           0 :   uint   has_compute_units_limit_update              = 0UL;
      81           0 :   uint   has_compute_units_price_update              = 0UL;
      82           0 :   uint   has_requested_heap_size                     = 0UL;
      83           0 :   uint   has_loaded_accounts_data_size_limit_update  = 0UL;
      84             : 
      85           0 :   ushort requested_heap_size_instr_index             = 0;
      86             : 
      87             :   /* SIMD-170 introduces a conservative CU limit of 3,000 CUs per non-migrated native program,
      88             :      and 200,000 CUs for all other programs (including migrated builtins). */
      89           0 :   ulong  num_builtin_instrs                          = 0UL;
      90           0 :   ulong  num_non_builtin_instrs                      = 0UL;
      91             : 
      92           0 :   uint   updated_compute_unit_limit                  = 0U;
      93           0 :   uint   updated_requested_heap_size                 = 0U;
      94           0 :   uint   updated_loaded_accounts_data_size_limit     = 0U;
      95           0 :   ulong  updated_compute_unit_price                  = 0UL;
      96             : 
      97           0 :   uint prioritization_fee_type = FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_COMPUTE_UNIT_PRICE;
      98             : 
      99           0 :   for( ushort i=0; i<ctx->txn_descriptor->instr_cnt; i++ ) {
     100           0 :     fd_txn_instr_t const * instr = &ctx->txn_descriptor->instr[i];
     101             : 
     102             :     /* Only `FD_PROGRAM_KIND_BUILTIN` gets charged as a builtin instruction */
     103           0 :     uchar program_kind = get_program_kind( ctx, instr );
     104           0 :     if( program_kind==FD_PROGRAM_KIND_BUILTIN ) {
     105           0 :       num_builtin_instrs++;
     106           0 :     } else {
     107           0 :       num_non_builtin_instrs++;
     108           0 :     }
     109             : 
     110           0 :     if( !is_compute_budget_instruction( ctx->txn_descriptor, ctx->_txn_raw, instr ) ) {
     111           0 :       continue;
     112           0 :     }
     113             : 
     114             :     /* Deserialize the ComputeBudgetInstruction enum */
     115           0 :     uchar * data = (uchar *)ctx->_txn_raw->raw + instr->data_off;
     116             : 
     117           0 :     int ret;
     118           0 :     fd_compute_budget_program_instruction_t * instruction =
     119           0 :       fd_bincode_decode_spad(
     120           0 :         compute_budget_program_instruction, ctx->spad,
     121           0 :         data, instr->data_sz, &ret );
     122           0 :     if( FD_UNLIKELY( ret ) ) {
     123           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     124           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     125           0 :     }
     126             : 
     127           0 :     switch( instruction->discriminant ) {
     128           0 :       case fd_compute_budget_program_instruction_enum_request_heap_frame: {
     129           0 :         if( FD_UNLIKELY( has_requested_heap_size ) ) {
     130           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     131           0 :         }
     132             : 
     133           0 :         has_requested_heap_size     = 1U;
     134           0 :         updated_requested_heap_size = instruction->inner.request_heap_frame;
     135           0 :         requested_heap_size_instr_index = i;
     136             : 
     137           0 :         break;
     138           0 :       }
     139           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_limit: {
     140           0 :         if( FD_UNLIKELY( has_compute_units_limit_update ) ) {
     141           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     142           0 :         }
     143             : 
     144           0 :         has_compute_units_limit_update  = 1U;
     145           0 :         updated_compute_unit_limit      = instruction->inner.set_compute_unit_limit;
     146             : 
     147           0 :         break;
     148           0 :       }
     149           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_price: {
     150           0 :         if( FD_UNLIKELY( has_compute_units_price_update ) ) {
     151           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     152           0 :         }
     153             : 
     154           0 :         has_compute_units_price_update = 1U;
     155           0 :         prioritization_fee_type        = FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_COMPUTE_UNIT_PRICE;
     156           0 :         updated_compute_unit_price     = instruction->inner.set_compute_unit_price;
     157             : 
     158           0 :         break;
     159           0 :       }
     160           0 :       case fd_compute_budget_program_instruction_enum_set_loaded_accounts_data_size_limit: {
     161           0 :           if( FD_UNLIKELY( has_loaded_accounts_data_size_limit_update ) ) {
     162           0 :             return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     163           0 :           }
     164             : 
     165           0 :           has_loaded_accounts_data_size_limit_update = 1U;
     166           0 :           updated_loaded_accounts_data_size_limit    = instruction->inner.set_loaded_accounts_data_size_limit;
     167             : 
     168           0 :           break;
     169           0 :       }
     170           0 :       default: {
     171           0 :         FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     172           0 :         return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     173           0 :       }
     174           0 :     }
     175           0 :   }
     176             : 
     177             :   /* https://github.com/anza-xyz/agave/blob/v2.1/runtime-transaction/src/compute_budget_instruction_details.rs#L51-L64 */
     178           0 :   if( has_requested_heap_size ) {
     179           0 :     if( FD_UNLIKELY( !sanitize_requested_heap_size( updated_requested_heap_size ) ) ) {
     180           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, requested_heap_size_instr_index );
     181           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     182           0 :     }
     183           0 :     ctx->heap_size = updated_requested_heap_size;
     184           0 :   }
     185             : 
     186             :   /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L137-L143 */
     187           0 :   if( has_compute_units_limit_update ) {
     188           0 :     ctx->compute_unit_limit = fd_ulong_min( FD_MAX_COMPUTE_UNIT_LIMIT, updated_compute_unit_limit );
     189           0 :   } else {
     190           0 :     ctx->compute_unit_limit = fd_ulong_min( calculate_default_compute_unit_limit( num_builtin_instrs, num_non_builtin_instrs ),
     191           0 :                                             FD_MAX_COMPUTE_UNIT_LIMIT );
     192           0 :   }
     193           0 :   ctx->compute_meter = ctx->compute_unit_limit;
     194             : 
     195           0 :   if( has_compute_units_price_update ) {
     196           0 :     ctx->prioritization_fee_type = prioritization_fee_type;
     197           0 :     ctx->compute_unit_price      = updated_compute_unit_price;
     198           0 :   }
     199             : 
     200             :   /* https://github.com/anza-xyz/agave/blob/v2.1/runtime-transaction/src/compute_budget_instruction_details.rs#L84-L93 */
     201           0 :   if( has_loaded_accounts_data_size_limit_update ) {
     202           0 :     if( FD_UNLIKELY( updated_loaded_accounts_data_size_limit==0UL ) ) {
     203           0 :       return FD_RUNTIME_TXN_ERR_INVALID_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
     204           0 :     }
     205           0 :     ctx->loaded_accounts_data_size_limit =
     206           0 :       fd_ulong_min( FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT, updated_loaded_accounts_data_size_limit );
     207           0 :   }
     208             : 
     209           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     210           0 : }
     211             : 
     212             : 
     213           0 : int fd_compute_budget_program_execute( fd_exec_instr_ctx_t * ctx ) {
     214           0 :   FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
     215           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     216           0 : }

Generated by: LCOV version 1.14