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