Skip to content

Commit

Permalink
Fix dust white list (#320)
Browse files Browse the repository at this point in the history
* 🐛 ($PALLET) Fix dust white list

* 🎨 ($PALLET) Format code
  • Loading branch information
AllenPocketGamer authored Sep 28, 2021
1 parent 8f571fe commit 0b0a642
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 15 deletions.
24 changes: 22 additions & 2 deletions node/primitives/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
#![allow(clippy::unnecessary_cast)]

use codec::FullCodec;
use frame_support::{dispatch::DispatchError, sp_runtime::TokenError};
use codec::{Decode, Encode, FullCodec};
use frame_support::{
dispatch::DispatchError,
sp_runtime::{traits::AccountIdConversion, TokenError, TypeId},
};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, MaybeSerializeDeserialize},
DispatchResult,
Expand Down Expand Up @@ -175,3 +178,20 @@ impl<Balance> BancorHandler<Balance> for () {
DispatchResult::from(DispatchError::Token(TokenError::NoFunds))
}
}

pub trait CheckSubAccount<T: Encode + Decode + Default> {
fn check_sub_account<S: Decode>(&self, account: &T) -> bool;
}

impl<T, Id> CheckSubAccount<T> for Id
where
T: Encode + Decode + Default,
Id: Encode + Decode + TypeId + AccountIdConversion<T> + Eq,
{
fn check_sub_account<S: Decode>(&self, account: &T) -> bool {
match Id::try_from_sub_account::<S>(account) {
Some((id, _)) => id.eq(self),
None => false,
}
}
}
23 changes: 19 additions & 4 deletions pallets/liquidity-mining/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use frame_support::{
traits::{BlakeTwo256, IdentifyAccount, IdentityLookup, Verify},
BuildStorage, MultiSignature,
},
traits::Contains,
PalletId,
};
use node_primitives::{Amount, Balance, CurrencyId, TokenSymbol};
use node_primitives::{traits::CheckSubAccount, Amount, Balance, CurrencyId, TokenSymbol};
use sp_core::H256;

use crate as lm;
use crate::PoolId;

pub(crate) type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub(crate) type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -125,20 +127,33 @@ impl orml_currencies::Config for Test {

orml_traits::parameter_type_with_key! {
pub ExistentialDeposits: |_currency_id: CurrencyId| -> Balance {
0
1_000_000
};
}

parameter_types! {
pub BifrostTreasuryFakeAccount: AccountId = AccountId::new([155u8;32]);
}

pub struct DustRemovalWhitelist;
impl Contains<AccountId> for DustRemovalWhitelist {
fn contains(a: &AccountId) -> bool {
*a == BifrostTreasuryFakeAccount::get() ||
*a == INVESTOR ||
LiquidityMiningPalletId::get().check_sub_account::<PoolId>(a)
}
}

impl orml_tokens::Config for Test {
type Amount = Amount;
type Balance = Balance;
type CurrencyId = CurrencyId;
type Event = Event;
type ExistentialDeposits = ExistentialDeposits;
type MaxLocks = MaxLocks;
type OnDust = ();
type OnDust = orml_tokens::TransferDust<Test, BifrostTreasuryFakeAccount>;
type WeightInfo = ();
type DustRemovalWhitelist = ();
type DustRemovalWhitelist = DustRemovalWhitelist;
}

parameter_types! {
Expand Down
87 changes: 87 additions & 0 deletions pallets/liquidity-mining/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2298,3 +2298,90 @@ fn simple_integration_test() {
assert!(LM::user_deposit_data(0, USER_2).is_none());
});
}

#[test]
fn fuck_bug() {
new_test_ext().execute_with(|| {
const ALICE: AccountId = AccountId::new([0u8; 32]);
const BOB: AccountId = AccountId::new([1u8; 32]);
const CHARLIE: AccountId = AccountId::new([2u8; 32]);

const INIT_AMOUNT: Balance = 1_000_000_000 * UNIT;

const REWARD_TOKEN: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);
const REWARD_AMOUNT: Balance = 10 * UNIT;

const DEPOSIT_TOKEN_1: CurrencyId = CurrencyId::VSToken(TokenSymbol::KSM);
const DEPOSIT_TOKEN_2: CurrencyId = CurrencyId::VSBond(TokenSymbol::KSM, 2001, 13, 20);

assert_ok!(Tokens::set_balance(Origin::root(), ALICE, REWARD_TOKEN, INIT_AMOUNT, 0));
assert_ok!(Tokens::set_balance(Origin::root(), BOB, DEPOSIT_TOKEN_1, 0, INIT_AMOUNT));
assert_ok!(Tokens::set_balance(Origin::root(), BOB, DEPOSIT_TOKEN_2, 0, INIT_AMOUNT));
assert_ok!(Tokens::set_balance(Origin::root(), CHARLIE, DEPOSIT_TOKEN_1, 0, INIT_AMOUNT));
assert_ok!(Tokens::set_balance(Origin::root(), CHARLIE, DEPOSIT_TOKEN_2, 0, INIT_AMOUNT));

run_to_block(134);

assert_ok!(LM::create_eb_farming_pool(
pallet_collective::RawOrigin::Member(TC_MEMBER_1).into(),
2001,
13,
20,
(REWARD_TOKEN, REWARD_AMOUNT),
vec![].try_into().unwrap(),
23,
UNIT,
0
));

run_to_block(135);

assert_ok!(LM::charge(Some(ALICE).into(), 0));

run_to_block(138);

assert_ok!(LM::deposit(Some(BOB).into(), 0, 13 * UNIT));

run_to_block(140);

assert_ok!(LM::deposit(Some(CHARLIE).into(), 0, 187 * UNIT));

run_to_block(179);

assert_ok!(LM::redeem_all(Some(BOB).into(), 0));
assert_ok!(LM::redeem_all(Some(CHARLIE).into(), 0));

assert!(LM::pool(200).is_none());

assert_ok!(LM::create_eb_farming_pool(
pallet_collective::RawOrigin::Member(TC_MEMBER_1).into(),
2001,
13,
20,
(REWARD_TOKEN, REWARD_AMOUNT),
vec![].try_into().unwrap(),
23,
UNIT,
0
));

run_to_block(235);

assert_ok!(LM::charge(Some(ALICE).into(), 1));

run_to_block(250);

assert_ok!(LM::deposit(Some(BOB).into(), 1, 23 * UNIT));

run_to_block(265);

assert_ok!(LM::deposit(Some(CHARLIE).into(), 1, 167 * UNIT));

run_to_block(280);

assert_ok!(LM::redeem_all(Some(BOB).into(), 1));
assert_ok!(LM::redeem_all(Some(CHARLIE).into(), 1));

assert!(LM::pool(1).is_none());
});
}
16 changes: 7 additions & 9 deletions runtime/bifrost/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ use frame_support::{
use frame_system::{EnsureOneOf, EnsureRoot, RawOrigin};
use hex_literal::hex;
pub use node_primitives::{
AccountId, Amount, Balance, BlockNumber, CurrencyId, ExtraFeeName, Moment, Nonce, ParaId,
ParachainDerivedProxyAccountType, ParachainTransactProxyType, ParachainTransactType, PoolId,
RpcContributionStatus, TokenSymbol, TransferOriginType, XcmBaseWeight,
traits::CheckSubAccount, AccountId, Amount, Balance, BlockNumber, CurrencyId, ExtraFeeName,
Moment, Nonce, ParaId, ParachainDerivedProxyAccountType, ParachainTransactProxyType,
ParachainTransactType, PoolId, RpcContributionStatus, TokenSymbol, TransferOriginType,
XcmBaseWeight,
};
// orml imports
use orml_currencies::BasicCurrencyAdapter;
Expand Down Expand Up @@ -234,11 +235,7 @@ parameter_types! {
}

pub fn get_all_pallet_accounts() -> Vec<AccountId> {
vec![
TreasuryPalletId::get().into_account(),
BifrostCrowdloanId::get().into_account(),
LiquidityMiningPalletId::get().into_account(),
]
vec![TreasuryPalletId::get().into_account(), BifrostCrowdloanId::get().into_account()]
}

impl frame_system::Config for Runtime {
Expand Down Expand Up @@ -892,7 +889,8 @@ orml_traits::parameter_type_with_key! {
pub struct DustRemovalWhitelist;
impl Contains<AccountId> for DustRemovalWhitelist {
fn contains(a: &AccountId) -> bool {
get_all_pallet_accounts().contains(a)
get_all_pallet_accounts().contains(a) ||
LiquidityMiningPalletId::get().check_sub_account::<PoolId>(a)
}
}

Expand Down

0 comments on commit 0b0a642

Please sign in to comment.