mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 01:11:04 +00:00
Rename Palette to FRAME (#4182)
* palette -> frame * PALETTE, Palette -> FRAME * Move folder pallete -> frame * Update docs/Structure.adoc Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com> * Update docs/README.adoc Co-Authored-By: Benjamin Kampmann <ben.kampmann@googlemail.com> * Update README.adoc
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,188 @@
|
||||
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Test utilities
|
||||
|
||||
use sr_primitives::{Perbill, traits::{ConvertInto, IdentityLookup}, testing::Header};
|
||||
use primitives::H256;
|
||||
use runtime_io;
|
||||
use support::{impl_outer_origin, parameter_types};
|
||||
use support::traits::Get;
|
||||
use support::weights::{Weight, DispatchInfo};
|
||||
use std::cell::RefCell;
|
||||
use crate::{GenesisConfig, Module, Trait};
|
||||
|
||||
impl_outer_origin!{
|
||||
pub enum Origin for Runtime {}
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
pub(crate) static EXISTENTIAL_DEPOSIT: RefCell<u64> = RefCell::new(0);
|
||||
static TRANSFER_FEE: RefCell<u64> = RefCell::new(0);
|
||||
static CREATION_FEE: RefCell<u64> = RefCell::new(0);
|
||||
}
|
||||
|
||||
pub struct ExistentialDeposit;
|
||||
impl Get<u64> for ExistentialDeposit {
|
||||
fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) }
|
||||
}
|
||||
|
||||
pub struct TransferFee;
|
||||
impl Get<u64> for TransferFee {
|
||||
fn get() -> u64 { TRANSFER_FEE.with(|v| *v.borrow()) }
|
||||
}
|
||||
|
||||
pub struct CreationFee;
|
||||
impl Get<u64> for CreationFee {
|
||||
fn get() -> u64 { CREATION_FEE.with(|v| *v.borrow()) }
|
||||
}
|
||||
|
||||
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
pub struct Runtime;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: u32 = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl system::Trait for Runtime {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Call = ();
|
||||
type Hash = H256;
|
||||
type Hashing = ::sr_primitives::traits::BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type Version = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const TransactionBaseFee: u64 = 0;
|
||||
pub const TransactionByteFee: u64 = 1;
|
||||
}
|
||||
impl transaction_payment::Trait for Runtime {
|
||||
type Currency = Module<Runtime>;
|
||||
type OnTransactionPayment = ();
|
||||
type TransactionBaseFee = TransactionBaseFee;
|
||||
type TransactionByteFee = TransactionByteFee;
|
||||
type WeightToFee = ConvertInto;
|
||||
type FeeMultiplierUpdate = ();
|
||||
}
|
||||
impl Trait for Runtime {
|
||||
type Balance = u64;
|
||||
type OnFreeBalanceZero = ();
|
||||
type OnNewAccount = ();
|
||||
type Event = ();
|
||||
type DustRemoval = ();
|
||||
type TransferPayment = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type TransferFee = TransferFee;
|
||||
type CreationFee = CreationFee;
|
||||
}
|
||||
|
||||
pub struct ExtBuilder {
|
||||
existential_deposit: u64,
|
||||
transfer_fee: u64,
|
||||
creation_fee: u64,
|
||||
monied: bool,
|
||||
vesting: bool,
|
||||
}
|
||||
impl Default for ExtBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
existential_deposit: 0,
|
||||
transfer_fee: 0,
|
||||
creation_fee: 0,
|
||||
monied: false,
|
||||
vesting: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ExtBuilder {
|
||||
pub fn existential_deposit(mut self, existential_deposit: u64) -> Self {
|
||||
self.existential_deposit = existential_deposit;
|
||||
self
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
pub fn transfer_fee(mut self, transfer_fee: u64) -> Self {
|
||||
self.transfer_fee = transfer_fee;
|
||||
self
|
||||
}
|
||||
pub fn creation_fee(mut self, creation_fee: u64) -> Self {
|
||||
self.creation_fee = creation_fee;
|
||||
self
|
||||
}
|
||||
pub fn monied(mut self, monied: bool) -> Self {
|
||||
self.monied = monied;
|
||||
if self.existential_deposit == 0 {
|
||||
self.existential_deposit = 1;
|
||||
}
|
||||
self
|
||||
}
|
||||
pub fn vesting(mut self, vesting: bool) -> Self {
|
||||
self.vesting = vesting;
|
||||
self
|
||||
}
|
||||
pub fn set_associated_consts(&self) {
|
||||
EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit);
|
||||
TRANSFER_FEE.with(|v| *v.borrow_mut() = self.transfer_fee);
|
||||
CREATION_FEE.with(|v| *v.borrow_mut() = self.creation_fee);
|
||||
}
|
||||
pub fn build(self) -> runtime_io::TestExternalities {
|
||||
self.set_associated_consts();
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
GenesisConfig::<Runtime> {
|
||||
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![]
|
||||
},
|
||||
vesting: if self.vesting && self.monied {
|
||||
vec![
|
||||
(1, 0, 10, 5 * self.existential_deposit),
|
||||
(2, 10, 20, 0),
|
||||
(12, 10, 20, 5 * self.existential_deposit)
|
||||
]
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
}
|
||||
|
||||
pub type System = system::Module<Runtime>;
|
||||
pub type Balances = Module<Runtime>;
|
||||
|
||||
pub const CALL: &<Runtime as system::Trait>::Call = &();
|
||||
|
||||
/// create a transaction info struct from weight. Handy to avoid building the whole struct.
|
||||
pub fn info_from_weight(w: Weight) -> DispatchInfo {
|
||||
DispatchInfo { weight: w, ..Default::default() }
|
||||
}
|
||||
@@ -0,0 +1,771 @@
|
||||
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Tests for the module.
|
||||
|
||||
use super::*;
|
||||
use mock::{Balances, ExtBuilder, Runtime, System, info_from_weight, CALL};
|
||||
use sr_primitives::traits::SignedExtension;
|
||||
use support::{
|
||||
assert_noop, assert_ok, assert_err,
|
||||
traits::{LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons,
|
||||
Currency, ReservableCurrency, ExistenceRequirement::AllowDeath}
|
||||
};
|
||||
use transaction_payment::ChargeTransactionPayment;
|
||||
use system::RawOrigin;
|
||||
|
||||
const ID_1: LockIdentifier = *b"1 ";
|
||||
const ID_2: LockIdentifier = *b"2 ";
|
||||
const ID_3: LockIdentifier = *b"3 ";
|
||||
|
||||
#[test]
|
||||
fn basic_locking_should_work() {
|
||||
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!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 5, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn partial_locking_should_work() {
|
||||
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, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_removal_should_work() {
|
||||
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, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_replacement_should_work() {
|
||||
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, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn double_locking_should_work() {
|
||||
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, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combination_locking_should_work() {
|
||||
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());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_value_extension_should_work() {
|
||||
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, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
Balances::extend_lock(ID_1, &1, 2, u64::max_value(), WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
Balances::extend_lock(ID_1, &1, 8, u64::max_value(), WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 3, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_reasons_should_work() {
|
||||
ExtBuilder::default()
|
||||
.existential_deposit(1)
|
||||
.monied(true)
|
||||
.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, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
assert_ok!(<Balances as ReservableCurrency<_>>::reserve(&1, 1));
|
||||
// NOTE: this causes a fee payment.
|
||||
assert!(<ChargeTransactionPayment<Runtime> as SignedExtension>::pre_dispatch(
|
||||
ChargeTransactionPayment::from(1),
|
||||
&1,
|
||||
CALL,
|
||||
info_from_weight(1),
|
||||
0,
|
||||
).is_ok());
|
||||
|
||||
Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Reserve.into());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
|
||||
assert_noop!(
|
||||
<Balances as ReservableCurrency<_>>::reserve(&1, 1),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
assert!(<ChargeTransactionPayment<Runtime> as SignedExtension>::pre_dispatch(
|
||||
ChargeTransactionPayment::from(1),
|
||||
&1,
|
||||
CALL,
|
||||
info_from_weight(1),
|
||||
0,
|
||||
).is_ok());
|
||||
|
||||
Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::TransactionPayment.into());
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
|
||||
assert_ok!(<Balances as ReservableCurrency<_>>::reserve(&1, 1));
|
||||
assert!(<ChargeTransactionPayment<Runtime> as SignedExtension>::pre_dispatch(
|
||||
ChargeTransactionPayment::from(1),
|
||||
&1,
|
||||
CALL,
|
||||
info_from_weight(1),
|
||||
0,
|
||||
).is_err());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_block_number_should_work() {
|
||||
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, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
|
||||
System::set_block_number(2);
|
||||
assert_ok!(<Balances as Currency<_>>::transfer(&1, &2, 1, AllowDeath));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_block_number_extension_should_work() {
|
||||
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, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
Balances::extend_lock(ID_1, &1, 10, 1, WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
System::set_block_number(2);
|
||||
Balances::extend_lock(ID_1, &1, 10, 8, WithdrawReasons::all());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 3, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lock_reasons_extension_should_work() {
|
||||
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, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReasons::none());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReason::Reserve.into());
|
||||
assert_noop!(
|
||||
<Balances as Currency<_>>::transfer(&1, &2, 6, AllowDeath),
|
||||
"account liquidity restrictions prevent withdrawal"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_indexing_on_new_accounts_should_not_work2() {
|
||||
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!(
|
||||
Balances::transfer(Some(1).into(), 5, 9),
|
||||
"value too low to create account",
|
||||
);
|
||||
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() {
|
||||
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);
|
||||
assert_eq!(Balances::total_balance(&2), 256 * 20);
|
||||
|
||||
assert_ok!(Balances::reserve(&2, 256 * 19 + 1)); // account 2 becomes mostly reserved
|
||||
assert_eq!(Balances::free_balance(&2), 0); // "free" account deleted."
|
||||
assert_eq!(Balances::total_balance(&2), 256 * 19 + 1); // reserve still exists.
|
||||
assert_eq!(Balances::is_dead_account(&2), false);
|
||||
assert_eq!(System::account_nonce(&2), 1);
|
||||
|
||||
// account 4 tries to take index 1 for account 5.
|
||||
assert_ok!(Balances::transfer(Some(4).into(), 5, 256 * 1 + 0x69));
|
||||
assert_eq!(Balances::total_balance(&5), 256 * 1 + 0x69);
|
||||
assert_eq!(Balances::is_dead_account(&5), false);
|
||||
|
||||
assert!(Balances::slash(&2, 256 * 18 + 2).1.is_zero()); // account 2 gets slashed
|
||||
// "reserve" account reduced to 255 (below ED) so account deleted
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
assert_eq!(System::account_nonce(&2), 0); // nonce zero
|
||||
assert_eq!(Balances::is_dead_account(&2), true);
|
||||
|
||||
// account 4 tries to take index 1 again for account 6.
|
||||
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() {
|
||||
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);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 120);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dust_account_removal_should_work() {
|
||||
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);
|
||||
|
||||
assert_ok!(Balances::transfer(Some(2).into(), 5, 1901)); // index 1 (account 2) becomes zombie
|
||||
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() {
|
||||
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);
|
||||
// index 1 (account 2) becomes zombie for 256*10 + 50(fee) < 256 * 10 (ext_deposit)
|
||||
assert_ok!(Balances::transfer(Some(2).into(), 5, 1851));
|
||||
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() {
|
||||
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);
|
||||
assert_eq!(Balances::total_balance(&1), 42);
|
||||
assert_eq!(Balances::free_balance(&2), 0);
|
||||
assert_eq!(Balances::reserved_balance(&2), 0);
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_works() {
|
||||
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);
|
||||
assert_eq!(Balances::total_balance(&2), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn force_transfer_works() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_noop!(
|
||||
Balances::force_transfer(Some(2).into(), 1, 2, 69),
|
||||
"RequireRootOrigin",
|
||||
);
|
||||
assert_ok!(Balances::force_transfer(RawOrigin::Root.into(), 1, 2, 69));
|
||||
assert_eq!(Balances::total_balance(&1), 42);
|
||||
assert_eq!(Balances::total_balance(&2), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reserving_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
|
||||
assert_eq!(Balances::total_balance(&1), 111);
|
||||
assert_eq!(Balances::free_balance(&1), 111);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
|
||||
assert_eq!(Balances::total_balance(&1), 111);
|
||||
assert_eq!(Balances::free_balance(&1), 42);
|
||||
assert_eq!(Balances::reserved_balance(&1), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn balance_transfer_when_reserved_should_not_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
assert_noop!(
|
||||
Balances::transfer(Some(1).into(), 2, 69),
|
||||
"balance too low to send value",
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deducting_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 69));
|
||||
assert_eq!(Balances::free_balance(&1), 42);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refunding_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 42);
|
||||
Balances::set_reserved_balance(&1, 69);
|
||||
Balances::unreserve(&1, 69);
|
||||
assert_eq!(Balances::free_balance(&1), 111);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slashing_balance_should_work() {
|
||||
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());
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(Balances::reserved_balance(&1), 42);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 42);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slashing_incomplete_balance_should_work() {
|
||||
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);
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unreserving_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 111);
|
||||
assert_ok!(Balances::reserve(&1, 111));
|
||||
Balances::unreserve(&1, 42);
|
||||
assert_eq!(Balances::reserved_balance(&1), 69);
|
||||
assert_eq!(Balances::free_balance(&1), 42);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slashing_reserved_balance_should_work() {
|
||||
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);
|
||||
assert_eq!(Balances::reserved_balance(&1), 69);
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slashing_incomplete_reserved_balance_should_work() {
|
||||
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);
|
||||
assert_eq!(Balances::free_balance(&1), 69);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
assert_eq!(<TotalIssuance<Runtime>>::get(), 69);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transferring_reserved_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 1);
|
||||
assert_ok!(Balances::reserve(&1, 110));
|
||||
assert_ok!(Balances::repatriate_reserved(&1, &2, 41), 0);
|
||||
assert_eq!(Balances::reserved_balance(&1), 69);
|
||||
assert_eq!(Balances::free_balance(&1), 0);
|
||||
assert_eq!(Balances::reserved_balance(&2), 0);
|
||||
assert_eq!(Balances::free_balance(&2), 42);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transferring_reserved_balance_to_nonexistent_should_fail() {
|
||||
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");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transferring_incomplete_reserved_balance_should_work() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 110);
|
||||
let _ = Balances::deposit_creating(&2, 1);
|
||||
assert_ok!(Balances::reserve(&1, 41));
|
||||
assert_ok!(Balances::repatriate_reserved(&1, &2, 69), 28);
|
||||
assert_eq!(Balances::reserved_balance(&1), 0);
|
||||
assert_eq!(Balances::free_balance(&1), 69);
|
||||
assert_eq!(Balances::reserved_balance(&2), 0);
|
||||
assert_eq!(Balances::free_balance(&2), 42);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transferring_too_high_value_should_not_panic() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
<FreeBalance<Runtime>>::insert(1, u64::max_value());
|
||||
<FreeBalance<Runtime>>::insert(2, 1);
|
||||
|
||||
assert_err!(
|
||||
Balances::transfer(Some(1).into(), 2, u64::max_value()),
|
||||
"destination balance too high to receive value",
|
||||
);
|
||||
|
||||
assert_eq!(Balances::free_balance(&1), u64::max_value());
|
||||
assert_eq!(Balances::free_balance(&2), 1);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn account_create_on_free_too_low_with_other() {
|
||||
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);
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn account_create_on_free_too_low() {
|
||||
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() {
|
||||
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);
|
||||
|
||||
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));
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_overflow_isnt_exploitable() {
|
||||
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",
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_vesting_status() {
|
||||
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);
|
||||
let user12_free_balance = Balances::free_balance(&12);
|
||||
assert_eq!(user1_free_balance, 256 * 10); // Account 1 has free balance
|
||||
assert_eq!(user2_free_balance, 256 * 20); // Account 2 has free balance
|
||||
assert_eq!(user12_free_balance, 256 * 10); // Account 12 has free balance
|
||||
let user1_vesting_schedule = VestingSchedule {
|
||||
locked: 256 * 5,
|
||||
per_block: 128, // Vesting over 10 blocks
|
||||
starting_block: 0,
|
||||
};
|
||||
let user2_vesting_schedule = VestingSchedule {
|
||||
locked: 256 * 20,
|
||||
per_block: 256, // Vesting over 20 blocks
|
||||
starting_block: 10,
|
||||
};
|
||||
let user12_vesting_schedule = VestingSchedule {
|
||||
locked: 256 * 5,
|
||||
per_block: 64, // Vesting over 20 blocks
|
||||
starting_block: 10,
|
||||
};
|
||||
assert_eq!(Balances::vesting(&1), Some(user1_vesting_schedule)); // Account 1 has a vesting schedule
|
||||
assert_eq!(Balances::vesting(&2), Some(user2_vesting_schedule)); // Account 2 has a vesting schedule
|
||||
assert_eq!(Balances::vesting(&12), Some(user12_vesting_schedule)); // Account 12 has a vesting schedule
|
||||
|
||||
// Account 1 has only 128 units vested from their illiquid 256 * 5 units at block 1
|
||||
assert_eq!(Balances::vesting_balance(&1), 128 * 9);
|
||||
// Account 2 has their full balance locked
|
||||
assert_eq!(Balances::vesting_balance(&2), user2_free_balance);
|
||||
// Account 12 has only their illiquid funds locked
|
||||
assert_eq!(Balances::vesting_balance(&12), user12_free_balance - 256 * 5);
|
||||
|
||||
System::set_block_number(10);
|
||||
assert_eq!(System::block_number(), 10);
|
||||
|
||||
// Account 1 has fully vested by block 10
|
||||
assert_eq!(Balances::vesting_balance(&1), 0);
|
||||
// Account 2 has started vesting by block 10
|
||||
assert_eq!(Balances::vesting_balance(&2), user2_free_balance);
|
||||
// Account 12 has started vesting by block 10
|
||||
assert_eq!(Balances::vesting_balance(&12), user12_free_balance - 256 * 5);
|
||||
|
||||
System::set_block_number(30);
|
||||
assert_eq!(System::block_number(), 30);
|
||||
|
||||
assert_eq!(Balances::vesting_balance(&1), 0); // Account 1 is still fully vested, and not negative
|
||||
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() {
|
||||
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_noop!(
|
||||
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() {
|
||||
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() {
|
||||
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));
|
||||
|
||||
let user1_free_balance = Balances::free_balance(&1);
|
||||
assert_eq!(user1_free_balance, 200); // Account 1 has 100 more free balance than normal
|
||||
|
||||
let user2_free_balance = Balances::free_balance(&2);
|
||||
assert_eq!(user2_free_balance, 300); // Account 2 has 100 more free balance than normal
|
||||
|
||||
// Account 1 has only 5 units vested at block 1 (plus 150 unvested)
|
||||
assert_eq!(Balances::vesting_balance(&1), 45);
|
||||
assert_ok!(Balances::transfer(Some(1).into(), 3, 155)); // Account 1 can send extra units gained
|
||||
|
||||
// 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() {
|
||||
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);
|
||||
|
||||
assert_eq!(user12_free_balance, 2560); // Account 12 has free balance
|
||||
// Account 12 has liquid funds
|
||||
assert_eq!(Balances::vesting_balance(&12), user12_free_balance - 256 * 5);
|
||||
|
||||
// Account 12 has delayed vesting
|
||||
let user12_vesting_schedule = VestingSchedule {
|
||||
locked: 256 * 5,
|
||||
per_block: 64, // Vesting over 20 blocks
|
||||
starting_block: 10,
|
||||
};
|
||||
assert_eq!(Balances::vesting(&12), Some(user12_vesting_schedule));
|
||||
|
||||
// Account 12 can still send liquid funds
|
||||
assert_ok!(Balances::transfer(Some(12).into(), 3, 256 * 5));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn burn_must_work() {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transfer_keep_alive_works() {
|
||||
ExtBuilder::default().existential_deposit(1).build().execute_with(|| {
|
||||
let _ = Balances::deposit_creating(&1, 100);
|
||||
assert_err!(
|
||||
Balances::transfer_keep_alive(Some(1).into(), 2, 100),
|
||||
"transfer would kill account"
|
||||
);
|
||||
assert_eq!(Balances::is_dead_account(&1), false);
|
||||
assert_eq!(Balances::total_balance(&1), 100);
|
||||
assert_eq!(Balances::total_balance(&2), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic="the balance of any account should always be more than existential deposit."]
|
||||
fn cannot_set_genesis_value_below_ed() {
|
||||
mock::EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = 11);
|
||||
let mut t = system::GenesisConfig::default().build_storage::<Runtime>().unwrap();
|
||||
let _ = GenesisConfig::<Runtime> {
|
||||
balances: vec![(1, 10)],
|
||||
vesting: vec![],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user