Composite accounts (#4820)

* Basic account composition.

* Add try_mutate_exists

* De-duplicate

* Refactor away the UpdateBalanceOutcome

* Expunge final UpdateBalanceOutcome refs

* Refactor transfer

* Refactor reservable currency stuff.

* Test with the alternative setup.

* Fixes

* Test with both setups.

* Fixes

* Fix

* Fix macros

* Make indices opt-in

* Remove CreationFee, and make indices opt-in.

* Fix construct_runtime

* Fix last few bits

* Fix tests

* Update trait impls

* Don't hardcode the system event

* Make tests build and fix some stuff.

* Pointlessly bump runtime version

* Fix benchmark

* Another fix

* Whitespace

* Make indices module economically safe

* Migrations for indices.

* Fix

* Whilespace

* Trim defunct migrations

* Remove unused storage item

* More contains_key fixes

* Docs.

* Bump runtime

* Remove unneeded code

* Fix test

* Fix test

* Update frame/balances/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Fix ED logic

* Repatriate reserved logic

* Typo

* Fix typo

* Update frame/system/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/system/src/lib.rs

Co-Authored-By: Shawn Tabrizi <shawntabrizi@gmail.com>

* Last few fixes

* Another fix

* Build fix

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Jaco Greeff <jacogr@gmail.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Gavin Wood
2020-02-14 00:47:51 +00:00
committed by GitHub
parent d3fa8c91af
commit 5b7512e2e4
79 changed files with 2459 additions and 2100 deletions
+27 -16
View File
@@ -166,7 +166,7 @@ use frame_support::{
decl_event, decl_module, decl_storage, ensure, decl_error,
traits::{
Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, ReservableCurrency,
SignedImbalance, UpdateBalanceOutcome, WithdrawReason, WithdrawReasons, TryDrop,
SignedImbalance, WithdrawReason, WithdrawReasons, TryDrop, BalanceStatus,
},
Parameter, StorageMap,
};
@@ -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()`.
@@ -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);
@@ -1080,8 +1090,8 @@ mod imbalances {
// its type declaration).
// This works as long as `increase_total_issuance_by` doesn't use the Imbalance
// types (basically for charging fees).
// This should eventually be refactored so that the three type items that do
// depend on the Imbalance type (TransactionPayment, TransferPayment, DustRemoval)
// This should eventually be refactored so that the two type items that do
// depend on the Imbalance type (TransactionPayment, DustRemoval)
// are placed in their own SRML module.
struct ElevatedTrait<T: Subtrait>(T);
impl<T: Subtrait> Clone for ElevatedTrait<T> {
@@ -1106,12 +1116,15 @@ impl<T: Subtrait> frame_system::Trait for ElevatedTrait<T> {
type Lookup = T::Lookup;
type Header = T::Header;
type Event = ();
type BlockHashCount = T::BlockHashCount;
type MaximumBlockWeight = T::MaximumBlockWeight;
type MaximumBlockLength = T::MaximumBlockLength;
type AvailableBlockRatio = T::AvailableBlockRatio;
type BlockHashCount = T::BlockHashCount;
type Version = T::Version;
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type OnReapAccount = ();
}
impl<T: Subtrait> Trait for ElevatedTrait<T> {
type Balance = T::Balance;
@@ -1189,7 +1202,7 @@ where
}
fn deposit_creating(who: &T::AccountId, value: Self::Balance) -> Self::PositiveImbalance {
let (imbalance, _) = Self::make_free_balance_be(who, Self::free_balance(who) + value);
let imbalance = Self::make_free_balance_be(who, Self::free_balance(who) + value);
if let SignedImbalance::Positive(p) = imbalance {
p
} else {
@@ -1201,10 +1214,7 @@ where
fn make_free_balance_be(
who: &T::AccountId,
balance: Self::Balance,
) -> (
SignedImbalance<Self::Balance, Self::PositiveImbalance>,
UpdateBalanceOutcome,
) {
) -> SignedImbalance<Self::Balance, Self::PositiveImbalance> {
let original = <Module<T>>::free_balance(&U::asset_id(), who);
let imbalance = if original <= balance {
SignedImbalance::Positive(PositiveImbalance::new(balance - original))
@@ -1212,7 +1222,7 @@ where
SignedImbalance::Negative(NegativeImbalance::new(original - balance))
};
<Module<T>>::set_free_balance(&U::asset_id(), who, balance);
(imbalance, UpdateBalanceOutcome::Updated)
imbalance
}
fn can_slash(who: &T::AccountId, value: Self::Balance) -> bool {
@@ -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))
}
}
+5 -1
View File
@@ -31,7 +31,7 @@ use frame_support::{parameter_types, impl_outer_event, impl_outer_origin, weight
use super::*;
impl_outer_origin! {
pub enum Origin for Test where system = frame_system {}
pub enum Origin for Test where system = frame_system {}
}
// For testing the module, we construct most of a mock runtime. This means
@@ -62,6 +62,9 @@ impl frame_system::Trait for Test {
type BlockHashCount = BlockHashCount;
type Version = ();
type ModuleToIndex = ();
type AccountData = ();
type OnNewAccount = ();
type OnReapAccount = ();
}
impl Trait for Test {
@@ -77,6 +80,7 @@ mod generic_asset {
use frame_system as system;
impl_outer_event! {
pub enum TestEvent for Test {
system<T>,
generic_asset<T>,
}
}
+2 -2
View File
@@ -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);
});
}
@@ -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);
});
}