Skip to content

Commit

Permalink
Use saturating_add
Browse files Browse the repository at this point in the history
  • Loading branch information
lrubasze committed Oct 29, 2024
1 parent 046b477 commit 59c0797
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions radix-engine/src/vm/wasm_runtime/scrypto_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl<'y, Y: SystemApi<RuntimeError>> ScryptoRuntime<'y, Y> {
// Add 7,000 base units to make sure the we do not undercharge for WASM execution,
// which might lead to system exploitation.
// This is especially important in corner-cases such as `costing::spin_loop_v2` benchmark.
// Use larger cost unit buffer to call `consume_wasm_execution_exceeding_buffer`
// less frequently.
7000
};
Expand All @@ -55,7 +54,6 @@ impl<'y, Y: SystemApi<RuntimeError>> ScryptoRuntime<'y, Y> {
wasm_execution_units_base,
}
}

pub fn parse_blueprint_id(
package_address: Vec<u8>,
blueprint_name: Vec<u8>,
Expand All @@ -74,9 +72,9 @@ impl<'y, Y: SystemApi<RuntimeError>> ScryptoRuntime<'y, Y> {
) -> Result<(), InvokeError<WasmRuntimeError>> {
assert!(n > self.wasm_execution_units_buffer);
let n_remaining_after_buffer_used = n - self.wasm_execution_units_buffer;
let amount_to_request = n_remaining_after_buffer_used
.checked_add(WASM_EXECUTION_COST_UNITS_BUFFER)
.unwrap_or(u32::MAX);
let amount_to_request =
n_remaining_after_buffer_used.saturating_add(WASM_EXECUTION_COST_UNITS_BUFFER);

self.api
.consume_cost_units(ClientCostingEntry::RunWasmCode {
package_address: &self.package_address,
Expand Down Expand Up @@ -416,9 +414,7 @@ impl<'y, Y: SystemApi<RuntimeError>> WasmRuntime for ScryptoRuntime<'y, Y> {
&mut self,
n: u32,
) -> Result<(), InvokeError<WasmRuntimeError>> {
let n = n
.checked_add(self.wasm_execution_units_base)
.unwrap_or(u32::MAX);
let n = n.saturating_add(self.wasm_execution_units_base);

if n <= self.wasm_execution_units_buffer {
self.wasm_execution_units_buffer -= n;
Expand Down

0 comments on commit 59c0797

Please sign in to comment.