Skip to content

Commit 0936cea

Browse files
committed
- Refactored the safe_multiply_offer helper function to return the product of two numbers after checked multiplication. This helps ensure arithmetic safety by throwing an ArithmeticOverflow error when an overflow occurs. Adjusted the start_take_sell_order function to incorporate these changes. It now uses the return value from safe_multiply_offer in calculations which increases the safety and reliability of our operations.
- Made adjustments in the test case `replicate_overflow_for_start_take_sell_order` omitting the unused variable `offer` for cleaner and more efficient code. - Revised the `safe_add` function to return the sum of two numbers after checked addition. This helps ensure arithmetic safety by throwing an `ArithmeticOverflow` error when an overflow occurs. The `do_calculate_drawdown_total_amount` and `do_calculate_revenue_total_amount` functions have been updated to use the returned value from `safe_add` which increases arithmetic safety in these functions.
1 parent e8244e8 commit 0936cea

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

pallets/afloat/src/functions.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,10 @@ impl<T: Config> Pallet<T> {
474474
Error::<T>::NotEnoughTaxCreditsAvailable
475475
);
476476

477-
// check arithmatic overflow
478-
Self::safe_multiply_offer(offer.price_per_credit, tax_credit_amount)?;
479477
//ensure user has enough afloat balance
480478
ensure!(
481479
Self::do_get_afloat_balance(who.clone())? >=
482-
offer.price_per_credit * tax_credit_amount.into(),
480+
Self::safe_multiply_offer(offer.price_per_credit, tax_credit_amount.into())?,
483481
Error::<T>::NotEnoughAfloatBalanceAvailable
484482
);
485483
let zero_balance: T::Balance = Zero::zero();
@@ -488,7 +486,7 @@ impl<T: Config> Pallet<T> {
488486

489487
let creation_date: u64 = T::Timestamp::now().into();
490488
let price_per_credit: T::Balance = offer.price_per_credit.into();
491-
let total_price: T::Balance = price_per_credit * tax_credit_amount;
489+
let total_price: T::Balance = Self::safe_multiply_offer(price_per_credit, tax_credit_amount)?;
492490
let fee: Option<T::Balance> = None;
493491
let tax_credit_id: <T as pallet_uniques::Config>::ItemId = offer.tax_credit_id;
494492
let seller_id: T::AccountId = offer.creator_id;
@@ -941,9 +939,9 @@ impl<T: Config> Pallet<T> {
941939
fn safe_multiply_offer(
942940
factor1: T::Balance,
943941
factor2: T::Balance
944-
) -> DispatchResult {
945-
factor1.checked_mul(&factor2).ok_or_else(|| Error::<T>::ArithmeticOverflow)?;
946-
Ok(())
942+
) -> Result<T::Balance, DispatchError> {
943+
let result = factor1.checked_mul(&factor2).ok_or_else(|| Error::<T>::ArithmeticOverflow)?;
944+
Ok(result)
947945
}
948946

949947
fn get_all_roles_for_user(account_id: T::AccountId) -> Result<Vec<AfloatRole>, DispatchError> {

pallets/afloat/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fn replicate_overflow_for_start_take_sell_order() {
5959
}
6060
));
6161

62-
let (offer_id, offer) = AfloatOffers::<Test>::iter().next().unwrap().clone();
62+
let (offer_id, _offer) = AfloatOffers::<Test>::iter().next().unwrap().clone();
6363
assert_noop!(
6464
Afloat::start_take_sell_order(
6565
RawOrigin::Signed(other_user.clone()).into(),

pallets/fund-admin/src/functions.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3120,9 +3120,12 @@ impl<T: Config> Pallet<T> {
31203120
Ok(())
31213121
}
31223122

3123-
fn safe_add(a: u64, b: u64) -> DispatchResult {
3124-
a.checked_add(b).ok_or_else(|| Error::<T>::ArithmeticOverflow)?;
3125-
Ok(())
3123+
fn safe_add(
3124+
a: u64,
3125+
b: u64
3126+
) -> Result<u64, DispatchError> {
3127+
let result = a.checked_add(b).ok_or_else(|| Error::<T>::ArithmeticOverflow)?;
3128+
Ok(result)
31263129
}
31273130

31283131
fn do_calculate_drawdown_total_amount(
@@ -3145,11 +3148,9 @@ impl<T: Config> Pallet<T> {
31453148
// Get transaction data
31463149
let transaction_data = TransactionsInfo::<T>::get(transaction_id)
31473150
.ok_or(Error::<T>::TransactionNotFound)?;
3148-
// Check arithmetic overflow
3149-
Self::safe_add(drawdown_total_amount, transaction_data.amount)?;
31503151

31513152
// Add transaction amount to drawdown total amount
3152-
drawdown_total_amount += transaction_data.amount;
3153+
drawdown_total_amount = Self::safe_add(drawdown_total_amount, transaction_data.amount)?;
31533154
}
31543155
}
31553156

@@ -3300,11 +3301,8 @@ impl<T: Config> Pallet<T> {
33003301
RevenueTransactionsInfo::<T>::get(revenue_transaction_id)
33013302
.ok_or(Error::<T>::RevenueTransactionNotFound)?;
33023303

3303-
// Check arithmetic overflow
3304-
Self::safe_add(revenue_total_amount, revenue_transaction_data.amount)?;
3305-
33063304
// Add revenue transaction amount to revenue total amount
3307-
revenue_total_amount += revenue_transaction_data.amount;
3305+
revenue_total_amount = Self::safe_add(revenue_total_amount, revenue_transaction_data.amount)?;
33083306
}
33093307
}
33103308

0 commit comments

Comments
 (0)