mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 00:51:06 +00:00
Fix/div by zero (#5041)
* Handle gas_price being zero separately * Bump spec_version * Add a unit & integration tests for gas price = 0
This commit is contained in:
@@ -250,7 +250,11 @@ pub fn refund_unused_gas<T: Trait>(
|
||||
pub fn approx_gas_for_balance<Balance>(gas_price: Balance, balance: Balance) -> Gas
|
||||
where Balance: AtLeast32Bit
|
||||
{
|
||||
(balance / gas_price).saturated_into::<Gas>()
|
||||
if gas_price.is_zero() {
|
||||
Zero::zero()
|
||||
} else {
|
||||
(balance / gas_price).saturated_into::<Gas>()
|
||||
}
|
||||
}
|
||||
|
||||
/// A simple utility macro that helps to match against a
|
||||
@@ -294,7 +298,7 @@ macro_rules! match_tokens {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{GasMeter, Token};
|
||||
use crate::tests::Test;
|
||||
use crate::{tests::Test, gas::approx_gas_for_balance};
|
||||
|
||||
/// A trivial token that charges the specified number of gas units.
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
@@ -382,4 +386,22 @@ mod tests {
|
||||
let mut gas_meter = GasMeter::<Test>::with_limit(25, 10);
|
||||
assert!(!gas_meter.charge(&(), SimpleToken(25)).is_out_of_gas());
|
||||
}
|
||||
|
||||
// A unit test for `fn approx_gas_for_balance()`, and makes
|
||||
// sure setting gas_price 0 does not cause `div by zero` error.
|
||||
#[test]
|
||||
fn approx_gas_for_balance_works() {
|
||||
let tests = vec![
|
||||
(approx_gas_for_balance(0_u64, 123), 0),
|
||||
(approx_gas_for_balance(0_u64, 456), 0),
|
||||
(approx_gas_for_balance(1_u64, 123), 123),
|
||||
(approx_gas_for_balance(1_u64, 456), 456),
|
||||
(approx_gas_for_balance(100_u64, 900), 9),
|
||||
(approx_gas_for_balance(123_u64, 900), 7),
|
||||
];
|
||||
|
||||
for (lhs, rhs) in tests {
|
||||
assert_eq!(lhs, rhs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user