Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted Vec storage to BoundedVec #487

Merged
merged 7 commits into from
May 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ parameter_type_with_key! {

parameter_types! {
pub DustAccount: AccountId = PalletId(*b"orml/dst").into_account();
pub MaxLocks: u32 = 100_000;
}

impl orml_tokens::Config for Runtime {
Expand All @@ -80,6 +81,7 @@ impl orml_tokens::Config for Runtime {
type WeightInfo = ();
type ExistentialDeposits = ExistentialDeposits;
type OnDust = orml_tokens::TransferDust<Runtime, DustAccount>;
type MaxLocks = MaxLocks;
}

pub const NATIVE_CURRENCY_ID: CurrencyId = 1;
Expand Down
13 changes: 9 additions & 4 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use frame_support::{
LockableCurrency as PalletLockableCurrency, ReservableCurrency as PalletReservableCurrency, SignedImbalance,
WithdrawReasons,
},
transactional, PalletId,
transactional, BoundedVec, PalletId,
};
use frame_system::{ensure_signed, pallet_prelude::*};
use orml_traits::{
Expand Down Expand Up @@ -181,6 +181,8 @@ pub mod module {

/// Handler to burn or transfer account's dust
type OnDust: OnDust<Self::AccountId, Self::CurrencyId, Self::Balance>;

type MaxLocks: Get<u32>;
}

#[pallet::error]
Expand Down Expand Up @@ -223,7 +225,7 @@ pub mod module {
T::AccountId,
Twox64Concat,
T::CurrencyId,
Vec<BalanceLock<T::Balance>>,
BoundedVec<BalanceLock<T::Balance>, T::MaxLocks>,
ValueQuery,
>;

Expand Down Expand Up @@ -444,7 +446,9 @@ impl<T: Config> Pallet<T> {
frame_system::Pallet::<T>::dec_consumers(who);
}
} else {
<Locks<T>>::insert(who, currency_id, locks);
let bounded_locks: BoundedVec<BalanceLock<T::Balance>, T::MaxLocks> =
locks.to_vec().try_into().expect("Too many locked balances");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only use expect for things that can never fail. should modify the method signature to make it return DispatchResult instead

<Locks<T>>::insert(who, currency_id, bounded_locks);
if !existed {
// increase account ref count when initialize lock
if frame_system::Pallet::<T>::inc_consumers(who).is_err() {
Expand Down Expand Up @@ -696,7 +700,8 @@ impl<T: Config> MultiLockableCurrency<T::AccountId> for Pallet<T> {
fn remove_lock(lock_id: LockIdentifier, currency_id: Self::CurrencyId, who: &T::AccountId) -> DispatchResult {
let mut locks = Self::locks(who, currency_id);
locks.retain(|lock| lock.id != lock_id);
Self::update_locks(currency_id, who, &locks[..]);
let locks_vec = locks.to_vec();
Self::update_locks(currency_id, who, &locks_vec[..]);
Ok(())
}
}
Expand Down
2 changes: 2 additions & 0 deletions tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ parameter_type_with_key! {

parameter_types! {
pub DustAccount: AccountId = PalletId(*b"orml/dst").into_account();
pub MaxLocks: u32 = 100_000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark this small and write test to check behavior when exceed the limit

}

impl Config for Runtime {
Expand All @@ -217,6 +218,7 @@ impl Config for Runtime {
type WeightInfo = ();
type ExistentialDeposits = ExistentialDeposits;
type OnDust = TransferDust<Runtime, DustAccount>;
type MaxLocks = MaxLocks;
}
pub type TreasuryCurrencyAdapter = <Runtime as pallet_treasury::Config>::Currency;

Expand Down