Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
17d7f63
Basic account composition.
gavofyork Feb 3, 2020
ae6f092
Add try_mutate_exists
gavofyork Feb 4, 2020
21e1565
De-duplicate
gavofyork Feb 4, 2020
c202dda
Refactor away the UpdateBalanceOutcome
gavofyork Feb 4, 2020
7aa6647
Expunge final UpdateBalanceOutcome refs
gavofyork Feb 4, 2020
b03d937
Refactor transfer
gavofyork Feb 4, 2020
1b74a2c
Refactor reservable currency stuff.
gavofyork Feb 4, 2020
74db69f
Test with the alternative setup.
gavofyork Feb 4, 2020
02d039d
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 4, 2020
d153672
Fixes
gavofyork Feb 4, 2020
459c30b
Test with both setups.
gavofyork Feb 5, 2020
f4ab417
Fixes
gavofyork Feb 5, 2020
35fcb32
Fix
gavofyork Feb 5, 2020
158863b
Fix macros
bkchr Feb 5, 2020
d7ce210
Make indices opt-in
gavofyork Feb 5, 2020
51ea88b
Remove CreationFee, and make indices opt-in.
gavofyork Feb 5, 2020
9c5539a
Fix construct_runtime
bkchr Feb 5, 2020
aed4da9
Fix last few bits
gavofyork Feb 6, 2020
21254ed
Fix tests
bkchr Feb 6, 2020
f929007
Update trait impls
gavofyork Feb 6, 2020
234ca8a
Don't hardcode the system event
bkchr Feb 6, 2020
c12175d
Make tests build and fix some stuff.
gavofyork Feb 6, 2020
ae0a7c6
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 6, 2020
133c060
Pointlessly bump runtime version
gavofyork Feb 6, 2020
5870f62
Fix benchmark
gavofyork Feb 6, 2020
2579ff7
Another fix
gavofyork Feb 6, 2020
571e209
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 6, 2020
f9adced
Whitespace
gavofyork Feb 6, 2020
2cc84bd
Make indices module economically safe
gavofyork Feb 7, 2020
06cd3a0
Migrations for indices.
gavofyork Feb 7, 2020
b656032
Fix
gavofyork Feb 7, 2020
6d02d1d
Whilespace
gavofyork Feb 7, 2020
091bf0e
Trim defunct migrations
gavofyork Feb 8, 2020
48a3450
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 8, 2020
ae75d69
Remove unused storage item
gavofyork Feb 8, 2020
10d28d2
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 9, 2020
02e67b7
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 9, 2020
e4a398a
More contains_key fixes
gavofyork Feb 9, 2020
9f01bf0
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 10, 2020
1a59997
Docs.
gavofyork Feb 10, 2020
fd6adae
Bump runtime
gavofyork Feb 10, 2020
29cefa1
Merge branch 'master' into gav-composite-account
gavofyork Feb 10, 2020
740e8a3
Merge branch 'master' into gav-composite-account
jacogr Feb 11, 2020
30cee70
Remove unneeded code
gavofyork Feb 11, 2020
6afee76
Merge branch 'gav-composite-account' of github.com:paritytech/substra…
gavofyork Feb 11, 2020
6444cc1
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 12, 2020
5890e24
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 12, 2020
411595d
Fix test
gavofyork Feb 12, 2020
a26c44d
Fix test
gavofyork Feb 12, 2020
bbacb94
Merge branch 'master' into gav-composite-account
gavofyork Feb 12, 2020
2f07990
Update frame/balances/src/lib.rs
gavofyork Feb 13, 2020
680c1f7
Fix ED logic
gavofyork Feb 13, 2020
446ace0
Merge branch 'gav-composite-account' of github.com:paritytech/substra…
gavofyork Feb 13, 2020
3c29022
Repatriate reserved logic
gavofyork Feb 13, 2020
cc6edb8
Typo
gavofyork Feb 13, 2020
df85e74
Fix typo
shawntabrizi Feb 13, 2020
db5c064
Merge branch 'gav-composite-account' of github.com:paritytech/substra…
gavofyork Feb 13, 2020
a7d2980
Update frame/system/src/lib.rs
gavofyork Feb 13, 2020
49bc2e0
Update frame/system/src/lib.rs
gavofyork Feb 13, 2020
c9e852c
Last few fixes
gavofyork Feb 13, 2020
86b132e
Merge branch 'gav-composite-account' of github.com:paritytech/substra…
gavofyork Feb 13, 2020
c472b53
Another fix
gavofyork Feb 13, 2020
dedc512
Merge remote-tracking branch 'origin/master' into gav-composite-account
gavofyork Feb 13, 2020
e424b22
Build fix
gavofyork Feb 13, 2020
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
Prev Previous commit
Next Next commit
Make indices module economically safe
  • Loading branch information
gavofyork committed Feb 7, 2020
commit 2cc84bdac7d9286b460b8cacc1c45d93baac6961
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ use frame_support::{
Currency, OnReapAccount, OnUnbalanced, TryDrop, StoredMap,
WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement,
Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive,
ExistenceRequirement::AllowDeath, IsDeadAccount
ExistenceRequirement::AllowDeath, IsDeadAccount, BalanceStatus as Status
}
};
use sp_runtime::{
Expand Down Expand Up @@ -1082,10 +1082,10 @@ impl<T: Trait<I>, I: Instance> Currency<T::AccountId> for Module<T, I> where
fn deposit_into_existing(
who: &T::AccountId,
value: Self::Balance
) -> result::Result<Self::PositiveImbalance, DispatchError> {
) -> Result<Self::PositiveImbalance, DispatchError> {
if value.is_zero() { return Ok(PositiveImbalance::zero()) }

Self::try_mutate_account(who, |account| -> DispatchResult {
Self::try_mutate_account(who, |account| -> Result<Self::PositiveImbalance, DispatchError> {
ensure!(!account.total().is_zero(), Error::<T, I>::DeadAccount);
account.free = account.free.checked_add(&value).ok_or(Error::<T, I>::Overflow)?;
Ok(PositiveImbalance::new(value))
Expand Down Expand Up @@ -1241,13 +1241,14 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
})
}

/// Move the reserved balance of one account into the free balance of another.
/// Move the reserved balance of one account into the balance of another, according to `status`.
///
/// Is a no-op if the value to be moved is zero.
fn repatriate_reserved(
slashed: &T::AccountId,
beneficiary: &T::AccountId,
value: Self::Balance,
status: Status,
) -> Result<Self::Balance, DispatchError> {
if value.is_zero() { return Ok (Zero::zero()) }

Expand All @@ -1259,7 +1260,10 @@ impl<T: Trait<I>, I: Instance> ReservableCurrency<T::AccountId> for Module<T, I>
ensure!(!to_account.total().is_zero(), Error::<T, I>::DeadAccount);
Self::try_mutate_account(slashed, |from_account| -> Result<Self::Balance, DispatchError> {
let actual = cmp::min(from_account.reserved, value);
to_account.free = to_account.free.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?;
match status {
Status::Free => to_account.free = to_account.free.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?,
Status::Reserved => to_account.reserved = to_account.reserved.checked_add(&actual).ok_or(Error::<T, I>::Overflow)?,
}
from_account.reserved -= actual;
Ok(value - actual)
})
Expand Down
22 changes: 18 additions & 4 deletions frame/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,25 +444,39 @@ macro_rules! decl_tests {
}

#[test]
fn transferring_reserved_balance_should_work() {
fn repatriating_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 110);
let _ = Balances::deposit_creating(&2, 1);
assert_ok!(Balances::reserve(&1, 110));
assert_ok!(Balances::repatriate_reserved(&1, &2, 41), 0);
assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Free), 0);
assert_eq!(Balances::reserved_balance(1), 69);
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::reserved_balance(2), 0);
assert_eq!(Balances::free_balance(2), 42);
});
}

#[test]
fn transferring_reserved_balance_should_work() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 110);
let _ = Balances::deposit_creating(&2, 1);
assert_ok!(Balances::reserve(&1, 110));
assert_ok!(Balances::repatriate_reserved(&1, &2, 41, Status::Reserved), 0);
assert_eq!(Balances::reserved_balance(1), 69);
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::reserved_balance(2), 41);
assert_eq!(Balances::free_balance(2), 1);
});
}

#[test]
fn transferring_reserved_balance_to_nonexistent_should_fail() {
<$ext_builder>::default().build().execute_with(|| {
let _ = Balances::deposit_creating(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
assert_noop!(Balances::repatriate_reserved(&1, &2, 42), Error::<$test, _>::DeadAccount);
assert_noop!(Balances::repatriate_reserved(&1, &2, 42, Status::Free), Error::<$test, _>::DeadAccount);
});
}

Expand All @@ -472,7 +486,7 @@ macro_rules! decl_tests {
let _ = Balances::deposit_creating(&1, 110);
let _ = Balances::deposit_creating(&2, 1);
assert_ok!(Balances::reserve(&1, 41));
assert_ok!(Balances::repatriate_reserved(&1, &2, 69), 28);
assert_ok!(Balances::repatriate_reserved(&1, &2, 69, Status::Free), 28);
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::free_balance(1), 69);
assert_eq!(Balances::reserved_balance(2), 0);
Expand Down
4 changes: 2 additions & 2 deletions frame/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use frame_support::{
weights::SimpleDispatchInfo,
traits::{
Currency, ReservableCurrency, LockableCurrency, WithdrawReason, LockIdentifier, Get,
OnReapAccount, OnUnbalanced
OnReapAccount, OnUnbalanced, BalanceStatus
}
};
use frame_system::{self as system, ensure_signed, ensure_root};
Expand Down Expand Up @@ -802,7 +802,7 @@ decl_module! {
let queue = <DispatchQueue<T>>::get();
ensure!(!queue.iter().any(|item| &item.1 == &proposal_hash), Error::<T>::Imminent);

let _ = T::Currency::repatriate_reserved(&old, &who, deposit);
let _ = T::Currency::repatriate_reserved(&old, &who, deposit, BalanceStatus::Free);
<Preimages<T>>::remove(&proposal_hash);
Self::deposit_event(RawEvent::PreimageReaped(proposal_hash, old, deposit, who));
}
Expand Down
4 changes: 2 additions & 2 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ use frame_support::{
decl_storage, decl_event, ensure, decl_module, decl_error, weights::SimpleDispatchInfo,
traits::{
Currency, Get, LockableCurrency, LockIdentifier, ReservableCurrency, WithdrawReasons,
ChangeMembers, OnUnbalanced, WithdrawReason, Contains
ChangeMembers, OnUnbalanced, WithdrawReason, Contains, BalanceStatus
}
};
use sp_phragmen::ExtendedBalance;
Expand Down Expand Up @@ -311,7 +311,7 @@ decl_module! {
let valid = Self::is_defunct_voter(&target);
if valid {
// reporter will get the voting bond of the target
T::Currency::repatriate_reserved(&target, &reporter, T::VotingBond::get())?;
T::Currency::repatriate_reserved(&target, &reporter, T::VotingBond::get(), BalanceStatus::Free)?;
// remove the target. They are defunct.
Self::do_remove_voter(&target, false);
} else {
Expand Down
4 changes: 2 additions & 2 deletions frame/elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
decl_storage, decl_event, ensure, decl_module, decl_error,
weights::SimpleDispatchInfo,
traits::{
Currency, ExistenceRequirement, Get, LockableCurrency, LockIdentifier,
Currency, ExistenceRequirement, Get, LockableCurrency, LockIdentifier, BalanceStatus,
OnUnbalanced, ReservableCurrency, WithdrawReason, WithdrawReasons, ChangeMembers
}
};
Expand Down Expand Up @@ -501,7 +501,7 @@ decl_module! {
if valid {
// This only fails if `reporter` doesn't exist, which it clearly must do since its
// the origin. Still, it's no more harmful to propagate any error at this point.
T::Currency::repatriate_reserved(&who, &reporter, T::VotingBond::get())?;
T::Currency::repatriate_reserved(&who, &reporter, T::VotingBond::get(), BalanceStatus::Free)?;
Self::deposit_event(RawEvent::VoterReaped(who, reporter));
} else {
let imbalance = T::Currency::slash_reserved(&reporter, T::VotingBond::get()).0;
Expand Down
25 changes: 18 additions & 7 deletions frame/generic-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ use frame_support::{
decl_event, decl_module, decl_storage, ensure, decl_error,
traits::{
Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, ReservableCurrency,
SignedImbalance, WithdrawReason, WithdrawReasons, TryDrop,
SignedImbalance, WithdrawReason, WithdrawReasons, TryDrop, BalanceStatus,
},
Parameter, StorageMap,
};
Expand Down Expand Up @@ -714,8 +714,8 @@ impl<T: Trait> Module<T> {
}
}

/// Move up to `amount` from reserved balance of account `who` to free balance of account
/// `beneficiary`.
/// Move up to `amount` from reserved balance of account `who` to balance of account
/// `beneficiary`, either free or reserved depending on `status`.
///
/// As much funds up to `amount` will be moved as possible. If this is less than `amount`, then
/// the `remaining` would be returned, else `Zero::zero()`.
Expand All @@ -726,13 +726,23 @@ impl<T: Trait> Module<T> {
who: &T::AccountId,
beneficiary: &T::AccountId,
amount: T::Balance,
status: BalanceStatus,
) -> T::Balance {
let b = Self::reserved_balance(asset_id, who);
let slash = sp_std::cmp::min(b, amount);

let original_free_balance = Self::free_balance(asset_id, beneficiary);
let new_free_balance = original_free_balance + slash;
Self::set_free_balance(asset_id, beneficiary, new_free_balance);
match status {
BalanceStatus::Free => {
let original_free_balance = Self::free_balance(asset_id, beneficiary);
let new_free_balance = original_free_balance + slash;
Self::set_free_balance(asset_id, beneficiary, new_free_balance);
}
BalanceStatus::Reserved => {
let original_reserved_balance = Self::reserved_balance(asset_id, beneficiary);
let new_reserved_balance = original_reserved_balance + slash;
Self::set_reserved_balance(asset_id, beneficiary, new_reserved_balance);
}
}

let new_reserve_balance = b - slash;
Self::set_reserved_balance(asset_id, who, new_reserve_balance);
Expand Down Expand Up @@ -1288,8 +1298,9 @@ where
slashed: &T::AccountId,
beneficiary: &T::AccountId,
value: Self::Balance,
status: BalanceStatus,
) -> result::Result<Self::Balance, DispatchError> {
Ok(<Module<T>>::repatriate_reserved(&U::asset_id(), slashed, beneficiary, value))
Ok(<Module<T>>::repatriate_reserved(&U::asset_id(), slashed, beneficiary, value, status))
}
}

Expand Down
4 changes: 2 additions & 2 deletions frame/generic-asset/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ fn slash_reserved_should_return_none() {
fn repatriate_reserved_return_amount_substracted_by_slash_amount() {
ExtBuilder::default().build().execute_with(|| {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 130), 30);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 130, BalanceStatus::Free), 30);
});
}

Expand All @@ -571,7 +571,7 @@ fn repatriate_reserved_return_amount_substracted_by_slash_amount() {
fn repatriate_reserved_return_none() {
ExtBuilder::default().build().execute_with(|| {
GenericAsset::set_reserved_balance(&1, &0, 100);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 90), 0);
assert_eq!(GenericAsset::repatriate_reserved(&1, &0, &1, 90, BalanceStatus::Free), 0);
});
}

Expand Down
4 changes: 2 additions & 2 deletions frame/identity/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ use sp_runtime::traits::{StaticLookup, EnsureOrigin, Zero, AppendZerosInput};
use frame_support::{
decl_module, decl_event, decl_storage, ensure, decl_error,
traits::{Currency, ReservableCurrency, OnUnbalanced, Get},
weights::SimpleDispatchInfo,
weights::SimpleDispatchInfo, BalanceStatus,
};
use frame_system::{self as system, ensure_signed, ensure_root};

Expand Down Expand Up @@ -821,7 +821,7 @@ decl_module! {
match id.judgements.binary_search_by_key(&reg_index, |x| x.0) {
Ok(position) => {
if let Judgement::FeePaid(fee) = id.judgements[position].1 {
let _ = T::Currency::repatriate_reserved(&target, &sender, fee);
let _ = T::Currency::repatriate_reserved(&target, &sender, fee, BalanceStatus::Free);
}
id.judgements[position] = item
}
Expand Down
4 changes: 4 additions & 0 deletions frame/indices/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ sp-core = { version = "2.0.0", default-features = false, path = "../../primitive
frame-support = { version = "2.0.0", default-features = false, path = "../support" }
frame-system = { version = "2.0.0", default-features = false, path = "../system" }

[dev-dependencies]
pallet-balances = { version = "2.0.0", default-features = false, path = "../balances" }

[features]
default = ["std"]
std = [
Expand All @@ -28,4 +31,5 @@ std = [
"frame-support/std",
"sp-runtime/std",
"frame-system/std",
"pallet-balances/std",
]
Loading