Skip to content

Commit

Permalink
Fail transaction if requested cu-limit exceeds max limit
Browse files Browse the repository at this point in the history
  • Loading branch information
tao-stones committed Apr 18, 2024
1 parent c7a5199 commit dc4c117
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions program-runtime/src/compute_budget_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ pub fn process_compute_budget_instructions<'a>(
}
}

// fail transaction if requesting cu-limit more than max
if let Some(user_requested_compute_unit_limit) = updated_compute_unit_limit {
if user_requested_compute_unit_limit > MAX_COMPUTE_UNIT_LIMIT {
return Err(TransactionError::InvalidComputeUnitLimitRequested);
}
}

// sanitize limits
let updated_heap_bytes = requested_heap_size
.unwrap_or(u32::try_from(MIN_HEAP_FRAME_BYTES).unwrap()) // loader's default heap_size
Expand Down Expand Up @@ -497,4 +504,16 @@ mod tests {
})
);
}

#[test]
fn test_process_instructions_invalid_compute_unit_limit() {
// larger than MAX_COMPUTE_UNIT_LIMIT
test!(
&[
ComputeBudgetInstruction::set_compute_unit_limit(MAX_COMPUTE_UNIT_LIMIT + 1),
Instruction::new_with_bincode(Pubkey::new_unique(), &0_u8, vec![]),
],
Err(TransactionError::InvalidComputeUnitLimitRequested)
);
}
}
5 changes: 5 additions & 0 deletions sdk/src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ pub enum TransactionError {
/// Program cache hit max limit.
#[error("Program cache hit max limit")]
ProgramCacheHitMaxLimit,

/// The requested compute-unit limit is either lesser than minimally required by the
/// transaction, or greater than maximal transaction compute-unit limit
#[error("Requested compute-unit limit is invalid")]
InvalidComputeUnitLimitRequested,
}

impl From<SanitizeError> for TransactionError {
Expand Down
1 change: 1 addition & 0 deletions storage-proto/proto/transaction_by_addr.proto
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ enum TransactionErrorType {
PROGRAM_EXECUTION_TEMPORARILY_RESTRICTED = 35;
UNBALANCED_TRANSACTION = 36;
PROGRAM_CACHE_HIT_MAX_LIMIT = 37;
INVALID_COMPUTE_UNIT_LIMIT_REQUESTED = 38;
}

message InstructionError {
Expand Down
12 changes: 12 additions & 0 deletions storage-proto/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ impl TryFrom<tx_by_addr::TransactionError> for TransactionError {
34 => TransactionError::ResanitizationNeeded,
36 => TransactionError::UnbalancedTransaction,
37 => TransactionError::ProgramCacheHitMaxLimit,
38 => TransactionError::InvalidComputeUnitLimitRequested,
_ => return Err("Invalid TransactionError"),
})
}
Expand Down Expand Up @@ -940,6 +941,9 @@ impl From<TransactionError> for tx_by_addr::TransactionError {
TransactionError::ProgramCacheHitMaxLimit => {
tx_by_addr::TransactionErrorType::ProgramCacheHitMaxLimit
}
TransactionError::InvalidComputeUnitLimitRequested => {
tx_by_addr::TransactionErrorType::InvalidComputeUnitLimitRequested
}
} as i32,
instruction_error: match transaction_error {
TransactionError::InstructionError(index, ref instruction_error) => {
Expand Down Expand Up @@ -1855,6 +1859,14 @@ mod test {
transaction_error,
tx_by_addr_transaction_error.try_into().unwrap()
);

let transaction_error = TransactionError::InvalidComputeUnitLimitRequested;
let tx_by_addr_transaction_error: tx_by_addr::TransactionError =
transaction_error.clone().into();
assert_eq!(
transaction_error,
tx_by_addr_transaction_error.try_into().unwrap()
);
}

#[test]
Expand Down

0 comments on commit dc4c117

Please sign in to comment.