Skip to content

Commit

Permalink
add clear error message to give more information at out of gas (#50)
Browse files Browse the repository at this point in the history
* add clear error message to give more information at out of gas

* unit round down
  • Loading branch information
beer-1 authored May 21, 2024
1 parent 32d0f33 commit 350eca3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion crates/gas/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,25 @@ impl InitiaGasMeter {

#[inline]
fn charge(&mut self, amount: InternalGas) -> PartialVMResult<()> {
// copy the value for error message
let balance = self.balance;

match self.balance.checked_sub(amount) {
Some(new_balance) => {
self.balance = new_balance;
Ok(())
}
None => {
self.balance = 0.into();
Err(PartialVMError::new(StatusCode::OUT_OF_GAS))
let gas_used: Gas = (self.gas_limit.checked_sub(balance).unwrap() + amount)
.to_unit_round_down_with_params(&self.gas_params.txn);

Err(
PartialVMError::new(StatusCode::OUT_OF_GAS).with_message(format!(
"gas_limit: {}, gas_used: {}",
self.gas_limit, gas_used
)),
)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/natives/src/crypto/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ pub fn native_batch_verify(
} else if public_keys_len == 1 && messages_len == signatures_len {
public_keys = repeats_vec_of_vec_u8(public_keys[0].to_vec(), signatures_len);
} else {
return Err(SafeNativeError::Abort { abort_code: NUMBER_OF_ARGUMENTS_MISMATCH });
return Err(SafeNativeError::Abort {
abort_code: NUMBER_OF_ARGUMENTS_MISMATCH,
});
}

debug_assert_eq!(messages.len(), signatures_len);
Expand Down

0 comments on commit 350eca3

Please sign in to comment.