Skip to content

Commit 4921569

Browse files
authored
Replace custom arithmetic error. (#495)
1 parent cda5f64 commit 4921569

File tree

8 files changed

+24
-36
lines changed

8 files changed

+24
-36
lines changed

authority/src/lib.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use frame_support::{
3131
use frame_system::pallet_prelude::*;
3232
use sp_runtime::{
3333
traits::{CheckedSub, Dispatchable, Saturating},
34-
DispatchError, DispatchResult, RuntimeDebug,
34+
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
3535
};
3636
use sp_std::prelude::*;
3737

@@ -163,8 +163,6 @@ pub mod module {
163163

164164
#[pallet::error]
165165
pub enum Error<T> {
166-
/// Calculation overflow.
167-
Overflow,
168166
/// Failed to schedule a task.
169167
FailedToSchedule,
170168
/// Failed to cancel a task.
@@ -234,12 +232,12 @@ pub mod module {
234232

235233
let id = NextTaskIndex::<T>::mutate(|id| -> sp_std::result::Result<ScheduleTaskIndex, DispatchError> {
236234
let current_id = *id;
237-
*id = id.checked_add(1).ok_or(Error::<T>::Overflow)?;
235+
*id = id.checked_add(1).ok_or(ArithmeticError::Overflow)?;
238236
Ok(current_id)
239237
})?;
240238
let now = frame_system::Pallet::<T>::block_number();
241239
let delay = match when {
242-
DispatchTime::At(x) => x.checked_sub(&now).ok_or(Error::<T>::Overflow)?,
240+
DispatchTime::At(x) => x.checked_sub(&now).ok_or(ArithmeticError::Overflow)?,
243241
DispatchTime::After(x) => x,
244242
};
245243
let schedule_origin = if with_delayed_origin {
@@ -278,7 +276,7 @@ pub mod module {
278276
) -> DispatchResultWithPostInfo {
279277
let now = frame_system::Pallet::<T>::block_number();
280278
let new_delay = match when {
281-
DispatchTime::At(x) => x.checked_sub(&now).ok_or(Error::<T>::Overflow)?,
279+
DispatchTime::At(x) => x.checked_sub(&now).ok_or(ArithmeticError::Overflow)?,
282280
DispatchTime::After(x) => x,
283281
};
284282
let dispatch_at = match when {

authority/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ fn schedule_dispatch_after_work() {
123123
run_to_block(1);
124124
assert_eq!(
125125
Authority::schedule_dispatch(Origin::root(), DispatchTime::At(0), 0, true, Box::new(call.clone())),
126-
Err(Error::<Runtime>::Overflow.into())
126+
Err(ArithmeticError::Overflow.into())
127127
);
128128

129129
assert_ok!(Authority::schedule_dispatch(

nft/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use codec::{Decode, Encode};
2525
use frame_support::{ensure, pallet_prelude::*, Parameter};
2626
use sp_runtime::{
2727
traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member, One, Zero},
28-
DispatchError, DispatchResult, RuntimeDebug,
28+
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
2929
};
3030
use sp_std::vec::Vec;
3131

@@ -103,8 +103,6 @@ pub mod module {
103103
ClassNotFound,
104104
/// The operator is not the owner of the token and has no permission
105105
NoPermission,
106-
/// Arithmetic calculation overflow
107-
NumOverflow,
108106
/// Can not destroy class
109107
/// Total issuance is not 0
110108
CannotDestroyClass,
@@ -236,7 +234,7 @@ impl<T: Config> Pallet<T> {
236234
info.total_issuance = info
237235
.total_issuance
238236
.checked_add(&One::one())
239-
.ok_or(Error::<T>::NumOverflow)?;
237+
.ok_or(ArithmeticError::Overflow)?;
240238
Ok(())
241239
})?;
242240

@@ -263,7 +261,7 @@ impl<T: Config> Pallet<T> {
263261
info.total_issuance = info
264262
.total_issuance
265263
.checked_sub(&One::one())
266-
.ok_or(Error::<T>::NumOverflow)?;
264+
.ok_or(ArithmeticError::Overflow)?;
267265
Ok(())
268266
})?;
269267

nft/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn mint_should_fail() {
5555
});
5656
assert_noop!(
5757
NonFungibleTokenModule::mint(&BOB, CLASS_ID, vec![1], ()),
58-
Error::<Runtime>::NumOverflow
58+
ArithmeticError::Overflow,
5959
);
6060

6161
NextTokenId::<Runtime>::mutate(CLASS_ID, |id| *id = <Runtime as Config>::TokenId::max_value());
@@ -136,7 +136,7 @@ fn burn_should_fail() {
136136
});
137137
assert_noop!(
138138
NonFungibleTokenModule::burn(&BOB, (CLASS_ID, TOKEN_ID)),
139-
Error::<Runtime>::NumOverflow
139+
ArithmeticError::Overflow,
140140
);
141141
});
142142
}

tokens/src/lib.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use sp_runtime::{
6161
AccountIdConversion, AtLeast32BitUnsigned, Bounded, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, Member,
6262
Saturating, StaticLookup, Zero,
6363
},
64-
DispatchError, DispatchResult, RuntimeDebug,
64+
ArithmeticError, DispatchError, DispatchResult, RuntimeDebug,
6565
};
6666
use sp_std::{
6767
convert::{Infallible, TryFrom, TryInto},
@@ -189,10 +189,6 @@ pub mod module {
189189
pub enum Error<T> {
190190
/// The balance is too low
191191
BalanceTooLow,
192-
/// This operation will cause balance to overflow
193-
BalanceOverflow,
194-
/// This operation will cause total issuance to overflow
195-
TotalIssuanceOverflow,
196192
/// Cannot convert Amount into Balance type
197193
AmountIntoBalanceFailed,
198194
/// Failed because liquidity restrictions due to locking
@@ -528,7 +524,7 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
528524
let from_balance = Self::free_balance(currency_id, from);
529525
let to_balance = Self::free_balance(currency_id, to)
530526
.checked_add(&amount)
531-
.ok_or(Error::<T>::BalanceOverflow)?;
527+
.ok_or(ArithmeticError::Overflow)?;
532528
// Cannot underflow because ensure_can_withdraw check
533529
Self::set_free_balance(currency_id, from, from_balance - amount);
534530
Self::set_free_balance(currency_id, to, to_balance);
@@ -545,9 +541,7 @@ impl<T: Config> MultiCurrency<T::AccountId> for Pallet<T> {
545541
}
546542

547543
TotalIssuance::<T>::try_mutate(currency_id, |total_issuance| -> DispatchResult {
548-
*total_issuance = total_issuance
549-
.checked_add(&amount)
550-
.ok_or(Error::<T>::TotalIssuanceOverflow)?;
544+
*total_issuance = total_issuance.checked_add(&amount).ok_or(ArithmeticError::Overflow)?;
551545

552546
Self::set_free_balance(currency_id, who, Self::free_balance(currency_id, who) + amount);
553547

@@ -932,7 +926,7 @@ where
932926
let currency_id = GetCurrencyId::get();
933927
let new_total = Pallet::<T>::free_balance(currency_id, who)
934928
.checked_add(&value)
935-
.ok_or(Error::<T>::TotalIssuanceOverflow)?;
929+
.ok_or(ArithmeticError::Overflow)?;
936930
Pallet::<T>::set_free_balance(currency_id, who, new_total);
937931

938932
Ok(Self::PositiveImbalance::new(value))

tokens/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ fn deposit_should_work() {
345345

346346
assert_noop!(
347347
Tokens::deposit(DOT, &ALICE, Balance::max_value()),
348-
Error::<Runtime>::TotalIssuanceOverflow,
348+
ArithmeticError::Overflow,
349349
);
350350
});
351351
}
@@ -961,7 +961,7 @@ fn currency_adapter_transferring_too_high_value_should_not_panic() {
961961
u64::max_value(),
962962
ExistenceRequirement::AllowDeath
963963
),
964-
Error::<Runtime>::BalanceOverflow,
964+
ArithmeticError::Overflow,
965965
);
966966

967967
assert_eq!(

vesting/src/lib.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use frame_support::{
3737
use frame_system::{ensure_root, ensure_signed, pallet_prelude::*};
3838
use sp_runtime::{
3939
traits::{AtLeast32Bit, CheckedAdd, Saturating, StaticLookup, Zero},
40-
DispatchResult, RuntimeDebug,
40+
ArithmeticError, DispatchResult, RuntimeDebug,
4141
};
4242
use sp_std::{
4343
cmp::{Eq, PartialEq},
@@ -144,8 +144,6 @@ pub mod module {
144144
ZeroVestingPeriod,
145145
/// Number of vests is zero
146146
ZeroVestingPeriodCount,
147-
/// Arithmetic calculation overflow
148-
NumOverflow,
149147
/// Insufficient amount of balance to lock
150148
InsufficientBalanceToLock,
151149
/// This account have too many vesting schedules
@@ -303,7 +301,7 @@ impl<T: Config> Pallet<T> {
303301

304302
let total_amount = Self::locked_balance(to)
305303
.checked_add(&schedule_amount)
306-
.ok_or(Error::<T>::NumOverflow)?;
304+
.ok_or(ArithmeticError::Overflow)?;
307305

308306
T::Currency::transfer(from, to, schedule_amount, ExistenceRequirement::AllowDeath)?;
309307
T::Currency::set_lock(VESTING_LOCK_ID, to, total_amount, WithdrawReasons::all());
@@ -312,7 +310,7 @@ impl<T: Config> Pallet<T> {
312310
}
313311

314312
fn do_update_vesting_schedules(who: &T::AccountId, schedules: Vec<VestingScheduleOf<T>>) -> DispatchResult {
315-
let total_amount = schedules.iter().try_fold::<_, _, Result<BalanceOf<T>, Error<T>>>(
313+
let total_amount = schedules.iter().try_fold::<_, _, Result<BalanceOf<T>, DispatchError>>(
316314
Zero::zero(),
317315
|acc_amount, schedule| {
318316
let amount = Self::ensure_valid_vesting_schedule(schedule)?;
@@ -331,12 +329,12 @@ impl<T: Config> Pallet<T> {
331329
}
332330

333331
/// Returns `Ok(amount)` if valid schedule, or error.
334-
fn ensure_valid_vesting_schedule(schedule: &VestingScheduleOf<T>) -> Result<BalanceOf<T>, Error<T>> {
332+
fn ensure_valid_vesting_schedule(schedule: &VestingScheduleOf<T>) -> Result<BalanceOf<T>, DispatchError> {
335333
ensure!(!schedule.period.is_zero(), Error::<T>::ZeroVestingPeriod);
336334
ensure!(!schedule.period_count.is_zero(), Error::<T>::ZeroVestingPeriodCount);
337-
ensure!(schedule.end().is_some(), Error::<T>::NumOverflow);
335+
ensure!(schedule.end().is_some(), ArithmeticError::Overflow);
338336

339-
let total = schedule.total_amount().ok_or(Error::<T>::NumOverflow)?;
337+
let total = schedule.total_amount().ok_or(ArithmeticError::Overflow)?;
340338

341339
ensure!(total >= T::MinVestedTransfer::get(), Error::<T>::AmountLow);
342340

vesting/src/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn vested_transfer_fails_if_overflow() {
172172
};
173173
assert_noop!(
174174
Vesting::vested_transfer(Origin::signed(ALICE), BOB, schedule),
175-
Error::<Runtime>::NumOverflow
175+
ArithmeticError::Overflow,
176176
);
177177

178178
let another_schedule = VestingSchedule {
@@ -183,7 +183,7 @@ fn vested_transfer_fails_if_overflow() {
183183
};
184184
assert_noop!(
185185
Vesting::vested_transfer(Origin::signed(ALICE), BOB, another_schedule),
186-
Error::<Runtime>::NumOverflow
186+
ArithmeticError::Overflow,
187187
);
188188
});
189189
}

0 commit comments

Comments
 (0)