mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-15 19:41:05 +00:00
Add execute_with to TestExternalities (#3793)
This function executes the given closure in a context where the test externalities are set. This makes the srml tests easier to write, as the test externalities need to be created anyway.
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
|
||||
use super::*;
|
||||
use mock::{Balances, ExtBuilder, Runtime, System, info_from_weight, CALL};
|
||||
use sr_primitives::set_and_run_with_externalities;
|
||||
use support::{
|
||||
assert_noop, assert_ok, assert_err,
|
||||
traits::{LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons,
|
||||
@@ -34,7 +33,7 @@ const ID_3: LockIdentifier = *b"3 ";
|
||||
|
||||
#[test]
|
||||
fn basic_locking_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(&1), 10);
|
||||
Balances::set_lock(ID_1, &1, 9, u64::max_value(), WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
@@ -46,7 +45,7 @@ fn basic_locking_should_work() {
|
||||
|
||||
#[test]
|
||||
fn partial_locking_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
|
||||
});
|
||||
@@ -54,7 +53,7 @@ fn partial_locking_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_removal_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
|
||||
Balances::remove_lock(ID_1, &1);
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
|
||||
@@ -63,7 +62,7 @@ fn lock_removal_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_replacement_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, u64::max_value(), u64::max_value(), WithdrawReasons::all());
|
||||
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
|
||||
@@ -72,7 +71,7 @@ fn lock_replacement_should_work() {
|
||||
|
||||
#[test]
|
||||
fn double_locking_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
|
||||
Balances::set_lock(ID_2, &1, 5, u64::max_value(), WithdrawReasons::all());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1));
|
||||
@@ -81,7 +80,7 @@ fn double_locking_should_work() {
|
||||
|
||||
#[test]
|
||||
fn combination_locking_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, u64::max_value(), 0, WithdrawReasons::none());
|
||||
Balances::set_lock(ID_2, &1, 0, u64::max_value(), WithdrawReasons::none());
|
||||
Balances::set_lock(ID_3, &1, 0, 0, WithdrawReasons::all());
|
||||
@@ -91,7 +90,7 @@ fn combination_locking_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_value_extension_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 5, u64::max_value(), WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6),
|
||||
@@ -112,12 +111,12 @@ fn lock_value_extension_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_reasons_should_work() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true).transaction_fees(0, 1, 0)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.transaction_fees(0, 1, 0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Transfer.into());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 1),
|
||||
@@ -157,13 +156,12 @@ fn lock_reasons_should_work() {
|
||||
info_from_weight(1),
|
||||
0,
|
||||
).is_err());
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_block_number_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 1),
|
||||
@@ -177,7 +175,7 @@ fn lock_block_number_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_block_number_extension_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 10, 2, WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6),
|
||||
@@ -199,7 +197,7 @@ fn lock_block_number_extension_should_work() {
|
||||
|
||||
#[test]
|
||||
fn lock_reasons_extension_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().existential_deposit(1).monied(true).build(), || {
|
||||
ExtBuilder::default().existential_deposit(1).monied(true).build().execute_with(|| {
|
||||
Balances::set_lock(ID_1, &1, 10, 10, WithdrawReason::Transfer.into());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6),
|
||||
@@ -220,14 +218,12 @@ fn lock_reasons_extension_should_work() {
|
||||
|
||||
#[test]
|
||||
fn default_indexing_on_new_accounts_should_not_work2() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.creation_fee(50)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.creation_fee(50)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(Balances::is_dead_account(&5), true); // account 5 should not exist
|
||||
// ext_deposit is 10, value is 9, not satisfies for ext_deposit
|
||||
assert_noop!(
|
||||
@@ -236,18 +232,16 @@ fn default_indexing_on_new_accounts_should_not_work2() {
|
||||
);
|
||||
assert_eq!(Balances::is_dead_account(&5), true); // account 5 should not exist
|
||||
assert_eq!(Balances::free_balance(&1), 100);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserved_balance_should_prevent_reclaim_count() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(256 * 1)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(256 * 1)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
System::inc_account_nonce(&2);
|
||||
assert_eq!(Balances::is_dead_account(&2), false);
|
||||
assert_eq!(Balances::is_dead_account(&5), true);
|
||||
@@ -274,14 +268,13 @@ fn reserved_balance_should_prevent_reclaim_count() {
|
||||
assert_ok!(Balances::transfer(Some(4).into(), 6, 256 * 1 + 0x69));
|
||||
assert_eq!(Balances::total_balance(&6), 256 * 1 + 0x69);
|
||||
assert_eq!(Balances::is_dead_account(&6), false);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn reward_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().monied(true).build(), || {
|
||||
ExtBuilder::default().monied(true).build().execute_with(|| {
|
||||
assert_eq!(Balances::total_balance(&1), 10);
|
||||
assert_ok!(Balances::deposit_into_existing(&1, 10).map(drop));
|
||||
assert_eq!(Balances::total_balance(&1), 20);
|
||||
@@ -291,12 +284,11 @@ fn reward_should_work() {
|
||||
|
||||
#[test]
|
||||
fn dust_account_removal_should_work() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
System::inc_account_nonce(&2);
|
||||
assert_eq!(System::account_nonce(&2), 1);
|
||||
assert_eq!(Balances::total_balance(&2), 2000);
|
||||
@@ -305,19 +297,17 @@ fn dust_account_removal_should_work() {
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
assert_eq!(Balances::total_balance(&5), 1901);
|
||||
assert_eq!(System::account_nonce(&2), 0);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dust_account_removal_should_work2() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.creation_fee(50)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(100)
|
||||
.creation_fee(50)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
System::inc_account_nonce(&2);
|
||||
assert_eq!(System::account_nonce(&2), 1);
|
||||
assert_eq!(Balances::total_balance(&2), 2000);
|
||||
@@ -326,13 +316,12 @@ fn dust_account_removal_should_work2() {
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
assert_eq!(Balances::total_balance(&5), 1851);
|
||||
assert_eq!(System::account_nonce(&2), 0);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_works() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 42);
|
||||
assert_eq!(Balances::free_balance(&1), 42);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
@@ -345,7 +334,7 @@ fn balance_works() {
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_works() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::transfer(Some(1).into(), 2, 69));
|
||||
assert_eq!(Balances::total_balance(&1), 42);
|
||||
@@ -355,7 +344,7 @@ fn balance_transfer_works() {
|
||||
|
||||
#[test]
|
||||
fn force_transfer_works() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_noop!(
|
||||
Balances::force_transfer(Some(2).into(), 1, 2, 69),
|
||||
@@ -369,7 +358,7 @@ fn force_transfer_works() {
|
||||
|
||||
#[test]
|
||||
fn reserving_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
|
||||
assert_eq!(Balances::total_balance(&1), 111);
|
||||
@@ -386,7 +375,7 @@ fn reserving_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_when_reserved_should_not_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
assert_noop!(
|
||||
@@ -398,7 +387,7 @@ fn balance_transfer_when_reserved_should_not_work() {
|
||||
|
||||
#[test]
|
||||
fn deducting_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
assert_eq!(Balances::free_balance(&1), 42);
|
||||
@@ -407,7 +396,7 @@ fn deducting_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn refunding_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 42);
|
||||
Balances::set_reserved_balance(&1, 69);
|
||||
Balances::unreserve(&1, 69);
|
||||
@@ -418,7 +407,7 @@ fn refunding_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn slashing_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
assert!(Balances::slash(&1, 69).1.is_zero());
|
||||
@@ -430,7 +419,7 @@ fn slashing_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn slashing_incomplete_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 42);
|
||||
assert_ok!(Balances::reserve(&1, 21));
|
||||
assert_eq!(Balances::slash(&1, 69).1, 27);
|
||||
@@ -442,7 +431,7 @@ fn slashing_incomplete_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn unreserving_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 111));
|
||||
Balances::unreserve(&1, 42);
|
||||
@@ -453,7 +442,7 @@ fn unreserving_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn slashing_reserved_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 111));
|
||||
assert_eq!(Balances::slash_reserved(&1, 42).1, 0);
|
||||
@@ -465,7 +454,7 @@ fn slashing_reserved_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn slashing_incomplete_reserved_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 42));
|
||||
assert_eq!(Balances::slash_reserved(&1, 69).1, 27);
|
||||
@@ -477,7 +466,7 @@ fn slashing_incomplete_reserved_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn transferring_reserved_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 1);
|
||||
assert_ok!(Balances::reserve(&1, 110));
|
||||
@@ -491,7 +480,7 @@ fn transferring_reserved_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn transferring_reserved_balance_to_nonexistent_should_fail() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 111));
|
||||
assert_noop!(Balances::repatriate_reserved(&1, &2, 42), "beneficiary account must pre-exist");
|
||||
@@ -500,7 +489,7 @@ fn transferring_reserved_balance_to_nonexistent_should_fail() {
|
||||
|
||||
#[test]
|
||||
fn transferring_incomplete_reserved_balance_should_work() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 1);
|
||||
assert_ok!(Balances::reserve(&1, 41));
|
||||
@@ -514,7 +503,7 @@ fn transferring_incomplete_reserved_balance_should_work() {
|
||||
|
||||
#[test]
|
||||
fn transferring_too_high_value_should_not_panic() {
|
||||
set_and_run_with_externalities(&mut ExtBuilder::default().build(), || {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
<FreeBalance<Runtime>>::insert(1, u64::max_value());
|
||||
<FreeBalance<Runtime>>::insert(2, 1);
|
||||
|
||||
@@ -530,89 +519,76 @@ fn transferring_too_high_value_should_not_panic() {
|
||||
|
||||
#[test]
|
||||
fn account_create_on_free_too_low_with_other() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default().existential_deposit(100).build(),
|
||||
|| {
|
||||
let _ = Balances::deposit_creating(&1, 100);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 100);
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 100);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 100);
|
||||
|
||||
// No-op.
|
||||
let _ = Balances::deposit_creating(&2, 50);
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 100);
|
||||
}
|
||||
)
|
||||
// No-op.
|
||||
let _ = Balances::deposit_creating(&2, 50);
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 100);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn account_create_on_free_too_low() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default().existential_deposit(100).build(),
|
||||
|| {
|
||||
// No-op.
|
||||
let _ = Balances::deposit_creating(&2, 50);
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
|
||||
}
|
||||
)
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
// No-op.
|
||||
let _ = Balances::deposit_creating(&2, 50);
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_removal_on_free_too_low() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default().existential_deposit(100).build(),
|
||||
|| {
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
|
||||
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
|
||||
|
||||
// Setup two accounts with free balance above the existential threshold.
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 110);
|
||||
// Setup two accounts with free balance above the existential threshold.
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 110);
|
||||
|
||||
assert_eq!(Balances::free_balance(&1), 110);
|
||||
assert_eq!(Balances::free_balance(&2), 110);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 220);
|
||||
assert_eq!(Balances::free_balance(&1), 110);
|
||||
assert_eq!(Balances::free_balance(&2), 110);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 220);
|
||||
|
||||
// Transfer funds from account 1 of such amount that after this transfer
|
||||
// the balance of account 1 will be below the existential threshold.
|
||||
// This should lead to the removal of all balance of this account.
|
||||
assert_ok!(Balances::transfer(Some(1).into(), 2, 20));
|
||||
// Transfer funds from account 1 of such amount that after this transfer
|
||||
// the balance of account 1 will be below the existential threshold.
|
||||
// This should lead to the removal of all balance of this account.
|
||||
assert_ok!(Balances::transfer(Some(1).into(), 2, 20));
|
||||
|
||||
// Verify free balance removal of account 1.
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(Balances::free_balance(&2), 130);
|
||||
// Verify free balance removal of account 1.
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(Balances::free_balance(&2), 130);
|
||||
|
||||
// Verify that TotalIssuance tracks balance removal when free balance is too low.
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 130);
|
||||
},
|
||||
);
|
||||
// Verify that TotalIssuance tracks balance removal when free balance is too low.
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 130);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_overflow_isnt_exploitable() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default().creation_fee(50).build(),
|
||||
|| {
|
||||
// Craft a value that will overflow if summed with `creation_fee`.
|
||||
let evil_value = u64::max_value() - 49;
|
||||
ExtBuilder::default().creation_fee(50).build().execute_with(|| {
|
||||
// Craft a value that will overflow if summed with `creation_fee`.
|
||||
let evil_value = u64::max_value() - 49;
|
||||
|
||||
assert_err!(
|
||||
Balances::transfer(Some(1).into(), 5, evil_value),
|
||||
"got overflow after adding a fee to value",
|
||||
);
|
||||
}
|
||||
);
|
||||
assert_err!(
|
||||
Balances::transfer(Some(1).into(), 5, evil_value),
|
||||
"got overflow after adding a fee to value",
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_vesting_status() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(256)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(256)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(System::block_number(), 1);
|
||||
let user1_free_balance = Balances::free_balance(&1);
|
||||
let user2_free_balance = Balances::free_balance(&2);
|
||||
@@ -663,19 +639,17 @@ fn check_vesting_status() {
|
||||
assert_eq!(Balances::vesting_balance(&2), 0); // Account 2 has fully vested by block 30
|
||||
assert_eq!(Balances::vesting_balance(&12), 0); // Account 2 has fully vested by block 30
|
||||
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unvested_balance_should_not_transfer() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(System::block_number(), 1);
|
||||
let user1_free_balance = Balances::free_balance(&1);
|
||||
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
|
||||
@@ -685,38 +659,34 @@ fn unvested_balance_should_not_transfer() {
|
||||
Balances::transfer(Some(1).into(), 2, 56),
|
||||
"vesting balance too high to send value",
|
||||
); // Account 1 cannot send more than vested amount
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vested_balance_should_transfer() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(System::block_number(), 1);
|
||||
let user1_free_balance = Balances::free_balance(&1);
|
||||
assert_eq!(user1_free_balance, 100); // Account 1 has free balance
|
||||
// Account 1 has only 5 units vested at block 1 (plus 50 unvested)
|
||||
assert_eq!(Balances::vesting_balance(&1), 45);
|
||||
assert_ok!(Balances::transfer(Some(1).into(), 2, 55));
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extra_balance_should_transfer() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(System::block_number(), 1);
|
||||
assert_ok!(Balances::transfer(Some(3).into(), 1, 100));
|
||||
assert_ok!(Balances::transfer(Some(3).into(), 2, 100));
|
||||
@@ -734,19 +704,17 @@ fn extra_balance_should_transfer() {
|
||||
// Account 2 has no units vested at block 1, but gained 100
|
||||
assert_eq!(Balances::vesting_balance(&2), 200);
|
||||
assert_ok!(Balances::transfer(Some(2).into(), 3, 100)); // Account 2 can send extra units gained
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn liquid_funds_should_transfer_with_delayed_vesting() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(256)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(256)
|
||||
.monied(true)
|
||||
.vesting(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(System::block_number(), 1);
|
||||
let user12_free_balance = Balances::free_balance(&12);
|
||||
|
||||
@@ -764,62 +732,64 @@ fn liquid_funds_should_transfer_with_delayed_vesting() {
|
||||
|
||||
// Account 12 can still send liquid funds
|
||||
assert_ok!(Balances::transfer(Some(12).into(), 3, 256 * 5));
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_extension_take_fees_work() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.transaction_fees(10, 1, 5)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(10)
|
||||
.transaction_fees(10, 1, 5)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let len = 10;
|
||||
assert!(TakeFees::<Runtime>::from(0).pre_dispatch(&1, CALL, info_from_weight(5), len).is_ok());
|
||||
assert!(
|
||||
TakeFees::<Runtime>::from(0)
|
||||
.pre_dispatch(&1, CALL, info_from_weight(5), len)
|
||||
.is_ok()
|
||||
);
|
||||
assert_eq!(Balances::free_balance(&1), 100 - 20 - 25);
|
||||
assert!(TakeFees::<Runtime>::from(5 /* tipped */).pre_dispatch(&1, CALL, info_from_weight(3), len).is_ok());
|
||||
assert!(
|
||||
TakeFees::<Runtime>::from(5 /* tipped */)
|
||||
.pre_dispatch(&1, CALL, info_from_weight(3), len)
|
||||
.is_ok()
|
||||
);
|
||||
assert_eq!(Balances::free_balance(&1), 100 - 20 - 25 - 20 - 5 - 15);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn signed_extension_take_fees_is_bounded() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.existential_deposit(1000)
|
||||
.transaction_fees(0, 0, 1)
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1000)
|
||||
.transaction_fees(0, 0, 1)
|
||||
.monied(true)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
use sr_primitives::weights::Weight;
|
||||
|
||||
// maximum weight possible
|
||||
assert!(TakeFees::<Runtime>::from(0).pre_dispatch(&1, CALL, info_from_weight(Weight::max_value()), 10).is_ok());
|
||||
assert!(
|
||||
TakeFees::<Runtime>::from(0)
|
||||
.pre_dispatch(&1, CALL, info_from_weight(Weight::max_value()), 10)
|
||||
.is_ok()
|
||||
);
|
||||
// fee will be proportional to what is the actual maximum weight in the runtime.
|
||||
assert_eq!(
|
||||
Balances::free_balance(&1),
|
||||
(10000 - <Runtime as system::Trait>::MaximumBlockWeight::get()) as u64
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn burn_must_work() {
|
||||
set_and_run_with_externalities(
|
||||
&mut ExtBuilder::default()
|
||||
.monied(true)
|
||||
.build(),
|
||||
|| {
|
||||
let init_total_issuance = Balances::total_issuance();
|
||||
let imbalance = Balances::burn(10);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance - 10);
|
||||
drop(imbalance);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance);
|
||||
}
|
||||
);
|
||||
ExtBuilder::default().monied(true).build().execute_with(|| {
|
||||
let init_total_issuance = Balances::total_issuance();
|
||||
let imbalance = Balances::burn(10);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance - 10);
|
||||
drop(imbalance);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user