seal: Prevent contracts from going below subsistence (#6623)

* seal: Do not allow transfers to bring total balance below subsistence deposit

This also reworks the rent system to take the total balance into account
when evaluating whether the account is above the subsistence deposit.

* Fix nits from review

* Fix typo

* Do not enforce subsistence when called from EOA

* Rename CallOrigin to TransactorKind

* Add debug asserts to check the invariants of a plain account transactor

* Fix typo

Co-authored-by: Sergei Shulepov <sergei@parity.io>

Co-authored-by: Sergei Shulepov <sergei@parity.io>
This commit is contained in:
Alexander Theißen
2020-07-10 14:45:31 +02:00
committed by GitHub
parent 64114267b2
commit efc69d8219
5 changed files with 116 additions and 47 deletions
+10 -7
View File
@@ -797,6 +797,7 @@ fn claim_surcharge(blocks: u64, trigger_call: impl Fn() -> bool, removes: bool)
/// * if balance is reached and balance > subsistence threshold
/// * if allowance is exceeded
/// * if balance is reached and balance < subsistence threshold
/// * this case cannot be triggered by a contract: we check whether a tombstone is left
fn removals(trigger_call: impl Fn() -> bool) {
let (wasm, code_hash) = compile_module::<Test>("set_rent").unwrap();
@@ -898,10 +899,12 @@ fn removals(trigger_call: impl Fn() -> bool) {
.execute_with(|| {
// Create
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
let subsistence_threshold =
Balances::minimum_balance() + <Test as Trait>::TombstoneDeposit::get();
assert_ok!(Contracts::put_code(Origin::signed(ALICE), wasm.clone()));
assert_ok!(Contracts::instantiate(
Origin::signed(ALICE),
50 + Balances::minimum_balance(),
50 + subsistence_threshold,
GAS_LIMIT,
code_hash.into(),
<Test as pallet_balances::Trait>::Balance::from(1_000u32).encode() // rent allowance
@@ -919,7 +922,7 @@ fn removals(trigger_call: impl Fn() -> bool) {
);
assert_eq!(
Balances::free_balance(BOB),
50 + Balances::minimum_balance()
50 + subsistence_threshold,
);
// Transfer funds
@@ -938,23 +941,23 @@ fn removals(trigger_call: impl Fn() -> bool) {
.rent_allowance,
1_000
);
assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance());
assert_eq!(Balances::free_balance(BOB), subsistence_threshold);
// Advance blocks
initialize_block(10);
// Trigger rent through call
assert!(trigger_call());
assert!(ContractInfoOf::<Test>::get(BOB).is_none());
assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance());
assert_matches!(ContractInfoOf::<Test>::get(BOB), Some(ContractInfo::Tombstone(_)));
assert_eq!(Balances::free_balance(BOB), subsistence_threshold);
// Advance blocks
initialize_block(20);
// Trigger rent must have no effect
assert!(trigger_call());
assert!(ContractInfoOf::<Test>::get(BOB).is_none());
assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance());
assert_matches!(ContractInfoOf::<Test>::get(BOB), Some(ContractInfo::Tombstone(_)));
assert_eq!(Balances::free_balance(BOB), subsistence_threshold);
});
}