Skip to content

Commit 69418f7

Browse files
committed
- Implemented a safe multiply function safe_multiply_offer in the Afloat pallet. This function checks for possible arithmetic overflow when multiplying two balances - a critical action in the process of starting a sell order. Ensuring the safety of this operation will increase the reliability and robustness of our pallet.
- Added the `ArithmeticOverflow` error to the Afloat pallet error definitions. This error is used within the implementation of the safe multiplication function to catch the scenario of multiplication operations exceeding the maximum balance limit. - Updated the `replicate_overflow_for_start_take_sell_order` test case in the Afloat pallet to confirm the correct operation of the `safe_multiply_offer` function and the proper detection of arithmetic overflows. It ensures that when a multiplication operation would result in an overflow, the `ArithmeticOverflow` error is correctly returned.
1 parent da3ce92 commit 69418f7

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

pallets/afloat/src/functions.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use frame_support::pallet_prelude::*;
44
use frame_system::{pallet_prelude::*, RawOrigin};
55
use pallet_fruniques::types::{Attributes, CollectionDescription, FruniqueRole, ParentInfo};
66
use pallet_gated_marketplace::types::{Marketplace, MarketplaceRole};
7-
use sp_runtime::{traits::StaticLookup, Permill};
87
// use frame_support::traits::OriginTrait;
98
use core::convert::TryInto;
109
use frame_support::traits::Time;
1110
use pallet_rbac::types::{IdOrVec, RoleBasedAccessControl, RoleId};
1211
use scale_info::prelude::vec;
1312
use sp_io::hashing::blake2_256;
1413
use sp_runtime::{
14+
Permill,
1515
sp_std::{str, vec::Vec},
16-
traits::Zero,
16+
traits::{Zero, StaticLookup, CheckedMul},
1717
};
1818

1919
impl<T: Config> Pallet<T> {
@@ -473,6 +473,9 @@ impl<T: Config> Pallet<T> {
473473
offer.tax_credit_amount_remaining >= tax_credit_amount,
474474
Error::<T>::NotEnoughTaxCreditsAvailable
475475
);
476+
477+
// check arithmatic overflow
478+
Self::safe_multiply_offer(offer.price_per_credit, tax_credit_amount)?;
476479
//ensure user has enough afloat balance
477480
ensure!(
478481
Self::do_get_afloat_balance(who.clone())? >=
@@ -935,6 +938,14 @@ impl<T: Config> Pallet<T> {
935938
})
936939
}
937940

941+
fn safe_multiply_offer(
942+
factor1: T::Balance,
943+
factor2: T::Balance
944+
) -> DispatchResult {
945+
factor1.checked_mul(&factor2).ok_or_else(|| Error::<T>::ArithmeticOverflow)?;
946+
Ok(())
947+
}
948+
938949
fn get_all_roles_for_user(account_id: T::AccountId) -> Result<Vec<AfloatRole>, DispatchError> {
939950
let roles_storage = <T as pallet::Config>::Rbac::get_roles_by_user(
940951
account_id.clone(),

pallets/afloat/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ pub mod pallet {
146146
ChildOfferIdNotFound,
147147
// Tax credit amount underflow
148148
Underflow,
149+
// Arithmetic multiplication overflow
150+
ArithmeticOverflow,
149151
// Afloat marketplace label too long
150152
LabelTooLong,
151153
// Afloat asset has not been set

pallets/afloat/src/tests.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ fn replicate_overflow_for_start_take_sell_order() {
6060
));
6161

6262
let (offer_id, offer) = AfloatOffers::<Test>::iter().next().unwrap().clone();
63-
assert_ok!(Afloat::start_take_sell_order(
64-
RawOrigin::Signed(other_user.clone()).into(),
65-
offer_id,
66-
10
67-
));
63+
assert_noop!(
64+
Afloat::start_take_sell_order(
65+
RawOrigin::Signed(other_user.clone()).into(),
66+
offer_id,
67+
10
68+
),
69+
Error::<Test>::ArithmeticOverflow
70+
);
6871
});
6972
}
7073

0 commit comments

Comments
 (0)