#![cfg(test)]
use crate::{
system::AccountInfo,
tests::{
ensure_ti_valid, get_test_account, Balances, ExtBuilder, System, Test, TestId, UseSystem,
},
AccountData, ExtraFlags, TotalIssuance,
};
use frame_support::{
assert_noop, assert_ok, hypothetically,
traits::{
fungible::{Mutate, MutateHold},
tokens::Precision,
},
};
use sp_runtime::DispatchError;
#[test]
fn regression_historic_acc_does_not_evaporate_reserve() {
ExtBuilder::default().build_and_execute_with(|| {
UseSystem::set(true);
let (alice, bob) = (0, 1);
Balances::set_balance(&alice, 100);
TotalIssuance::<Test>::put(100);
ensure_ti_valid();
assert_ok!(Balances::hold(&TestId::Foo, &alice, 10));
System::dec_consumers(&alice);
assert_eq!(
get_test_account(alice),
AccountInfo {
data: AccountData {
free: 90,
reserved: 10,
frozen: 0,
flags: ExtraFlags(1u128 << 127),
},
nonce: 0,
consumers: 0, providers: 1,
sufficients: 0,
}
);
ensure_ti_valid();
assert_noop!(
Balances::transfer_allow_death(Some(alice).into(), bob, 90),
DispatchError::ConsumerRemaining
);
assert_noop!(
Balances::transfer_all(Some(alice).into(), bob, false),
DispatchError::ConsumerRemaining
);
hypothetically!({
assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40));
assert_eq!(System::consumers(&alice), 1);
ensure_ti_valid();
});
hypothetically!({
assert_ok!(Balances::transfer_all(Some(alice).into(), bob, true));
assert_eq!(System::consumers(&alice), 1);
ensure_ti_valid();
});
hypothetically!({
assert_ok!(Balances::release(&TestId::Foo, &alice, 10, Precision::Exact));
assert_eq!(System::consumers(&alice), 0);
assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40));
assert_eq!(System::consumers(&alice), 0);
ensure_ti_valid();
});
hypothetically!({
assert_ok!(Balances::release(&TestId::Foo, &alice, 5, Precision::Exact));
assert_eq!(System::consumers(&alice), 1);
assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40));
assert_eq!(System::consumers(&alice), 1);
ensure_ti_valid();
});
});
}
#[cfg(feature = "try-runtime")]
#[test]
fn try_state_works() {
use crate::{Config, Freezes, Holds};
use frame_support::{
storage,
traits::{Get, Hooks, VariantCount},
};
ExtBuilder::default().auto_try_state(false).build_and_execute_with(|| {
storage::unhashed::put(
&Holds::<Test>::hashed_key_for(1),
&vec![0u8; <Test as Config>::RuntimeHoldReason::VARIANT_COUNT as usize + 1],
);
assert!(format!("{:?}", Balances::try_state(0).unwrap_err())
.contains("Found `Hold` with too many elements"));
});
ExtBuilder::default().auto_try_state(false).build_and_execute_with(|| {
let max_freezes: u32 = <Test as Config>::MaxFreezes::get();
storage::unhashed::put(
&Freezes::<Test>::hashed_key_for(1),
&vec![0u8; max_freezes as usize + 1],
);
assert!(format!("{:?}", Balances::try_state(0).unwrap_err())
.contains("Found `Freeze` with too many elements"));
});
}