|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +#![cfg(test)] |
| 19 | + |
| 20 | +use crate::{ |
| 21 | + system::AccountInfo, |
| 22 | + tests::{ensure_ti_valid, Balances, ExtBuilder, System, Test, TestId, UseSystem}, |
| 23 | + AccountData, ExtraFlags, TotalIssuance, |
| 24 | +}; |
| 25 | +use frame_support::{ |
| 26 | + assert_noop, assert_ok, hypothetically, |
| 27 | + traits::{ |
| 28 | + fungible::{Mutate, MutateHold}, |
| 29 | + tokens::Precision, |
| 30 | + }, |
| 31 | +}; |
| 32 | +use sp_runtime::DispatchError; |
| 33 | + |
| 34 | +/// There are some accounts that have one consumer ref too few. These accounts are at risk of losing |
| 35 | +/// their held (reserved) balance. They do not just lose it - it is also not accounted for in the |
| 36 | +/// Total Issuance. Here we test the case that the account does not reap in such a case, but gets |
| 37 | +/// one consumer ref for its reserved balance. |
| 38 | +#[test] |
| 39 | +fn regression_historic_acc_does_not_evaporate_reserve() { |
| 40 | + ExtBuilder::default().build_and_execute_with(|| { |
| 41 | + UseSystem::set(true); |
| 42 | + let (alice, bob) = (0, 1); |
| 43 | + // Alice is in a bad state with consumer == 0 && reserved > 0: |
| 44 | + Balances::set_balance(&alice, 100); |
| 45 | + TotalIssuance::<Test>::put(100); |
| 46 | + ensure_ti_valid(); |
| 47 | + |
| 48 | + assert_ok!(Balances::hold(&TestId::Foo, &alice, 10)); |
| 49 | + // This is the issue of the account: |
| 50 | + System::dec_consumers(&alice); |
| 51 | + |
| 52 | + assert_eq!( |
| 53 | + System::account(&alice), |
| 54 | + AccountInfo { |
| 55 | + data: AccountData { |
| 56 | + free: 90, |
| 57 | + reserved: 10, |
| 58 | + frozen: 0, |
| 59 | + flags: ExtraFlags(1u128 << 127), |
| 60 | + }, |
| 61 | + nonce: 0, |
| 62 | + consumers: 0, // should be 1 on a good acc |
| 63 | + providers: 1, |
| 64 | + sufficients: 0, |
| 65 | + } |
| 66 | + ); |
| 67 | + |
| 68 | + ensure_ti_valid(); |
| 69 | + |
| 70 | + // Reaping the account is prevented by the new logic: |
| 71 | + assert_noop!( |
| 72 | + Balances::transfer_allow_death(Some(alice).into(), bob, 90), |
| 73 | + DispatchError::ConsumerRemaining |
| 74 | + ); |
| 75 | + assert_noop!( |
| 76 | + Balances::transfer_all(Some(alice).into(), bob, false), |
| 77 | + DispatchError::ConsumerRemaining |
| 78 | + ); |
| 79 | + |
| 80 | + // normal transfers still work: |
| 81 | + hypothetically!({ |
| 82 | + assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40)); |
| 83 | + // Alice got back her consumer ref: |
| 84 | + assert_eq!(System::consumers(&alice), 1); |
| 85 | + ensure_ti_valid(); |
| 86 | + }); |
| 87 | + hypothetically!({ |
| 88 | + assert_ok!(Balances::transfer_all(Some(alice).into(), bob, true)); |
| 89 | + // Alice got back her consumer ref: |
| 90 | + assert_eq!(System::consumers(&alice), 1); |
| 91 | + ensure_ti_valid(); |
| 92 | + }); |
| 93 | + |
| 94 | + // un-reserving all does not add a consumer ref: |
| 95 | + hypothetically!({ |
| 96 | + assert_ok!(Balances::release(&TestId::Foo, &alice, 10, Precision::Exact)); |
| 97 | + assert_eq!(System::consumers(&alice), 0); |
| 98 | + assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40)); |
| 99 | + assert_eq!(System::consumers(&alice), 0); |
| 100 | + ensure_ti_valid(); |
| 101 | + }); |
| 102 | + // un-reserving some does add a consumer ref: |
| 103 | + hypothetically!({ |
| 104 | + assert_ok!(Balances::release(&TestId::Foo, &alice, 5, Precision::Exact)); |
| 105 | + assert_eq!(System::consumers(&alice), 1); |
| 106 | + assert_ok!(Balances::transfer_keep_alive(Some(alice).into(), bob, 40)); |
| 107 | + assert_eq!(System::consumers(&alice), 1); |
| 108 | + ensure_ti_valid(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +} |
0 commit comments