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 154 0.0 %
Date: 2025-03-20 12:08:36 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 "../context/fd_exec_epoch_ctx.h"
      10             : #include "../../vm/fd_vm.h"
      11             : #include "fd_builtin_programs.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 * ctx,
      21           0 :                   fd_txn_instr_t const *    instr ) {
      22           0 :   fd_pubkey_t const * txn_accs       = ctx->account_keys;
      23           0 :   fd_pubkey_t const * program_pubkey = &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( 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_exec_txn_ctx_t const * ctx,
      49           0 :                                fd_txn_instr_t    const * instr ) {
      50           0 :   fd_pubkey_t const * txn_accs       = ctx->account_keys;
      51           0 :   fd_pubkey_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( fd_exec_txn_ctx_t const * ctx,
      62             :                                       ulong                     num_builtin_instrs,
      63             :                                       ulong                     num_non_builtin_instrs,
      64           0 :                                       ulong                     num_non_compute_budget_instrs ) {
      65           0 :   if( FD_FEATURE_ACTIVE( ctx->slot, ctx->features, reserve_minimal_cus_for_builtin_instructions ) ) {
      66             :     /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L227-L234 */
      67           0 :     return fd_ulong_sat_add( fd_ulong_sat_mul( num_builtin_instrs, MAX_BUILTIN_ALLOCATION_COMPUTE_UNIT_LIMIT ),
      68           0 :                              fd_ulong_sat_mul( num_non_builtin_instrs, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT ) );
      69           0 :   } else {
      70             :     /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L236-L237 */
      71           0 :     return fd_ulong_sat_mul( num_non_compute_budget_instrs, DEFAULT_INSTRUCTION_COMPUTE_UNIT_LIMIT );
      72           0 :   }
      73           0 : }
      74             : 
      75             : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/compute-budget/src/compute_budget_processor.rs#L150-L153 */
      76             : FD_FN_PURE static inline int
      77           0 : sanitize_requested_heap_size( ulong bytes ) {
      78           0 :   return !(bytes>FD_MAX_HEAP_FRAME_BYTES || bytes<FD_MIN_HEAP_FRAME_BYTES || bytes%FD_HEAP_FRAME_BYTES_GRANULARITY);
      79           0 : }
      80             : 
      81             : /* https://github.com/anza-xyz/agave/blob/16de8b75ebcd57022409b422de557dd37b1de8db/compute-budget/src/compute_budget_processor.rs#L69-L148 */
      82             : int
      83           0 : fd_executor_compute_budget_program_execute_instructions( fd_exec_txn_ctx_t * ctx, fd_rawtxn_b_t const * txn_raw ) {
      84           0 :   uint   has_compute_units_limit_update              = 0UL;
      85           0 :   uint   has_compute_units_price_update              = 0UL;
      86           0 :   uint   has_requested_heap_size                     = 0UL;
      87           0 :   uint   has_loaded_accounts_data_size_limit_update  = 0UL;
      88             : 
      89           0 :   ushort requested_heap_size_instr_index             = 0;
      90             : 
      91             :   /* SIMD-170 introduces a conservative CU limit of 3,000 CUs per non-migrated native program,
      92             :      and 200,000 CUs for all other programs (including migrated builtins). */
      93           0 :   ulong  num_non_compute_budget_instrs               = 0UL;
      94           0 :   ulong  num_builtin_instrs                          = 0UL;
      95           0 :   ulong  num_non_builtin_instrs                      = 0UL;
      96             : 
      97           0 :   uint   updated_compute_unit_limit                  = 0U;
      98           0 :   uint   updated_requested_heap_size                 = 0U;
      99           0 :   uint   updated_loaded_accounts_data_size_limit     = 0U;
     100           0 :   ulong  updated_compute_unit_price                  = 0UL;
     101             : 
     102           0 :   uint prioritization_fee_type = FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_COMPUTE_UNIT_PRICE;
     103             : 
     104           0 :   for( ushort i=0; i<ctx->txn_descriptor->instr_cnt; i++ ) {
     105           0 :     fd_txn_instr_t const * instr = &ctx->txn_descriptor->instr[i];
     106             : 
     107             :     /* Track builtin vs non-builtin metrics only if SIMD-170 is active */
     108           0 :     if( FD_FEATURE_ACTIVE( ctx->slot, ctx->features, reserve_minimal_cus_for_builtin_instructions ) ) {
     109             :       /* Only `FD_PROGRAM_KIND_BUILTIN` gets charged as a builtin instruction */
     110           0 :       uchar program_kind = get_program_kind( ctx, instr );
     111           0 :       if( program_kind==FD_PROGRAM_KIND_BUILTIN ) {
     112           0 :         num_builtin_instrs++;
     113           0 :       } else {
     114           0 :         num_non_builtin_instrs++;
     115           0 :       }
     116           0 :     }
     117             : 
     118           0 :     if( !is_compute_budget_instruction( ctx, instr ) ) {
     119           0 :       num_non_compute_budget_instrs++;
     120           0 :       continue;
     121           0 :     }
     122             : 
     123             :     /* Deserialize the ComputeBudgetInstruction enum */
     124           0 :     uchar * data = (uchar *)txn_raw->raw + instr->data_off;
     125             : 
     126           0 :     fd_bincode_decode_ctx_t decode_ctx = {
     127           0 :       .data    = data,
     128           0 :       .dataend = &data[ instr->data_sz ],
     129           0 :     };
     130             : 
     131           0 :     ulong total_sz = 0UL;
     132           0 :     int   ret      = fd_compute_budget_program_instruction_decode_footprint( &decode_ctx, &total_sz );
     133           0 :     if( FD_UNLIKELY( ret ) ) {
     134           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     135           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     136           0 :     }
     137             : 
     138           0 :     uchar * mem = fd_spad_alloc( ctx->spad, fd_compute_budget_program_instruction_align(), total_sz );
     139           0 :     if( FD_UNLIKELY( !mem ) ) {
     140           0 :       FD_LOG_ERR(( "Unable to allocate memory for ComputeBudgetInstruction" ));
     141           0 :     }
     142             : 
     143           0 :     fd_compute_budget_program_instruction_t * instruction =
     144           0 :       fd_compute_budget_program_instruction_decode( mem, &decode_ctx );
     145             : 
     146           0 :     switch( instruction->discriminant ) {
     147           0 :       case fd_compute_budget_program_instruction_enum_request_heap_frame: {
     148           0 :         if( FD_UNLIKELY( has_requested_heap_size ) ) {
     149           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     150           0 :         }
     151             : 
     152           0 :         has_requested_heap_size     = 1U;
     153           0 :         updated_requested_heap_size = instruction->inner.request_heap_frame;
     154           0 :         requested_heap_size_instr_index = i;
     155             : 
     156           0 :         break;
     157           0 :       }
     158           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_limit: {
     159           0 :         if( FD_UNLIKELY( has_compute_units_limit_update ) ) {
     160           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     161           0 :         }
     162             : 
     163           0 :         has_compute_units_limit_update  = 1U;
     164           0 :         updated_compute_unit_limit      = instruction->inner.set_compute_unit_limit;
     165             : 
     166           0 :         break;
     167           0 :       }
     168           0 :       case fd_compute_budget_program_instruction_enum_set_compute_unit_price: {
     169           0 :         if( FD_UNLIKELY( has_compute_units_price_update ) ) {
     170           0 :           return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     171           0 :         }
     172             : 
     173           0 :         has_compute_units_price_update = 1U;
     174           0 :         prioritization_fee_type        = FD_COMPUTE_BUDGET_PRIORITIZATION_FEE_TYPE_COMPUTE_UNIT_PRICE;
     175           0 :         updated_compute_unit_price     = instruction->inner.set_compute_unit_price;
     176             : 
     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( has_loaded_accounts_data_size_limit_update ) ) {
     181           0 :             return FD_RUNTIME_TXN_ERR_DUPLICATE_INSTRUCTION;
     182           0 :           }
     183             : 
     184           0 :           has_loaded_accounts_data_size_limit_update = 1U;
     185           0 :           updated_loaded_accounts_data_size_limit    = instruction->inner.set_loaded_accounts_data_size_limit;
     186             : 
     187           0 :           break;
     188           0 :       }
     189           0 :       default: {
     190           0 :         FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, i );
     191           0 :         return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     192           0 :       }
     193           0 :     }
     194           0 :   }
     195             : 
     196             :   /* https://github.com/anza-xyz/agave/blob/v2.1/runtime-transaction/src/compute_budget_instruction_details.rs#L51-L64 */
     197           0 :   if( has_requested_heap_size ) {
     198           0 :     if( FD_UNLIKELY( !sanitize_requested_heap_size( updated_requested_heap_size ) ) ) {
     199           0 :       FD_TXN_ERR_FOR_LOG_INSTR( ctx, FD_EXECUTOR_INSTR_ERR_INVALID_INSTR_DATA, requested_heap_size_instr_index );
     200           0 :       return FD_RUNTIME_TXN_ERR_INSTRUCTION_ERROR;
     201           0 :     }
     202           0 :     ctx->heap_size = updated_requested_heap_size;
     203           0 :   }
     204             : 
     205             :   /* https://github.com/anza-xyz/agave/blob/v2.1.13/runtime-transaction/src/compute_budget_instruction_details.rs#L137-L143 */
     206           0 :   if( has_compute_units_limit_update ) {
     207           0 :     ctx->compute_unit_limit = fd_ulong_min( FD_MAX_COMPUTE_UNIT_LIMIT, updated_compute_unit_limit );
     208           0 :   } else {
     209           0 :     ctx->compute_unit_limit = fd_ulong_min( calculate_default_compute_unit_limit(ctx,
     210           0 :                                                                                     num_builtin_instrs,
     211           0 :                                                                                     num_non_builtin_instrs,
     212           0 :                                                                                     num_non_compute_budget_instrs),
     213           0 :                                             FD_MAX_COMPUTE_UNIT_LIMIT );
     214           0 :   }
     215           0 :   ctx->compute_meter = ctx->compute_unit_limit;
     216             : 
     217           0 :   if( has_compute_units_price_update ) {
     218           0 :     ctx->prioritization_fee_type = prioritization_fee_type;
     219           0 :     ctx->compute_unit_price      = updated_compute_unit_price;
     220           0 :   }
     221             : 
     222             :   /* https://github.com/anza-xyz/agave/blob/v2.1/runtime-transaction/src/compute_budget_instruction_details.rs#L84-L93 */
     223           0 :   if( has_loaded_accounts_data_size_limit_update ) {
     224           0 :     if( FD_UNLIKELY( updated_loaded_accounts_data_size_limit==0UL ) ) {
     225           0 :       return FD_RUNTIME_TXN_ERR_INVALID_LOADED_ACCOUNTS_DATA_SIZE_LIMIT;
     226           0 :     }
     227           0 :     ctx->loaded_accounts_data_size_limit =
     228           0 :       fd_ulong_min( FD_VM_LOADED_ACCOUNTS_DATA_SIZE_LIMIT, updated_loaded_accounts_data_size_limit );
     229           0 :   }
     230             : 
     231           0 :   return FD_RUNTIME_EXECUTE_SUCCESS;
     232           0 : }
     233             : 
     234             : 
     235           0 : int fd_compute_budget_program_execute( fd_exec_instr_ctx_t * ctx ) {
     236           0 :   FD_EXEC_CU_UPDATE( ctx, DEFAULT_COMPUTE_UNITS );
     237           0 :   return FD_EXECUTOR_INSTR_SUCCESS;
     238           0 : }

Generated by: LCOV version 1.14