Skip to content

Commit 6f44b2c

Browse files
authored
fix issues per review (#293)
* fix issues per review * update
1 parent b023edf commit 6f44b2c

File tree

7 files changed

+80
-96
lines changed

7 files changed

+80
-96
lines changed

authority/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! # Authority
22
//! A module to provide features for governance including dispatch method on
3-
//! behalf other accounts and schdule dispatchables.
3+
//! behalf of other accounts and schedule dispatchables.
44
//!
55
//! - [`Trait`](./trait.Trait.html)
66
//! - [`Call`](./enum.Call.html)
@@ -9,7 +9,7 @@
99
//! ## Overview
1010
//!
1111
//! Two functionalities are provided by this module:
12-
//! - schdule a dispatchable
12+
//! - schedule a dispatchable
1313
//! - dispatch method with on behalf of other origins
1414
1515
#![cfg_attr(not(feature = "std"), no_std)]
@@ -93,7 +93,7 @@ pub type Origin<T> = DelayedOrigin<<T as frame_system::Trait>::BlockNumber, <T a
9393

9494
/// Config for orml-authority
9595
pub trait AuthorityConfig<Origin, PalletsOrigin, BlockNumber> {
96-
/// Check if the `origin` is allow to schedule a dispatchable call with a
96+
/// Check if the `origin` is allowed to schedule a dispatchable call with a
9797
/// given `priority`.
9898
fn check_schedule_dispatch(origin: Origin, priority: Priority) -> DispatchResult;
9999
/// Check if the `origin` is allow to fast track a scheduled task that
@@ -214,7 +214,7 @@ decl_module! {
214214
Self::deposit_event(RawEvent::Dispatched(e.map(|_| ()).map_err(|e| e.error)));
215215
}
216216

217-
/// Schdule a dispatchable to be dispatched at later block.
217+
/// Schedule a dispatchable to be dispatched at later block.
218218
/// This is the only way to dispatch a call with `DelayedOrigin`.
219219
#[weight = T::WeightInfo::schedule_dispatch()]
220220
pub fn schedule_dispatch(
@@ -265,6 +265,7 @@ decl_module! {
265265
}
266266

267267
/// Fast track a scheduled dispatchable.
268+
/// WARN: This is not fully implemented.
268269
#[weight = T::WeightInfo::fast_track_scheduled_dispatch()]
269270
pub fn fast_track_scheduled_dispatch(
270271
origin,
@@ -294,6 +295,7 @@ decl_module! {
294295
}
295296

296297
/// Delay a scheduled dispatchable.
298+
/// WARN: This is not fully implemented.
297299
#[weight = T::WeightInfo::delay_scheduled_dispatch()]
298300
pub fn delay_scheduled_dispatch(
299301
origin,

currencies/src/lib.rs

Lines changed: 45 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
3939
#![cfg_attr(not(feature = "std"), no_std)]
4040

41-
use codec::{Codec, FullCodec};
41+
use codec::Codec;
4242
use frame_support::{
4343
decl_error, decl_event, decl_module, decl_storage,
4444
traits::{
@@ -49,7 +49,7 @@ use frame_support::{
4949
};
5050
use frame_system::{ensure_root, ensure_signed};
5151
use sp_runtime::{
52-
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize, StaticLookup, Zero},
52+
traits::{CheckedSub, MaybeSerializeDeserialize, StaticLookup, Zero},
5353
DispatchError, DispatchResult,
5454
};
5555
use sp_std::{
@@ -542,102 +542,85 @@ where
542542
pub type NativeCurrencyOf<T> = Currency<T, <T as Trait>::GetNativeCurrencyId>;
543543

544544
/// Adapt other currency traits implementation to `BasicCurrency`.
545-
pub struct BasicCurrencyAdapter<Currency, Balance, BalanceConvert, Amount, Moment>(
546-
marker::PhantomData<(Currency, Balance, BalanceConvert, Amount, Moment)>,
547-
);
545+
pub struct BasicCurrencyAdapter<T, Currency, Amount, Moment>(marker::PhantomData<(T, Currency, Amount, Moment)>);
548546

549547
type PalletBalanceOf<A, Currency> = <Currency as PalletCurrency<A>>::Balance;
550548

551549
// Adapt `frame_support::traits::Currency`
552-
impl<AccountId, Currency, Balance, BalanceConvert, Amount, Moment> BasicCurrency<AccountId>
553-
for BasicCurrencyAdapter<Currency, Balance, BalanceConvert, Amount, Moment>
550+
impl<T, AccountId, Currency, Amount, Moment> BasicCurrency<AccountId>
551+
for BasicCurrencyAdapter<T, Currency, Amount, Moment>
554552
where
555553
Currency: PalletCurrency<AccountId>,
556-
Balance: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
557-
BalanceConvert: From<PalletBalanceOf<AccountId, Currency>>
558-
+ Into<PalletBalanceOf<AccountId, Currency>>
559-
+ From<Balance>
560-
+ Into<Balance>,
554+
T: Trait,
561555
{
562-
type Balance = Balance;
556+
type Balance = PalletBalanceOf<AccountId, Currency>;
563557

564558
fn total_issuance() -> Self::Balance {
565-
BalanceConvert::from(Currency::total_issuance()).into()
559+
Currency::total_issuance()
566560
}
567561

568562
fn total_balance(who: &AccountId) -> Self::Balance {
569-
BalanceConvert::from(Currency::total_balance(who)).into()
563+
Currency::total_balance(who)
570564
}
571565

572566
fn free_balance(who: &AccountId) -> Self::Balance {
573-
BalanceConvert::from(Currency::free_balance(who)).into()
567+
Currency::free_balance(who)
574568
}
575569

576570
fn ensure_can_withdraw(who: &AccountId, amount: Self::Balance) -> DispatchResult {
577-
let new_balance_pallet = {
578-
let new_balance = Self::free_balance(who)
579-
.checked_sub(&amount)
580-
.ok_or("InsufficientBalance")?;
581-
BalanceConvert::from(new_balance).into()
582-
};
583-
let amount_pallet = BalanceConvert::from(amount).into();
584-
Currency::ensure_can_withdraw(who, amount_pallet, WithdrawReasons::all(), new_balance_pallet)
571+
let new_balance = Self::free_balance(who)
572+
.checked_sub(&amount)
573+
.ok_or(Error::<T>::BalanceTooLow)?;
574+
575+
Currency::ensure_can_withdraw(who, amount, WithdrawReasons::all(), new_balance)
585576
}
586577

587578
fn transfer(from: &AccountId, to: &AccountId, amount: Self::Balance) -> DispatchResult {
588-
let amount_pallet = BalanceConvert::from(amount).into();
589-
Currency::transfer(from, to, amount_pallet, ExistenceRequirement::AllowDeath)
579+
Currency::transfer(from, to, amount, ExistenceRequirement::AllowDeath)
590580
}
591581

592582
fn deposit(who: &AccountId, amount: Self::Balance) -> DispatchResult {
593-
let _ = Currency::deposit_creating(who, BalanceConvert::from(amount).into());
583+
let _ = Currency::deposit_creating(who, amount);
594584
Ok(())
595585
}
596586

597587
fn withdraw(who: &AccountId, amount: Self::Balance) -> DispatchResult {
598-
Currency::withdraw(
599-
who,
600-
BalanceConvert::from(amount).into(),
601-
WithdrawReasons::all(),
602-
ExistenceRequirement::AllowDeath,
603-
)
604-
.map(|_| ())
588+
Currency::withdraw(who, amount, WithdrawReasons::all(), ExistenceRequirement::AllowDeath).map(|_| ())
605589
}
606590

607591
fn can_slash(who: &AccountId, amount: Self::Balance) -> bool {
608-
Currency::can_slash(who, BalanceConvert::from(amount).into())
592+
Currency::can_slash(who, amount)
609593
}
610594

611595
fn slash(who: &AccountId, amount: Self::Balance) -> Self::Balance {
612-
let (_, gap) = Currency::slash(who, BalanceConvert::from(amount).into());
613-
BalanceConvert::from(gap).into()
596+
let (_, gap) = Currency::slash(who, amount);
597+
gap
614598
}
615599
}
616600

617601
// Adapt `frame_support::traits::Currency`
618-
impl<AccountId, Currency, Balance, BalanceConvert, Amount, Moment> BasicCurrencyExtended<AccountId>
619-
for BasicCurrencyAdapter<Currency, Balance, BalanceConvert, Amount, Moment>
602+
impl<T, AccountId, Currency, Amount, Moment> BasicCurrencyExtended<AccountId>
603+
for BasicCurrencyAdapter<T, Currency, Amount, Moment>
620604
where
621-
Balance: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
622605
Amount: Signed
623-
+ TryInto<Balance>
624-
+ TryFrom<Balance>
606+
+ TryInto<PalletBalanceOf<AccountId, Currency>>
607+
+ TryFrom<PalletBalanceOf<AccountId, Currency>>
625608
+ SimpleArithmetic
626609
+ Codec
627610
+ Copy
628611
+ MaybeSerializeDeserialize
629612
+ Debug
630613
+ Default,
631614
Currency: PalletCurrency<AccountId>,
632-
BalanceConvert: From<PalletBalanceOf<AccountId, Currency>>
633-
+ Into<PalletBalanceOf<AccountId, Currency>>
634-
+ From<Balance>
635-
+ Into<Balance>,
615+
T: Trait,
636616
{
637617
type Amount = Amount;
638618

639619
fn update_balance(who: &AccountId, by_amount: Self::Amount) -> DispatchResult {
640-
let by_balance = by_amount.abs().try_into().map_err(|_| "AmountIntoBalanceFailed")?;
620+
let by_balance = by_amount
621+
.abs()
622+
.try_into()
623+
.map_err(|_| Error::<T>::AmountIntoBalanceFailed)?;
641624
if by_amount.is_positive() {
642625
Self::deposit(who, by_balance)
643626
} else {
@@ -647,34 +630,20 @@ where
647630
}
648631

649632
// Adapt `frame_support::traits::LockableCurrency`
650-
impl<AccountId, Currency, Balance, BalanceConvert, Amount, Moment> BasicLockableCurrency<AccountId>
651-
for BasicCurrencyAdapter<Currency, Balance, BalanceConvert, Amount, Moment>
633+
impl<T, AccountId, Currency, Amount, Moment> BasicLockableCurrency<AccountId>
634+
for BasicCurrencyAdapter<T, Currency, Amount, Moment>
652635
where
653-
Balance: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
654636
Currency: PalletLockableCurrency<AccountId>,
655-
BalanceConvert: From<PalletBalanceOf<AccountId, Currency>>
656-
+ Into<PalletBalanceOf<AccountId, Currency>>
657-
+ From<Balance>
658-
+ Into<Balance>,
637+
T: Trait,
659638
{
660639
type Moment = Moment;
661640

662641
fn set_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
663-
Currency::set_lock(
664-
lock_id,
665-
who,
666-
BalanceConvert::from(amount).into(),
667-
WithdrawReasons::all(),
668-
);
642+
Currency::set_lock(lock_id, who, amount, WithdrawReasons::all());
669643
}
670644

671645
fn extend_lock(lock_id: LockIdentifier, who: &AccountId, amount: Self::Balance) {
672-
Currency::extend_lock(
673-
lock_id,
674-
who,
675-
BalanceConvert::from(amount).into(),
676-
WithdrawReasons::all(),
677-
);
646+
Currency::extend_lock(lock_id, who, amount, WithdrawReasons::all());
678647
}
679648

680649
fn remove_lock(lock_id: LockIdentifier, who: &AccountId) {
@@ -683,35 +652,31 @@ where
683652
}
684653

685654
// Adapt `frame_support::traits::ReservableCurrency`
686-
impl<AccountId, Currency, Balance, BalanceConvert, Amount, Moment> BasicReservableCurrency<AccountId>
687-
for BasicCurrencyAdapter<Currency, Balance, BalanceConvert, Amount, Moment>
655+
impl<T, AccountId, Currency, Amount, Moment> BasicReservableCurrency<AccountId>
656+
for BasicCurrencyAdapter<T, Currency, Amount, Moment>
688657
where
689-
Balance: AtLeast32BitUnsigned + FullCodec + Copy + MaybeSerializeDeserialize + Debug + Default,
690658
Currency: PalletReservableCurrency<AccountId>,
691-
BalanceConvert: From<PalletBalanceOf<AccountId, Currency>>
692-
+ Into<PalletBalanceOf<AccountId, Currency>>
693-
+ From<Balance>
694-
+ Into<Balance>,
659+
T: Trait,
695660
{
696661
fn can_reserve(who: &AccountId, value: Self::Balance) -> bool {
697-
Currency::can_reserve(who, BalanceConvert::from(value).into())
662+
Currency::can_reserve(who, value)
698663
}
699664

700665
fn slash_reserved(who: &AccountId, value: Self::Balance) -> Self::Balance {
701-
let (_, gap) = Currency::slash_reserved(who, BalanceConvert::from(value).into());
702-
BalanceConvert::from(gap).into()
666+
let (_, gap) = Currency::slash_reserved(who, value);
667+
gap
703668
}
704669

705670
fn reserved_balance(who: &AccountId) -> Self::Balance {
706-
BalanceConvert::from(Currency::reserved_balance(who)).into()
671+
Currency::reserved_balance(who)
707672
}
708673

709674
fn reserve(who: &AccountId, value: Self::Balance) -> DispatchResult {
710-
Currency::reserve(who, BalanceConvert::from(value).into())
675+
Currency::reserve(who, value)
711676
}
712677

713678
fn unreserve(who: &AccountId, value: Self::Balance) -> Self::Balance {
714-
BalanceConvert::from(Currency::unreserve(who, BalanceConvert::from(value).into())).into()
679+
Currency::unreserve(who, value)
715680
}
716681

717682
fn repatriate_reserved(
@@ -720,7 +685,6 @@ where
720685
value: Self::Balance,
721686
status: BalanceStatus,
722687
) -> result::Result<Self::Balance, DispatchError> {
723-
Currency::repatriate_reserved(slashed, beneficiary, BalanceConvert::from(value).into(), status)
724-
.map(|a| BalanceConvert::from(a).into())
688+
Currency::repatriate_reserved(slashed, beneficiary, value, status)
725689
}
726690
}

currencies/src/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl Trait for Runtime {
113113
}
114114
pub type Currencies = Module<Runtime>;
115115
pub type NativeCurrency = NativeCurrencyOf<Runtime>;
116-
pub type AdaptedBasicCurrency = BasicCurrencyAdapter<PalletBalances, Balance, Balance, i64, u64>;
116+
pub type AdaptedBasicCurrency = BasicCurrencyAdapter<Runtime, PalletBalances, i64, u64>;
117117

118118
pub const ALICE: AccountId = 1;
119119
pub const BOB: AccountId = 2;

tokens/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
380380
let to_balance = Self::free_balance(currency_id, to)
381381
.checked_add(&amount)
382382
.ok_or(Error::<T>::BalanceOverflow)?;
383+
// Cannot underflow because ensure_can_withdraw check
383384
Self::set_free_balance(currency_id, from, from_balance - amount);
384385
Self::set_free_balance(currency_id, to, to_balance);
385386
T::OnReceived::on_received(to, currency_id, amount);
@@ -411,6 +412,7 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
411412
}
412413
Self::ensure_can_withdraw(currency_id, who, amount)?;
413414

415+
// Cannot underflow because ensure_can_withdraw check
414416
<TotalIssuance<T>>::mutate(currency_id, |v| *v -= amount);
415417
Self::set_free_balance(currency_id, who, Self::free_balance(currency_id, who) - amount);
416418

@@ -439,20 +441,26 @@ impl<T: Trait> MultiCurrency<T::AccountId> for Module<T> {
439441

440442
let account = Self::accounts(who, currency_id);
441443
let free_slashed_amount = account.free.min(amount);
444+
// Cannot underflow becuase free_slashed_amount can never be greater than amount
442445
let mut remaining_slash = amount - free_slashed_amount;
443446

444447
// slash free balance
445448
if !free_slashed_amount.is_zero() {
449+
// Cannot underflow becuase free_slashed_amount can never be greater than
450+
// account.free
446451
Self::set_free_balance(currency_id, who, account.free - free_slashed_amount);
447452
}
448453

449454
// slash reserved balance
450455
if !remaining_slash.is_zero() {
451456
let reserved_slashed_amount = account.reserved.min(remaining_slash);
457+
// Cannot underflow due to above line
452458
remaining_slash -= reserved_slashed_amount;
453459
Self::set_reserved_balance(currency_id, who, account.reserved - reserved_slashed_amount);
454460
}
455461

462+
// Cannot underflow because the slashed value cannot be greater than total
463+
// issuance
456464
<TotalIssuance<T>>::mutate(currency_id, |v| *v -= amount - remaining_slash);
457465
remaining_slash
458466
}
@@ -577,6 +585,8 @@ impl<T: Trait> MultiReservableCurrency<T::AccountId> for Module<T> {
577585

578586
let account = Self::accounts(who, currency_id);
579587
Self::set_free_balance(currency_id, who, account.free - value);
588+
// Cannot overflow becuase total issuance is using the same balance type and
589+
// this doesn't increase total issuance
580590
Self::set_reserved_balance(currency_id, who, account.reserved + value);
581591
Ok(())
582592
}

utilities/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn with_transaction_result<R>(f: impl FnOnce() -> Result<R, DispatchError>)
3333
#[cfg(test)]
3434
mod tests {
3535
use super::*;
36-
use frame_support::{assert_err, assert_ok, decl_module, decl_storage};
36+
use frame_support::{assert_noop, assert_ok, decl_module, decl_storage};
3737
use sp_io::TestExternalities;
3838
use sp_runtime::{DispatchError, DispatchResult};
3939

@@ -75,7 +75,7 @@ mod tests {
7575
assert_eq!(Value::get(), 0);
7676
assert_eq!(Map::get("val0"), 0);
7777

78-
assert_err!(
78+
assert_noop!(
7979
with_transaction_result(|| -> DispatchResult {
8080
Value::set(99);
8181
Map::insert("val0", 99);

0 commit comments

Comments
 (0)