Skip to content
This repository was archived by the owner on Jul 4, 2022. It is now read-only.

Commit 3cf1183

Browse files
Fixed GA test (#147)
1 parent 6b68bcd commit 3cf1183

File tree

4 files changed

+325
-289
lines changed

4 files changed

+325
-289
lines changed

prml/generic-asset/src/imbalances.rs

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use sp_std::{mem, result};
3636
/// denoting that funds have been created without any equal and opposite
3737
/// accounting.
3838
#[must_use]
39+
#[derive(Debug)]
3940
pub struct PositiveImbalance<T: Trait> {
4041
amount: T::Balance,
4142
asset_id: T::AssetId,
@@ -52,6 +53,7 @@ impl<T: Trait> PositiveImbalance<T> {
5253
/// denoting that funds have been destroyed without any equal and opposite
5354
/// accounting.
5455
#[must_use]
56+
#[derive(Debug)]
5557
pub struct NegativeImbalance<T: Trait> {
5658
amount: T::Balance,
5759
asset_id: T::AssetId,
@@ -77,7 +79,7 @@ impl<T: Trait> Imbalance<T::Balance> for PositiveImbalance<T> {
7779
Self::new(Zero::zero(), Zero::zero())
7880
}
7981
fn drop_zero(self) -> result::Result<(), Self> {
80-
if self.amount.is_zero() {
82+
if self.amount.is_zero() || self.asset_id.is_zero() {
8183
Ok(())
8284
} else {
8385
Err(self)
@@ -130,7 +132,7 @@ impl<T: Trait> Imbalance<T::Balance> for NegativeImbalance<T> {
130132
Self::new(Zero::zero(), Zero::zero())
131133
}
132134
fn drop_zero(self) -> result::Result<(), Self> {
133-
if self.amount.is_zero() {
135+
if self.amount.is_zero() || self.asset_id.is_zero() {
134136
Ok(())
135137
} else {
136138
Err(self)
@@ -185,41 +187,78 @@ impl<T: Trait> Drop for NegativeImbalance<T> {
185187
}
186188

187189
/// The result of an offset operation
190+
#[derive(Debug)]
188191
pub enum OffsetResult<T: Trait, I: Imbalance<T::Balance>> {
189192
Imbalance(I),
190193
Opposite(I::Opposite),
191194
}
192195

196+
#[derive(Debug, PartialEq, Eq)]
197+
pub enum Error {
198+
/// The operation cannot occur on imbalances with different asset IDs
199+
DifferentAssetIds,
200+
/// The operation cannot occur when asset id is 0 and amount is not 0
201+
ZeroIdWithNonZeroAmount,
202+
}
203+
193204
/// Provides a safe API around imbalances with asset ID awareness
194205
pub trait CheckedImbalance<T: Trait>: Imbalance<T::Balance> {
195206
/// Get the imbalance asset ID
196207
fn asset_id(&self) -> T::AssetId;
197208
/// Get the imbalance amount
198209
fn amount(&self) -> T::Balance;
210+
/// Set the imbalance asset ID
211+
fn set_asset_id(&mut self, new_asset_id: T::AssetId);
199212
/// Offset with asset ID safety checks
200-
fn checked_offset(self, other: Self::Opposite) -> result::Result<OffsetResult<T, Self>, ()>
213+
fn checked_offset(self, other: Self::Opposite) -> result::Result<OffsetResult<T, Self>, Error>
201214
where
202215
Self::Opposite: CheckedImbalance<T>,
203216
{
217+
if other.asset_id().is_zero() {
218+
return Ok(OffsetResult::Imbalance(self))
219+
}
220+
if self.asset_id().is_zero() && !self.amount().is_zero() {
221+
return Err(Error::ZeroIdWithNonZeroAmount);
222+
}
204223
if self.asset_id() != other.asset_id() {
205-
return Err(());
224+
return Err(Error::DifferentAssetIds);
206225
}
207226
match self.offset(other) {
208227
Ok(i) => Ok(OffsetResult::Imbalance(i)),
209228
Err(i) => Ok(OffsetResult::Opposite(i)),
210229
}
211230
}
212231
/// Subsume with asset ID safety checks
213-
fn checked_subsume(&mut self, other: Self) -> result::Result<(), ()> {
232+
fn checked_subsume(&mut self, other: Self) -> result::Result<(), Error> {
233+
if other.asset_id().is_zero() {
234+
// noop, rhs is 0
235+
return Ok(());
236+
}
237+
if self.asset_id().is_zero() && !self.amount().is_zero() {
238+
return Err(Error::ZeroIdWithNonZeroAmount);
239+
}
240+
if self.asset_id().is_zero() {
241+
self.set_asset_id(other.asset_id());
242+
}
214243
if self.asset_id() != other.asset_id() {
215-
return Err(());
244+
return Err(Error::DifferentAssetIds);
216245
}
217246
Ok(self.subsume(other))
218247
}
219248
/// Merge with asset ID safety checks
220-
fn checked_merge(self, other: Self) -> result::Result<Self, ()> {
249+
fn checked_merge(mut self, other: Self) -> result::Result<Self, Error> {
250+
if other.asset_id().is_zero() {
251+
// noop, rhs is 0
252+
return Ok(self);
253+
}
254+
if self.asset_id().is_zero() && !self.amount().is_zero() {
255+
return Err(Error::ZeroIdWithNonZeroAmount);
256+
}
257+
if self.asset_id().is_zero() {
258+
self.set_asset_id(other.asset_id());
259+
}
221260
if self.asset_id() != other.asset_id() {
222-
return Err(());
261+
return Err(Error::DifferentAssetIds);
223262
}
224263
Ok(self.merge(other))
225264
}
@@ -232,6 +271,10 @@ impl<T: Trait> CheckedImbalance<T> for PositiveImbalance<T> {
232271
fn amount(&self) -> T::Balance {
233272
self.amount
234273
}
274+
/// Set the imbalance asset ID
275+
fn set_asset_id(&mut self, new_asset_id: T::AssetId) {
276+
self.asset_id = new_asset_id;
277+
}
235278
}
236279

237280
impl<T: Trait> CheckedImbalance<T> for NegativeImbalance<T> {
@@ -241,4 +284,8 @@ impl<T: Trait> CheckedImbalance<T> for NegativeImbalance<T> {
241284
fn amount(&self) -> T::Balance {
242285
self.amount
243286
}
287+
/// Set the imbalance asset ID
288+
fn set_asset_id(&mut self, new_asset_id: T::AssetId) {
289+
self.asset_id = new_asset_id;
290+
}
244291
}

prml/generic-asset/src/lib.rs

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ use frame_support::{
163163
decl_error, decl_event, decl_module, decl_storage, ensure,
164164
traits::{
165165
BalanceStatus, Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, ReservableCurrency,
166-
SignedImbalance, TryDrop, WithdrawReason, WithdrawReasons,
166+
SignedImbalance, WithdrawReason, WithdrawReasons,
167167
},
168168
weights::Weight,
169169
Parameter, StorageMap,
170170
};
171-
use frame_system::{self as system, ensure_root, ensure_signed};
171+
use frame_system::{ensure_root, ensure_signed};
172172
use sp_std::prelude::*;
173173
use sp_std::{cmp, fmt::Debug, result};
174174

@@ -699,36 +699,20 @@ impl<T: Trait> Module<T> {
699699
/// - the `slashed` id equal to `beneficiary` and the `status` is `Reserved`.
700700
pub fn repatriate_reserved(
701701
asset_id: T::AssetId,
702-
slashed: &T::AccountId,
702+
who: &T::AccountId,
703703
beneficiary: &T::AccountId,
704-
value: T::Balance,
705-
status: BalanceStatus,
704+
amount: T::Balance,
706705
) -> Result<T::Balance, DispatchError> {
707-
Ok(Zero::zero())
708-
// TODO:
709-
// if value.is_zero() { return Ok(Zero::zero()) }
710-
711-
// if slashed == beneficiary {
712-
// return match status {
713-
// BalanceStatus::Free => Ok(Self::unreserve(asset_id, slashed, value)),
714-
// BalanceStatus::Reserved => Ok(value.saturating_sub(Self::reserved_balance(asset_id, slashed))),
715-
// };
716-
// }
717-
718-
// let actual = Self::try_mutate_account(beneficiary, |to_account, is_new|-> Result<T::Balance, DispatchError> {
719-
// ensure!(!is_new, Error::<T, I>::DeadAccount);
720-
// Self::try_mutate_account(slashed, |from_account, _| -> Result<T::Balance, DispatchError> {
721-
// let actual = cmp::min(from_account.reserved, value);
722-
// match status {
723-
// BalanceStatus::Free => to_account.free = to_account.free.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?,
724-
// BalanceStatus::Reserved => to_account.reserved = to_account.reserved.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?,
725-
// }
726-
// from_account.reserved -= actual;
727-
// Ok(actual)
728-
// })
729-
// })?;
730-
731-
// Ok(value - actual)
706+
let b = Self::reserved_balance(asset_id, who);
707+
let slash = sp_std::cmp::min(b, amount);
708+
709+
let original_free_balance = Self::free_balance(asset_id, beneficiary);
710+
let new_free_balance = original_free_balance + slash;
711+
Self::set_free_balance(asset_id, beneficiary, new_free_balance);
712+
713+
let new_reserve_balance = b - slash;
714+
Self::set_reserved_balance(asset_id, who, new_reserve_balance);
715+
Ok(amount - slash)
732716
}
733717

734718
/// Check permission to perform burn, mint or update.
@@ -1018,9 +1002,9 @@ where
10181002
slashed: &T::AccountId,
10191003
beneficiary: &T::AccountId,
10201004
value: Self::Balance,
1021-
status: BalanceStatus,
1005+
_status: BalanceStatus,
10221006
) -> result::Result<Self::Balance, DispatchError> {
1023-
<Module<T>>::repatriate_reserved(U::asset_id(), slashed, beneficiary, value, status)
1007+
<Module<T>>::repatriate_reserved(U::asset_id(), slashed, beneficiary, value)
10241008
}
10251009
}
10261010

prml/generic-asset/src/mock.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,38 @@ parameter_types! {
7171
pub const AvailableBlockRatio: Perbill = Perbill::one();
7272
}
7373
impl frame_system::Trait for Test {
74+
type BaseCallFilter = ();
7475
type Origin = Origin;
7576
type Index = u64;
7677
type BlockNumber = u64;
7778
type Call = ();
7879
type Hash = H256;
7980
type Hashing = BlakeTwo256;
8081
type AccountId = u64;
81-
type Lookup = IdentityLookup<u64>;
82+
type Lookup = IdentityLookup<Self::AccountId>;
8283
type Header = Header;
84+
type BlockHashCount = BlockHashCount;
8385
type Event = TestEvent;
86+
type DbWeight = ();
87+
type BlockExecutionWeight = ();
88+
type ExtrinsicBaseWeight = ();
89+
type MaximumExtrinsicWeight = MaximumBlockWeight;
8490
type MaximumBlockWeight = MaximumBlockWeight;
8591
type MaximumBlockLength = MaximumBlockLength;
8692
type AvailableBlockRatio = AvailableBlockRatio;
87-
type BlockHashCount = BlockHashCount;
8893
type Version = ();
89-
type ModuleToIndex = ();
90-
type Doughnut = ();
91-
type DelegatedDispatchVerifier = ();
94+
type PalletInfo = ();
95+
type AccountData = ();
96+
type OnNewAccount = ();
97+
type OnKilledAccount = ();
98+
type SystemWeightInfo = ();
9299
}
93100

94101
impl Trait for Test {
95102
type Balance = u64;
96103
type AssetId = u32;
97104
type Event = TestEvent;
105+
type WeightInfo = ();
98106
}
99107

100108
mod generic_asset {
@@ -104,7 +112,7 @@ mod generic_asset {
104112
use frame_system as system;
105113
impl_outer_event! {
106114
pub enum TestEvent for Test {
107-
system,
115+
system<T>,
108116
generic_asset<T>,
109117
}
110118
}

0 commit comments

Comments
 (0)