Skip to content

Commit

Permalink
Remove unchecked integer arithmetic (#719)
Browse files Browse the repository at this point in the history
* Remove remaining unchecked integer arithmetic

* Reformat

* Remove `let_unit` pattern
  • Loading branch information
maltekliemann authored Jul 27, 2022
1 parent 8faf2be commit b00d69f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
12 changes: 8 additions & 4 deletions zrml/court/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,10 @@ mod pallet {

let slash_and_remove_juror = |ai: &T::AccountId| {
let all_reserved = CurrencyOf::<T>::reserved_balance_named(&RESERVE_ID, ai);
// Division will never overflow
let slash = all_reserved / BalanceOf::<T>::from(TARDY_PUNISHMENT_DIVISOR);
// Unsigned division will never overflow
let slash = all_reserved
.checked_div(&BalanceOf::<T>::from(TARDY_PUNISHMENT_DIVISOR))
.ok_or(DispatchError::Other("Zero division"))?;
let _ = CurrencyOf::<T>::repatriate_reserved_named(
&RESERVE_ID,
ai,
Expand Down Expand Up @@ -373,8 +375,10 @@ mod pallet {
total_winners = total_winners.saturating_add(BalanceOf::<T>::from(1u8));
} else {
let all_reserved = CurrencyOf::<T>::reserved_balance_named(&RESERVE_ID, jai);
// Division will never overflow
let slash = all_reserved / BalanceOf::<T>::from(2u8);
// Unsigned division will never overflow
let slash = all_reserved
.checked_div(&BalanceOf::<T>::from(2u8))
.ok_or(DispatchError::Other("Zero division"))?;
CurrencyOf::<T>::slash_reserved_named(&RESERVE_ID, jai, slash);
total_incentives = total_incentives.saturating_add(slash);
}
Expand Down
10 changes: 5 additions & 5 deletions zrml/prediction-markets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mod pallet {

let status: MarketStatus = match creation {
MarketCreation::Permissionless => {
let required_bond = T::ValidityBond::get() + T::OracleBond::get();
let required_bond = T::ValidityBond::get().saturating_add(T::OracleBond::get());
T::AssetManager::reserve_named(
&RESERVE_ID,
Asset::Ztg,
Expand All @@ -535,7 +535,7 @@ mod pallet {
}
}
MarketCreation::Advised => {
let required_bond = T::AdvisoryBond::get() + T::OracleBond::get();
let required_bond = T::AdvisoryBond::get().saturating_add(T::OracleBond::get());
T::AssetManager::reserve_named(
&RESERVE_ID,
Asset::Ztg,
Expand Down Expand Up @@ -767,7 +767,7 @@ mod pallet {
// ever not true then we have an accounting problem.
ensure!(
T::AssetManager::free_balance(Asset::Ztg, &market_account)
>= long_payout + short_payout,
>= long_payout.saturating_add(short_payout),
Error::<T>::InsufficientFundsInMarketAccount,
);

Expand Down Expand Up @@ -1917,8 +1917,8 @@ mod pallet {

// Unreserve funds reserved during market creation
if m.creation == MarketCreation::Permissionless {
let required_bond =
T::ValidityBond::get() + T::OracleBond::get();
let required_bond = T::ValidityBond::get()
.saturating_add(T::OracleBond::get());
T::AssetManager::unreserve_named(
&RESERVE_ID,
Asset::Ztg,
Expand Down
12 changes: 6 additions & 6 deletions zrml/rikiddo/src/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn default_prepare_calculation() -> (u8, f64, Vec<f64>, Vec<<Runtime as Config>:
// Adds volume for Timestamp 0 and 2 and returns the new sigmoid fee
fn default_fill_market_volume() -> f64 {
let frac_dec_places = <Runtime as Config>::BalanceFractionalDecimals::get();
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
assert_ok!(Rikiddo::update_volume(0, 1000));
run_to_block(1);
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
assert_eq!(Rikiddo::update_volume(0, 1000).unwrap(), Some(10u128.pow(frac_dec_places as u32)));

let fee = Rikiddo::fee(0).unwrap();
Expand Down Expand Up @@ -107,11 +107,11 @@ fn rikiddo_pallet_update_market_data_returns_correct_result() {
Rikiddo::update_volume(0, 1000),
crate::Error::<Runtime>::RikiddoNotFoundForPool
);
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
assert_ok!(Rikiddo::create(0, rikiddo));
assert_ok!(Rikiddo::update_volume(0, 1000));
run_to_block(1);
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
assert_eq!(
Rikiddo::update_volume(0, 1000).unwrap(),
Some(10u128.pow(<Runtime as Config>::BalanceFractionalDecimals::get() as u32))
Expand All @@ -131,7 +131,7 @@ fn rikiddo_pallet_fee_return_correct_result() {
rikiddo.ma_short.config.ema_period = Timespan::Seconds(1);
rikiddo.ma_long.config.ema_period = Timespan::Seconds(1);
assert_noop!(Rikiddo::fee(0), crate::Error::<Runtime>::RikiddoNotFoundForPool);
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 0).unwrap();
assert_ok!(Rikiddo::create(0, rikiddo));
let fee_reference_balance: Balance =
FixedS::from_num(initial_fee).to_fixed_decimal(frac_dec_places).unwrap();
Expand All @@ -152,7 +152,7 @@ fn rikiddo_pallet_fee_return_correct_result() {
// Now we check if the fee has changed, since enough volume data was collected
assert_ok!(Rikiddo::update_volume(0, 1000));
run_to_block(1);
let _ = <Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
<Runtime as Config>::Timestamp::set(RawOrigin::None.into(), 2).unwrap();
assert_ok!(Rikiddo::update_volume(0, 1000));
assert_ne!(Rikiddo::fee(0).unwrap(), fee_pallet_balance);
});
Expand Down
7 changes: 4 additions & 3 deletions zrml/swaps/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ mod pallet {
&who,
|user_subsidy| {
if let Some(prev_val) = user_subsidy {
*prev_val += amount;
*prev_val = prev_val.saturating_add(amount);
} else {
// If the account adds subsidy for the first time, ensure that it's
// larger than the minimum amount.
Expand All @@ -446,7 +446,7 @@ mod pallet {
*user_subsidy = Some(amount);
}

pool.total_subsidy = Some(total_subsidy + amount);
pool.total_subsidy = Some(total_subsidy.saturating_add(amount));
Ok(())
},
)?;
Expand Down Expand Up @@ -2057,7 +2057,8 @@ mod pallet {
outstanding_before.push(total_amount);

if *asset == asset_out {
outstanding_after.push(total_amount + asset_amount_out);
outstanding_after
.push(total_amount.saturating_add(asset_amount_out));
} else {
outstanding_after.push(total_amount);
}
Expand Down

0 comments on commit b00d69f

Please sign in to comment.