pallet-balances 51.0.2

FRAME pallet to manage balances
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests.

#![cfg(test)]

use crate::{
	self as pallet_balances, AccountData, Config, CreditOf, Error, Pallet, TotalIssuance,
	DEFAULT_ADDRESS_URI,
};
use codec::{Decode, DecodeWithMemTracking, Encode, MaxEncodedLen};
use frame_support::{
	assert_err, assert_noop, assert_ok, assert_storage_noop, derive_impl,
	dispatch::{DispatchInfo, GetDispatchInfo},
	parameter_types,
	traits::{
		fungible, ConstU32, ConstU8, Imbalance as ImbalanceT, LockIdentifier, LockableCurrency,
		OnUnbalanced, StorageMapShim, StoredMap, VariantCount, VariantCountOf,
		WhitelistedStorageKeys, WithdrawReasons,
	},
	weights::{IdentityFee, Weight},
};
use frame_system::{self as system, RawOrigin};
use pallet_transaction_payment::{ChargeTransactionPayment, FungibleAdapter, Multiplier};
use scale_info::TypeInfo;
use sp_core::{hexdisplay::HexDisplay, sr25519::Pair as SrPair, Pair};
use sp_io;
use sp_runtime::{
	traits::{BadOrigin, Zero},
	ArithmeticError, BuildStorage, Debug, DispatchError, DispatchResult, FixedPointNumber,
	TokenError,
};
use std::collections::BTreeSet;

mod consumer_limit_tests;
mod currency_tests;
mod dispatchable_tests;
mod fungible_and_currency;
mod fungible_conformance_tests;
mod fungible_tests;
mod general_tests;
mod reentrancy_tests;

type Block = frame_system::mocking::MockBlock<Test>;

#[derive(
	Encode,
	Decode,
	DecodeWithMemTracking,
	Copy,
	Clone,
	Eq,
	PartialEq,
	Ord,
	PartialOrd,
	MaxEncodedLen,
	TypeInfo,
	Debug,
)]
pub enum TestId {
	Foo,
	Bar,
	Baz,
}

impl VariantCount for TestId {
	const VARIANT_COUNT: u32 = 3;
}

pub(crate) type AccountId = <Test as frame_system::Config>::AccountId;
pub(crate) type Balance = <Test as Config>::Balance;

frame_support::construct_runtime!(
	pub enum Test {
		System: frame_system,
		Balances: pallet_balances,
		TransactionPayment: pallet_transaction_payment,
	}
);

parameter_types! {
	pub BlockWeights: frame_system::limits::BlockWeights =
		frame_system::limits::BlockWeights::simple_max(
			frame_support::weights::Weight::from_parts(1024, u64::MAX),
		);
	pub static ExistentialDeposit: u64 = 1;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
	type Block = Block;
	type AccountData = super::AccountData<u64>;
}

#[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)]
impl pallet_transaction_payment::Config for Test {
	type RuntimeEvent = RuntimeEvent;
	type OnChargeTransaction = FungibleAdapter<Pallet<Test>, ()>;
	type OperationalFeeMultiplier = ConstU8<5>;
	type WeightToFee = IdentityFee<u64>;
	type LengthToFee = IdentityFee<u64>;
}

parameter_types! {
	pub FooReason: TestId = TestId::Foo;
}

#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl Config for Test {
	type DustRemoval = DustTrap;
	type ExistentialDeposit = ExistentialDeposit;
	type AccountStore = TestAccountStore;
	type MaxReserves = ConstU32<2>;
	type ReserveIdentifier = TestId;
	type RuntimeHoldReason = TestId;
	type RuntimeFreezeReason = TestId;
	type FreezeIdentifier = TestId;
	type MaxFreezes = VariantCountOf<TestId>;
}

#[derive(Clone)]
pub struct ExtBuilder {
	existential_deposit: u64,
	monied: bool,
	dust_trap: Option<u64>,
	reentrant_dust_action: Option<ReentrantDustAction>,
}
impl Default for ExtBuilder {
	fn default() -> Self {
		Self { existential_deposit: 1, monied: false, dust_trap: None, reentrant_dust_action: None }
	}
}
impl ExtBuilder {
	pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
		self.existential_deposit = existential_deposit;
		self
	}
	pub fn monied(mut self, monied: bool) -> Self {
		self.monied = monied;
		if self.existential_deposit == 0 {
			self.existential_deposit = 1;
		}
		self
	}
	pub fn dust_trap(mut self, account: u64) -> Self {
		self.dust_trap = Some(account);
		self
	}
	pub fn reentrant_dust_hold(mut self, account: u64) -> Self {
		self.reentrant_dust_action = Some(ReentrantDustAction::Hold(account));
		self
	}
	pub fn reentrant_dust_freeze(mut self, account: u64) -> Self {
		self.reentrant_dust_action = Some(ReentrantDustAction::Freeze(account));
		self
	}
	pub fn reentrant_dust_lock(mut self, account: u64) -> Self {
		self.reentrant_dust_action = Some(ReentrantDustAction::Lock(account));
		self
	}
	#[cfg(feature = "try-runtime")]
	pub fn auto_try_state(self, auto_try_state: bool) -> Self {
		AutoTryState::set(auto_try_state);
		self
	}
	pub fn set_associated_consts(&self) {
		DUST_TRAP_TARGET.with(|v| v.replace(self.dust_trap));
		REENTRANT_DUST_ACTION_VALUE.with(|v| v.replace(self.reentrant_dust_action));
		EXISTENTIAL_DEPOSIT.with(|v| v.replace(self.existential_deposit));
	}
	pub fn build(self) -> sp_io::TestExternalities {
		self.set_associated_consts();
		let mut t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
		pallet_balances::GenesisConfig::<Test> {
			balances: if self.monied {
				vec![
					(1, 10 * self.existential_deposit),
					(2, 20 * self.existential_deposit),
					(3, 30 * self.existential_deposit),
					(4, 40 * self.existential_deposit),
					(12, 10 * self.existential_deposit),
				]
			} else {
				vec![]
			},
			dev_accounts: Some((
				1000,
				self.existential_deposit,
				Some(DEFAULT_ADDRESS_URI.to_string()),
			)),
		}
		.assimilate_storage(&mut t)
		.unwrap();

		let mut ext = sp_io::TestExternalities::new(t);
		ext.execute_with(|| System::set_block_number(1));
		ext
	}
	pub fn build_and_execute_with(self, f: impl Fn()) {
		let other = self.clone();
		UseSystem::set(false);
		other.build().execute_with(|| {
			f();
			if AutoTryState::get() {
				Balances::do_try_state(System::block_number()).unwrap();
			}
		});
		UseSystem::set(true);
		self.build().execute_with(|| {
			f();
			if AutoTryState::get() {
				Balances::do_try_state(System::block_number()).unwrap();
			}
		});
	}
}

#[derive(Clone, Copy)]
enum ReentrantDustAction {
	Hold(u64),
	Freeze(u64),
	Lock(u64),
}

const REENTRANT_LOCK_ID: LockIdentifier = *b"RELOCK__";

parameter_types! {
	static DustTrapTarget: Option<u64> = None;
	static ReentrantDustActionValue: Option<ReentrantDustAction> = None;
}

pub struct DustTrap;

impl OnUnbalanced<CreditOf<Test, ()>> for DustTrap {
	fn on_nonzero_unbalanced(amount: CreditOf<Test, ()>) {
		match ReentrantDustActionValue::get() {
			Some(ReentrantDustAction::Hold(account)) => {
				<Balances as fungible::Mutate<_>>::mint_into(&account, 20)
					.expect("reentrant mint should succeed");
				<Balances as fungible::MutateHold<_>>::hold(&TestId::Bar, &account, 7)
					.expect("reentrant hold should succeed");
			},
			Some(ReentrantDustAction::Freeze(account)) => {
				<Balances as fungible::MutateFreeze<_>>::set_freeze(&TestId::Bar, &account, 7)
					.expect("reentrant freeze should succeed");
			},
			Some(ReentrantDustAction::Lock(account)) => {
				<Balances as fungible::Mutate<_>>::mint_into(&account, 20)
					.expect("reentrant mint should succeed");
				<Balances as LockableCurrency<_>>::set_lock(
					REENTRANT_LOCK_ID,
					&account,
					7,
					WithdrawReasons::all(),
				);
			},
			None => {},
		}

		match DustTrapTarget::get() {
			None => drop(amount),
			Some(a) => {
				let result = <Balances as fungible::Balanced<_>>::resolve(&a, amount);
				debug_assert!(result.is_ok());
			},
		}
	}
}

parameter_types! {
	pub static UseSystem: bool = false;
	pub static AutoTryState: bool = true;
}

type BalancesAccountStore = StorageMapShim<super::Account<Test>, u64, super::AccountData<u64>>;
type SystemAccountStore = frame_system::Pallet<Test>;

pub struct TestAccountStore;
impl StoredMap<u64, super::AccountData<u64>> for TestAccountStore {
	fn get(k: &u64) -> super::AccountData<u64> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::get(k)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::get(k)
		}
	}
	fn try_mutate_exists<R, E: From<DispatchError>>(
		k: &u64,
		f: impl FnOnce(&mut Option<super::AccountData<u64>>) -> Result<R, E>,
	) -> Result<R, E> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::try_mutate_exists(k, f)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::try_mutate_exists(k, f)
		}
	}
	fn mutate<R>(
		k: &u64,
		f: impl FnOnce(&mut super::AccountData<u64>) -> R,
	) -> Result<R, DispatchError> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::mutate(k, f)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::mutate(k, f)
		}
	}
	fn mutate_exists<R>(
		k: &u64,
		f: impl FnOnce(&mut Option<super::AccountData<u64>>) -> R,
	) -> Result<R, DispatchError> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::mutate_exists(k, f)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::mutate_exists(k, f)
		}
	}
	fn insert(k: &u64, t: super::AccountData<u64>) -> Result<(), DispatchError> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::insert(k, t)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::insert(k, t)
		}
	}
	fn remove(k: &u64) -> Result<(), DispatchError> {
		if UseSystem::get() {
			<SystemAccountStore as StoredMap<_, _>>::remove(k)
		} else {
			<BalancesAccountStore as StoredMap<_, _>>::remove(k)
		}
	}
}

pub fn events() -> Vec<RuntimeEvent> {
	let evt = System::events().into_iter().map(|evt| evt.event).collect::<Vec<_>>();
	System::reset_events();
	evt
}

/// create a transaction info struct from weight. Handy to avoid building the whole struct.
pub fn info_from_weight(w: Weight) -> DispatchInfo {
	DispatchInfo { call_weight: w, ..Default::default() }
}

/// Check that the total-issuance matches the sum of all accounts' total balances.
pub fn ensure_ti_valid() {
	let mut sum = 0;

	// Fetch the dev accounts from Account Storage.
	let dev_accounts = (1000, EXISTENTIAL_DEPOSIT, DEFAULT_ADDRESS_URI.to_string());
	let (num_accounts, _balance, ref derivation) = dev_accounts;

	// Generate the dev account public keys.
	let dev_account_ids: Vec<_> = (0..num_accounts)
		.map(|index| {
			let derivation_string = derivation.replace("{}", &index.to_string());
			let pair: SrPair =
				Pair::from_string(&derivation_string, None).expect("Invalid derivation string");
			<crate::tests::Test as frame_system::Config>::AccountId::decode(
				&mut &pair.public().encode()[..],
			)
			.unwrap()
		})
		.collect();

	// Iterate over all account keys (i.e., the account IDs).
	for acc in frame_system::Account::<Test>::iter_keys() {
		// Skip dev accounts by checking if the account is in the dev_account_ids list.
		// This also proves dev_accounts exists in storage.
		if dev_account_ids.contains(&acc) {
			continue;
		}

		// Check if we are using the system pallet or some other custom storage for accounts.
		if UseSystem::get() {
			let data = frame_system::Pallet::<Test>::account(acc);
			sum += data.data.total();
		} else {
			let data = crate::Account::<Test>::get(acc);
			sum += data.total();
		}
	}

	// Ensure the total issuance matches the sum of the account balances
	assert_eq!(TotalIssuance::<Test>::get(), sum, "Total Issuance is incorrect");
}

#[test]
fn weights_sane() {
	let info = crate::Call::<Test>::transfer_allow_death { dest: 10, value: 4 }.get_dispatch_info();
	assert_eq!(<() as crate::WeightInfo>::transfer_allow_death(), info.call_weight);

	let info = crate::Call::<Test>::force_unreserve { who: 10, amount: 4 }.get_dispatch_info();
	assert_eq!(<() as crate::WeightInfo>::force_unreserve(), info.call_weight);
}

#[test]
fn check_whitelist() {
	let whitelist: BTreeSet<String> = AllPalletsWithSystem::whitelisted_storage_keys()
		.iter()
		.map(|s| HexDisplay::from(&s.key).to_string())
		.collect();
	// Inactive Issuance
	assert!(whitelist.contains("c2261276cc9d1f8598ea4b6a74b15c2f1ccde6872881f893a21de93dfe970cd5"));
	// Total Issuance
	assert!(whitelist.contains("c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80"));
}

/// This pallet runs tests twice, once with system as `type AccountStore` and once this pallet. This
/// function will return the right value based on the `UseSystem` flag.
pub(crate) fn get_test_account_data(who: AccountId) -> AccountData<Balance> {
	if UseSystem::get() {
		<SystemAccountStore as StoredMap<_, _>>::get(&who)
	} else {
		<BalancesAccountStore as StoredMap<_, _>>::get(&who)
	}
}

/// Same as `get_test_account_data`, but returns a `frame_system::AccountInfo` with the data filled
/// in.
pub(crate) fn get_test_account(
	who: AccountId,
) -> frame_system::AccountInfo<u32, AccountData<Balance>> {
	let mut system_account = frame_system::Account::<Test>::get(&who);
	let account_data = get_test_account_data(who);
	system_account.data = account_data;
	system_account
}