Skip to content

Commit

Permalink
check limit exceedance before updating the usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadkaouk committed Sep 12, 2023
1 parent ba9552d commit f14d3e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
15 changes: 8 additions & 7 deletions frame/evm/src/runner/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ impl StorageMeter {
/// Records the given amount of storage usage. The amount is added to the current usage.
/// If the limit is reached, an error is returned.
pub fn record(&mut self, amount: u64) -> Result<(), MeterError> {
self.usage = self.usage.checked_add(amount).ok_or_else(|| {
self.usage = self.limit;
MeterError::LimitExceeded
})?;
let usage = self
.usage
.checked_add(amount)
.ok_or_else(|| MeterError::LimitExceeded)?;

if self.usage > self.limit {
if usage > self.limit {
return Err(MeterError::LimitExceeded);
}
self.usage = usage;
Ok(())
}

Expand Down Expand Up @@ -143,7 +144,7 @@ mod test {

// Exceeding the limit
let res = meter.record(1);
assert_eq!(meter.usage(), limit + 1);
assert_eq!(meter.usage(), limit);
assert!(res.is_err());
assert_eq!(res, Err(MeterError::LimitExceeded));
}
Expand Down Expand Up @@ -204,6 +205,6 @@ mod test {
let res = meter.record_dynamic_opcode_cost(Opcode::SSTORE, gas_cost, target);
assert!(res.is_err());
assert_eq!(res, Err(MeterError::LimitExceeded));
assert_eq!(meter.usage(), 232);
assert_eq!(meter.usage(), 116);
}
}
7 changes: 1 addition & 6 deletions frame/evm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,12 +709,7 @@ mod storage_growth_test {
result.exit_reason,
crate::ExitReason::Error(crate::ExitError::OutOfGas)
);
assert_eq!(
result.used_gas.effective.as_u64(),
expected_contract_create_storage_growth_gas(
PROOF_SIZE_CALLEE_CONTRACT_BYTECODE_LEN
)
);
assert_eq!(result.used_gas.effective.as_u64(), 78485);
// Assert that the contract entry does not exists in the storage.
assert!(!AccountCodes::<Test>::contains_key(result.value));
});
Expand Down

0 comments on commit f14d3e2

Please sign in to comment.