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 122 0.0 %
Date: 2025-08-05 05:04:49 Functions: 0 7 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             : #include "../fd_compute_budget_details.h"
      12             : 
      13           0 : #define DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT    (200000UL)
      14             : #define DEFAULT_COMPUTE_UNITS                     (150UL)
      15             : 
      16             : /* https://github.com/anza-xyz/agave/blob/v2.1.13/compute-budget/src/compute_budget_limits.rs#L11-L13 */
      17           0 : #define MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT (3000UL)
      18             : 
      19             : FD_FN_PURE static inline uchar
      20             : get_program_kind( fd_exec_txn_ctx_t const * txn_ctx,
      21           0 :                   fd_txn_instr_t const *    instr ) {
      22           0 :   fd_acct_addr_t const * txn_accs       = fd_txn_get_acct_addrs( txn_ctx->txn_descriptor, txn_ctx->_txn_raw->raw );
      23           0 :   fd_pubkey_t const *    program_pubkey = fd_type_pun_const( &txn_accs[ instr->program_id ] );
      24             : 
      25             :   /* The program is a standard, non-migrating builtin (e.g. system program) */
      26           0 :   if( fd_is_non_migrating_builtin_program( program_pubkey ) ) {
      27           0 :     return FD_PROGRAM_KIND_BUILTIN;
      28           0 :   }
      29             : 
      30           0 :   uchar migrated_yet;
      31           0 :   uchar is_migrating_program = fd_is_migrating_builtin_program( txn_ctx, program_pubkey, &migrated_yet );
      32             : 
      33             :   /* The program has a BPF migration config but has not been migrated yet, so it's still a builtin program */
      34           0 :   if( is_migrating_program && !migrated_yet ) {
      35           0 :     return FD_PROGRAM_KIND_BUILTIN;
      36           0 :   }
      37             : 
      38             :   /* The program has a BPF migration config AND has been migrated */
      39           0 :   if( is_migrating_program && migrated_yet ) {
      40           0 :     return FD_PROGRAM_KIND_MIGRATING_BUILTIN;
      41           0 :   }
      42             : 
      43             :   /* The program is some other program kind, i.e. not a builtin */
      44           0 :   return FD_PROGRAM_KIND_NOT_BUILTIN;
      45           0 : }
      46             : 
      47             : FD_FN_PURE static inline int
      48             : is_compute_budget_instruction( fd_txn_t const *       txn,
      49             :                                fd_rawtxn_b_t const *  txn_raw,
      50           0 :                                fd_txn_instr_t const * instr ) {
      51           0 :   fd_acct_addr_t const * txn_accs       = fd_txn_get_acct_addrs( txn, txn_raw->raw );
      52           0 :   fd_acct_addr_t const * program_pubkey = &txn_accs[ instr->program_id ];
      53           0 :   return !memcmp(program_pubkey, fd_solana_compute_budget_program_id.key, sizeof(fd_pubkey_t));
      54           0 : }
      55             : 
      56             : /* In our implementation of this function, our parameters map to Agave's as follows:
      57             :   - `num_builtin_instrs` -> `num_non_migratable_builtin_instructions` + `num_not_migrated`
      58             :   - `num_non_builtin_instrs` -> `num_non_builtin_instructions` + `num_migrated`
      59             : 
      60             :    https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L211-L239 */
      61             : FD_FN_PURE static inline ulong
      62             : calculate_default_compute_unit_limit( ulong num_builtin_instrs,
      63           0 :                                       ulong num_non_builtin_instrs ) {
      64             :   /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L227-L234 */
      65           0 :   return fd_ulong_sat_add( fd_ulong_sat_mul( num_builtin_instrs, MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT ),
      66           0 :                            fd_ulong_sat_mul( num_non_builtin_instrs, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT ) );
      67             : 
      68           0 : }
      69             : 
      70             : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/compute-budget/src/compute_budget_processor.rs#L150-L153 */
      71             : FD_FN_PURE static inline int
      72           0 : sanitize_requested_heap_size( ulong bytes ) {
      73           0 :   return !(bytes>FD_MAX_HEAP_FRAME_BYTES || bytes<FD_MIN_HEAP_FRAME_BYTES || bytes%FD_HEAP_FRAME_BYTES_GRANULARITY);
      74           0 : }
      75             : 
      76             : int
      77           0 : fd_sanitize_compute_unit_limits( fd_exec_txn_ctx_t * ctx ) {
      78           0 :   fd_compute_budget_details_t * details = &ctx->compute_budget_details;
      79             : 
      80             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget-instruction/src/compute_budget_instruction_details.rs#L106-L119 */
      81           0 :   if( details->has_requested_heap_size ) {
      82           0 :     if( FD_UNLIKELY( !sanitize_requested_heap_size( details->heap_size ) ) ) {
      83           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, details->requested_heap_size_instr_index );
      84           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
      85           0 :     }
      86           0 :   }
      87             : 
      88             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget-instruction/src/compute_budget_instruction_details.rs#L122-L128 */
      89           0 :   if( !details->has_compute_units_limit_update ) {
      90           0 :     details->compute_unit_limit = calculate_default_compute_unit_limit( details->num_builtin_instrs,
      91           0 :                                                                         details->num_non_builtin_instrs );
      92           0 :   }
      93           0 :   details->compute_unit_limit = fd_ulong_min( FD_MAX_COMPUTE_UNIT_LIMIT, details->compute_unit_limit );
      94           0 :   details->compute_meter      = details->compute_unit_limit;
      95             : 
      96             :   /* https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget-instruction/src/compute_budget_instruction_details.rs#L136-L145 */
      97           0 :   if( details->has_loaded_accounts_data_size_limit_update ) {
      98           0 :     if( FD_UNLIKELY( details->loaded_accounts_data_size_limit==0UL ) ) {
      99           0 :       return FD_RUNTIME_TXN_ERR_INVALID_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
     100           0 :     }
     101           0 :     details->loaded_accounts_data_size_limit = fd_ulong_min( FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT,
     102           0 :                                                              details->loaded_accounts_data_size_limit );
     103           0 :   }
     104             : 
     105           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     106           0 : }
     107             : 
     108             : /* Like Agave, this function is called during transaction verification
     109             :    and is responsible for simply reading and decoding the compute budget
     110             :    instruction data. Throws an error if any compute budget instruction
     111             :    in the transaction has invalid instruction data, or if there are duplicate
     112             :    compute budget instructions.
     113             : 
     114             :    NOTE: At this point, the transaction context has NOT been fully
     115             :    initialized (namely, the accounts). The accounts are NOT safe to access.
     116             : 
     117             :    https://github.com/anza-xyz/agave/blob/v2.3.1/compute-budget-instruction/src/compute_budget_instruction_details.rs#L54-L99 */
     118             : int
     119           0 : fd_executor_compute_budget_program_execute_instructions( fd_exec_txn_ctx_t * ctx ) {
     120           0 :   fd_compute_budget_details_t * details = &ctx->compute_budget_details;
     121             : 
     122           0 :   for( ushort i=0; i<ctx->txn_descriptor->instr_cnt; i++ ) {
     123           0 :     fd_txn_instr_t const * instr = &ctx->txn_descriptor->instr[i];
     124             : 
     125             :     /* Only `FD_PROGRAM_KIND_BUILTIN` gets charged as a builtin instruction */
     126           0 :     uchar program_kind = get_program_kind( ctx, instr );
     127           0 :     if( program_kind==FD_PROGRAM_KIND_BUILTIN ) {
     128           0 :       details->num_builtin_instrs++;
     129           0 :     } else {
     130           0 :       details->num_non_builtin_instrs++;
     131           0 :     }
     132             : 
     133           0 :     if( !is_compute_budget_instruction( ctx->txn_descriptor, ctx->_txn_raw, instr ) ) {
     134           0 :       continue;
     135           0 :     }
     136             : 
     137             :     /* Deserialize the ComputeBudgetInstruction enum */
     138           0 :     uchar * data = (uchar *)ctx->_txn_raw->raw + instr->data_off;
     139             : 
     140           0 :     int ret;
     141           0 :     fd_compute_budget_program_instruction_t * instruction =
     142           0 :       fd_bincode_decode_spad(
     143           0 :         compute_budget_program_instruction, ctx->spad,
     144           0 :         data, instr->data_sz, &ret );
     145           0 :     if( FD_UNLIKELY( ret ) ) {
     146           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     147           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     148           0 :     }
     149             : 
     150           0 :     switch( instruction->discriminant ) {
     151           0 :       case fd_compute_budget_program_instruction_enum_request_heap_frame: {
     152           0 :         if( FD_UNLIKELY( details->has_requested_heap_size ) ) {
     153           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     154           0 :         }
     155             : 
     156           0 :         details->has_requested_heap_size         = 1;
     157           0 :         details->heap_size                       = instruction->inner.request_heap_frame;
     158           0 :         details->requested_heap_size_instr_index = i;
     159           0 :         break;
     160           0 :       }
     161           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_limit: {
     162           0 :         if( FD_UNLIKELY( details->has_compute_units_limit_update ) ) {
     163           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     164           0 :         }
     165             : 
     166           0 :         details->has_compute_units_limit_update = 1;
     167           0 :         details->compute_unit_limit             = instruction->inner.set_compute_unit_limit;
     168           0 :         break;
     169           0 :       }
     170           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_price: {
     171           0 :         if( FD_UNLIKELY( details->has_compute_units_price_update ) ) {
     172           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     173           0 :         }
     174             : 
     175           0 :         details->has_compute_units_price_update = 1;
     176           0 :         details->compute_unit_price             = instruction->inner.set_compute_unit_price;
     177           0 :         break;
     178           0 :       }
     179           0 :       case fd_compute_budget_program_instruction_enum_set_loaded_accounts_data_size_limit: {
     180           0 :           if( FD_UNLIKELY( details->has_loaded_accounts_data_size_limit_update ) ) {
     181           0 :             return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     182           0 :           }
     183             : 
     184           0 :           details->has_loaded_accounts_data_size_limit_update = 1;
     185           0 :           details->loaded_accounts_data_size_limit            = instruction->inner.set_loaded_accounts_data_size_limit;
     186           0 :           break;
     187           0 :       }
     188           0 :       default: {
     189           0 :         FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     190           0 :         return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     191           0 :       }
     192           0 :     }
     193           0 :   }
     194             : 
     195           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     196           0 : }
     197             : 
     198           0 : int fd_compute_budget_program_execute( fd_exec_instr_ctx_t * ctx ) {
     199           0 :   FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
     200           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     201           0 : }

Generated by: LCOV version 1.14