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
+59 -23
View File
@@ -19,49 +19,85 @@
#![cfg(test)]
use super::*;
use crate::mock::{Indices, new_test_ext, make_account, kill_account, TestIsDeadAccount};
use super::mock::*;
use frame_support::{assert_ok, assert_noop};
use pallet_balances::Error as BalancesError;
#[test]
fn claiming_should_work() {
new_test_ext().execute_with(|| {
assert_noop!(Indices::claim(Some(0).into(), 0), BalancesError::<Test, _>::InsufficientBalance);
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_noop!(Indices::claim(Some(2).into(), 0), Error::<Test>::InUse);
assert_eq!(Balances::reserved_balance(1), 1);
});
}
#[test]
fn freeing_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_ok!(Indices::claim(Some(2).into(), 1));
assert_noop!(Indices::free(Some(0).into(), 0), Error::<Test>::NotOwner);
assert_noop!(Indices::free(Some(1).into(), 1), Error::<Test>::NotOwner);
assert_noop!(Indices::free(Some(1).into(), 2), Error::<Test>::NotAssigned);
assert_ok!(Indices::free(Some(1).into(), 0));
assert_eq!(Balances::reserved_balance(1), 0);
assert_noop!(Indices::free(Some(1).into(), 0), Error::<Test>::NotAssigned);
});
}
#[test]
fn indexing_lookup_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_ok!(Indices::claim(Some(2).into(), 1));
assert_eq!(Indices::lookup_index(0), Some(1));
assert_eq!(Indices::lookup_index(1), Some(2));
assert_eq!(Indices::lookup_index(2), Some(3));
assert_eq!(Indices::lookup_index(3), Some(4));
assert_eq!(Indices::lookup_index(4), None);
assert_eq!(Indices::lookup_index(2), None);
});
}
#[test]
fn default_indexing_on_new_accounts_should_work() {
fn reclaim_index_on_accounts_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(Indices::lookup_index(4), None);
make_account(5);
assert_eq!(Indices::lookup_index(4), Some(5));
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_ok!(Indices::free(Some(1).into(), 0));
assert_ok!(Indices::claim(Some(2).into(), 0));
assert_eq!(Indices::lookup_index(0), Some(2));
assert_eq!(Balances::reserved_balance(2), 1);
});
}
#[test]
fn reclaim_indexing_on_new_accounts_should_work() {
fn transfer_index_on_accounts_should_work() {
new_test_ext().execute_with(|| {
assert_eq!(Indices::lookup_index(1), Some(2));
assert_eq!(Indices::lookup_index(4), None);
kill_account(2); // index 1 no longer locked to id 2
make_account(1 + 256); // id 257 takes index 1.
assert_eq!(Indices::lookup_index(1), Some(257));
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_noop!(Indices::transfer(Some(1).into(), 2, 1), Error::<Test>::NotAssigned);
assert_noop!(Indices::transfer(Some(2).into(), 3, 0), Error::<Test>::NotOwner);
assert_ok!(Indices::transfer(Some(1).into(), 3, 0));
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::reserved_balance(3), 1);
assert_eq!(Indices::lookup_index(0), Some(3));
});
}
#[test]
fn alive_account_should_prevent_reclaim() {
fn force_transfer_index_on_preowned_should_work() {
new_test_ext().execute_with(|| {
assert!(!TestIsDeadAccount::is_dead_account(&2));
assert_eq!(Indices::lookup_index(1), Some(2));
assert_eq!(Indices::lookup_index(4), None);
make_account(1 + 256); // id 257 takes index 1.
assert_eq!(Indices::lookup_index(4), Some(257));
assert_ok!(Indices::claim(Some(1).into(), 0));
assert_ok!(Indices::force_transfer(Origin::ROOT, 3, 0));
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::reserved_balance(3), 0);
assert_eq!(Indices::lookup_index(0), Some(3));
});
}
#[test]
fn force_transfer_index_on_free_should_work() {
new_test_ext().execute_with(|| {
assert_ok!(Indices::force_transfer(Origin::ROOT, 3, 0));
assert_eq!(Balances::reserved_balance(3), 0);
assert_eq!(Indices::lookup_index(0), Some(3));
});
}