Replace bitmask with bitflags (#7159)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2020-10-29 20:19:59 +08:00
committed by GitHub
parent c8a245e5e0
commit bd450c24ff
15 changed files with 67 additions and 80 deletions
+8 -8
View File
@@ -163,7 +163,7 @@ use frame_support::{
StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, ensure,
traits::{
Currency, OnKilledAccount, OnUnbalanced, TryDrop, StoredMap,
WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status,
}
@@ -292,9 +292,9 @@ pub enum Reasons {
impl From<WithdrawReasons> for Reasons {
fn from(r: WithdrawReasons) -> Reasons {
if r == WithdrawReasons::from(WithdrawReason::TransactionPayment) {
if r == WithdrawReasons::from(WithdrawReasons::TRANSACTION_PAYMENT) {
Reasons::Fee
} else if r.contains(WithdrawReason::TransactionPayment) {
} else if r.contains(WithdrawReasons::TRANSACTION_PAYMENT) {
Reasons::All
} else {
Reasons::Misc
@@ -1011,7 +1011,7 @@ impl<T: Trait<I>, I: Instance> Currency<T::AccountId> for Module<T, I> where
Self::ensure_can_withdraw(
transactor,
value,
WithdrawReason::Transfer.into(),
WithdrawReasons::TRANSFER,
from_account.free,
)?;
@@ -1170,7 +1170,7 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
Self::account(who).free
.checked_sub(&value)
.map_or(false, |new_balance|
Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance).is_ok()
Self::ensure_can_withdraw(who, value, WithdrawReasons::RESERVE, new_balance).is_ok()
)
}
@@ -1187,7 +1187,7 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
Self::try_mutate_account(who, |account, _| -> DispatchResult {
account.free = account.free.checked_sub(&value).ok_or(Error::<T, I>::InsufficientBalance)?;
account.reserved = account.reserved.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
Self::ensure_can_withdraw(&who, value.clone(), WithdrawReason::Reserve.into(), account.free)
Self::ensure_can_withdraw(&who, value.clone(), WithdrawReasons::RESERVE, account.free)
})?;
Self::deposit_event(RawEvent::Reserved(who.clone(), value));
@@ -1303,7 +1303,7 @@ where
amount: T::Balance,
reasons: WithdrawReasons,
) {
if amount.is_zero() || reasons.is_none() { return }
if amount.is_zero() || reasons.is_empty() { return }
let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() });
let mut locks = Self::locks(who).into_iter()
.filter_map(|l| if l.id == id { new_lock.take() } else { Some(l) })
@@ -1322,7 +1322,7 @@ where
amount: T::Balance,
reasons: WithdrawReasons,
) {
if amount.is_zero() || reasons.is_none() { return }
if amount.is_zero() || reasons.is_empty() { return }
let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() });
let mut locks = Self::locks(who).into_iter().filter_map(|l|
if l.id == id {
+7 -7
View File
@@ -42,7 +42,7 @@ macro_rules! decl_tests {
use frame_support::{
assert_noop, assert_ok, assert_err,
traits::{
LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons,
LockableCurrency, LockIdentifier, WithdrawReasons,
Currency, ReservableCurrency, ExistenceRequirement::AllowDeath, StoredMap
}
};
@@ -133,7 +133,7 @@ macro_rules! decl_tests {
#[test]
fn combination_locking_should_work() {
<$ext_builder>::default().existential_deposit(1).monied(true).build().execute_with(|| {
Balances::set_lock(ID_1, &1, u64::max_value(), WithdrawReasons::none());
Balances::set_lock(ID_1, &1, u64::max_value(), WithdrawReasons::empty());
Balances::set_lock(ID_2, &1, 0, WithdrawReasons::all());
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
});
@@ -168,7 +168,7 @@ macro_rules! decl_tests {
.build()
.execute_with(|| {
pallet_transaction_payment::NextFeeMultiplier::put(Multiplier::saturating_from_integer(1));
Balances::set_lock(ID_1, &1, 10, WithdrawReason::Reserve.into());
Balances::set_lock(ID_1, &1, 10, WithdrawReasons::RESERVE);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath),
Error::<$test, _>::LiquidityRestrictions
@@ -192,7 +192,7 @@ macro_rules! decl_tests {
1,
).is_ok());
Balances::set_lock(ID_1, &1, 10, WithdrawReason::TransactionPayment.into());
Balances::set_lock(ID_1, &1, 10, WithdrawReasons::TRANSACTION_PAYMENT);
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
assert_ok!(<Balances as ReservableCurrency<_>>::reserve(&1, 1));
assert!(<ChargeTransactionPayment<$test> as SignedExtension>::pre_dispatch(
@@ -237,17 +237,17 @@ macro_rules! decl_tests {
#[test]
fn lock_reasons_extension_should_work() {
<$ext_builder>::default().existential_deposit(1).monied(true).build().execute_with(|| {
Balances::set_lock(ID_1, &1, 10, WithdrawReason::Transfer.into());
Balances::set_lock(ID_1, &1, 10, WithdrawReasons::TRANSFER);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<$test, _>::LiquidityRestrictions
);
Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::none());
Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::empty());
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<$test, _>::LiquidityRestrictions
);
Balances::extend_lock(ID_1, &1, 10, WithdrawReason::Reserve.into());
Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::RESERVE);
assert_noop!(
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
Error::<$test, _>::LiquidityRestrictions