diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index b3b2c36611..64652d30ed 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3683,6 +3683,7 @@ version = "2.0.0" dependencies = [ "frame-support 2.0.0", "frame-system 2.0.0", + "hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "pallet-balances 2.0.0", "parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", @@ -3690,6 +3691,7 @@ dependencies = [ "sp-io 2.0.0", "sp-runtime 2.0.0", "sp-std 2.0.0", + "sp-storage 2.0.0", ] [[package]] @@ -4125,6 +4127,24 @@ dependencies = [ "sp-std 2.0.0", ] +[[package]] +name = "pallet-vesting" +version = "2.0.0" +dependencies = [ + "enumflags2 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "frame-support 2.0.0", + "frame-system 2.0.0", + "hex-literal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pallet-balances 2.0.0", + "parity-scale-codec 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-core 2.0.0", + "sp-io 2.0.0", + "sp-runtime 2.0.0", + "sp-std 2.0.0", + "sp-storage 2.0.0", +] + [[package]] name = "parity-bytes" version = "0.1.1" diff --git a/substrate/Cargo.toml b/substrate/Cargo.toml index 10465042de..0bd6b4c0b0 100644 --- a/substrate/Cargo.toml +++ b/substrate/Cargo.toml @@ -99,6 +99,7 @@ members = [ "frame/transaction-payment/rpc/runtime-api", "frame/treasury", "frame/utility", + "frame/vesting", "primitives/allocator", "primitives/application-crypto", "primitives/application-crypto/test", diff --git a/substrate/bin/node-template/runtime/src/lib.rs b/substrate/bin/node-template/runtime/src/lib.rs index 1276f20d17..ea4535e26d 100644 --- a/substrate/bin/node-template/runtime/src/lib.rs +++ b/substrate/bin/node-template/runtime/src/lib.rs @@ -196,15 +196,12 @@ impl timestamp::Trait for Runtime { parameter_types! { pub const ExistentialDeposit: u128 = 500; - pub const TransferFee: u128 = 0; pub const CreationFee: u128 = 0; } impl balances::Trait for Runtime { /// The type for recording an account's balance. type Balance = Balance; - /// What to do if an account's free balance gets zeroed. - type OnFreeBalanceZero = (); /// What to do if an account is fully reaped from the system. type OnReapAccount = System; /// What to do if a new account is created. @@ -214,7 +211,6 @@ impl balances::Trait for Runtime { type DustRemoval = (); type TransferPayment = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } diff --git a/substrate/bin/node-template/src/chain_spec.rs b/substrate/bin/node-template/src/chain_spec.rs index 6c6ab492a3..aa50d06b23 100644 --- a/substrate/bin/node-template/src/chain_spec.rs +++ b/substrate/bin/node-template/src/chain_spec.rs @@ -132,7 +132,6 @@ fn testnet_genesis(initial_authorities: Vec<(AuraId, GrandpaId)>, }), balances: Some(BalancesConfig { balances: endowed_accounts.iter().cloned().map(|k|(k, 1 << 60)).collect(), - vesting: vec![], }), sudo: Some(SudoConfig { key: root_key, diff --git a/substrate/bin/node/cli/src/chain_spec.rs b/substrate/bin/node/cli/src/chain_spec.rs index b8ff948b01..4ce0c8f49b 100644 --- a/substrate/bin/node/cli/src/chain_spec.rs +++ b/substrate/bin/node/cli/src/chain_spec.rs @@ -237,7 +237,6 @@ pub fn testnet_genesis( .map(|k| (k, ENDOWMENT)) .chain(initial_authorities.iter().map(|x| (x.0.clone(), STASH))) .collect(), - vesting: vec![], }), pallet_indices: Some(IndicesConfig { ids: endowed_accounts.iter().cloned() diff --git a/substrate/bin/node/executor/tests/basic.rs b/substrate/bin/node/executor/tests/basic.rs index 817ebbb173..b306852f45 100644 --- a/substrate/bin/node/executor/tests/basic.rs +++ b/substrate/bin/node/executor/tests/basic.rs @@ -35,8 +35,7 @@ use frame_system::{self, EventRecord, Phase}; use node_runtime::{ Header, Block, UncheckedExtrinsic, CheckedExtrinsic, Call, Runtime, Balances, - System, TransactionPayment, Event, - TransferFee, TransactionBaseFee, TransactionByteFee, + System, TransactionPayment, Event, TransactionBaseFee, TransactionByteFee, CreationFee, constants::currency::*, }; use node_primitives::{Balance, Hash}; @@ -62,7 +61,7 @@ fn transfer_fee(extrinsic: &E, fee_multiplier: Fixed64) -> Balance { ::WeightToFee::convert(weight); let base_fee = TransactionBaseFee::get(); - base_fee + fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) + TransferFee::get() + base_fee + fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee) } fn xt() -> UncheckedExtrinsic { @@ -164,8 +163,8 @@ fn block_with_size(time: u64, nonce: u32, size: usize) -> (Vec, Hash) { fn panic_execution_with_foreign_code_gives_error() { let mut t = TestExternalities::::new_with_code(BLOATY_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - 69_u128.encode() + >::hashed_key_for(alice()) => { + (69u128, 0u128, 0u128, 0u128).encode() }, >::hashed_key().to_vec() => { 69_u128.encode() @@ -203,8 +202,8 @@ fn panic_execution_with_foreign_code_gives_error() { fn bad_extrinsic_with_native_equivalent_code_gives_error() { let mut t = TestExternalities::::new_with_code(COMPACT_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - 69_u128.encode() + >::hashed_key_for(alice()) => { + (69u128, 0u128, 0u128, 0u128).encode() }, >::hashed_key().to_vec() => { 69_u128.encode() @@ -242,8 +241,8 @@ fn bad_extrinsic_with_native_equivalent_code_gives_error() { fn successful_execution_with_native_equivalent_code_gives_ok() { let mut t = TestExternalities::::new_with_code(COMPACT_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - (111 * DOLLARS).encode() + >::hashed_key_for(alice()) => { + (111 * DOLLARS, 0u128, 0u128, 0u128).encode() }, >::hashed_key().to_vec() => { (111 * DOLLARS).encode() @@ -275,7 +274,8 @@ fn successful_execution_with_native_equivalent_code_gives_ok() { assert!(r.is_ok()); t.execute_with(|| { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt(), fm)); + let fees = transfer_fee(&xt(), fm) + CreationFee::get(); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } @@ -284,8 +284,8 @@ fn successful_execution_with_native_equivalent_code_gives_ok() { fn successful_execution_with_foreign_code_gives_ok() { let mut t = TestExternalities::::new_with_code(BLOATY_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - (111 * DOLLARS).encode() + >::hashed_key_for(alice()) => { + (111 * DOLLARS, 0u128, 0u128, 0u128).encode() }, >::hashed_key().to_vec() => { (111 * DOLLARS).encode() @@ -317,7 +317,8 @@ fn successful_execution_with_foreign_code_gives_ok() { assert!(r.is_ok()); t.execute_with(|| { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt(), fm)); + let fees = transfer_fee(&xt(), fm) + CreationFee::get(); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } @@ -340,7 +341,8 @@ fn full_native_block_import_works() { ).0.unwrap(); t.execute_with(|| { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - transfer_fee(&xt(), fm)); + let fees = transfer_fee(&xt(), fm); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees); assert_eq!(Balances::total_balance(&bob()), 169 * DOLLARS); alice_last_known_balance = Balances::total_balance(&alice()); let events = vec![ @@ -362,7 +364,7 @@ fn full_native_block_import_works() { alice().into(), bob().into(), 69 * DOLLARS, - 1 * CENTS, + 0, )), topics: vec![], }, @@ -416,7 +418,7 @@ fn full_native_block_import_works() { bob().into(), alice().into(), 5 * DOLLARS, - 1 * CENTS, + 0, ) ), topics: vec![], @@ -440,7 +442,7 @@ fn full_native_block_import_works() { alice().into(), bob().into(), 15 * DOLLARS, - 1 * CENTS, + 0, ) ), topics: vec![], @@ -710,8 +712,8 @@ fn native_big_block_import_fails_on_fallback() { fn panic_execution_gives_error() { let mut t = TestExternalities::::new_with_code(BLOATY_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - 0_u128.encode() + >::hashed_key_for(alice()) => { + (0_u128, 0_u128, 0_u128, 0_u128).encode() }, >::hashed_key().to_vec() => { 0_u128.encode() @@ -745,8 +747,8 @@ fn panic_execution_gives_error() { fn successful_execution_gives_ok() { let mut t = TestExternalities::::new_with_code(COMPACT_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - (111 * DOLLARS).encode() + >::hashed_key_for(alice()) => { + (111 * DOLLARS, 0u128, 0u128, 0u128).encode() }, >::hashed_key().to_vec() => { (111 * DOLLARS).encode() @@ -779,7 +781,8 @@ fn successful_execution_gives_ok() { .expect("Extrinsic did not fail"); t.execute_with(|| { - assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - 1 * transfer_fee(&xt(), fm)); + let fees = transfer_fee(&xt(), fm) + CreationFee::get(); + assert_eq!(Balances::total_balance(&alice()), 42 * DOLLARS - fees); assert_eq!(Balances::total_balance(&bob()), 69 * DOLLARS); }); } diff --git a/substrate/bin/node/executor/tests/fees.rs b/substrate/bin/node/executor/tests/fees.rs index ca34452031..155cefa4cc 100644 --- a/substrate/bin/node/executor/tests/fees.rs +++ b/substrate/bin/node/executor/tests/fees.rs @@ -30,7 +30,7 @@ use sp_runtime::{ }; use node_runtime::{ CheckedExtrinsic, Call, Runtime, Balances, - TransactionPayment, TransferFee, TransactionBaseFee, TransactionByteFee, + TransactionPayment, TransactionBaseFee, TransactionByteFee, WeightFeeCoefficient, constants::currency::*, }; use node_runtime::impls::LinearWeightToFee; @@ -134,14 +134,14 @@ fn transaction_fee_is_correct_ultimate() { // (this baed on assigning 0.1 CENT to the cheapest tx with `weight = 100`) let mut t = TestExternalities::::new_with_code(COMPACT_CODE, Storage { top: map![ - >::hashed_key_for(alice()) => { - (100 * DOLLARS).encode() + >::hashed_key_for(alice()) => { + (100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() }, - >::hashed_key_for(bob()) => { - (10 * DOLLARS).encode() + >::hashed_key_for(bob()) => { + (10 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() }, >::hashed_key().to_vec() => { - (110 * DOLLARS).encode() + (110 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode() }, >::hashed_key().to_vec() => vec![0u8; 16], >::hashed_key_for(0) => vec![0u8; 32] @@ -193,9 +193,7 @@ fn transaction_fee_is_correct_ultimate() { // we know that weight to fee multiplier is effect-less in block 1. assert_eq!(weight_fee as Balance, MILLICENTS); balance_alice -= weight_fee; - balance_alice -= tip; - balance_alice -= TransferFee::get(); assert_eq!(Balances::total_balance(&alice()), balance_alice); }); diff --git a/substrate/bin/node/executor/tests/submit_transaction.rs b/substrate/bin/node/executor/tests/submit_transaction.rs index 9e91ffc76b..40c0c6b80f 100644 --- a/substrate/bin/node/executor/tests/submit_transaction.rs +++ b/substrate/bin/node/executor/tests/submit_transaction.rs @@ -167,7 +167,8 @@ fn submitted_transaction_should_be_valid() { // add balance to the account let author = extrinsic.signature.clone().unwrap().0; let address = Indices::lookup(author).unwrap(); - >::insert(&address, 5_000_000_000_000); + let account = pallet_balances::AccountData { free: 5_000_000_000_000, ..Default::default() }; + >::insert(&address, account); // check validity let res = Executive::validate_transaction(extrinsic); diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index a4a1c90665..0e6a337a8b 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -167,20 +167,17 @@ impl pallet_indices::Trait for Runtime { parameter_types! { pub const ExistentialDeposit: Balance = 1 * DOLLARS; - pub const TransferFee: Balance = 1 * CENTS; pub const CreationFee: Balance = 1 * CENTS; } impl pallet_balances::Trait for Runtime { type Balance = Balance; - type OnFreeBalanceZero = ((Staking, Contracts), Session); - type OnReapAccount = (System, Recovery); + type OnReapAccount = ((((System, Staking), Contracts), Session), Recovery); type OnNewAccount = Indices; type Event = Event; type DustRemoval = (); type TransferPayment = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -430,7 +427,6 @@ impl pallet_contracts::Trait for Runtime { type RentByteFee = RentByteFee; type RentDepositOffset = RentDepositOffset; type SurchargeReward = SurchargeReward; - type TransferFee = ContractTransferFee; type CreationFee = ContractCreationFee; type TransactionBaseFee = ContractTransactionBaseFee; type TransactionByteFee = ContractTransactionByteFee; diff --git a/substrate/bin/node/testing/src/genesis.rs b/substrate/bin/node/testing/src/genesis.rs index 183515f272..5949781275 100644 --- a/substrate/bin/node/testing/src/genesis.rs +++ b/substrate/bin/node/testing/src/genesis.rs @@ -49,7 +49,6 @@ pub fn config(support_changes_trie: bool, code: Option<&[u8]>) -> GenesisConfig (eve(), 101 * DOLLARS), (ferdie(), 100 * DOLLARS), ], - vesting: vec![], }), pallet_session: Some(SessionConfig { keys: vec![ diff --git a/substrate/frame/balances/Cargo.toml b/substrate/frame/balances/Cargo.toml index f2e24dbae4..871290b182 100644 --- a/substrate/frame/balances/Cargo.toml +++ b/substrate/frame/balances/Cargo.toml @@ -9,6 +9,7 @@ license = "GPL-3.0" serde = { version = "1.0.101", optional = true } codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } frame-support = { version = "2.0.0", default-features = false, path = "../support" } frame-system = { version = "2.0.0", default-features = false, path = "../system" } @@ -24,7 +25,8 @@ std = [ "serde", "codec/std", "sp-std/std", - "frame-support/std", + "sp-io/std", "sp-runtime/std", + "frame-support/std", "frame-system/std", ] diff --git a/substrate/frame/balances/src/lib.rs b/substrate/frame/balances/src/lib.rs index 77d28e89e9..65ad226d53 100644 --- a/substrate/frame/balances/src/lib.rs +++ b/substrate/frame/balances/src/lib.rs @@ -38,37 +38,31 @@ //! ### Terminology //! //! - **Existential Deposit:** The minimum balance required to create or keep an account open. This prevents -//! "dust accounts" from filling storage. -//! - **Total Issuance:** The total number of units in existence in a system. -//! - **Reaping an account:** The act of removing an account by resetting its nonce. Happens after its balance is set -//! to zero. -//! - **Free Balance:** The portion of a balance that is not reserved. The free balance is the only -//! balance that matters for most operations. When this balance falls below the existential -//! deposit, most functionality of the account is removed. When both it and the reserved balance -//! are deleted, then the account is said to be dead. -//! -//! No account should ever have a free balance that is strictly between 0 and the existential +//! "dust accounts" from filling storage. When the free plus the reserved balance (i.e. the total balance) +//! fall below this, then the account is said to be dead; and it loses its functionality as well as any +//! prior history and all information on it is removed from the chain's state. +//! No account should ever have a total balance that is strictly between 0 and the existential //! deposit (exclusive). If this ever happens, it indicates either a bug in this module or an //! erroneous raw mutation of storage. //! +//! - **Total Issuance:** The total number of units in existence in a system. +//! +//! - **Reaping an account:** The act of removing an account by resetting its nonce. Happens after its +//! total balance has become zero (or, strictly speaking, less than the Existential Deposit). +//! +//! - **Free Balance:** The portion of a balance that is not reserved. The free balance is the only +//! balance that matters for most operations. +//! //! - **Reserved Balance:** Reserved balance still belongs to the account holder, but is suspended. //! Reserved balance can still be slashed, but only after all the free balance has been slashed. -//! If the reserved balance falls below the existential deposit, it and any related functionality -//! will be deleted. When both it and the free balance are deleted, then the account is said to -//! be dead. -//! -//! No account should ever have a reserved balance that is strictly between 0 and the existential -//! deposit (exclusive). If this ever happens, it indicates either a bug in this module or an -//! erroneous raw mutation of storage. //! //! - **Imbalance:** A condition when some funds were credited or debited without equal and opposite accounting //! (i.e. a difference between total issuance and account balances). Functions that result in an imbalance will //! return an object of the `Imbalance` trait that can be managed within your runtime logic. (If an imbalance is //! simply dropped, it should automatically maintain any book-keeping such as total issuance.) +//! //! - **Lock:** A freeze on a specified amount of an account's free balance until a specified block number. Multiple //! locks always operate over the same funds, so they "overlay" rather than "stack". -//! - **Vesting:** Similar to a lock, this is another, but independent, liquidity restriction that reduces linearly -//! over time. //! //! ### Implementations //! @@ -94,10 +88,6 @@ //! - `transfer` - Transfer some liquid free balance to another account. //! - `set_balance` - Set the balances of a given account. The origin of this call must be root. //! -//! ### Public Functions -//! -//! - `vesting_balance` - Get the amount that is currently being vested and cannot be transferred out of this account. -//! //! ## Usage //! //! The following examples show how to use the Balances module in your custom module. @@ -141,7 +131,6 @@ //! STAKING_ID, //! &ledger.stash, //! ledger.total, -//! T::BlockNumber::max_value(), //! WithdrawReasons::all() //! ); //! // >::insert(controller, ledger); // Commented out as we don't have access to Staking's storage here. @@ -159,44 +148,39 @@ #![cfg_attr(not(feature = "std"), no_std)] -use sp_std::prelude::*; -use sp_std::{cmp, result, mem, fmt::Debug}; -use codec::{Codec, Encode, Decode}; -use frame_support::{ - StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, - traits::{ - UpdateBalanceOutcome, Currency, OnFreeBalanceZero, OnReapAccount, OnUnbalanced, TryDrop, - WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, - Imbalance, SignedImbalance, ReservableCurrency, Get, VestingCurrency, - }, - weights::SimpleDispatchInfo, -}; -use sp_runtime::{ - RuntimeDebug, DispatchResult, DispatchError, - traits::{ - Zero, SimpleArithmetic, StaticLookup, Member, CheckedAdd, CheckedSub, MaybeSerializeDeserialize, - Saturating, Bounded, - }, -}; -use frame_system::{self as system, IsDeadAccount, OnNewAccount, ensure_signed, ensure_root}; - #[cfg(test)] mod mock; #[cfg(test)] mod tests; +mod migration; + +use sp_std::prelude::*; +use sp_std::{cmp, result, mem, fmt::Debug, ops::BitOr}; +use codec::{Codec, Encode, Decode}; +use frame_support::{ + StorageValue, Parameter, decl_event, decl_storage, decl_module, decl_error, ensure, + weights::SimpleDispatchInfo, traits::{ + UpdateBalanceOutcome, Currency, OnReapAccount, OnUnbalanced, TryDrop, + WithdrawReason, WithdrawReasons, LockIdentifier, LockableCurrency, ExistenceRequirement, + Imbalance, SignedImbalance, ReservableCurrency, Get, ExistenceRequirement::KeepAlive + } +}; +use sp_runtime::{ + RuntimeDebug, DispatchResult, DispatchError, + traits::{ + Zero, SimpleArithmetic, StaticLookup, Member, CheckedAdd, CheckedSub, + MaybeSerializeDeserialize, Saturating, Bounded, + }, +}; +use frame_system::{self as system, IsDeadAccount, OnNewAccount, ensure_signed, ensure_root}; +use migration::{get_storage_value, put_storage_value, StorageIterator}; pub use self::imbalances::{PositiveImbalance, NegativeImbalance}; pub trait Subtrait: frame_system::Trait { /// The balance of an account. type Balance: Parameter + Member + SimpleArithmetic + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug + From; - - /// A function that is invoked when the free-balance has fallen below the existential deposit and - /// has been reduced to zero. - /// - /// Gives a chance to clean up resources associated with the given account. - type OnFreeBalanceZero: OnFreeBalanceZero; + MaybeSerializeDeserialize + Debug; /// A function that is invoked when the free-balance and the reserved-balance has fallen below /// the existential deposit and both have been reduced to zero. @@ -210,23 +194,15 @@ pub trait Subtrait: frame_system::Trait { /// The minimum amount required to keep an account open. type ExistentialDeposit: Get; - /// The fee required to make a transfer. - type TransferFee: Get; - - /// The fee required to create an account. + /// The fee required to create an account. If you're doing significant stuff with `OnNewAccount` + /// then you'll probably want to make this non-zero. type CreationFee: Get; } pub trait Trait: frame_system::Trait { /// The balance of an account. type Balance: Parameter + Member + SimpleArithmetic + Codec + Default + Copy + - MaybeSerializeDeserialize + Debug + From; - - /// A function that is invoked when the free-balance has fallen below the existential deposit and - /// has been reduced to zero. - /// - /// Gives a chance to clean up resources associated with the given account. - type OnFreeBalanceZero: OnFreeBalanceZero; + MaybeSerializeDeserialize + Debug; /// A function that is invoked when the free-balance and the reserved-balance has fallen below /// the existential deposit and both have been reduced to zero. @@ -250,20 +226,15 @@ pub trait Trait: frame_system::Trait { /// The minimum amount required to keep an account open. type ExistentialDeposit: Get; - /// The fee required to make a transfer. - type TransferFee: Get; - /// The fee required to create an account. type CreationFee: Get; } impl, I: Instance> Subtrait for T { type Balance = T::Balance; - type OnFreeBalanceZero = T::OnFreeBalanceZero; type OnReapAccount = T::OnReapAccount; type OnNewAccount = T::OnNewAccount; type ExistentialDeposit = T::ExistentialDeposit; - type TransferFee = T::TransferFee; type CreationFee = T::CreationFee; } @@ -306,40 +277,91 @@ decl_error! { } } -/// Struct to encode the vesting schedule of an individual account. -#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug)] -pub struct VestingSchedule { - /// Locked amount at genesis. - pub locked: Balance, - /// Amount that gets unlocked every block after `starting_block`. - pub per_block: Balance, - /// Starting block for unlocking(vesting). - pub starting_block: BlockNumber, +/// Simplified reasons for withdrawing balance. +#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug)] +pub enum Reasons { + /// Paying system transaction fees. + Fee = 0, + /// Any reason other than paying system transaction fees. + Misc = 1, + /// Any reason at all. + All = 2, } -impl VestingSchedule { - /// Amount locked at block `n`. - pub fn locked_at(&self, n: BlockNumber) -> Balance - where Balance: From - { - // Number of blocks that count toward vesting - // Saturating to 0 when n < starting_block - let vested_block_count = n.saturating_sub(self.starting_block); - // Return amount that is still locked in vesting - if let Some(x) = Balance::from(vested_block_count).checked_mul(&self.per_block) { - self.locked.max(x) - x +impl From for Reasons { + fn from(r: WithdrawReasons) -> Reasons { + if r == WithdrawReasons::from(WithdrawReason::TransactionPayment) { + Reasons::Fee + } else if r.contains(WithdrawReason::TransactionPayment) { + Reasons::All } else { - Zero::zero() + Reasons::Misc } } } +impl BitOr for Reasons { + type Output = Reasons; + fn bitor(self, other: Reasons) -> Reasons { + if self == other { return self } + Reasons::All + } +} + +/// A single lock on a balance. There can be many of these on an account and they "overlap", so the +/// same balance is frozen by multiple locks. #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] -pub struct BalanceLock { +pub struct BalanceLock { + /// An identifier for this lock. Only one lock may be in existence for each identifier. pub id: LockIdentifier, + /// The amount which the free balance may not drop below when this lock is in effect. pub amount: Balance, - pub until: BlockNumber, - pub reasons: WithdrawReasons, + /// If true, then the lock remains in effect even for payment of transaction fees. + pub reasons: Reasons, +} + +/// All balance information for an account. +#[derive(Encode, Decode, Clone, PartialEq, Eq, Default, RuntimeDebug)] +pub struct AccountData { + /// Non-reserved part of the balance. There may still be restrictions on this, but it is the + /// total pool what may in principle be transferred, reserved and used for tipping. + /// + /// This is the only balance that matters in terms of most operations on tokens. It + /// alone is used to determine the balance when in the contract execution environment. + pub free: Balance, + /// Balance which is reserved and may not be used at all. + /// + /// This can still get slashed, but gets slashed last of all. + /// + /// This balance is a 'reserve' balance that other subsystems use in order to set aside tokens + /// that are still 'owned' by the account holder, but which are suspendable. + pub reserved: Balance, + /// The amount that `free` may not drop below when withdrawing for *anything except transaction + /// fee payment*. + pub misc_frozen: Balance, + /// The amount that `free` may not drop below when withdrawing specifically for transaction + /// fee payment. + pub fee_frozen: Balance, +} + +impl AccountData { + /// How much this account's balance can be reduced for the given `reasons`. + fn usable(&self, reasons: Reasons) -> Balance { + self.free.saturating_sub(self.frozen(reasons)) + } + /// The amount that this account's free balance may not be reduced beyond for the given + /// `reasons`. + fn frozen(&self, reasons: Reasons) -> Balance { + match reasons { + Reasons::All => self.misc_frozen.max(self.fee_frozen), + Reasons::Misc => self.misc_frozen, + Reasons::Fee => self.fee_frozen, + } + } + /// The total balance in this account including any that is reserved and ignoring any frozen. + fn total(&self) -> Balance { + self.free.saturating_add(self.reserved) + } } decl_storage! { @@ -349,71 +371,25 @@ decl_storage! { config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n) }): T::Balance; - /// Information regarding the vesting of a given account. - pub Vesting get(fn vesting) build(|config: &GenesisConfig| { - // Generate initial vesting configuration - // * who - Account which we are generating vesting configuration for - // * begin - Block when the account will start to vest - // * length - Number of blocks from `begin` until fully vested - // * liquid - Number of units which can be spent before vesting begins - config.vesting.iter().filter_map(|&(ref who, begin, length, liquid)| { - let length = >::from(length); - - config.balances.iter() - .find(|&&(ref w, _)| w == who) - .map(|&(_, balance)| { - // Total genesis `balance` minus `liquid` equals funds locked for vesting - let locked = balance.saturating_sub(liquid); - // Number of units unlocked per block after `begin` - let per_block = locked / length.max(sp_runtime::traits::One::one()); - - (who.clone(), VestingSchedule { - locked: locked, - per_block: per_block, - starting_block: begin - }) - }) - }).collect::>() - }): - map hasher(blake2_256) T::AccountId - => Option>; - - /// The 'free' balance of a given account. + /// The balance of an account. /// - /// This is the only balance that matters in terms of most operations on tokens. It - /// alone is used to determine the balance when in the contract execution environment. When this - /// balance falls below the value of `ExistentialDeposit`, then the 'current account' is - /// deleted: specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback - /// is invoked, giving a chance to external modules to clean up data associated with - /// the deleted account. - /// - /// `frame_system::AccountNonce` is also deleted if `ReservedBalance` is also zero (it also gets - /// collapsed to zero if it ever becomes less than `ExistentialDeposit`. - pub FreeBalance get(fn free_balance) - build(|config: &GenesisConfig| config.balances.clone()): - map hasher(blake2_256) T::AccountId => T::Balance; - - /// The amount of the balance of a given account that is externally reserved; this can still get - /// slashed, but gets slashed last of all. - /// - /// This balance is a 'reserve' balance that other subsystems use in order to set aside tokens - /// that are still 'owned' by the account holder, but which are suspendable. - /// - /// When this balance falls below the value of `ExistentialDeposit`, then this 'reserve account' - /// is deleted: specifically, `ReservedBalance`. - /// - /// `frame_system::AccountNonce` is also deleted if `FreeBalance` is also zero (it also gets - /// collapsed to zero if it ever becomes less than `ExistentialDeposit`.) - pub ReservedBalance get(fn reserved_balance): - map hasher(blake2_256) T::AccountId => T::Balance; + /// NOTE: THIS MAY NEVER BE IN EXISTENCE AND YET HAVE A `total().is_zero()`. If the total + /// is ever zero, then the entry *MUST* be removed. + pub Account get(fn account) + build(|config: &GenesisConfig| config.balances.iter() + .map(|&(ref who, free)| (who.clone(), AccountData { free, .. Default::default() })) + .collect::>() + ): map hasher(blake2_256) T::AccountId => AccountData; /// Any liquidity locks on some account balances. - pub Locks get(fn locks): - map hasher(blake2_256) T::AccountId => Vec>; + /// NOTE: Should only be accessed when setting, changing and freeing a lock. + pub Locks get(fn locks): map hasher(blake2_256) T::AccountId => Vec>; + + /// True if network has been upgraded to this version. + IsUpgraded: bool; } add_extra_genesis { config(balances): Vec<(T::AccountId, T::Balance)>; - config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, T::Balance)>; // ^^ begin, length, amount liquid at genesis build(|config: &GenesisConfig| { for (_, balance) in &config.balances { @@ -433,9 +409,6 @@ decl_module! { /// The minimum amount required to keep an account open. const ExistentialDeposit: T::Balance = T::ExistentialDeposit::get(); - /// The fee required to make a transfer. - const TransferFee: T::Balance = T::TransferFee::get(); - /// The fee required to create an account. const CreationFee: T::Balance = T::CreationFee::get(); @@ -460,8 +433,7 @@ decl_module! { /// - `ensure_can_withdraw` is always called internally but has a bounded complexity. /// - Transferring balances to accounts that did not exist before will cause /// `T::OnNewAccount::on_new_account` to be called. - /// - Removing enough funds from an account will trigger - /// `T::DustRemoval::on_unbalanced` and `T::OnFreeBalanceZero::on_free_balance_zero`. + /// - Removing enough funds from an account will trigger `T::DustRemoval::on_unbalanced`. /// - `transfer_keep_alive` works the same way as `transfer`, but has an additional /// check that the transfer will not kill the origin account. /// @@ -501,26 +473,28 @@ decl_module! { let who = T::Lookup::lookup(who)?; let existential_deposit = T::ExistentialDeposit::get(); - let new_free = if new_free < existential_deposit { Zero::zero() } else { new_free }; - let new_reserved = if new_reserved < existential_deposit { Zero::zero() } else { new_reserved }; + let wipeout = new_free + new_reserved < existential_deposit; + let new_free = if wipeout { Zero::zero() } else { new_free }; + let new_reserved = if wipeout { Zero::zero() } else { new_reserved }; - let current_free = >::get(&who); - if new_free > current_free { - mem::drop(PositiveImbalance::::new(new_free - current_free)); - } else if new_free < current_free { - mem::drop(NegativeImbalance::::new(current_free - new_free)); + let old_account = Account::::get(&who); + + if new_free > old_account.free { + mem::drop(PositiveImbalance::::new(new_free - old_account.free)); + } else if new_free < old_account.free { + mem::drop(NegativeImbalance::::new(old_account.free - new_free)); } - Self::set_free_balance(&who, new_free); - let current_reserved = >::get(&who); - if new_reserved > current_reserved { - mem::drop(PositiveImbalance::::new(new_reserved - current_reserved)); - } else if new_reserved < current_reserved { - mem::drop(NegativeImbalance::::new(current_reserved - new_reserved)); + if new_reserved > old_account.reserved { + mem::drop(PositiveImbalance::::new(new_reserved - old_account.reserved)); + } else if new_reserved < old_account.reserved { + mem::drop(NegativeImbalance::::new(old_account.reserved - new_reserved)); } - Self::set_reserved_balance(&who, new_reserved); - Self::deposit_event(RawEvent::BalanceSet(who, new_free, new_reserved)); + let account = AccountData { free: new_free, reserved: new_reserved, ..old_account }; + Self::set_account(&who, &account, &old_account); + + Self::deposit_event(RawEvent::BalanceSet(who, account.free, account.reserved)); } /// Exactly as `transfer`, except the origin must be root and the source account may be @@ -552,52 +526,154 @@ decl_module! { ) { let transactor = ensure_signed(origin)?; let dest = T::Lookup::lookup(dest)?; - >::transfer(&transactor, &dest, value, ExistenceRequirement::KeepAlive)?; + >::transfer(&transactor, &dest, value, KeepAlive)?; } + fn on_initialize() { + if !IsUpgraded::::get() { + IsUpgraded::::put(true); + Self::do_upgrade(); + } + } + } +} + +#[derive(Decode)] +struct OldBalanceLock { + id: LockIdentifier, + amount: Balance, + until: BlockNumber, + reasons: WithdrawReasons, +} + +impl OldBalanceLock { + fn upgraded(self) -> (BalanceLock, BlockNumber) { + (BalanceLock { + id: self.id, + amount: self.amount, + reasons: self.reasons.into(), + }, self.until) } } impl, I: Instance> Module { // PRIVATE MUTABLES - /// Set the reserved balance of an account to some new value. Will enforce `ExistentialDeposit` - /// law, annulling the account as needed. - /// - /// Doesn't do any preparatory work for creating a new account, so should only be used when it - /// is known that the account already exists. - /// - /// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that - /// the caller will do this. - fn set_reserved_balance(who: &T::AccountId, balance: T::Balance) -> UpdateBalanceOutcome { - if balance < T::ExistentialDeposit::get() { - >::insert(who, balance); - Self::on_reserved_too_low(who); - UpdateBalanceOutcome::AccountKilled - } else { - >::insert(who, balance); - UpdateBalanceOutcome::Updated + // Upgrade from the pre-#4649 balances/vesting into the new balances. + pub fn do_upgrade() { + // First, migrate from old FreeBalance to new Account. + // We also move all locks across since only accounts with FreeBalance values have locks. + // FreeBalance: map T::AccountId => T::Balance + for (hash, free) in StorageIterator::::new(b"Balances", b"FreeBalance").drain() { + let mut account = AccountData { free, ..Default::default() }; + // Locks: map T::AccountId => Vec + let old_locks = get_storage_value::>>(b"Balances", b"Locks", &hash); + if let Some(locks) = old_locks { + let locks = locks.into_iter() + .map(|i| { + let (result, expiry) = i.upgraded(); + if expiry != T::BlockNumber::max_value() { + // Any `until`s that are not T::BlockNumber::max_value come from + // democracy and need to be migrated over there. + // Democracy: Locks get(locks): map T::AccountId => Option; + put_storage_value(b"Democracy", b"Locks", &hash, expiry); + } + result + }) + .collect::>(); + for l in locks.iter() { + if l.reasons == Reasons::All || l.reasons == Reasons::Misc { + account.misc_frozen = account.misc_frozen.max(l.amount); + } + if l.reasons == Reasons::All || l.reasons == Reasons::Fee { + account.fee_frozen = account.fee_frozen.max(l.amount); + } + } + put_storage_value(b"Balances", b"Locks", &hash, locks); + } + put_storage_value(b"Balances", b"Account", &hash, account); + } + // Second, migrate old ReservedBalance into new Account. + // ReservedBalance: map T::AccountId => T::Balance + for (hash, reserved) in StorageIterator::::new(b"Balances", b"ReservedBalance").drain() { + let mut account = get_storage_value::>(b"Balances", b"Account", &hash).unwrap_or_default(); + account.reserved = reserved; + put_storage_value(b"Balances", b"Account", &hash, account); + } + + // Finally, migrate vesting and ensure locks are in place. We will be lazy and just lock + // for the maximum amount (i.e. at genesis). Users will need to call "vest" to reduce the + // lock to something sensible. + // pub Vesting: map T::AccountId => Option; + for (hash, vesting) in StorageIterator::<(T::Balance, T::Balance, T::BlockNumber)>::new(b"Balances", b"Vesting").drain() { + let mut account = get_storage_value::>(b"Balances", b"Account", &hash).unwrap_or_default(); + let mut locks = get_storage_value::>>(b"Balances", b"Locks", &hash).unwrap_or_default(); + locks.push(BalanceLock { + id: *b"vesting ", + amount: vesting.0.clone(), + reasons: Reasons::Misc, + }); + account.misc_frozen = account.misc_frozen.max(vesting.0.clone()); + put_storage_value(b"Vesting", b"Vesting", &hash, vesting); + put_storage_value(b"Balances", b"Locks", &hash, locks); + put_storage_value(b"Balances", b"Account", &hash, account); } } - /// Set the free balance of an account to some new value. Will enforce `ExistentialDeposit` - /// law, annulling the account as needed. + /// Get the free balance of an account. + pub fn free_balance(who: impl sp_std::borrow::Borrow) -> T::Balance { + Account::::get(who.borrow()).free + } + + /// Get the balance of an account that can be used for transfers, reservations, or any other + /// non-locking, non-transaction-fee activity. Will be at most `free_balance`. + pub fn usable_balance(who: impl sp_std::borrow::Borrow) -> T::Balance { + Account::::get(who.borrow()).usable(Reasons::Misc) + } + + /// Get the balance of an account that can be used for paying transaction fees (not tipping, + /// or any other kind of fees, though). Will be at most `free_balance`. + pub fn usable_balance_for_fees(who: impl sp_std::borrow::Borrow) -> T::Balance { + Account::::get(who.borrow()).usable(Reasons::Fee) + } + + /// Get the reserved balance of an account. + pub fn reserved_balance(who: impl sp_std::borrow::Borrow) -> T::Balance { + Account::::get(who.borrow()).reserved + } + + /// Set both the free and reserved balance of an account to some new value. Will enforce + /// `ExistentialDeposit` law, annulling the account as needed. /// - /// Doesn't do any preparatory work for creating a new account, so should only be used when it - /// is known that the account already exists. + /// Will return `AccountKilled` if either reserved or free are too low. + /// + /// NOTE: This assumes that `account` is the same as `Self::account(who)` except for altered + /// values of `free` and `balance`. + /// + /// NOTE: Doesn't do any preparatory work for creating a new account, so should only be used + /// when it is known that the account already exists. /// /// NOTE: LOW-LEVEL: This will not attempt to maintain total issuance. It is expected that /// the caller will do this. - fn set_free_balance(who: &T::AccountId, balance: T::Balance) -> UpdateBalanceOutcome { - // Commented out for now - but consider it instructive. - // assert!(!Self::total_balance(who).is_zero()); - // assert!(Self::free_balance(who) > T::ExistentialDeposit::get()); - if balance < T::ExistentialDeposit::get() { - >::insert(who, balance); - Self::on_free_too_low(who); - UpdateBalanceOutcome::AccountKilled + fn set_account( + who: &T::AccountId, + account: &AccountData, + old: &AccountData, + ) -> UpdateBalanceOutcome { + let total = account.free + account.reserved; + if total < T::ExistentialDeposit::get() { + T::DustRemoval::on_unbalanced(NegativeImbalance::new(total)); + if !old.total().is_zero() { + Self::reap_account(who, total); + UpdateBalanceOutcome::AccountKilled + } else { + UpdateBalanceOutcome::StillDead + } } else { - >::insert(who, balance); + if old.total().is_zero() { + Self::about_to_create_account(who, account.free); + } + Account::::insert(who, account); UpdateBalanceOutcome::Updated } } @@ -605,7 +681,7 @@ impl, I: Instance> Module { /// Register a new account (with existential balance). /// /// This just calls appropriate hooks. It doesn't (necessarily) make any state changes. - fn new_account(who: &T::AccountId, balance: T::Balance) { + fn about_to_create_account(who: &T::AccountId, balance: T::Balance) { T::OnNewAccount::on_new_account(&who); Self::deposit_event(RawEvent::NewAccount(who.clone(), balance.clone())); } @@ -614,61 +690,27 @@ impl, I: Instance> Module { /// /// This just removes the nonce and leaves an event. fn reap_account(who: &T::AccountId, dust: T::Balance) { + Locks::::remove(who); + Account::::remove(who); T::OnReapAccount::on_reap_account(who); Self::deposit_event(RawEvent::ReapedAccount(who.clone(), dust)); } - /// Account's free balance has dropped below existential deposit. Kill its - /// free side and the account completely if its reserved size is already dead. - /// - /// Will maintain total issuance. - fn on_free_too_low(who: &T::AccountId) { - let dust = >::take(who); - >::remove(who); - - T::OnFreeBalanceZero::on_free_balance_zero(who); - - let mut reserved_balance = Self::reserved_balance(who); - - if !dust.is_zero() { - if reserved_balance >= T::ExistentialDeposit::get() { - // any individual account cannot cause overflow in balance. - reserved_balance += dust; - Self::set_reserved_balance(who, reserved_balance); - } else { - // underflow should never happen, but if it does, there's not much we can do. - T::DustRemoval::on_unbalanced(NegativeImbalance::new(dust)); + /// Update the account entry for `who`, given the locks. + fn update_locks(who: &T::AccountId, locks: &[BalanceLock]) { + Account::::mutate(who, |b| { + b.misc_frozen = Zero::zero(); + b.fee_frozen = Zero::zero(); + for l in locks.iter() { + if l.reasons == Reasons::All || l.reasons == Reasons::Misc { + b.misc_frozen = b.misc_frozen.max(l.amount); + } + if l.reasons == Reasons::All || l.reasons == Reasons::Fee { + b.fee_frozen = b.fee_frozen.max(l.amount); + } } - } - - if reserved_balance.is_zero() { - Self::reap_account(who, dust); - } - } - - /// Account's reserved balance has dropped below existential deposit. Kill its - /// reserved side and the account completely if its free size is already dead. - /// - /// Will maintain total issuance. - fn on_reserved_too_low(who: &T::AccountId) { - let dust = >::take(who); - - let mut free_balance = Self::free_balance(who); - - if !dust.is_zero() { - if free_balance >= T::ExistentialDeposit::get() { - // any individual account cannot cause overflow in balance. - free_balance += dust; - Self::set_free_balance(who, free_balance); - } else { - // underflow should never happen, but it if does, there's nothing to be done here. - T::DustRemoval::on_unbalanced(NegativeImbalance::new(dust)); - } - } - - if free_balance.is_zero() { - Self::reap_account(who, dust); - } + }); + Locks::::insert(who, locks); } } @@ -866,19 +908,16 @@ impl, I: Instance> frame_system::Trait for ElevatedTrait { } impl, I: Instance> Trait for ElevatedTrait { type Balance = T::Balance; - type OnFreeBalanceZero = T::OnFreeBalanceZero; type OnReapAccount = T::OnReapAccount; type OnNewAccount = T::OnNewAccount; type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = T::ExistentialDeposit; - type TransferFee = T::TransferFee; type CreationFee = T::CreationFee; } -impl, I: Instance> Currency for Module -where +impl, I: Instance> Currency for Module where T::Balance: MaybeSerializeDeserialize + Debug { type Balance = T::Balance; @@ -886,11 +925,10 @@ where type NegativeImbalance = NegativeImbalance; fn total_balance(who: &T::AccountId) -> Self::Balance { - Self::free_balance(who) + Self::reserved_balance(who) + Self::account(who).total() } // Check if `value` amount of free balance can be slashed from `who`. - // Is a no-op if value to be slashed is zero. fn can_slash(who: &T::AccountId, value: Self::Balance) -> bool { if value.is_zero() { return true } Self::free_balance(who) >= value @@ -905,7 +943,7 @@ where } fn free_balance(who: &T::AccountId) -> Self::Balance { - >::get(who) + Account::::get(who).free } // Burn funds from the total issuance, returning a positive imbalance for the amount burned. @@ -950,75 +988,60 @@ where new_balance: T::Balance, ) -> DispatchResult { if amount.is_zero() { return Ok(()) } - if reasons.intersects(WithdrawReason::Reserve | WithdrawReason::Transfer) - && Self::vesting_balance(who) > new_balance - { - Err(Error::::VestingBalance)? - } - let locks = Self::locks(who); - if locks.is_empty() { - return Ok(()) - } - - let now = >::block_number(); - if locks.into_iter() - .all(|l| - now >= l.until - || new_balance >= l.amount - || !l.reasons.intersects(reasons) - ) - { - Ok(()) - } else { - Err(Error::::LiquidityRestrictions.into()) - } + let min_balance = Account::::get(who).frozen(reasons.into()); + ensure!(new_balance >= min_balance, Error::::LiquidityRestrictions); + Ok(()) } // Transfer some free balance from `transactor` to `dest`, respecting existence requirements. - // Is a no-op if value to be transferred is zero. + // Is a no-op if value to be transferred is zero or the `transactor` is the same as `dest`. fn transfer( transactor: &T::AccountId, dest: &T::AccountId, value: Self::Balance, existence_requirement: ExistenceRequirement, ) -> DispatchResult { - if value.is_zero() { return Ok(()) } - let from_balance = Self::free_balance(transactor); - let to_balance = Self::free_balance(dest); - let would_create = to_balance.is_zero(); - let fee = if would_create { T::CreationFee::get() } else { T::TransferFee::get() }; - let liability = value.checked_add(&fee).ok_or(Error::::Overflow)?; - let new_from_balance = from_balance.checked_sub(&liability).ok_or(Error::::InsufficientBalance)?; + if value.is_zero() || transactor == dest { return Ok(()) } - if would_create && value < T::ExistentialDeposit::get() { - Err(Error::::ExistentialDeposit)? - } - Self::ensure_can_withdraw(transactor, value, WithdrawReason::Transfer.into(), new_from_balance)?; + let old_from_account = Self::account(transactor); + let mut from_account = old_from_account.clone(); + let old_to_account = Self::account(dest); + let mut to_account = old_to_account.clone(); + + let would_create = to_account.total().is_zero(); + let fee = if would_create { T::CreationFee::get() } else { Zero::zero() }; + let liability = value.checked_add(&fee).ok_or(Error::::Overflow)?; + + from_account.free = from_account.free.checked_sub(&liability) + .ok_or(Error::::InsufficientBalance)?; // NOTE: total stake being stored in the same type means that this could never overflow // but better to be safe than sorry. - let new_to_balance = to_balance.checked_add(&value).ok_or(Error::::Overflow)?; + to_account.free = to_account.free.checked_add(&value).ok_or(Error::::Overflow)?; - if transactor != dest { - if existence_requirement == ExistenceRequirement::KeepAlive { - if new_from_balance < Self::minimum_balance() { - Err(Error::::KeepAlive)? - } - } + let ed = T::ExistentialDeposit::get(); + ensure!(to_account.free >= ed, Error::::ExistentialDeposit); - Self::set_free_balance(transactor, new_from_balance); - if !>::exists(dest) { - Self::new_account(dest, new_to_balance); - } + Self::ensure_can_withdraw( + transactor, + value, + WithdrawReason::Transfer.into(), + from_account.free, + )?; - // Emit transfer event. - Self::deposit_event(RawEvent::Transfer(transactor.clone(), dest.clone(), value, fee)); + let allow_death = existence_requirement == ExistenceRequirement::AllowDeath; + ensure!(allow_death || from_account.free >= ed, Error::::KeepAlive); - // Take action on the set_free_balance call. - // This will emit events that _resulted_ from the transfer. - Self::set_free_balance(dest, new_to_balance); - T::TransferPayment::on_unbalanced(NegativeImbalance::new(fee)); - } + Self::set_account(transactor, &from_account, &old_from_account); + + // Take action on the set_account call. + // This will emit events that _resulted_ from the transfer. + Self::set_account(dest, &to_account, &old_to_account); + + // Emit transfer event. + Self::deposit_event(RawEvent::Transfer(transactor.clone(), dest.clone(), value, fee)); + + T::TransferPayment::on_unbalanced(NegativeImbalance::new(fee)); Ok(()) } @@ -1033,95 +1056,122 @@ where ) -> result::Result { if value.is_zero() { return Ok(NegativeImbalance::zero()); } - let old_balance = Self::free_balance(who); - if let Some(new_balance) = old_balance.checked_sub(&value) { + let old_account = Self::account(who); + let mut account = old_account.clone(); + if let Some(new_free_account) = account.free.checked_sub(&value) { // if we need to keep the account alive... if liveness == ExistenceRequirement::KeepAlive // ...and it would be dead afterwards... - && new_balance < T::ExistentialDeposit::get() + && new_free_account < T::ExistentialDeposit::get() // ...yet is was alive before - && old_balance >= T::ExistentialDeposit::get() + && account.free >= T::ExistentialDeposit::get() { Err(Error::::KeepAlive)? } - Self::ensure_can_withdraw(who, value, reasons, new_balance)?; - Self::set_free_balance(who, new_balance); + Self::ensure_can_withdraw(who, value, reasons, new_free_account)?; + account.free = new_free_account; + Self::set_account(who, &account, &old_account); Ok(NegativeImbalance::new(value)) } else { Err(Error::::InsufficientBalance)? } } - // Slash an account, returning the negative imbalance created and any left over - // amount that could not be slashed. - // Is a no-op if value to be slashed is zero. + /// Slash a target account `who`, returning the negative imbalance created and any left over + /// amount that could not be slashed. + /// + /// Is a no-op if `value` to be slashed is zero. + /// + /// NOTE: `slash()` prefers free balance, but assumes that reserve balance can be drawn + /// from in extreme circumstances. `can_slash()` should be used prior to `slash()` to avoid having + /// to draw from reserved funds, however we err on the side of punishment if things are inconsistent + /// or `can_slash` wasn't used appropriately. fn slash( who: &T::AccountId, value: Self::Balance ) -> (Self::NegativeImbalance, Self::Balance) { if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) } - let free_balance = Self::free_balance(who); - let free_slash = cmp::min(free_balance, value); + let old_account = Self::account(who); + let mut account = old_account.clone(); + + let free_slash = cmp::min(account.free, value); + account.free -= free_slash; - Self::set_free_balance(who, free_balance - free_slash); let remaining_slash = value - free_slash; - // NOTE: `slash()` prefers free balance, but assumes that reserve balance can be drawn - // from in extreme circumstances. `can_slash()` should be used prior to `slash()` to avoid having - // to draw from reserved funds, however we err on the side of punishment if things are inconsistent - // or `can_slash` wasn't used appropriately. - if !remaining_slash.is_zero() { - let reserved_balance = Self::reserved_balance(who); - let reserved_slash = cmp::min(reserved_balance, remaining_slash); - Self::set_reserved_balance(who, reserved_balance - reserved_slash); + let result = if !remaining_slash.is_zero() { + let reserved_slash = cmp::min(account.reserved, remaining_slash); + account.reserved -= reserved_slash; (NegativeImbalance::new(free_slash + reserved_slash), remaining_slash - reserved_slash) } else { (NegativeImbalance::new(value), Zero::zero()) - } + }; + Self::set_account(who, &account, &old_account); + result } - // Deposit some `value` into the free balance of an existing account. - // Is a no-op if the value to be deposited is zero. + /// Deposit some `value` into the free balance of an existing target account `who`. + /// + /// Is a no-op if the `value` to be deposited is zero. fn deposit_into_existing( who: &T::AccountId, value: Self::Balance ) -> result::Result { if value.is_zero() { return Ok(PositiveImbalance::zero()) } - if Self::total_balance(who).is_zero() { - Err(Error::::DeadAccount)? - } - Self::set_free_balance(who, Self::free_balance(who) + value); + let old_account = Self::account(who); + let mut account = old_account.clone(); + ensure!(!account.total().is_zero(), Error::::DeadAccount); + account.free = account.free.checked_add(&value).ok_or(Error::::Overflow)?; + + Self::set_account(who, &account, &old_account); Ok(PositiveImbalance::new(value)) } - // Deposit some `value` into the free balance of `who`, possibly creating a new account. - // Is a no-op if the value to be deposited is zero. + /// Deposit some `value` into the free balance of `who`, possibly creating a new account. + /// + /// This function is a no-op if: + /// - the `value` to be deposited is zero; or + /// - if the `value` to be deposited is less than the ED and the account does not yet exist; or + /// - `value` is so large it would cause the balance of `who` to overflow. fn deposit_creating( who: &T::AccountId, value: Self::Balance, ) -> Self::PositiveImbalance { if value.is_zero() { return Self::PositiveImbalance::zero() } - let (imbalance, _) = Self::make_free_balance_be(who, Self::free_balance(who) + value); - if let SignedImbalance::Positive(p) = imbalance { - p - } else { - // Impossible, but be defensive. - Self::PositiveImbalance::zero() - } + let old_account = Self::account(who); + let mut account = old_account.clone(); + let ed = T::ExistentialDeposit::get(); + + // bail if not yet created and this operation wouldn't be enough to create it. + if value < ed && account.total().is_zero() { return Self::PositiveImbalance::zero() } + + // defensive only: overflow should never happen, however in case it does, then this + // operation is a no-op. + account.free = match account.free.checked_add(&value) { + Some(f) => f, + None => return Self::PositiveImbalance::zero(), + }; + + Self::set_account(who, &account, &old_account); + + PositiveImbalance::new(value) } - fn make_free_balance_be(who: &T::AccountId, balance: Self::Balance) -> ( + /// Force the new free balance of a target account `who` to some new value `balance`. + fn make_free_balance_be(who: &T::AccountId, value: Self::Balance) -> ( SignedImbalance, UpdateBalanceOutcome ) { - let original = Self::free_balance(who); - if balance < T::ExistentialDeposit::get() && original.is_zero() { + let old_account = Self::account(who); + let mut account = old_account.clone(); + + if value < T::ExistentialDeposit::get() && account.free.is_zero() { // If we're attempting to set an existing account to less than ED, then // bypass the entire operation. It's a no-op if you follow it through, but // since this is an instance where we might account for a negative imbalance - // (in the dust cleaner of set_free_balance) before we account for its actual + // (in the dust cleaner of set_account) before we account for its actual // equal and opposite cause (returned as an Imbalance), then in the // instance that there's no other accounts on the system at all, we might // underflow the issuance and our arithmetic will be off. @@ -1130,42 +1180,31 @@ where UpdateBalanceOutcome::AccountKilled, ) } - let imbalance = if original <= balance { - SignedImbalance::Positive(PositiveImbalance::new(balance - original)) + let imbalance = if account.free <= value { + SignedImbalance::Positive(PositiveImbalance::new(value - account.free)) } else { - SignedImbalance::Negative(NegativeImbalance::new(original - balance)) + SignedImbalance::Negative(NegativeImbalance::new(account.free - value)) }; + account.free = value; + // If the balance is too low, then the account is reaped. - // NOTE: There are two balances for every account: `reserved_balance` and - // `free_balance`. This contract subsystem only cares about the latter: whenever - // the term "balance" is used *here* it should be assumed to mean "free balance" - // in the rest of the module. // Free balance can never be less than ED. If that happens, it gets reduced to zero // and the account information relevant to this subsystem is deleted (i.e. the // account is reaped). - let outcome = if balance < T::ExistentialDeposit::get() { - Self::set_free_balance(who, balance); - UpdateBalanceOutcome::AccountKilled - } else { - if !>::exists(who) { - Self::new_account(&who, balance); - } - Self::set_free_balance(who, balance); - UpdateBalanceOutcome::Updated - }; + let outcome = Self::set_account(who, &account, &old_account); (imbalance, outcome) } } -impl, I: Instance> ReservableCurrency for Module -where +impl, I: Instance> ReservableCurrency for Module where T::Balance: MaybeSerializeDeserialize + Debug { - // Check if `who` can reserve `value` from their free balance. - // Is a no-op if value to be reserved is zero. + /// Check if `who` can reserve `value` from their free balance. + /// + /// Always `true` if value to be reserved is zero. fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool { if value.is_zero() { return true } - Self::free_balance(who) + Self::account(who).free .checked_sub(&value) .map_or(false, |new_balance| Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance).is_ok() @@ -1173,66 +1212,97 @@ where } fn reserved_balance(who: &T::AccountId) -> Self::Balance { - >::get(who) + Self::account(who).reserved } - // Move `value` from the free balance from `who` to their reserved balance. - // Is a no-op if value to be reserved is zero. + /// Move `value` from the free balance from `who` to their reserved balance. + /// + /// Is a no-op if value to be reserved is zero. fn reserve(who: &T::AccountId, value: Self::Balance) -> result::Result<(), DispatchError> { if value.is_zero() { return Ok(()) } - let b = Self::free_balance(who); - if b < value { - Err(Error::::InsufficientBalance)? - } - let new_balance = b - value; - Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), new_balance)?; - Self::set_reserved_balance(who, Self::reserved_balance(who) + value); - Self::set_free_balance(who, new_balance); + + let old_account = Self::account(who); + let mut account = old_account.clone(); + + account.free = account.free.checked_sub(&value).ok_or(Error::::InsufficientBalance)?; + account.reserved = account.reserved.checked_add(&value).ok_or(Error::::Overflow)?; + Self::ensure_can_withdraw(who, value, WithdrawReason::Reserve.into(), account.free)?; + + Self::set_account(who, &account, &old_account); Ok(()) } - // Unreserve some funds, returning any amount that was unable to be unreserved. - // Is a no-op if the value to be unreserved is zero. + /// Unreserve some funds, returning any amount that was unable to be unreserved. + /// + /// Is a no-op if the value to be unreserved is zero. fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance { if value.is_zero() { return Zero::zero() } - let b = Self::reserved_balance(who); - let actual = cmp::min(b, value); - Self::set_free_balance(who, Self::free_balance(who) + actual); - Self::set_reserved_balance(who, b - actual); + + let old_account = Self::account(who); + let mut account = old_account.clone(); + + let actual = cmp::min(account.reserved, value); + account.reserved -= actual; + // defensive only: this can never fail since total issuance which is at least free+reserved + // fits into the same datatype. + account.free = account.free.saturating_add(actual); + + Self::set_account(who, &account, &old_account); + value - actual } - // Slash from reserved balance, returning the negative imbalance created, - // and any amount that was unable to be slashed. - // Is a no-op if the value to be slashed is zero. + /// Slash from reserved balance, returning the negative imbalance created, + /// and any amount that was unable to be slashed. + /// + /// Is a no-op if the value to be slashed is zero. fn slash_reserved( who: &T::AccountId, value: Self::Balance ) -> (Self::NegativeImbalance, Self::Balance) { if value.is_zero() { return (NegativeImbalance::zero(), Zero::zero()) } - let b = Self::reserved_balance(who); - let slash = cmp::min(b, value); + + let old_account = Self::account(who); + let mut account = old_account.clone(); + // underflow should never happen, but it if does, there's nothing to be done here. - Self::set_reserved_balance(who, b - slash); - (NegativeImbalance::new(slash), value - slash) + let actual = cmp::min(account.reserved, value); + account.reserved -= actual; + + Self::set_account(who, &account, &old_account); + + (NegativeImbalance::new(actual), value - actual) } - // Move the reserved balance of one account into the free balance of another. - // Is a no-op if the value to be moved is zero. + /// Move the reserved balance of one account into the free balance of another. + /// + /// Is a no-op if the value to be moved is zero. fn repatriate_reserved( slashed: &T::AccountId, beneficiary: &T::AccountId, value: Self::Balance, ) -> result::Result { if value.is_zero() { return Ok (Zero::zero()) } - if Self::total_balance(beneficiary).is_zero() { - Err(Error::::DeadAccount)? + + if slashed == beneficiary { + return Ok(Self::unreserve(slashed, value)); } - let b = Self::reserved_balance(slashed); - let slash = cmp::min(b, value); - Self::set_free_balance(beneficiary, Self::free_balance(beneficiary) + slash); - Self::set_reserved_balance(slashed, b - slash); - Ok(value - slash) + + let old_to_account = Self::account(beneficiary); + let mut to_account = old_to_account.clone(); + ensure!(!to_account.total().is_zero(), Error::::DeadAccount); + + let old_from_account = Self::account(slashed); + let mut from_account = old_from_account.clone(); + let actual = cmp::min(from_account.reserved, value); + + to_account.free = to_account.free.checked_add(&actual).ok_or(Error::::Overflow)?; + from_account.reserved -= actual; + + Self::set_account(slashed, &from_account, &old_from_account); + Self::set_account(beneficiary, &to_account, &old_to_account); + + Ok(value - actual) } } @@ -1243,119 +1313,62 @@ where type Moment = T::BlockNumber; // Set a lock on the balance of `who`. - // Is a no-op if lock amount is zero. + // Is a no-op if lock amount is zero or `reasons` `is_none()`. fn set_lock( id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - if amount.is_zero() { return } - let now = >::block_number(); - let mut new_lock = Some(BalanceLock { id, amount, until, reasons }); + if amount.is_zero() || reasons.is_none() { return } + let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() }); let mut locks = Self::locks(who).into_iter().filter_map(|l| if l.id == id { new_lock.take() - } else if l.until > now { - Some(l) } else { - None + Some(l) }).collect::>(); if let Some(lock) = new_lock { locks.push(lock) } - >::insert(who, locks); + Self::update_locks(who, &locks[..]); } + // Extend a lock on the balance of `who`. + // Is a no-op if lock amount is zero or `reasons` `is_none()`. fn extend_lock( id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - let now = >::block_number(); - let mut new_lock = Some(BalanceLock { id, amount, until, reasons }); + if amount.is_zero() || reasons.is_none() { return } + let mut new_lock = Some(BalanceLock { id, amount, reasons: reasons.into() }); let mut locks = Self::locks(who).into_iter().filter_map(|l| if l.id == id { new_lock.take().map(|nl| { BalanceLock { id: l.id, amount: l.amount.max(nl.amount), - until: l.until.max(nl.until), reasons: l.reasons | nl.reasons, } }) - } else if l.until > now { - Some(l) } else { - None + Some(l) }).collect::>(); if let Some(lock) = new_lock { locks.push(lock) } - >::insert(who, locks); + Self::update_locks(who, &locks[..]); } fn remove_lock( id: LockIdentifier, who: &T::AccountId, ) { - let now = >::block_number(); - let locks = Self::locks(who).into_iter().filter_map(|l| - if l.until > now && l.id != id { - Some(l) - } else { - None - }).collect::>(); - >::insert(who, locks); - } -} - -impl, I: Instance> VestingCurrency for Module -where - T::Balance: MaybeSerializeDeserialize + Debug -{ - type Moment = T::BlockNumber; - - /// Get the amount that is currently being vested and cannot be transferred out of this account. - fn vesting_balance(who: &T::AccountId) -> T::Balance { - if let Some(v) = Self::vesting(who) { - Self::free_balance(who) - .min(v.locked_at(>::block_number())) - } else { - Zero::zero() - } - } - - /// Adds a vesting schedule to a given account. - /// - /// If there already exists a vesting schedule for the given account, an `Err` is returned - /// and nothing is updated. - /// Is a no-op if the amount to be vested is zero. - fn add_vesting_schedule( - who: &T::AccountId, - locked: T::Balance, - per_block: T::Balance, - starting_block: T::BlockNumber - ) -> DispatchResult { - if locked.is_zero() { return Ok(()) } - if >::exists(who) { - Err(Error::::ExistingVestingSchedule)? - } - let vesting_schedule = VestingSchedule { - locked, - per_block, - starting_block - }; - >::insert(who, vesting_schedule); - Ok(()) - } - - /// Remove a vesting schedule for a given account. - fn remove_vesting_schedule(who: &T::AccountId) { - >::remove(who); + let mut locks = Self::locks(who); + locks.retain(|l| l.id != id); + Self::update_locks(who, &locks[..]); } } @@ -1364,6 +1377,7 @@ where T::Balance: MaybeSerializeDeserialize + Debug { fn is_dead_account(who: &T::AccountId) -> bool { - Self::total_balance(who).is_zero() + // this should always be exactly equivalent to `Self::account(who).total().is_zero()` + !Account::::exists(who) } } diff --git a/substrate/frame/balances/src/migration.rs b/substrate/frame/balances/src/migration.rs new file mode 100644 index 0000000000..4748cf3913 --- /dev/null +++ b/substrate/frame/balances/src/migration.rs @@ -0,0 +1,84 @@ +// Copyright 2017-2020 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 . + +//! Some utilities for helping access storage with arbitrary key types. + +use sp_std::prelude::*; +use codec::{Encode, Decode}; +use frame_support::{StorageHasher, Twox128}; + +pub struct StorageIterator { + prefix: [u8; 32], + previous_key: Vec, + drain: bool, + _phantom: ::sp_std::marker::PhantomData, +} + +impl StorageIterator { + pub fn new(module: &[u8], item: &[u8]) -> Self { + let mut prefix = [0u8; 32]; + prefix[0..16].copy_from_slice(&Twox128::hash(module)); + prefix[16..32].copy_from_slice(&Twox128::hash(item)); + Self { prefix, previous_key: prefix[..].to_vec(), drain: false, _phantom: Default::default() } + } + pub fn drain(mut self) -> Self { + self.drain = true; + self + } +} + +impl Iterator for StorageIterator { + type Item = (Vec, T); + + fn next(&mut self) -> Option<(Vec, T)> { + loop { + let maybe_next = sp_io::storage::next_key(&self.previous_key) + .filter(|n| n.starts_with(&self.prefix)); + break match maybe_next { + Some(next) => { + self.previous_key = next.clone(); + let maybe_value = frame_support::storage::unhashed::get::(&next); + match maybe_value { + Some(value) => { + if self.drain { + frame_support::storage::unhashed::kill(&next); + } + Some((self.previous_key[32..].to_vec(), value)) + } + None => continue, + } + } + None => None, + } + } + } +} + +pub fn get_storage_value(module: &[u8], item: &[u8], hash: &[u8]) -> Option { + let mut key = vec![0u8; 32 + hash.len()]; + key[0..16].copy_from_slice(&Twox128::hash(module)); + key[16..32].copy_from_slice(&Twox128::hash(item)); + key[32..].copy_from_slice(hash); + frame_support::storage::unhashed::get::(&key) +} + +pub fn put_storage_value(module: &[u8], item: &[u8], hash: &[u8], value: T) { + let mut key = vec![0u8; 32 + hash.len()]; + key[0..16].copy_from_slice(&Twox128::hash(module)); + key[16..32].copy_from_slice(&Twox128::hash(item)); + key[32..].copy_from_slice(hash); + frame_support::storage::unhashed::put(&key, &value); +} diff --git a/substrate/frame/balances/src/mock.rs b/substrate/frame/balances/src/mock.rs index 5a3d671e8d..5eb722733f 100644 --- a/substrate/frame/balances/src/mock.rs +++ b/substrate/frame/balances/src/mock.rs @@ -32,7 +32,6 @@ impl_outer_origin!{ thread_local! { pub(crate) static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); - static TRANSFER_FEE: RefCell = RefCell::new(0); static CREATION_FEE: RefCell = RefCell::new(0); } @@ -41,11 +40,6 @@ impl Get for ExistentialDeposit { fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) } } -pub struct TransferFee; -impl Get for TransferFee { - fn get() -> u64 { TRANSFER_FEE.with(|v| *v.borrow()) } -} - pub struct CreationFee; impl Get for CreationFee { fn get() -> u64 { CREATION_FEE.with(|v| *v.borrow()) } @@ -92,32 +86,26 @@ impl pallet_transaction_payment::Trait for Test { } impl Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; 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, } } } @@ -126,11 +114,6 @@ impl ExtBuilder { 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 @@ -142,13 +125,8 @@ impl ExtBuilder { } 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) -> sp_io::TestExternalities { @@ -166,15 +144,6 @@ impl ExtBuilder { } 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() } @@ -187,5 +156,5 @@ pub const CALL: &::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() } + DispatchInfo { weight: w, pays_fee: true, ..Default::default() } } diff --git a/substrate/frame/balances/src/tests.rs b/substrate/frame/balances/src/tests.rs index 175e87aea4..816768901b 100644 --- a/substrate/frame/balances/src/tests.rs +++ b/substrate/frame/balances/src/tests.rs @@ -18,7 +18,7 @@ use super::*; use mock::{Balances, ExtBuilder, Test, System, info_from_weight, CALL}; -use sp_runtime::traits::{SignedExtension, BadOrigin}; +use sp_runtime::{Fixed64, traits::{SignedExtension, BadOrigin}}; use frame_support::{ assert_noop, assert_ok, assert_err, traits::{LockableCurrency, LockIdentifier, WithdrawReason, WithdrawReasons, @@ -29,13 +29,12 @@ use frame_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_eq!(Balances::free_balance(1), 10); + Balances::set_lock(ID_1, &1, 9, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 5, AllowDeath), Error::::LiquidityRestrictions @@ -46,7 +45,7 @@ fn basic_locking_should_work() { #[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()); + Balances::set_lock(ID_1, &1, 5, WithdrawReasons::all()); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); }); } @@ -54,7 +53,7 @@ fn partial_locking_should_work() { #[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::set_lock(ID_1, &1, u64::max_value(), WithdrawReasons::all()); Balances::remove_lock(ID_1, &1); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); }); @@ -63,8 +62,8 @@ fn lock_removal_should_work() { #[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()); + Balances::set_lock(ID_1, &1, u64::max_value(), WithdrawReasons::all()); + Balances::set_lock(ID_1, &1, 5, WithdrawReasons::all()); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); }); } @@ -72,8 +71,8 @@ fn lock_replacement_should_work() { #[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()); + Balances::set_lock(ID_1, &1, 5, WithdrawReasons::all()); + Balances::set_lock(ID_2, &1, 5, WithdrawReasons::all()); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); }); } @@ -81,9 +80,8 @@ fn double_locking_should_work() { #[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()); + Balances::set_lock(ID_1, &1, u64::max_value(), WithdrawReasons::none()); + Balances::set_lock(ID_2, &1, 0, WithdrawReasons::all()); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); }); } @@ -91,17 +89,17 @@ fn combination_locking_should_work() { #[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()); + Balances::set_lock(ID_1, &1, 5, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); - Balances::extend_lock(ID_1, &1, 2, u64::max_value(), WithdrawReasons::all()); + Balances::extend_lock(ID_1, &1, 2, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); - Balances::extend_lock(ID_1, &1, 8, u64::max_value(), WithdrawReasons::all()); + Balances::extend_lock(ID_1, &1, 8, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 3, AllowDeath), Error::::LiquidityRestrictions @@ -116,23 +114,12 @@ fn lock_reasons_should_work() { .monied(true) .build() .execute_with(|| { - Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::Transfer.into()); + pallet_transaction_payment::NextFeeMultiplier::put(Fixed64::from_natural(1)); + Balances::set_lock(ID_1, &1, 10, WithdrawReason::Reserve.into()); assert_noop!( >::transfer(&1, &2, 1, AllowDeath), Error::::LiquidityRestrictions ); - assert_ok!(>::reserve(&1, 1)); - // NOTE: this causes a fee payment. - assert!( 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!(>::transfer(&1, &2, 1, AllowDeath)); assert_noop!( >::reserve(&1, 1), Error::::LiquidityRestrictions @@ -142,10 +129,17 @@ fn lock_reasons_should_work() { &1, CALL, info_from_weight(1), - 0, + 1, + ).is_err()); + assert!( as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &1, + CALL, + info_from_weight(1), + 1, ).is_ok()); - Balances::set_lock(ID_1, &1, 10, u64::max_value(), WithdrawReason::TransactionPayment.into()); + Balances::set_lock(ID_1, &1, 10, WithdrawReason::TransactionPayment.into()); assert_ok!(>::transfer(&1, &2, 1, AllowDeath)); assert_ok!(>::reserve(&1, 1)); assert!( as SignedExtension>::pre_dispatch( @@ -153,40 +147,33 @@ fn lock_reasons_should_work() { &1, CALL, info_from_weight(1), - 0, + 1, + ).is_err()); + assert!( as SignedExtension>::pre_dispatch( + ChargeTransactionPayment::from(0), + &1, + CALL, + info_from_weight(1), + 1, ).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!( - >::transfer(&1, &2, 1, AllowDeath), - Error::::LiquidityRestrictions - ); - - System::set_block_number(2); - assert_ok!(>::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()); + Balances::set_lock(ID_1, &1, 10, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); - Balances::extend_lock(ID_1, &1, 10, 1, WithdrawReasons::all()); + Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); System::set_block_number(2); - Balances::extend_lock(ID_1, &1, 10, 8, WithdrawReasons::all()); + Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::all()); assert_noop!( >::transfer(&1, &2, 3, AllowDeath), Error::::LiquidityRestrictions @@ -197,17 +184,17 @@ fn lock_block_number_extension_should_work() { #[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()); + Balances::set_lock(ID_1, &1, 10, WithdrawReason::Transfer.into()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); - Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReasons::none()); + Balances::extend_lock(ID_1, &1, 10, WithdrawReasons::none()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions ); - Balances::extend_lock(ID_1, &1, 10, 10, WithdrawReason::Reserve.into()); + Balances::extend_lock(ID_1, &1, 10, WithdrawReason::Reserve.into()); assert_noop!( >::transfer(&1, &2, 6, AllowDeath), Error::::LiquidityRestrictions @@ -230,7 +217,7 @@ fn default_indexing_on_new_accounts_should_not_work2() { Error::::ExistentialDeposit, ); assert_eq!(Balances::is_dead_account(&5), true); // account 5 should not exist - assert_eq!(Balances::free_balance(&1), 100); + assert_eq!(Balances::free_balance(1), 100); }); } @@ -247,7 +234,7 @@ fn reserved_balance_should_prevent_reclaim_count() { 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::free_balance(2), 255); // "free" account deleted." assert_eq!(Balances::total_balance(&2), 256 * 20); // reserve still exists. assert_eq!(Balances::is_dead_account(&2), false); assert_eq!(System::account_nonce(&2), 1); @@ -322,11 +309,11 @@ fn dust_account_removal_should_work2() { 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::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::free_balance(2), 0); + assert_eq!(Balances::reserved_balance(2), 0); assert_eq!(Balances::total_balance(&2), 0); }); } @@ -361,14 +348,14 @@ fn reserving_balance_should_work() { 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_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); + assert_eq!(Balances::free_balance(1), 42); + assert_eq!(Balances::reserved_balance(1), 69); }); } @@ -389,7 +376,7 @@ 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); + assert_eq!(Balances::free_balance(1), 42); }); } @@ -397,10 +384,11 @@ fn deducting_balance_should_work() { fn refunding_balance_should_work() { ExtBuilder::default().build().execute_with(|| { let _ = Balances::deposit_creating(&1, 42); - Balances::set_reserved_balance(&1, 69); + let account = Balances::account(&1); + Balances::set_account(&1, &AccountData { reserved: 69, ..account }, &account); Balances::unreserve(&1, 69); - assert_eq!(Balances::free_balance(&1), 111); - assert_eq!(Balances::reserved_balance(&1), 0); + assert_eq!(Balances::free_balance(1), 111); + assert_eq!(Balances::reserved_balance(1), 0); }); } @@ -410,8 +398,8 @@ fn slashing_balance_should_work() { 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!(Balances::free_balance(1), 0); + assert_eq!(Balances::reserved_balance(1), 42); assert_eq!(>::get(), 42); }); } @@ -422,8 +410,8 @@ fn slashing_incomplete_balance_should_work() { 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!(Balances::free_balance(1), 0); + assert_eq!(Balances::reserved_balance(1), 0); assert_eq!(>::get(), 0); }); } @@ -434,8 +422,8 @@ fn unreserving_balance_should_work() { 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); + assert_eq!(Balances::reserved_balance(1), 69); + assert_eq!(Balances::free_balance(1), 42); }); } @@ -445,8 +433,8 @@ fn slashing_reserved_balance_should_work() { 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!(Balances::reserved_balance(1), 69); + assert_eq!(Balances::free_balance(1), 0); assert_eq!(>::get(), 69); }); } @@ -457,8 +445,8 @@ fn slashing_incomplete_reserved_balance_should_work() { 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!(Balances::free_balance(1), 69); + assert_eq!(Balances::reserved_balance(1), 0); assert_eq!(>::get(), 69); }); } @@ -470,10 +458,10 @@ fn transferring_reserved_balance_should_work() { 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); + 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); }); } @@ -493,26 +481,26 @@ fn transferring_incomplete_reserved_balance_should_work() { 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); + 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(|| { - >::insert(1, u64::max_value()); - >::insert(2, 1); + Account::::insert(1, AccountData { free: u64::max_value(), .. Default::default() }); + Account::::insert(2, AccountData { free: 1, .. Default::default() }); assert_err!( Balances::transfer(Some(1).into(), 2, u64::max_value()), Error::::Overflow, ); - assert_eq!(Balances::free_balance(&1), u64::max_value()); - assert_eq!(Balances::free_balance(&2), 1); + assert_eq!(Balances::free_balance(1), u64::max_value()); + assert_eq!(Balances::free_balance(2), 1); }); } @@ -524,7 +512,7 @@ fn account_create_on_free_too_low_with_other() { // No-op. let _ = Balances::deposit_creating(&2, 50); - assert_eq!(Balances::free_balance(&2), 0); + assert_eq!(Balances::free_balance(2), 0); assert_eq!(>::get(), 100); }) } @@ -535,7 +523,7 @@ 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!(Balances::free_balance(2), 0); assert_eq!(>::get(), 0); }) } @@ -549,8 +537,8 @@ fn account_removal_on_free_too_low() { 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!(Balances::free_balance(1), 110); + assert_eq!(Balances::free_balance(2), 110); assert_eq!(>::get(), 220); // Transfer funds from account 1 of such amount that after this transfer @@ -559,8 +547,8 @@ fn account_removal_on_free_too_low() { 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); + 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!(>::get(), 130); @@ -580,160 +568,6 @@ fn transfer_overflow_isnt_exploitable() { }); } -#[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), - Error::::VestingBalance, - ); // 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(|| { @@ -766,45 +600,58 @@ fn cannot_set_genesis_value_below_ed() { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); let _ = GenesisConfig:: { balances: vec![(1, 10)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); } #[test] fn dust_moves_between_free_and_reserved() { ExtBuilder::default() - .existential_deposit(100) - .build() - .execute_with(|| { - // Set balance to free and reserved at the existential deposit - assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 100)); - assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 2, 100, 100)); - // Check balance - assert_eq!(Balances::free_balance(1), 100); - assert_eq!(Balances::reserved_balance(1), 100); - assert_eq!(Balances::free_balance(2), 100); - assert_eq!(Balances::reserved_balance(2), 100); + .existential_deposit(100) + .build() + .execute_with(|| { + // Set balance to free and reserved at the existential deposit + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 0)); + // Check balance + assert_eq!(Balances::free_balance(1), 100); + assert_eq!(Balances::reserved_balance(1), 0); - // Drop 1 free_balance below ED - assert_ok!(Balances::transfer(Some(1).into(), 2, 1)); - // Check balance, the other 99 should move to reserved_balance - assert_eq!(Balances::free_balance(1), 0); - assert_eq!(Balances::reserved_balance(1), 199); + // Reserve some free balance + assert_ok!(Balances::reserve(&1, 50)); + // Check balance, the account should be ok. + assert_eq!(Balances::free_balance(1), 50); + assert_eq!(Balances::reserved_balance(1), 50); - // Reset accounts - assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 100, 100)); - assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 2, 100, 100)); + // Reserve the rest of the free balance + assert_ok!(Balances::reserve(&1, 50)); + // Check balance, the account should be ok. + assert_eq!(Balances::free_balance(1), 0); + assert_eq!(Balances::reserved_balance(1), 100); - // Drop 2 reserved_balance below ED - Balances::unreserve(&2, 1); - // Check balance, all 100 should move to free_balance - assert_eq!(Balances::free_balance(2), 200); - assert_eq!(Balances::reserved_balance(2), 0); - - // An account with both too little free and reserved is completely killed - assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 99, 99)); - // Check balance is 0 for everything - assert_eq!(Balances::free_balance(1), 0); - assert_eq!(Balances::reserved_balance(1), 0); - }); + // Unreserve everything + Balances::unreserve(&1, 100); + // Check balance, all 100 should move to free_balance + assert_eq!(Balances::free_balance(1), 100); + assert_eq!(Balances::reserved_balance(1), 0); + }); +} + +#[test] +fn account_deleted_when_just_dust() { + ExtBuilder::default() + .existential_deposit(100) + .build() + .execute_with(|| { + // Set balance to free and reserved at the existential deposit + assert_ok!(Balances::set_balance(RawOrigin::Root.into(), 1, 50, 50)); + // Check balance + assert_eq!(Balances::free_balance(1), 50); + assert_eq!(Balances::reserved_balance(1), 50); + + // Reserve some free balance + let _ = Balances::slash(&1, 1); + // The account should be dead. + assert!(Balances::is_dead_account(&1)); + assert_eq!(Balances::free_balance(1), 0); + assert_eq!(Balances::reserved_balance(1), 0); + }); } diff --git a/substrate/frame/contracts/src/account_db.rs b/substrate/frame/contracts/src/account_db.rs index 3615673f2d..814983c586 100644 --- a/substrate/frame/contracts/src/account_db.rs +++ b/substrate/frame/contracts/src/account_db.rs @@ -149,7 +149,7 @@ impl AccountDb for DirectAccountDb { let (imbalance, outcome) = T::Currency::make_free_balance_be(&address, balance); total_imbalance = total_imbalance.merge(imbalance); if let UpdateBalanceOutcome::AccountKilled = outcome { - // Account killed. This will ultimately lead to calling `OnFreeBalanceZero` callback + // Account killed. This will ultimately lead to calling `OnReapAccount` callback // which will make removal of CodeHashOf and AccountStorage for this account. // In order to avoid writing over the deleted properties we `continue` here. continue; diff --git a/substrate/frame/contracts/src/exec.rs b/substrate/frame/contracts/src/exec.rs index cfbefa2a72..d0033e0bd1 100644 --- a/substrate/frame/contracts/src/exec.rs +++ b/substrate/frame/contracts/src/exec.rs @@ -573,7 +573,7 @@ impl Token for TransferFeeToken> { let balance_fee = match self.kind { TransferFeeKind::ContractInstantiate => metadata.contract_account_instantiate_fee, TransferFeeKind::AccountCreate => metadata.account_create_fee, - TransferFeeKind::Transfer => metadata.transfer_fee, + TransferFeeKind::Transfer => return metadata.schedule.transfer_cost, }; approx_gas_for_balance(self.gas_price, balance_fee) } diff --git a/substrate/frame/contracts/src/lib.rs b/substrate/frame/contracts/src/lib.rs index ff4a729b2c..bd1e91f1a9 100644 --- a/substrate/frame/contracts/src/lib.rs +++ b/substrate/frame/contracts/src/lib.rs @@ -125,7 +125,7 @@ use frame_support::{ parameter_types, IsSubType, weights::DispatchInfo, }; -use frame_support::traits::{OnFreeBalanceZero, OnUnbalanced, Currency, Get, Time, Randomness}; +use frame_support::traits::{OnReapAccount, OnUnbalanced, Currency, Get, Time, Randomness}; use frame_system::{self as system, ensure_signed, RawOrigin, ensure_root}; use sp_core::storage::well_known_keys::CHILD_STORAGE_KEY_PREFIX; use pallet_contracts_primitives::{RentProjection, ContractAccessError}; @@ -401,9 +401,6 @@ pub trait Trait: frame_system::Trait { /// to removal of a contract. type SurchargeReward: Get>; - /// The fee required to make a transfer. - type TransferFee: Get>; - /// The fee required to create an account. type CreationFee: Get>; @@ -520,9 +517,6 @@ decl_module! { /// to removal of a contract. const SurchargeReward: BalanceOf = T::SurchargeReward::get(); - /// The fee required to make a transfer. - const TransferFee: BalanceOf = T::TransferFee::get(); - /// The fee required to create an account. const CreationFee: BalanceOf = T::CreationFee::get(); @@ -953,8 +947,8 @@ decl_storage! { } } -impl OnFreeBalanceZero for Module { - fn on_free_balance_zero(who: &T::AccountId) { +impl OnReapAccount for Module { + fn on_reap_account(who: &T::AccountId) { if let Some(ContractInfo::Alive(info)) = >::take(who) { child::kill_storage(&info.trie_id, info.child_trie_unique_id()); } @@ -973,7 +967,6 @@ pub struct Config { pub max_value_size: u32, pub contract_account_instantiate_fee: BalanceOf, pub account_create_fee: BalanceOf, - pub transfer_fee: BalanceOf, } impl Config { @@ -986,7 +979,6 @@ impl Config { max_value_size: T::MaxValueSize::get(), contract_account_instantiate_fee: T::ContractFee::get(), account_create_fee: T::CreationFee::get(), - transfer_fee: T::TransferFee::get(), } } } @@ -1031,6 +1023,9 @@ pub struct Schedule { /// Gas cost per one byte written to the sandbox memory. pub sandbox_data_write_cost: Gas, + /// Cost for a simple balance transfer. + pub transfer_cost: Gas, + /// The maximum number of topics supported by an event. pub max_event_topics: u32, @@ -1069,6 +1064,7 @@ impl Default for Schedule { instantiate_base_cost: 175, sandbox_data_read_cost: 1, sandbox_data_write_cost: 1, + transfer_cost: 100, max_event_topics: 4, max_stack_height: 64 * 1024, max_memory_pages: 16, diff --git a/substrate/frame/contracts/src/tests.rs b/substrate/frame/contracts/src/tests.rs index 9a2ef36bb8..5eb7bce48a 100644 --- a/substrate/frame/contracts/src/tests.rs +++ b/substrate/frame/contracts/src/tests.rs @@ -121,14 +121,12 @@ impl frame_system::Trait for Test { } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = Contract; - type OnReapAccount = System; + type OnReapAccount = (System, Contract); type OnNewAccount = (); type Event = MetaEvent; type DustRemoval = (); type TransferPayment = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -171,7 +169,6 @@ impl Trait for Test { type RentByteFee = RentByteFee; type RentDepositOffset = RentDepositOffset; type SurchargeReward = SurchargeReward; - type TransferFee = TransferFee; type CreationFee = CreationFee; type TransactionBaseFee = TransactionBaseFee; type TransactionByteFee = TransactionByteFee; @@ -278,7 +275,6 @@ impl ExtBuilder { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); GenesisConfig:: { current_schedule: Schedule { @@ -311,7 +307,7 @@ fn refunds_unused_gas() { assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, Vec::new())); // 2 * 135 - gas price multiplied by the call base fee. - assert_eq!(Balances::free_balance(&ALICE), 100_000_000 - (2 * 135)); + assert_eq!(Balances::free_balance(ALICE), 100_000_000 - (2 * 135)); }); } @@ -1018,7 +1014,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert_eq!(ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap().rent_allowance, 1_000); - assert_eq!(Balances::free_balance(&BOB), 100); + assert_eq!(Balances::free_balance(BOB), 100); // Advance blocks initialize_block(10); @@ -1026,7 +1022,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent through call assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).unwrap().get_tombstone().is_some()); - assert_eq!(Balances::free_balance(&BOB), subsistence_threshold); + assert_eq!(Balances::free_balance(BOB), subsistence_threshold); // Advance blocks initialize_block(20); @@ -1034,7 +1030,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).unwrap().get_tombstone().is_some()); - assert_eq!(Balances::free_balance(&BOB), subsistence_threshold); + assert_eq!(Balances::free_balance(BOB), subsistence_threshold); }); // Allowance exceeded @@ -1052,7 +1048,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert_eq!(ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap().rent_allowance, 100); - assert_eq!(Balances::free_balance(&BOB), 1_000); + assert_eq!(Balances::free_balance(BOB), 1_000); // Advance blocks initialize_block(10); @@ -1061,7 +1057,7 @@ fn removals(trigger_call: impl Fn() -> bool) { assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).unwrap().get_tombstone().is_some()); // Balance should be initial balance - initial rent_allowance - assert_eq!(Balances::free_balance(&BOB), 900); + assert_eq!(Balances::free_balance(BOB), 900); // Advance blocks initialize_block(20); @@ -1069,7 +1065,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).unwrap().get_tombstone().is_some()); - assert_eq!(Balances::free_balance(&BOB), 900); + assert_eq!(Balances::free_balance(BOB), 900); }); // Balance reached and inferior to subsistence threshold @@ -1087,12 +1083,12 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert_eq!(ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap().rent_allowance, 1_000); - assert_eq!(Balances::free_balance(&BOB), 50 + Balances::minimum_balance()); + assert_eq!(Balances::free_balance(BOB), 50 + Balances::minimum_balance()); // Transfer funds assert_ok!(Contract::call(Origin::signed(ALICE), BOB, 0, 100_000, call::transfer())); assert_eq!(ContractInfoOf::::get(BOB).unwrap().get_alive().unwrap().rent_allowance, 1_000); - assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance()); + assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance()); // Advance blocks initialize_block(10); @@ -1100,7 +1096,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent through call assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).is_none()); - assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance()); + assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance()); // Advance blocks initialize_block(20); @@ -1108,7 +1104,7 @@ fn removals(trigger_call: impl Fn() -> bool) { // Trigger rent must have no effect assert!(trigger_call()); assert!(ContractInfoOf::::get(BOB).is_none()); - assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance()); + assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance()); }); } diff --git a/substrate/frame/democracy/Cargo.toml b/substrate/frame/democracy/Cargo.toml index 12d5dfc158..2428d2c154 100644 --- a/substrate/frame/democracy/Cargo.toml +++ b/substrate/frame/democracy/Cargo.toml @@ -17,6 +17,8 @@ frame-system = { version = "2.0.0", default-features = false, path = "../system" [dev-dependencies] sp-core = { version = "2.0.0", path = "../../primitives/core" } pallet-balances = { version = "2.0.0", path = "../balances" } +sp-storage = { version = "2.0.0", path = "../../primitives/storage" } +hex-literal = "0.2.1" [features] default = ["std"] diff --git a/substrate/frame/democracy/src/lib.rs b/substrate/frame/democracy/src/lib.rs index 682ce59f25..da0cccb1d0 100644 --- a/substrate/frame/democracy/src/lib.rs +++ b/substrate/frame/democracy/src/lib.rs @@ -30,7 +30,7 @@ use frame_support::{ weights::SimpleDispatchInfo, traits::{ Currency, ReservableCurrency, LockableCurrency, WithdrawReason, LockIdentifier, Get, - OnFreeBalanceZero, OnUnbalanced + OnReapAccount, OnUnbalanced } }; use frame_system::{self as system, ensure_signed, ensure_root}; @@ -305,6 +305,10 @@ decl_storage! { pub Delegations get(fn delegations): linked_map hasher(blake2_256) T::AccountId => (T::AccountId, Conviction); + /// Accounts for which there are locks in action which may be removed at some point in the + /// future. The value is the block number at which the lock expires and may be removed. + pub Locks get(locks): map hasher(blake2_256) T::AccountId => Option; + /// True if the last referendum tabled was submitted externally. False if it was a public /// proposal. pub LastTabledWasExternal: bool; @@ -364,6 +368,8 @@ decl_event! { PreimageMissing(Hash, ReferendumIndex), /// A registered preimage was removed and the deposit collected by the reaper (last item). PreimageReaped(Hash, AccountId, Balance, AccountId), + /// An account has been unlocked successfully. + Unlocked(AccountId), } } @@ -413,6 +419,10 @@ decl_error! { PreimageInvalid, /// No proposals waiting NoneWaiting, + /// The target account does not have a lock. + NotLocked, + /// The lock on the account to be unlocked has not yet expired. + NotExpired, } } @@ -701,9 +711,9 @@ decl_module! { DEMOCRACY_ID, &who, Bounded::max_value(), - T::BlockNumber::max_value(), WithdrawReason::Transfer.into() ); + Locks::::remove(&who); Self::deposit_event(RawEvent::Delegated(who, to)); } @@ -720,12 +730,12 @@ decl_module! { // Indefinite lock is reduced to the maximum voting lock that could be possible. let now = >::block_number(); let locked_until = now + T::EnactmentPeriod::get() * conviction.lock_periods().into(); + Locks::::insert(&who, locked_until); T::Currency::set_lock( DEMOCRACY_ID, &who, Bounded::max_value(), - locked_until, - WithdrawReason::Transfer.into() + WithdrawReason::Transfer.into(), ); Self::deposit_event(RawEvent::Undelegated(who)); } @@ -796,6 +806,18 @@ decl_module! { >::remove(&proposal_hash); Self::deposit_event(RawEvent::PreimageReaped(proposal_hash, old, deposit, who)); } + + #[weight = SimpleDispatchInfo::FixedNormal(10_000)] + fn unlock(origin, target: T::AccountId) { + ensure_signed(origin)?; + + let expiry = Locks::::get(&target).ok_or(Error::::NotLocked)?; + ensure!(expiry <= system::Module::::block_number(), Error::::NotExpired); + + T::Currency::remove_lock(DEMOCRACY_ID, &target); + Locks::::remove(&target); + Self::deposit_event(RawEvent::Unlocked(target)); + } } } @@ -1081,14 +1103,15 @@ impl Module { // now plus: the base lock period multiplied by the number of periods this voter // offered to lock should they win... let locked_until = now + T::EnactmentPeriod::get() * conviction.lock_periods().into(); + Locks::::insert(&a, locked_until); // ...extend their bondage until at least then. T::Currency::extend_lock( DEMOCRACY_ID, &a, Bounded::max_value(), - locked_until, WithdrawReason::Transfer.into() ); + } Self::clear_referendum(index); @@ -1139,8 +1162,8 @@ impl Module { } } -impl OnFreeBalanceZero for Module { - fn on_free_balance_zero(who: &T::AccountId) { +impl OnReapAccount for Module { + fn on_reap_account(who: &T::AccountId) { >::remove(who) } } @@ -1155,11 +1178,12 @@ mod tests { }; use sp_core::H256; use sp_runtime::{ - traits::{BlakeTwo256, IdentityLookup, Bounded, BadOrigin}, + traits::{BlakeTwo256, IdentityLookup, Bounded, BadOrigin, OnInitialize}, testing::Header, Perbill, }; use pallet_balances::{BalanceLock, Error as BalancesError}; use frame_system::EnsureSignedBy; + use sp_storage::Storage; const AYE: Vote = Vote{ aye: true, conviction: Conviction::None }; const NAY: Vote = Vote{ aye: false, conviction: Conviction::None }; @@ -1206,19 +1230,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -1273,7 +1294,6 @@ mod tests { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig::{ balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); GenesisConfig::default().assimilate_storage(&mut t).unwrap(); sp_io::TestExternalities::new(t) @@ -1283,11 +1303,60 @@ mod tests { type Balances = pallet_balances::Module; type Democracy = Module; + #[test] + fn lock_info_via_migration_should_work() { + let mut s = Storage::default(); + use hex_literal::hex; + // A dump of data from the previous version for which we know account 1 has 5 of its 10 + // reserved and 3 of the rest is locked for misc. Account 2 has all 20 locked until block 5 + // for everything and additionally 3 locked for just fees. + let data = vec![ + (hex!["26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac"].to_vec(), hex!["0100000000000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850"].to_vec(), hex!["02000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"].to_vec(), hex!["08000000000000000000000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef78a42f33323cb5ced3b44dd825fda9fcc"].to_vec(), hex!["4545454545454545454545454545454545454545454545454545454545454545"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef7a44704b568d21667356a5a050c11874681e47a19e6b29b0a65b9591762ce5143ed30d0261e5d24a3201752506b20f15c"].to_vec(), hex!["4545454545454545454545454545454545454545454545454545454545454545"].to_vec()), + (hex!["3a636f6465"].to_vec(), hex![""].to_vec()), + (hex!["3a65787472696e7369635f696e646578"].to_vec(), hex!["00000000"].to_vec()), + (hex!["3a686561707061676573"].to_vec(), hex!["0800000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc61dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["046d697363202020200300000000000000ffffffffffffffff04"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc66cddb367afbd583bb48f9bbd7d5ba3b1d0738b4881b1cddd38169526d8158137"].to_vec(), hex!["0474786665657320200300000000000000ffffffffffffffff01"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc6e88b43fded6323ef02ffeffbd8c40846ee09bf316271bd22369659c959dd733a"].to_vec(), hex!["08616c6c20202020200300000000000000ffffffffffffffff1f64656d6f63726163ffffffffffffffff030000000000000002"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f3c22813def93ef32c365b55cb92f10f91dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["0500000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80"].to_vec(), hex!["d200000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f5f27b51b5ec208ee9cb25b55d8728243b8788bb218b185b63e3e92653953f29b6b143fb8cf5159fc908632e6fe490501"].to_vec(), hex!["1e0000000000000006000000000000000200000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b41dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["0500000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b46cddb367afbd583bb48f9bbd7d5ba3b1d0738b4881b1cddd38169526d8158137"].to_vec(), hex!["1e00000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4b8788bb218b185b63e3e92653953f29b6b143fb8cf5159fc908632e6fe490501"].to_vec(), hex!["3c00000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4e88b43fded6323ef02ffeffbd8c40846ee09bf316271bd22369659c959dd733a"].to_vec(), hex!["1400000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4e96760d274653a39b429a87ebaae9d3aa4fdf58b9096cf0bebc7c4e5a4c2ed8d"].to_vec(), hex!["2800000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4effb728943197fd12e694cbf3f3ede28fbf7498b0370c6dfa0013874b417c178"].to_vec(), hex!["3200000000000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911b7f17cdfbfa73331856cca0acddd7842e"].to_vec(), hex!["00000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911bbdcb0c5143a8617ed38ae3810dd45bc6"].to_vec(), hex!["00000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911be2f6cb0456905c189bcb0458f9440f13"].to_vec(), hex!["00000000"].to_vec()), + ]; + s.top = data.into_iter().collect(); + sp_io::TestExternalities::new(s).execute_with(|| { + Balances::on_initialize(1); + assert_eq!(Balances::free_balance(1), 5); + assert_eq!(Balances::reserved_balance(1), 5); + assert_eq!(Balances::usable_balance(&1), 2); + assert_eq!(Balances::usable_balance_for_fees(&1), 5); + assert_eq!(Balances::free_balance(2), 20); + assert_eq!(Balances::reserved_balance(2), 0); + assert_eq!(Balances::usable_balance(&2), 0); + assert_eq!(Balances::usable_balance_for_fees(&2), 17); + fast_forward_to(5); + assert_ok!(Democracy::unlock(Origin::signed(2), 2)); + assert_eq!(Balances::usable_balance(&2), 17); + }); + } + #[test] fn params_should_work() { new_test_ext().execute_with(|| { assert_eq!(Democracy::referendum_count(), 0); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); assert_eq!(Balances::total_issuance(), 210); }); } @@ -1353,7 +1422,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); }); } @@ -1405,8 +1474,8 @@ mod tests { next_block(); assert_ok!(Democracy::reap_preimage(Origin::signed(6), set_balance_proposal_hash(2))); - assert_eq!(Balances::reserved_balance(6), 0); assert_eq!(Balances::free_balance(6), 60); + assert_eq!(Balances::reserved_balance(6), 0); }); } @@ -1869,7 +1938,7 @@ mod tests { // referendum passes and wait another two blocks for enactment. fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -1942,7 +2011,7 @@ mod tests { assert_eq!(Democracy::tally(r), (1, 0, 1)); fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -1967,7 +2036,7 @@ mod tests { fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -1993,7 +2062,7 @@ mod tests { fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2020,7 +2089,7 @@ mod tests { fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2048,7 +2117,7 @@ mod tests { fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2080,7 +2149,7 @@ mod tests { fast_forward_to(6); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2093,9 +2162,9 @@ mod tests { assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0)); - assert_eq!(Balances::free_balance(&1), 5); - assert_eq!(Balances::free_balance(&2), 15); - assert_eq!(Balances::free_balance(&5), 35); + assert_eq!(Balances::free_balance(1), 5); + assert_eq!(Balances::free_balance(2), 15); + assert_eq!(Balances::free_balance(5), 35); }); } @@ -2109,9 +2178,9 @@ mod tests { assert_ok!(Democracy::second(Origin::signed(5), 0)); assert_ok!(Democracy::second(Origin::signed(5), 0)); fast_forward_to(3); - assert_eq!(Balances::free_balance(&1), 10); - assert_eq!(Balances::free_balance(&2), 20); - assert_eq!(Balances::free_balance(&5), 50); + assert_eq!(Balances::free_balance(1), 10); + assert_eq!(Balances::free_balance(2), 20); + assert_eq!(Balances::free_balance(5), 50); }); } @@ -2179,7 +2248,7 @@ mod tests { assert_eq!(Democracy::tally(r2), (1, 0, 1)); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); assert_ok!(Democracy::vote(Origin::signed(1), r1, AYE)); assert_eq!(Democracy::voters_for(r1), vec![1]); @@ -2187,7 +2256,7 @@ mod tests { assert_eq!(Democracy::tally(r1), (1, 0, 1)); next_block(); - assert_eq!(Balances::free_balance(&42), 3); + assert_eq!(Balances::free_balance(42), 3); }); } @@ -2210,7 +2279,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2230,7 +2299,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); }); } @@ -2253,7 +2322,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); }); } @@ -2280,7 +2349,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2304,11 +2373,11 @@ mod tests { assert_eq!(Democracy::tally(r), (21, 0, 21)); next_block(); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2330,14 +2399,14 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); }); } #[test] fn passing_low_turnout_voting_should_work() { new_test_ext().execute_with(|| { - assert_eq!(Balances::free_balance(&42), 0); + assert_eq!(Balances::free_balance(42), 0); assert_eq!(Balances::total_issuance(), 210); System::set_block_number(1); @@ -2356,7 +2425,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } @@ -2399,24 +2468,44 @@ mod tests { assert_eq!(Balances::locks(2), vec![BalanceLock { id: DEMOCRACY_ID, amount: u64::max_value(), - until: 18, - reasons: WithdrawReason::Transfer.into() + reasons: pallet_balances::Reasons::Misc, }]); + assert_eq!(Democracy::locks(2), Some(18)); assert_eq!(Balances::locks(3), vec![BalanceLock { id: DEMOCRACY_ID, amount: u64::max_value(), - until: 10, - reasons: WithdrawReason::Transfer.into() + reasons: pallet_balances::Reasons::Misc, }]); + assert_eq!(Democracy::locks(3), Some(10)); assert_eq!(Balances::locks(4), vec![BalanceLock { id: DEMOCRACY_ID, amount: u64::max_value(), - until: 6, - reasons: WithdrawReason::Transfer.into() + reasons: pallet_balances::Reasons::Misc, }]); + assert_eq!(Democracy::locks(4), Some(6)); assert_eq!(Balances::locks(5), vec![]); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); + + assert_noop!(Democracy::unlock(Origin::signed(1), 1), Error::::NotLocked); + + fast_forward_to(5); + assert_noop!(Democracy::unlock(Origin::signed(1), 4), Error::::NotExpired); + fast_forward_to(6); + assert_ok!(Democracy::unlock(Origin::signed(1), 4)); + assert_noop!(Democracy::unlock(Origin::signed(1), 4), Error::::NotLocked); + + fast_forward_to(9); + assert_noop!(Democracy::unlock(Origin::signed(1), 3), Error::::NotExpired); + fast_forward_to(10); + assert_ok!(Democracy::unlock(Origin::signed(1), 3)); + assert_noop!(Democracy::unlock(Origin::signed(1), 3), Error::::NotLocked); + + fast_forward_to(17); + assert_noop!(Democracy::unlock(Origin::signed(1), 2), Error::::NotExpired); + fast_forward_to(18); + assert_ok!(Democracy::unlock(Origin::signed(1), 2)); + assert_noop!(Democracy::unlock(Origin::signed(1), 2), Error::::NotLocked); }); } @@ -2453,7 +2542,7 @@ mod tests { next_block(); next_block(); - assert_eq!(Balances::free_balance(&42), 2); + assert_eq!(Balances::free_balance(42), 2); }); } } diff --git a/substrate/frame/elections-phragmen/src/lib.rs b/substrate/frame/elections-phragmen/src/lib.rs index e022b24f01..33d4c46e91 100644 --- a/substrate/frame/elections-phragmen/src/lib.rs +++ b/substrate/frame/elections-phragmen/src/lib.rs @@ -83,7 +83,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use sp_std::prelude::*; -use sp_runtime::{print, DispatchResult, DispatchError, traits::{Zero, StaticLookup, Bounded, Convert}}; +use sp_runtime::{print, DispatchResult, DispatchError, traits::{Zero, StaticLookup, Convert}}; use frame_support::{ decl_storage, decl_event, ensure, decl_module, decl_error, weights::SimpleDispatchInfo, traits::{ @@ -260,7 +260,6 @@ decl_module! { MODULE_ID, &who, locked_balance, - T::BlockNumber::max_value(), WithdrawReasons::except(WithdrawReason::TransactionPayment), ); >::insert(&who, locked_balance); @@ -816,20 +815,17 @@ mod tests { parameter_types! { pub const ExistentialDeposit: u64 = 1; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; type OnNewAccount = (); - type OnFreeBalanceZero = (); type OnReapAccount = System; type Event = Event; type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -989,7 +985,6 @@ mod tests { (5, 50 * self.balance_factor), (6, 60 * self.balance_factor) ], - vesting: vec![], }), }.build_storage().unwrap().into() } diff --git a/substrate/frame/elections/src/lib.rs b/substrate/frame/elections/src/lib.rs index 504aa5d8a0..899fcfac8a 100644 --- a/substrate/frame/elections/src/lib.rs +++ b/substrate/frame/elections/src/lib.rs @@ -26,7 +26,7 @@ use sp_std::prelude::*; use sp_runtime::{ RuntimeDebug, DispatchResult, print, - traits::{Zero, One, StaticLookup, Bounded, Saturating}, + traits::{Zero, One, StaticLookup, Saturating}, }; use frame_support::{ decl_storage, decl_event, ensure, decl_module, decl_error, @@ -894,7 +894,6 @@ impl Module { MODULE_ID, &who, locked_balance, - T::BlockNumber::max_value(), WithdrawReasons::all(), ); diff --git a/substrate/frame/elections/src/mock.rs b/substrate/frame/elections/src/mock.rs index 178637e088..3274dd57b4 100644 --- a/substrate/frame/elections/src/mock.rs +++ b/substrate/frame/elections/src/mock.rs @@ -58,19 +58,16 @@ impl frame_system::Trait for Test { parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; type OnNewAccount = (); - type OnFreeBalanceZero = (); type OnReapAccount = System; type Event = Event; type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -222,7 +219,6 @@ impl ExtBuilder { (5, 50 * self.balance_factor), (6, 60 * self.balance_factor) ], - vesting: vec![], }), elections: Some(elections::GenesisConfig::{ members: vec![], @@ -270,7 +266,8 @@ pub(crate) fn create_candidate(i: u64, index: u32) { } pub(crate) fn balances(who: &u64) -> (u64, u64) { - (Balances::free_balance(who), Balances::reserved_balance(who)) + let a = Balances::account(who); + (a.free, a.reserved) } pub(crate) fn locks(who: &u64) -> Vec { diff --git a/substrate/frame/elections/src/tests.rs b/substrate/frame/elections/src/tests.rs index 48161bcee4..e26f031290 100644 --- a/substrate/frame/elections/src/tests.rs +++ b/substrate/frame/elections/src/tests.rs @@ -1179,14 +1179,14 @@ fn election_present_when_presenter_is_poor_should_not_work() { // -3 assert_ok!(Elections::submit_candidacy(Origin::signed(1), 0)); - assert_eq!(Balances::free_balance(&1), 12); + assert_eq!(Balances::free_balance(1), 12); // -2 -5 assert_ok!(Elections::set_approvals(Origin::signed(1), vec![true], 0, 0, 15)); assert_ok!(Elections::end_block(System::block_number())); System::set_block_number(6); - assert_eq!(Balances::free_balance(&1), 5); - assert_eq!(Balances::reserved_balance(&1), 5); + assert_eq!(Balances::free_balance(1), 5); + assert_eq!(Balances::reserved_balance(1), 5); if p > 5 { assert_noop!(Elections::present_winner( Origin::signed(1), 1, 10, 0), diff --git a/substrate/frame/example/src/lib.rs b/substrate/frame/example/src/lib.rs index 56d3de14dc..b3a86b6acd 100644 --- a/substrate/frame/example/src/lib.rs +++ b/substrate/frame/example/src/lib.rs @@ -690,19 +690,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } impl Trait for Test { diff --git a/substrate/frame/executive/src/lib.rs b/substrate/frame/executive/src/lib.rs index cafb9fad27..936da70211 100644 --- a/substrate/frame/executive/src/lib.rs +++ b/substrate/frame/executive/src/lib.rs @@ -454,19 +454,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Runtime { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = MetaEvent; type DustRemoval = (); type TransferPayment = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -528,7 +525,6 @@ mod tests { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![(1, 211)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); let xt = sp_runtime::testing::TestXt(sign_extra(1, 0, 0), Call::Balances(BalancesCall::transfer(2, 69))); let weight = xt.get_dispatch_info().weight as u64; @@ -552,7 +548,6 @@ mod tests { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![(1, 111 * balance_factor)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } @@ -564,7 +559,7 @@ mod tests { header: Header { parent_hash: [69u8; 32].into(), number: 1, - state_root: hex!("c6b01b27df520ba23adb96e7fc032acb7c586ba1b477c6282de43184111f2091").into(), + state_root: hex!("a0b84fec49718caf59350dab6ec2993f12db399a7cccdb80f3cf79618ed93bd8").into(), extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), digest: Digest { logs: vec![], }, }, @@ -711,7 +706,6 @@ mod tests { id, &1, 110, - Bounded::max_value(), lock, ); let xt = sp_runtime::testing::TestXt( diff --git a/substrate/frame/generic-asset/src/lib.rs b/substrate/frame/generic-asset/src/lib.rs index 8b7668dc12..3b8ebe0535 100644 --- a/substrate/frame/generic-asset/src/lib.rs +++ b/substrate/frame/generic-asset/src/lib.rs @@ -427,10 +427,9 @@ decl_module! { } #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug)] -pub struct BalanceLock { +pub struct BalanceLock { pub id: LockIdentifier, pub amount: Balance, - pub until: BlockNumber, pub reasons: WithdrawReasons, } @@ -459,7 +458,7 @@ decl_storage! { /// Any liquidity locks on some account balances. pub Locks get(fn locks): - map hasher(blake2_256) T::AccountId => Vec>; + map hasher(blake2_256) T::AccountId => Vec>; /// The identity of the asset which is the one that is designated for the chain's staking system. pub StakingAssetId get(fn staking_asset_id) config(): T::AssetId; @@ -796,10 +795,8 @@ impl Module { if locks.is_empty() { return Ok(()); } - let now = >::block_number(); if Self::locks(who) - .into_iter() - .all(|l| now >= l.until || new_balance >= l.amount || !l.reasons.intersects(reasons)) + .into_iter().all(|l| new_balance >= l.amount || !l.reasons.intersects(reasons)) { Ok(()) } else { @@ -825,14 +822,11 @@ impl Module { id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - let now = >::block_number(); let mut new_lock = Some(BalanceLock { id, amount, - until, reasons, }); let mut locks = >::locks(who) @@ -840,10 +834,8 @@ impl Module { .filter_map(|l| { if l.id == id { new_lock.take() - } else if l.until > now { - Some(l) } else { - None + Some(l) } }) .collect::>(); @@ -857,14 +849,11 @@ impl Module { id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - let now = >::block_number(); let mut new_lock = Some(BalanceLock { id, amount, - until, reasons, }); let mut locks = >::locks(who) @@ -874,13 +863,10 @@ impl Module { new_lock.take().map(|nl| BalanceLock { id: l.id, amount: l.amount.max(nl.amount), - until: l.until.max(nl.until), reasons: l.reasons | nl.reasons, }) - } else if l.until > now { - Some(l) } else { - None + Some(l) } }) .collect::>(); @@ -891,11 +877,8 @@ impl Module { } fn remove_lock(id: LockIdentifier, who: &T::AccountId) { - let now = >::block_number(); - let locks = >::locks(who) - .into_iter() - .filter_map(|l| if l.until > now && l.id != id { Some(l) } else { None }) - .collect::>(); + let mut locks = >::locks(who); + locks.retain(|l| l.id != id); >::insert(who, locks); } } @@ -1339,20 +1322,18 @@ where id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - >::set_lock(id, who, amount, until, reasons) + >::set_lock(id, who, amount, reasons) } fn extend_lock( id: LockIdentifier, who: &T::AccountId, amount: T::Balance, - until: T::BlockNumber, reasons: WithdrawReasons, ) { - >::extend_lock(id, who, amount, until, reasons) + >::extend_lock(id, who, amount, reasons) } fn remove_lock(id: LockIdentifier, who: &T::AccountId) { diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index b16e87ff81..56960b2d1d 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -925,19 +925,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -981,7 +978,6 @@ mod tests { (20, 100), (30, 100), ], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } diff --git a/substrate/frame/nicks/src/lib.rs b/substrate/frame/nicks/src/lib.rs index 1067e9a5de..82aea43ad4 100644 --- a/substrate/frame/nicks/src/lib.rs +++ b/substrate/frame/nicks/src/lib.rs @@ -288,19 +288,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -334,7 +331,6 @@ mod tests { (1, 10), (2, 10), ], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } @@ -359,9 +355,9 @@ mod tests { ); assert_ok!(Nicks::set_name(Origin::signed(2), b"Dave".to_vec())); - assert_eq!(Balances::reserved_balance(&2), 2); + assert_eq!(Balances::reserved_balance(2), 2); assert_ok!(Nicks::force_name(Origin::signed(1), 2, b"Dr. David Brubeck, III".to_vec())); - assert_eq!(Balances::reserved_balance(&2), 2); + assert_eq!(Balances::reserved_balance(2), 2); assert_eq!(>::get(2).unwrap(), (b"Dr. David Brubeck, III".to_vec(), 2)); }); } @@ -370,18 +366,18 @@ mod tests { fn normal_operation_should_work() { new_test_ext().execute_with(|| { assert_ok!(Nicks::set_name(Origin::signed(1), b"Gav".to_vec())); - assert_eq!(Balances::reserved_balance(&1), 2); - assert_eq!(Balances::free_balance(&1), 8); + assert_eq!(Balances::reserved_balance(1), 2); + assert_eq!(Balances::free_balance(1), 8); assert_eq!(>::get(1).unwrap().0, b"Gav".to_vec()); assert_ok!(Nicks::set_name(Origin::signed(1), b"Gavin".to_vec())); - assert_eq!(Balances::reserved_balance(&1), 2); - assert_eq!(Balances::free_balance(&1), 8); + assert_eq!(Balances::reserved_balance(1), 2); + assert_eq!(Balances::free_balance(1), 8); assert_eq!(>::get(1).unwrap().0, b"Gavin".to_vec()); assert_ok!(Nicks::clear_name(Origin::signed(1))); - assert_eq!(Balances::reserved_balance(&1), 0); - assert_eq!(Balances::free_balance(&1), 10); + assert_eq!(Balances::reserved_balance(1), 0); + assert_eq!(Balances::free_balance(1), 10); }); } diff --git a/substrate/frame/recovery/src/mock.rs b/substrate/frame/recovery/src/mock.rs index be9ed5c97e..fa074e1faf 100644 --- a/substrate/frame/recovery/src/mock.rs +++ b/substrate/frame/recovery/src/mock.rs @@ -81,20 +81,17 @@ impl frame_system::Trait for Test { parameter_types! { pub const ExistentialDeposit: u64 = 1; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u128; - type OnFreeBalanceZero = (); type OnReapAccount = (System, Recovery); type OnNewAccount = (); type Event = TestEvent; type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -126,7 +123,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![(1, 100), (2, 100), (3, 100), (4, 100), (5, 100)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } diff --git a/substrate/frame/recovery/src/tests.rs b/substrate/frame/recovery/src/tests.rs index 76f05156ef..cfc8e25b12 100644 --- a/substrate/frame/recovery/src/tests.rs +++ b/substrate/frame/recovery/src/tests.rs @@ -35,7 +35,7 @@ fn basic_setup_works() { assert_eq!(Recovery::active_recovery(&1, &2), None); assert_eq!(Recovery::recovery_config(&1), None); // Everyone should have starting balance of 100 - assert_eq!(Balances::free_balance(&1), 100); + assert_eq!(Balances::free_balance(1), 100); }); } @@ -219,7 +219,7 @@ fn initiate_recovery_handles_basic_errors() { assert_ok!(Recovery::initiate_recovery(Origin::signed(1), 5)); assert_noop!(Recovery::initiate_recovery(Origin::signed(1), 5), Error::::AlreadyStarted); // No double deposit - assert_eq!(Balances::reserved_balance(&1), 10); + assert_eq!(Balances::reserved_balance(1), 10); }); } @@ -234,7 +234,7 @@ fn initiate_recovery_works() { // Recovery can be initiated assert_ok!(Recovery::initiate_recovery(Origin::signed(1), 5)); // Deposit is reserved - assert_eq!(Balances::reserved_balance(&1), 10); + assert_eq!(Balances::reserved_balance(1), 10); // Recovery status object is created correctly let recovery_status = ActiveRecovery { created: 1, diff --git a/substrate/frame/scored-pool/src/mock.rs b/substrate/frame/scored-pool/src/mock.rs index dd59bbc84f..fa8b11e69e 100644 --- a/substrate/frame/scored-pool/src/mock.rs +++ b/substrate/frame/scored-pool/src/mock.rs @@ -47,7 +47,6 @@ parameter_types! { pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } ord_parameter_types! { @@ -76,14 +75,12 @@ impl frame_system::Trait for Test { impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -144,7 +141,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities { (40, 500_000), (99, 1), ], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); GenesisConfig::{ pool: vec![ diff --git a/substrate/frame/scored-pool/src/tests.rs b/substrate/frame/scored-pool/src/tests.rs index d8c887f4ec..4b21339505 100644 --- a/substrate/frame/scored-pool/src/tests.rs +++ b/substrate/frame/scored-pool/src/tests.rs @@ -30,8 +30,8 @@ type Balances = pallet_balances::Module; fn query_membership_works() { new_test_ext().execute_with(|| { assert_eq!(ScoredPool::members(), vec![20, 40]); - assert_eq!(Balances::reserved_balance(&31), CandidateDeposit::get()); - assert_eq!(Balances::reserved_balance(&40), CandidateDeposit::get()); + assert_eq!(Balances::reserved_balance(31), CandidateDeposit::get()); + assert_eq!(Balances::reserved_balance(40), CandidateDeposit::get()); assert_eq!(MEMBERS.with(|m| m.borrow().clone()), vec![20, 40]); }); } @@ -61,7 +61,7 @@ fn submit_candidacy_works() { assert_eq!(fetch_from_pool(15), Some((who, None))); // then - assert_eq!(Balances::reserved_balance(&who), CandidateDeposit::get()); + assert_eq!(Balances::reserved_balance(who), CandidateDeposit::get()); }); } @@ -117,7 +117,7 @@ fn kicking_works() { new_test_ext().execute_with(|| { // given let who = 40; - assert_eq!(Balances::reserved_balance(&who), CandidateDeposit::get()); + assert_eq!(Balances::reserved_balance(who), CandidateDeposit::get()); assert_eq!(find_in_pool(who), Some(0)); // when @@ -128,7 +128,7 @@ fn kicking_works() { assert_eq!(find_in_pool(who), None); assert_eq!(ScoredPool::members(), vec![20, 31]); assert_eq!(MEMBERS.with(|m| m.borrow().clone()), ScoredPool::members()); - assert_eq!(Balances::reserved_balance(&who), 0); // deposit must have been returned + assert_eq!(Balances::reserved_balance(who), 0); // deposit must have been returned }); } @@ -246,7 +246,7 @@ fn withdraw_scored_candidacy_must_work() { new_test_ext().execute_with(|| { // given let who = 40; - assert_eq!(Balances::reserved_balance(&who), CandidateDeposit::get()); + assert_eq!(Balances::reserved_balance(who), CandidateDeposit::get()); // when let index = find_in_pool(who).expect("entity must be in pool") as u32; @@ -255,7 +255,7 @@ fn withdraw_scored_candidacy_must_work() { // then assert_eq!(fetch_from_pool(who), None); assert_eq!(ScoredPool::members(), vec![20, 31]); - assert_eq!(Balances::reserved_balance(&who), 0); + assert_eq!(Balances::reserved_balance(who), 0); }); } diff --git a/substrate/frame/session/src/lib.rs b/substrate/frame/session/src/lib.rs index f98e334cbb..a18d55d26c 100644 --- a/substrate/frame/session/src/lib.rs +++ b/substrate/frame/session/src/lib.rs @@ -106,7 +106,7 @@ use frame_support::weights::SimpleDispatchInfo; use sp_runtime::traits::{Convert, Zero, Member, OpaqueKeys}; use sp_staking::SessionIndex; use frame_support::{dispatch, ConsensusEngineId, decl_module, decl_event, decl_storage, decl_error}; -use frame_support::{ensure, traits::{OnFreeBalanceZero, Get, FindAuthor, ValidatorRegistration}, Parameter}; +use frame_support::{ensure, traits::{OnReapAccount, Get, FindAuthor, ValidatorRegistration}, Parameter}; use frame_system::{self as system, ensure_signed}; #[cfg(test)] @@ -676,8 +676,8 @@ impl Module { } } -impl OnFreeBalanceZero for Module { - fn on_free_balance_zero(who: &T::ValidatorId) { +impl OnReapAccount for Module { + fn on_reap_account(who: &T::ValidatorId) { Self::prune_dead_keys(who); } } @@ -754,7 +754,7 @@ mod tests { let id = DUMMY; assert_eq!(Session::key_owner(id, UintAuthorityId(1).get_raw(id)), Some(1)); - Session::on_free_balance_zero(&1); + Session::on_reap_account(&1); assert_eq!(Session::load_keys(&1), None); assert_eq!(Session::key_owner(id, UintAuthorityId(1).get_raw(id)), None); }) diff --git a/substrate/frame/society/src/mock.rs b/substrate/frame/society/src/mock.rs index b8f249f35c..5c772478c8 100644 --- a/substrate/frame/society/src/mock.rs +++ b/substrate/frame/society/src/mock.rs @@ -51,7 +51,6 @@ parameter_types! { pub const AvailableBlockRatio: Perbill = Perbill::one(); pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } @@ -81,13 +80,11 @@ impl frame_system::Trait for Test { impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; type OnReapAccount = System; } @@ -146,7 +143,6 @@ impl EnvBuilder { self.balances.push((Society::account_id(), self.balance.max(self.pot))); pallet_balances::GenesisConfig:: { balances: self.balances, - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); GenesisConfig::{ members: self.members, diff --git a/substrate/frame/staking/src/lib.rs b/substrate/frame/staking/src/lib.rs index 757f75bf0a..ca2a2bf092 100644 --- a/substrate/frame/staking/src/lib.rs +++ b/substrate/frame/staking/src/lib.rs @@ -261,7 +261,7 @@ use frame_support::{ decl_module, decl_event, decl_storage, ensure, decl_error, weights::SimpleDispatchInfo, traits::{ - Currency, OnFreeBalanceZero, LockIdentifier, LockableCurrency, + Currency, LockIdentifier, LockableCurrency, WithdrawReasons, OnUnbalanced, Imbalance, Get, Time } }; @@ -284,6 +284,7 @@ use sp_runtime::{Serialize, Deserialize}; use frame_system::{self as system, ensure_signed, ensure_root}; use sp_phragmen::ExtendedBalance; +use frame_support::traits::OnReapAccount; const DEFAULT_MINIMUM_VALIDATOR_COUNT: u32 = 4; const MAX_NOMINATIONS: usize = 16; @@ -1286,7 +1287,6 @@ impl Module { STAKING_ID, &ledger.stash, ledger.total, - T::BlockNumber::max_value(), WithdrawReasons::all(), ); >::insert(controller, ledger); @@ -1682,8 +1682,8 @@ impl SessionManager> } } -impl OnFreeBalanceZero for Module { - fn on_free_balance_zero(stash: &T::AccountId) { +impl OnReapAccount for Module { + fn on_reap_account(stash: &T::AccountId) { Self::ensure_storage_upgraded(); Self::kill_stash(stash); } diff --git a/substrate/frame/staking/src/mock.rs b/substrate/frame/staking/src/mock.rs index 8aa20c19c6..3b2443c2ce 100644 --- a/substrate/frame/staking/src/mock.rs +++ b/substrate/frame/staking/src/mock.rs @@ -140,19 +140,16 @@ impl frame_system::Trait for Test { type ModuleToIndex = (); } parameter_types! { - pub const TransferFee: Balance = 0; pub const CreationFee: Balance = 0; } impl pallet_balances::Trait for Test { type Balance = Balance; - type OnFreeBalanceZero = Staking; - type OnReapAccount = System; + type OnReapAccount = (System, Staking); type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -323,7 +320,6 @@ impl ExtBuilder { // This allow us to have a total_payout different from 0. (999, 1_000_000_000_000), ], - vesting: vec![], }.assimilate_storage(&mut storage); let stake_21 = if self.fair { 1000 } else { 2000 }; diff --git a/substrate/frame/staking/src/tests.rs b/substrate/frame/staking/src/tests.rs index 43a2d00791..a9b6e16598 100644 --- a/substrate/frame/staking/src/tests.rs +++ b/substrate/frame/staking/src/tests.rs @@ -110,8 +110,8 @@ fn basic_setup_works() { assert_eq!(Staking::current_era(), 0); // Account 10 has `balance_factor` free balance - assert_eq!(Balances::free_balance(&10), 1); - assert_eq!(Balances::free_balance(&10), 1); + assert_eq!(Balances::free_balance(10), 1); + assert_eq!(Balances::free_balance(10), 1); // New era is not being forced assert_eq!(Staking::force_era(), Forcing::NotForcing); @@ -759,7 +759,7 @@ fn cannot_transfer_staked_balance() { // Confirm account 11 is stashed assert_eq!(Staking::bonded(&11), Some(10)); // Confirm account 11 has some free balance - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); // Confirm account 11 (via controller 10) is totally staked assert_eq!(Staking::stakers(&11).total, 1000); // Confirm account 11 cannot transfer as a result @@ -788,7 +788,7 @@ fn cannot_transfer_staked_balance_2() { // Confirm account 21 is stashed assert_eq!(Staking::bonded(&21), Some(20)); // Confirm account 21 has some free balance - assert_eq!(Balances::free_balance(&21), 2000); + assert_eq!(Balances::free_balance(21), 2000); // Confirm account 21 (via controller 20) is totally staked assert_eq!(Staking::stakers(&21).total, 1000); // Confirm account 21 can transfer at most 1000 @@ -811,7 +811,7 @@ fn cannot_reserve_staked_balance() { // Confirm account 11 is stashed assert_eq!(Staking::bonded(&11), Some(10)); // Confirm account 11 has some free balance - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); // Confirm account 11 (via controller 10) is totally staked assert_eq!(Staking::stakers(&11).own, 1000); // Confirm account 11 cannot transfer as a result @@ -838,9 +838,9 @@ fn reward_destination_works() { // Check that account 11 is a validator assert!(Staking::current_elected().contains(&11)); // Check the balance of the validator account - assert_eq!(Balances::free_balance(&10), 1); + assert_eq!(Balances::free_balance(10), 1); // Check the balance of the stash account - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); // Check how much is at stake assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, @@ -859,7 +859,7 @@ fn reward_destination_works() { // Check that RewardDestination is Staked (default) assert_eq!(Staking::payee(&11), RewardDestination::Staked); // Check that reward went to the stash account of validator - assert_eq!(Balances::free_balance(&11), 1000 + total_payout_0); + assert_eq!(Balances::free_balance(11), 1000 + total_payout_0); // Check that amount at stake increased accordingly assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, @@ -881,7 +881,7 @@ fn reward_destination_works() { // Check that RewardDestination is Stash assert_eq!(Staking::payee(&11), RewardDestination::Stash); // Check that reward went to the stash account - assert_eq!(Balances::free_balance(&11), 1000 + total_payout_0 + total_payout_1); + assert_eq!(Balances::free_balance(11), 1000 + total_payout_0 + total_payout_1); // Record this value let recorded_stash_balance = 1000 + total_payout_0 + total_payout_1; // Check that amount at stake is NOT increased @@ -896,7 +896,7 @@ fn reward_destination_works() { >::insert(&11, RewardDestination::Controller); // Check controller balance - assert_eq!(Balances::free_balance(&10), 1); + assert_eq!(Balances::free_balance(10), 1); // Compute total payout now for whole duration as other parameter won't change let total_payout_2 = current_total_payout_for_duration(3000); @@ -908,7 +908,7 @@ fn reward_destination_works() { // Check that RewardDestination is Controller assert_eq!(Staking::payee(&11), RewardDestination::Controller); // Check that reward went to the controller account - assert_eq!(Balances::free_balance(&10), 1 + total_payout_2); + assert_eq!(Balances::free_balance(10), 1 + total_payout_2); // Check that amount at stake is NOT increased assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, @@ -917,7 +917,7 @@ fn reward_destination_works() { unlocking: vec![], })); // Check that amount in staked account is NOT increased. - assert_eq!(Balances::free_balance(&11), recorded_stash_balance); + assert_eq!(Balances::free_balance(11), recorded_stash_balance); }); } @@ -1422,9 +1422,9 @@ fn on_free_balance_zero_stash_removes_validator() { // Tests that storage items are untouched when controller is empty ExtBuilder::default().existential_deposit(10).build().execute_with(|| { // Check the balance of the validator account - assert_eq!(Balances::free_balance(&10), 256); + assert_eq!(Balances::free_balance(10), 256); // Check the balance of the stash account - assert_eq!(Balances::free_balance(&11), 256000); + assert_eq!(Balances::free_balance(11), 256000); // Check these two accounts are bonded assert_eq!(Staking::bonded(&11), Some(10)); @@ -1442,7 +1442,7 @@ fn on_free_balance_zero_stash_removes_validator() { let _ = Balances::slash(&10, u64::max_value()); // Check the balance of the stash account has not been touched - assert_eq!(Balances::free_balance(&11), 256000); + assert_eq!(Balances::free_balance(11), 256000); // Check these two accounts are still bonded assert_eq!(Staking::bonded(&11), Some(10)); @@ -1476,9 +1476,9 @@ fn on_free_balance_zero_stash_removes_nominator() { // Check that account 10 is a nominator assert!(>::exists(11)); // Check the balance of the nominator account - assert_eq!(Balances::free_balance(&10), 256); + assert_eq!(Balances::free_balance(10), 256); // Check the balance of the stash account - assert_eq!(Balances::free_balance(&11), 256000); + assert_eq!(Balances::free_balance(11), 256000); // Set payee information assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash)); @@ -1495,7 +1495,7 @@ fn on_free_balance_zero_stash_removes_nominator() { assert_eq!(Balances::total_balance(&10), 0); // Check the balance of the stash account has not been touched - assert_eq!(Balances::free_balance(&11), 256000); + assert_eq!(Balances::free_balance(11), 256000); // Check these two accounts are still bonded assert_eq!(Staking::bonded(&11), Some(10)); @@ -1689,9 +1689,9 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() { assert_eq!(Staking::slot_stake(), 1); // Old ones are rewarded. - assert_eq!(Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3); + assert_eq!(Balances::free_balance(10), init_balance_10 + total_payout_0 / 3); // no rewards paid to 2. This was initial election. - assert_eq!(Balances::free_balance(&2), init_balance_2); + assert_eq!(Balances::free_balance(2), init_balance_2); let total_payout_1 = current_total_payout_for_duration(3000); assert!(total_payout_1 > 100); // Test is meaningfull if reward something @@ -1701,7 +1701,7 @@ fn bond_with_little_staked_value_bounded_by_slot_stake() { assert_eq_uvec!(validator_controllers(), vec![20, 10, 2]); assert_eq!(Staking::slot_stake(), 1); - assert_eq!(Balances::free_balance(&2), init_balance_2 + total_payout_1 / 3); + assert_eq!(Balances::free_balance(2), init_balance_2 + total_payout_1 / 3); assert_eq!( Balances::free_balance(&10), init_balance_10 + total_payout_0 / 3 + total_payout_1 / 3, @@ -2008,7 +2008,7 @@ fn slashing_performed_according_exposure() { ); // The stash account should be slashed for 250 (50% of 500). - assert_eq!(Balances::free_balance(&11), 1000 - 250); + assert_eq!(Balances::free_balance(11), 1000 - 250); }); } @@ -2102,8 +2102,8 @@ fn reporters_receive_their_slice() { // 50% * (10% * initial_balance / 2) let reward = (initial_balance / 20) / 2; let reward_each = reward / 2; // split into two pieces. - assert_eq!(Balances::free_balance(&1), 10 + reward_each); - assert_eq!(Balances::free_balance(&2), 20 + reward_each); + assert_eq!(Balances::free_balance(1), 10 + reward_each); + assert_eq!(Balances::free_balance(2), 20 + reward_each); assert_ledger_consistent(11); }); } @@ -2132,7 +2132,7 @@ fn subsequent_reports_in_same_span_pay_out_less() { // F1 * (reward_proportion * slash - 0) // 50% * (10% * initial_balance * 20%) let reward = (initial_balance / 5) / 20; - assert_eq!(Balances::free_balance(&1), 10 + reward); + assert_eq!(Balances::free_balance(1), 10 + reward); on_offence_now( &[OffenceDetails { @@ -2150,7 +2150,7 @@ fn subsequent_reports_in_same_span_pay_out_less() { // F1 * (reward_proportion * slash - prior_payout) // 50% * (10% * (initial_balance / 2) - prior_payout) let reward = ((initial_balance / 20) - prior_payout) / 2; - assert_eq!(Balances::free_balance(&1), 10 + prior_payout + reward); + assert_eq!(Balances::free_balance(1), 10 + prior_payout + reward); assert_ledger_consistent(11); }); } @@ -2159,8 +2159,8 @@ fn subsequent_reports_in_same_span_pay_out_less() { fn invulnerables_are_not_slashed() { // For invulnerable validators no slashing is performed. ExtBuilder::default().invulnerables(vec![11]).build().execute_with(|| { - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&21), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(21), 2000); let exposure = Staking::stakers(&21); let initial_balance = Staking::slashable_balance_of(&21); @@ -2183,9 +2183,9 @@ fn invulnerables_are_not_slashed() { ); // The validator 11 hasn't been slashed, but 21 has been. - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); // 2000 - (0.2 * initial_balance) - assert_eq!(Balances::free_balance(&21), 2000 - (2 * initial_balance / 10)); + assert_eq!(Balances::free_balance(21), 2000 - (2 * initial_balance / 10)); // ensure that nominators were slashed as well. for (initial_balance, other) in nominator_balances.into_iter().zip(exposure.others) { @@ -2203,7 +2203,7 @@ fn invulnerables_are_not_slashed() { fn dont_slash_if_fraction_is_zero() { // Don't slash if the fraction is zero. ExtBuilder::default().build().execute_with(|| { - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); on_offence_now( &[OffenceDetails { @@ -2217,7 +2217,7 @@ fn dont_slash_if_fraction_is_zero() { ); // The validator hasn't been slashed. The new era is not forced. - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); assert_ledger_consistent(11); }); } @@ -2225,7 +2225,7 @@ fn dont_slash_if_fraction_is_zero() { #[test] fn only_slash_for_max_in_era() { ExtBuilder::default().build().execute_with(|| { - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); on_offence_now( &[ @@ -2238,7 +2238,7 @@ fn only_slash_for_max_in_era() { ); // The validator has been slashed and has been force-chilled. - assert_eq!(Balances::free_balance(&11), 500); + assert_eq!(Balances::free_balance(11), 500); assert_eq!(Staking::force_era(), Forcing::ForceNew); on_offence_now( @@ -2252,7 +2252,7 @@ fn only_slash_for_max_in_era() { ); // The validator has not been slashed additionally. - assert_eq!(Balances::free_balance(&11), 500); + assert_eq!(Balances::free_balance(11), 500); on_offence_now( &[ @@ -2265,7 +2265,7 @@ fn only_slash_for_max_in_era() { ); // The validator got slashed 10% more. - assert_eq!(Balances::free_balance(&11), 400); + assert_eq!(Balances::free_balance(11), 400); assert_ledger_consistent(11); }) } @@ -2273,7 +2273,7 @@ fn only_slash_for_max_in_era() { #[test] fn garbage_collection_after_slashing() { ExtBuilder::default().existential_deposit(1).build().execute_with(|| { - assert_eq!(Balances::free_balance(&11), 256_000); + assert_eq!(Balances::free_balance(11), 256_000); on_offence_now( &[ @@ -2285,7 +2285,7 @@ fn garbage_collection_after_slashing() { &[Perbill::from_percent(10)], ); - assert_eq!(Balances::free_balance(&11), 256_000 - 25_600); + assert_eq!(Balances::free_balance(11), 256_000 - 25_600); assert!(::SlashingSpans::get(&11).is_some()); assert_eq!(::SpanSlash::get(&(11, 0)).amount_slashed(), &25_600); @@ -2302,7 +2302,7 @@ fn garbage_collection_after_slashing() { // validator and nominator slash in era are garbage-collected by era change, // so we don't test those here. - assert_eq!(Balances::free_balance(&11), 0); + assert_eq!(Balances::free_balance(11), 0); assert!(::SlashingSpans::get(&11).is_none()); assert_eq!(::SpanSlash::get(&(11, 0)).amount_slashed(), &0); }) @@ -2313,10 +2313,10 @@ fn garbage_collection_on_window_pruning() { ExtBuilder::default().build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_now( @@ -2331,8 +2331,8 @@ fn garbage_collection_on_window_pruning() { let now = Staking::current_era(); - assert_eq!(Balances::free_balance(&11), 900); - assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10)); + assert_eq!(Balances::free_balance(11), 900); + assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10)); assert!(::ValidatorSlashInEra::get(&now, &11).is_some()); assert!(::NominatorSlashInEra::get(&now, &101).is_some()); @@ -2357,15 +2357,15 @@ fn slashing_nominators_by_span_max() { start_era(2); start_era(3); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&21), 2000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(21), 2000); + assert_eq!(Balances::free_balance(101), 2000); assert_eq!(Staking::slashable_balance_of(&21), 1000); let exposure_11 = Staking::stakers(&11); let exposure_21 = Staking::stakers(&21); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); let nominated_value_11 = exposure_11.others.iter().find(|o| o.who == 101).unwrap().value; let nominated_value_21 = exposure_21.others.iter().find(|o| o.who == 101).unwrap().value; @@ -2380,10 +2380,10 @@ fn slashing_nominators_by_span_max() { 2, ); - assert_eq!(Balances::free_balance(&11), 900); + assert_eq!(Balances::free_balance(11), 900); let slash_1_amount = Perbill::from_percent(10) * nominated_value_11; - assert_eq!(Balances::free_balance(&101), 2000 - slash_1_amount); + assert_eq!(Balances::free_balance(101), 2000 - slash_1_amount); let expected_spans = vec![ slashing::SlashingSpan { index: 1, start: 4, length: None }, @@ -2415,14 +2415,14 @@ fn slashing_nominators_by_span_max() { ); // 11 was not further slashed, but 21 and 101 were. - assert_eq!(Balances::free_balance(&11), 900); - assert_eq!(Balances::free_balance(&21), 1700); + assert_eq!(Balances::free_balance(11), 900); + assert_eq!(Balances::free_balance(21), 1700); let slash_2_amount = Perbill::from_percent(30) * nominated_value_21; assert!(slash_2_amount > slash_1_amount); // only the maximum slash in a single span is taken. - assert_eq!(Balances::free_balance(&101), 2000 - slash_2_amount); + assert_eq!(Balances::free_balance(101), 2000 - slash_2_amount); // third slash: in same era and on same validator as first, higher // in-era value, but lower slash value than slash 2. @@ -2438,15 +2438,15 @@ fn slashing_nominators_by_span_max() { ); // 11 was further slashed, but 21 and 101 were not. - assert_eq!(Balances::free_balance(&11), 800); - assert_eq!(Balances::free_balance(&21), 1700); + assert_eq!(Balances::free_balance(11), 800); + assert_eq!(Balances::free_balance(21), 1700); let slash_3_amount = Perbill::from_percent(20) * nominated_value_21; assert!(slash_3_amount < slash_2_amount); assert!(slash_3_amount > slash_1_amount); // only the maximum slash in a single span is taken. - assert_eq!(Balances::free_balance(&101), 2000 - slash_2_amount); + assert_eq!(Balances::free_balance(101), 2000 - slash_2_amount); }); } @@ -2457,7 +2457,7 @@ fn slashes_are_summed_across_spans() { start_era(2); start_era(3); - assert_eq!(Balances::free_balance(&21), 2000); + assert_eq!(Balances::free_balance(21), 2000); assert_eq!(Staking::slashable_balance_of(&21), 1000); let get_span = |account| ::SlashingSpans::get(&account).unwrap(); @@ -2478,7 +2478,7 @@ fn slashes_are_summed_across_spans() { ]; assert_eq!(get_span(21).iter().collect::>(), expected_spans); - assert_eq!(Balances::free_balance(&21), 1900); + assert_eq!(Balances::free_balance(21), 1900); // 21 has been force-chilled. re-signal intent to validate. Staking::validate(Origin::signed(20), Default::default()).unwrap(); @@ -2504,7 +2504,7 @@ fn slashes_are_summed_across_spans() { ]; assert_eq!(get_span(21).iter().collect::>(), expected_spans); - assert_eq!(Balances::free_balance(&21), 1810); + assert_eq!(Balances::free_balance(21), 1810); }); } @@ -2513,10 +2513,10 @@ fn deferred_slashes_are_deferred() { ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_now( @@ -2529,25 +2529,25 @@ fn deferred_slashes_are_deferred() { &[Perbill::from_percent(10)], ); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); start_era(2); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); start_era(3); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); // at the start of era 4, slashes from era 1 are processed, // after being deferred for at least 2 full eras. start_era(4); - assert_eq!(Balances::free_balance(&11), 900); - assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10)); + assert_eq!(Balances::free_balance(11), 900); + assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10)); }) } @@ -2556,10 +2556,10 @@ fn remove_deferred() { ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_now( @@ -2572,8 +2572,8 @@ fn remove_deferred() { &[Perbill::from_percent(10)], ); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); start_era(2); @@ -2590,21 +2590,21 @@ fn remove_deferred() { Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0]).unwrap(); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); start_era(3); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); // at the start of era 4, slashes from era 1 are processed, // after being deferred for at least 2 full eras. start_era(4); // the first slash for 10% was cancelled, so no effect. - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); start_era(5); @@ -2616,8 +2616,8 @@ fn remove_deferred() { let actual_slash = total_slash - initial_slash; // 5% slash (15 - 10) processed now. - assert_eq!(Balances::free_balance(&11), 950); - assert_eq!(Balances::free_balance(&101), 2000 - actual_slash); + assert_eq!(Balances::free_balance(11), 950); + assert_eq!(Balances::free_balance(101), 2000 - actual_slash); }) } @@ -2626,10 +2626,10 @@ fn remove_multi_deferred() { ExtBuilder::default().slash_defer_duration(2).build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); on_offence_now( &[ @@ -2682,10 +2682,10 @@ fn slash_kicks_validators_not_nominators() { ExtBuilder::default().build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); let nominated_value = exposure.others.iter().find(|o| o.who == 101).unwrap().value; on_offence_now( @@ -2698,8 +2698,8 @@ fn slash_kicks_validators_not_nominators() { &[Perbill::from_percent(10)], ); - assert_eq!(Balances::free_balance(&11), 900); - assert_eq!(Balances::free_balance(&101), 2000 - (nominated_value / 10)); + assert_eq!(Balances::free_balance(11), 900); + assert_eq!(Balances::free_balance(101), 2000 - (nominated_value / 10)); // This is the best way to check that the validator was chilled; `get` will // return default value. @@ -2771,10 +2771,10 @@ fn zero_slash_keeps_nominators() { ExtBuilder::default().build().execute_with(|| { start_era(1); - assert_eq!(Balances::free_balance(&11), 1000); + assert_eq!(Balances::free_balance(11), 1000); let exposure = Staking::stakers(&11); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(101), 2000); on_offence_now( &[ @@ -2786,8 +2786,8 @@ fn zero_slash_keeps_nominators() { &[Perbill::from_percent(0)], ); - assert_eq!(Balances::free_balance(&11), 1000); - assert_eq!(Balances::free_balance(&101), 2000); + assert_eq!(Balances::free_balance(11), 1000); + assert_eq!(Balances::free_balance(101), 2000); // This is the best way to check that the validator was chilled; `get` will // return default value. diff --git a/substrate/frame/support/src/lib.rs b/substrate/frame/support/src/lib.rs index e601544b01..531f255714 100644 --- a/substrate/frame/support/src/lib.rs +++ b/substrate/frame/support/src/lib.rs @@ -67,7 +67,8 @@ pub mod traits; pub mod weights; pub use self::hash::{ - Twox256, Twox128, Blake2_256, Blake2_128, Twox64Concat, Blake2_128Concat, Hashable + Twox256, Twox128, Blake2_256, Blake2_128, Twox64Concat, Blake2_128Concat, Hashable, + StorageHasher }; pub use self::storage::{ StorageValue, StorageMap, StorageLinkedMap, StorageDoubleMap, StoragePrefixedMap diff --git a/substrate/frame/support/src/traits.rs b/substrate/frame/support/src/traits.rs index cb22283924..02e6085046 100644 --- a/substrate/frame/support/src/traits.rs +++ b/substrate/frame/support/src/traits.rs @@ -64,13 +64,6 @@ pub trait Contains { fn count() -> usize { Self::sorted_members().len() } } -/// The account with the given id was killed. -#[impl_trait_for_tuples::impl_for_tuples(30)] -pub trait OnFreeBalanceZero { - /// The account with the given id was killed. - fn on_free_balance_zero(who: &AccountId); -} - /// The account with the given id was reaped. #[impl_trait_for_tuples::impl_for_tuples(30)] pub trait OnReapAccount { @@ -84,6 +77,12 @@ pub enum UpdateBalanceOutcome { Updated, /// The update led to killing the account. AccountKilled, + /// Free balance became zero as a result of this update. + FreeBalanceZero, + /// Reserved balance became zero as a result of this update. + ReservedBalanceZero, + /// The account started and ended non-existent. + StillDead, } /// A trait for finding the author of a block header based on the `PreRuntime` digests contained @@ -376,9 +375,7 @@ pub trait Currency { /// This is the only balance that matters in terms of most operations on tokens. It alone /// is used to determine the balance when in the contract execution environment. When this /// balance falls below the value of `ExistentialDeposit`, then the 'current account' is - /// deleted: specifically `FreeBalance`. Further, the `OnFreeBalanceZero` callback - /// is invoked, giving a chance to external modules to clean up data associated with - /// the deleted account. + /// deleted: specifically `FreeBalance`. /// /// `system::AccountNonce` is also deleted if `ReservedBalance` is also zero (it also gets /// collapsed to zero if it ever becomes less than `ExistentialDeposit`. @@ -581,7 +578,6 @@ pub trait LockableCurrency: Currency { id: LockIdentifier, who: &AccountId, amount: Self::Balance, - until: Self::Moment, reasons: WithdrawReasons, ); @@ -592,13 +588,11 @@ pub trait LockableCurrency: Currency { /// applies the most severe constraints of the two, while `set_lock` replaces the lock /// with the new parameters. As in, `extend_lock` will set: /// - maximum `amount` - /// - farthest duration (`until`) /// - bitwise mask of all `reasons` fn extend_lock( id: LockIdentifier, who: &AccountId, amount: Self::Balance, - until: Self::Moment, reasons: WithdrawReasons, ); @@ -609,13 +603,17 @@ pub trait LockableCurrency: Currency { ); } -/// A currency whose accounts can have balances which vest over time. -pub trait VestingCurrency: Currency { +/// A vesting schedule over a currency. This allows a particular currency to have vesting limits +/// applied to it. +pub trait VestingSchedule { /// The quantity used to denote time; usually just a `BlockNumber`. type Moment; + /// The currency that this schedule applies to. + type Currency: Currency; + /// Get the amount that is currently being vested and cannot be transferred out of this account. - fn vesting_balance(who: &AccountId) -> Self::Balance; + fn vesting_balance(who: &AccountId) -> >::Balance; /// Adds a vesting schedule to a given account. /// @@ -623,8 +621,8 @@ pub trait VestingCurrency: Currency { /// and nothing is updated. fn add_vesting_schedule( who: &AccountId, - locked: Self::Balance, - per_block: Self::Balance, + locked: >::Balance, + per_block: >::Balance, starting_block: Self::Moment, ) -> DispatchResult; @@ -644,7 +642,7 @@ bitmask! { TransactionPayment = 0b00000001, /// In order to transfer ownership. Transfer = 0b00000010, - /// In order to reserve some funds for a later return or repatriation + /// In order to reserve some funds for a later return or repatriation. Reserve = 0b00000100, /// In order to pay some other (higher-level) fees. Fee = 0b00001000, diff --git a/substrate/frame/transaction-payment/src/lib.rs b/substrate/frame/transaction-payment/src/lib.rs index 34768d808e..645c3f8af5 100644 --- a/substrate/frame/transaction-payment/src/lib.rs +++ b/substrate/frame/transaction-payment/src/lib.rs @@ -76,7 +76,7 @@ pub trait Trait: frame_system::Trait { decl_storage! { trait Store for Module as Balances { - NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::from_parts(0); + pub NextFeeMultiplier get(fn next_fee_multiplier): Multiplier = Multiplier::from_parts(0); } } @@ -178,9 +178,7 @@ impl ChargeTransactionPayment { let adjusted_fee = targeted_fee_adjustment.saturated_multiply_accumulate(adjustable_fee); let base_fee = T::TransactionBaseFee::get(); - let final_fee = base_fee.saturating_add(adjusted_fee).saturating_add(tip); - - final_fee + base_fee.saturating_add(adjusted_fee).saturating_add(tip) } else { tip } @@ -307,21 +305,18 @@ mod tests { } parameter_types! { - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; pub const ExistentialDeposit: u64 = 0; } impl pallet_balances::Trait for Runtime { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } @@ -407,7 +402,6 @@ mod tests { (5, 50 * self.balance_factor), (6, 60 * self.balance_factor) ], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } @@ -432,14 +426,14 @@ mod tests { .pre_dispatch(&1, CALL, info_from_weight(5), len) .is_ok() ); - assert_eq!(Balances::free_balance(&1), 100 - 5 - 5 - 10); + assert_eq!(Balances::free_balance(1), 100 - 5 - 5 - 10); assert!( ChargeTransactionPayment::::from(5 /* tipped */) .pre_dispatch(&2, CALL, info_from_weight(3), len) .is_ok() ); - assert_eq!(Balances::free_balance(&2), 200 - 5 - 10 - 3 - 5); + assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 3 - 5); }); } @@ -474,7 +468,7 @@ mod tests { .execute_with(|| { // 1 ain't have a penny. - assert_eq!(Balances::free_balance(&1), 0); + assert_eq!(Balances::free_balance(1), 0); let len = 100; @@ -521,7 +515,7 @@ mod tests { .pre_dispatch(&1, CALL, info_from_weight(3), len) .is_ok() ); - assert_eq!(Balances::free_balance(&1), 100 - 10 - 5 - (10 + 3) * 3 / 2); + assert_eq!(Balances::free_balance(1), 100 - 10 - 5 - (10 + 3) * 3 / 2); }) } diff --git a/substrate/frame/treasury/src/lib.rs b/substrate/frame/treasury/src/lib.rs index 8d1c25ad66..0872bc718c 100644 --- a/substrate/frame/treasury/src/lib.rs +++ b/substrate/frame/treasury/src/lib.rs @@ -759,19 +759,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 1; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; type OnNewAccount = (); - type OnFreeBalanceZero = (); type OnReapAccount = System; type Event = (); type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } pub struct TenToFourteen; @@ -818,7 +815,6 @@ mod tests { pallet_balances::GenesisConfig::{ // Total issuance will be 200 with treasury account initialized at ED. balances: vec![(0, 100), (1, 98), (2, 1)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); GenesisConfig::default().assimilate_storage::(&mut t).unwrap(); t.into() @@ -853,8 +849,8 @@ mod tests { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&Treasury::account_id(), 101); assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3)); - assert_eq!(Balances::reserved_balance(&0), 12); - assert_eq!(Balances::free_balance(&0), 88); + assert_eq!(Balances::reserved_balance(0), 12); + assert_eq!(Balances::free_balance(0), 88); // other reports don't count. assert_noop!( @@ -869,9 +865,9 @@ mod tests { assert_noop!(Treasury::tip(Origin::signed(9), h.clone(), 10), BadOrigin); System::set_block_number(2); assert_ok!(Treasury::close_tip(Origin::signed(100), h.into())); - assert_eq!(Balances::reserved_balance(&0), 0); - assert_eq!(Balances::free_balance(&0), 102); - assert_eq!(Balances::free_balance(&3), 8); + assert_eq!(Balances::reserved_balance(0), 0); + assert_eq!(Balances::free_balance(0), 102); + assert_eq!(Balances::free_balance(3), 8); }); } @@ -880,16 +876,16 @@ mod tests { new_test_ext().execute_with(|| { Balances::make_free_balance_be(&Treasury::account_id(), 101); assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 0)); - assert_eq!(Balances::reserved_balance(&0), 12); - assert_eq!(Balances::free_balance(&0), 88); + assert_eq!(Balances::reserved_balance(0), 12); + assert_eq!(Balances::free_balance(0), 88); let h = BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 0u64)); assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10)); assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10)); assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10)); System::set_block_number(2); assert_ok!(Treasury::close_tip(Origin::signed(100), h.into())); - assert_eq!(Balances::reserved_balance(&0), 0); - assert_eq!(Balances::free_balance(&0), 110); + assert_eq!(Balances::reserved_balance(0), 0); + assert_eq!(Balances::free_balance(0), 110); }); } @@ -910,7 +906,7 @@ mod tests { System::set_block_number(2); assert_noop!(Treasury::close_tip(Origin::NONE, h.into()), BadOrigin); assert_ok!(Treasury::close_tip(Origin::signed(0), h.into())); - assert_eq!(Balances::free_balance(&3), 10); + assert_eq!(Balances::free_balance(3), 10); assert_noop!(Treasury::close_tip(Origin::signed(100), h.into()), Error::::UnknownTip); }); @@ -942,7 +938,7 @@ mod tests { assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 1000000)); System::set_block_number(2); assert_ok!(Treasury::close_tip(Origin::signed(0), h.into())); - assert_eq!(Balances::free_balance(&3), 10); + assert_eq!(Balances::free_balance(3), 10); }); } @@ -961,7 +957,7 @@ mod tests { assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10)); System::set_block_number(2); assert_ok!(Treasury::close_tip(Origin::signed(0), h.into())); - assert_eq!(Balances::free_balance(&3), 10); + assert_eq!(Balances::free_balance(3), 10); }); } @@ -978,8 +974,8 @@ mod tests { fn spend_proposal_takes_min_deposit() { new_test_ext().execute_with(|| { assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3)); - assert_eq!(Balances::free_balance(&0), 99); - assert_eq!(Balances::reserved_balance(&0), 1); + assert_eq!(Balances::free_balance(0), 99); + assert_eq!(Balances::reserved_balance(0), 1); }); } @@ -987,8 +983,8 @@ mod tests { fn spend_proposal_takes_proportional_deposit() { new_test_ext().execute_with(|| { assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3)); - assert_eq!(Balances::free_balance(&0), 95); - assert_eq!(Balances::reserved_balance(&0), 5); + assert_eq!(Balances::free_balance(0), 95); + assert_eq!(Balances::reserved_balance(0), 5); }); } @@ -1011,7 +1007,7 @@ mod tests { assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0)); >::on_finalize(1); - assert_eq!(Balances::free_balance(&3), 0); + assert_eq!(Balances::free_balance(3), 0); assert_eq!(Treasury::pot(), 100); }); } @@ -1038,7 +1034,7 @@ mod tests { assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0)); >::on_finalize(2); - assert_eq!(Balances::free_balance(&3), 0); + assert_eq!(Balances::free_balance(3), 0); assert_eq!(Treasury::pot(), 50); }); } @@ -1089,7 +1085,7 @@ mod tests { assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0)); >::on_finalize(2); - assert_eq!(Balances::free_balance(&3), 100); + assert_eq!(Balances::free_balance(3), 100); assert_eq!(Treasury::pot(), 0); }); } @@ -1108,7 +1104,7 @@ mod tests { let _ = Balances::deposit_into_existing(&Treasury::account_id(), 100).unwrap(); >::on_finalize(4); - assert_eq!(Balances::free_balance(&3), 150); // Fund has been spent + assert_eq!(Balances::free_balance(3), 150); // Fund has been spent assert_eq!(Treasury::pot(), 25); // Pot has finally changed }); } @@ -1133,7 +1129,7 @@ mod tests { >::on_finalize(4); assert_eq!(Treasury::pot(), 0); // Pot is emptied - assert_eq!(Balances::free_balance(&Treasury::account_id()), 1); // but the account is still there + assert_eq!(Balances::free_balance(Treasury::account_id()), 1); // but the account is still there }); } @@ -1144,13 +1140,12 @@ mod tests { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig::{ balances: vec![(0, 100), (1, 99), (2, 1)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); // Treasury genesis config is not build thus treasury account does not exist let mut t: sp_io::TestExternalities = t.into(); t.execute_with(|| { - assert_eq!(Balances::free_balance(&Treasury::account_id()), 0); // Account does not exist + assert_eq!(Balances::free_balance(Treasury::account_id()), 0); // Account does not exist assert_eq!(Treasury::pot(), 0); // Pot is empty assert_ok!(Treasury::propose_spend(Origin::signed(0), 99, 3)); @@ -1159,16 +1154,16 @@ mod tests { assert_ok!(Treasury::approve_proposal(Origin::ROOT, 1)); >::on_finalize(2); assert_eq!(Treasury::pot(), 0); // Pot hasn't changed - assert_eq!(Balances::free_balance(&3), 0); // Balance of `3` hasn't changed + assert_eq!(Balances::free_balance(3), 0); // Balance of `3` hasn't changed Balances::make_free_balance_be(&Treasury::account_id(), 100); assert_eq!(Treasury::pot(), 99); // Pot now contains funds - assert_eq!(Balances::free_balance(&Treasury::account_id()), 100); // Account does exist + assert_eq!(Balances::free_balance(Treasury::account_id()), 100); // Account does exist >::on_finalize(4); assert_eq!(Treasury::pot(), 0); // Pot has changed - assert_eq!(Balances::free_balance(&3), 99); // Balance of `3` has changed + assert_eq!(Balances::free_balance(3), 99); // Balance of `3` has changed }); } } diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 7344294684..fcd340eef5 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -703,19 +703,16 @@ mod tests { } parameter_types! { pub const ExistentialDeposit: u64 = 0; - pub const TransferFee: u64 = 0; pub const CreationFee: u64 = 0; } impl pallet_balances::Trait for Test { type Balance = u64; - type OnFreeBalanceZero = (); type OnReapAccount = System; type OnNewAccount = (); type Event = TestEvent; type TransferPayment = (); type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; - type TransferFee = TransferFee; type CreationFee = CreationFee; } parameter_types! { @@ -742,7 +739,6 @@ mod tests { let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); pallet_balances::GenesisConfig:: { balances: vec![(1, 10), (2, 10), (3, 10), (4, 10), (5, 10)], - vesting: vec![], }.assimilate_storage(&mut t).unwrap(); t.into() } diff --git a/substrate/frame/vesting/Cargo.toml b/substrate/frame/vesting/Cargo.toml new file mode 100644 index 0000000000..6923297af5 --- /dev/null +++ b/substrate/frame/vesting/Cargo.toml @@ -0,0 +1,33 @@ +[package] +name = "pallet-vesting" +version = "2.0.0" +authors = ["Parity Technologies "] +edition = "2018" + +[dependencies] +serde = { version = "1.0.101", optional = true } +codec = { package = "parity-scale-codec", version = "1.0.0", default-features = false, features = ["derive"] } +enumflags2 = { version = "0.6.2" } +sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } +sp-io = { version = "2.0.0", default-features = false, path = "../../primitives/io" } +sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } +frame-support = { version = "2.0.0", default-features = false, path = "../support" } +frame-system = { version = "2.0.0", default-features = false, path = "../system" } + +[dev-dependencies] +sp-core = { version = "2.0.0", path = "../../primitives/core" } +pallet-balances = { version = "2.0.0", path = "../balances" } +sp-storage = { version = "2.0.0", path = "../../primitives/storage" } +hex-literal = "0.2.1" + +[features] +default = ["std"] +std = [ + "serde", + "codec/std", + "sp-std/std", + "sp-io/std", + "sp-runtime/std", + "frame-support/std", + "frame-system/std", +] diff --git a/substrate/frame/vesting/src/lib.rs b/substrate/frame/vesting/src/lib.rs new file mode 100644 index 0000000000..4a079e7693 --- /dev/null +++ b/substrate/frame/vesting/src/lib.rs @@ -0,0 +1,610 @@ +// Copyright 2019-2020 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 . + +//! # Vesting Module +//! +//! - [`vesting::Trait`](./trait.Trait.html) +//! - [`Call`](./enum.Call.html) +//! +//! ## Overview +//! +//! A simple module providing a means of placing a linear curve on an account's locked balance. This +//! module ensures that there is a lock in place preventing the balance to drop below the *unvested* +//! amount for any reason other than transaction fee payment. +//! +//! As the amount vested increases over time, the amount unvested reduces. However, locks remain in +//! place and explicit action is needed on behalf of the user to ensure that the amount locked is +//! equivalent to the amount remaining to be vested. This is done through a dispatchable function, +//! either `vest` (in typical case where the sender is calling on their own behalf) or `vest_other` +//! in case the sender is calling on another account's behalf. +//! +//! ## Interface +//! +//! This module implements the `VestingSchedule` trait. +//! +//! ### Dispatchable Functions +//! +//! - `vest` - Update the lock, reducing it in line with the amount "vested" so far. +//! - `vest_other` - Update the lock of another account, reducing it in line with the amount +//! "vested" so far. +//! +//! [`Call`]: ./enum.Call.html +//! [`Trait`]: ./trait.Trait.html + +#![cfg_attr(not(feature = "std"), no_std)] + +use sp_std::prelude::*; +use sp_std::fmt::Debug; +use codec::{Encode, Decode}; +use sp_runtime::{DispatchResult, RuntimeDebug, traits::{ + StaticLookup, Zero, SimpleArithmetic, MaybeSerializeDeserialize, Saturating, Convert +}}; +use frame_support::{decl_module, decl_event, decl_storage, ensure, decl_error}; +use frame_support::traits::{ + Currency, LockableCurrency, VestingSchedule, WithdrawReason, LockIdentifier +}; +use frame_system::{self as system, ensure_signed}; + +type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; + +pub trait Trait: frame_system::Trait { + /// The overarching event type. + type Event: From> + Into<::Event>; + + /// The currency trait. + type Currency: LockableCurrency; + + /// Convert the block number into a balance. + type BlockNumberToBalance: Convert>; +} + +const VESTING_ID: LockIdentifier = *b"vesting "; + +/// Struct to encode the vesting schedule of an individual account. +#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, RuntimeDebug)] +pub struct VestingInfo { + /// Locked amount at genesis. + pub locked: Balance, + /// Amount that gets unlocked every block after `starting_block`. + pub per_block: Balance, + /// Starting block for unlocking(vesting). + pub starting_block: BlockNumber, +} + +impl< + Balance: SimpleArithmetic + Copy, + BlockNumber: SimpleArithmetic + Copy, +> VestingInfo { + /// Amount locked at block `n`. + pub fn locked_at< + BlockNumberToBalance: Convert + >(&self, n: BlockNumber) -> Balance { + // Number of blocks that count toward vesting + // Saturating to 0 when n < starting_block + let vested_block_count = n.saturating_sub(self.starting_block); + let vested_block_count = BlockNumberToBalance::convert(vested_block_count); + // Return amount that is still locked in vesting + let maybe_balance = vested_block_count.checked_mul(&self.per_block); + if let Some(balance) = maybe_balance { + self.locked.saturating_sub(balance) + } else { + Zero::zero() + } + } +} + +decl_storage! { + trait Store for Module as Vesting { + /// Information regarding the vesting of a given account. + pub Vesting get(fn vesting): + map hasher(blake2_256) T::AccountId => Option, T::BlockNumber>>; + } + add_extra_genesis { + config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, BalanceOf)>; + build(|config: &GenesisConfig| { + // Generate initial vesting configuration + // * who - Account which we are generating vesting configuration for + // * begin - Block when the account will start to vest + // * length - Number of blocks from `begin` until fully vested + // * liquid - Number of units which can be spent before vesting begins + for &(ref who, begin, length, liquid) in config.vesting.iter() { + let balance = T::Currency::free_balance(who); + assert!(!balance.is_zero(), "Currencies must be init'd before vesting"); + // Total genesis `balance` minus `liquid` equals funds locked for vesting + let locked = balance.saturating_sub(liquid); + let length_as_balance = T::BlockNumberToBalance::convert(length); + let per_block = locked / length_as_balance.max(sp_runtime::traits::One::one()); + + Vesting::::insert(who, VestingInfo { + locked: locked, + per_block: per_block, + starting_block: begin + }); + let reasons = WithdrawReason::Transfer | WithdrawReason::Reserve; + T::Currency::set_lock(VESTING_ID, who, locked, reasons); + } + }) + } +} + +decl_event!( + pub enum Event where AccountId = ::AccountId, Balance = BalanceOf { + /// The amount vested has been updated. This could indicate more funds are available. The + /// balance given is the amount which is left unvested (and thus locked). + VestingUpdated(AccountId, Balance), + /// An account (given) has become fully vested. No further vesting can happen. + VestingCompleted(AccountId), + } +); + +decl_error! { + /// Error for the vesting module. + pub enum Error for Module { + /// The account given is not vesting. + NotVesting, + /// An existing vesting schedule already exists for this account that cannot be clobbered. + ExistingVestingSchedule, + } +} + +decl_module! { + // Simple declaration of the `Module` type. Lets the macro know what it's working on. + pub struct Module for enum Call where origin: T::Origin { + type Error = Error; + + fn deposit_event() = default; + + /// Unlock any vested funds of the sender account. + /// + /// The dispatch origin for this call must be _Signed_ and the sender must have funds still + /// locked under this module. + /// + /// Emits either `VestingCompleted` or `VestingUpdated`. + /// + /// # + /// - `O(1)`. + /// - One balance-lock operation. + /// - One storage read (codec `O(1)`) and up to one removal. + /// - One event. + /// # + fn vest(origin) -> DispatchResult { + let who = ensure_signed(origin)?; + Self::update_lock(who) + } + + /// Unlock any vested funds of a `target` account. + /// + /// The dispatch origin for this call must be _Signed_. + /// + /// - `target`: The account whose vested funds should be unlocked. Must have funds still + /// locked under this module. + /// + /// Emits either `VestingCompleted` or `VestingUpdated`. + /// + /// # + /// - `O(1)`. + /// - Up to one account lookup. + /// - One balance-lock operation. + /// - One storage read (codec `O(1)`) and up to one removal. + /// - One event. + /// # + fn vest_other(origin, target: ::Source) -> DispatchResult { + ensure_signed(origin)?; + Self::update_lock(T::Lookup::lookup(target)?) + } + } +} + +impl Module { + /// (Re)set or remove the module's currency lock on `who`'s account in accordance with their + /// current unvested amount. + fn update_lock(who: T::AccountId) -> DispatchResult { + ensure!(Vesting::::exists(&who), Error::::NotVesting); + let unvested = Self::vesting_balance(&who); + if unvested.is_zero() { + T::Currency::remove_lock(VESTING_ID, &who); + Vesting::::remove(&who); + Self::deposit_event(RawEvent::VestingCompleted(who)); + } else { + let reasons = WithdrawReason::Transfer | WithdrawReason::Reserve; + T::Currency::set_lock(VESTING_ID, &who, unvested, reasons); + Self::deposit_event(RawEvent::VestingUpdated(who, unvested)); + } + Ok(()) + } +} + +impl VestingSchedule for Module where + BalanceOf: MaybeSerializeDeserialize + Debug +{ + type Moment = T::BlockNumber; + type Currency = T::Currency; + + /// Get the amount that is currently being vested and cannot be transferred out of this account. + fn vesting_balance(who: &T::AccountId) -> BalanceOf { + if let Some(v) = Self::vesting(who) { + let now = >::block_number(); + let locked_now = v.locked_at::(now); + T::Currency::free_balance(who).min(locked_now) + } else { + Zero::zero() + } + } + + /// Adds a vesting schedule to a given account. + /// + /// If there already exists a vesting schedule for the given account, an `Err` is returned + /// and nothing is updated. + /// + /// Is a no-op if the amount to be vested is zero. + fn add_vesting_schedule( + who: &T::AccountId, + locked: BalanceOf, + per_block: BalanceOf, + starting_block: T::BlockNumber + ) -> DispatchResult { + if locked.is_zero() { return Ok(()) } + if Vesting::::exists(who) { + Err(Error::::ExistingVestingSchedule)? + } + let vesting_schedule = VestingInfo { + locked, + per_block, + starting_block + }; + Vesting::::insert(who, vesting_schedule); + // it can't fail, but even if somehow it did, we don't really care. + let _ = Self::update_lock(who.clone()); + Ok(()) + } + + /// Remove a vesting schedule for a given account. + fn remove_vesting_schedule(who: &T::AccountId) { + Vesting::::remove(who); + // it can't fail, but even if somehow it did, we don't really care. + let _ = Self::update_lock(who.clone()); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + use std::cell::RefCell; + use frame_support::{ + assert_ok, assert_noop, impl_outer_origin, parameter_types, weights::Weight, + traits::Get + }; + use sp_core::H256; + // The testing primitives are very useful for avoiding having to work with signatures + // or public keys. `u64` is used as the `AccountId` and no `Signature`s are required. + use sp_runtime::{ + Perbill, testing::Header, traits::{BlakeTwo256, IdentityLookup, Identity, OnInitialize}, + }; + use sp_storage::Storage; + + impl_outer_origin! { + pub enum Origin for Test where system = frame_system {} + } + + // For testing the module, we construct most of a mock runtime. This means + // first constructing a configuration type (`Test`) which `impl`s each of the + // configuration traits of modules we want to use. + #[derive(Clone, Eq, PartialEq)] + pub struct Test; + parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const MaximumBlockWeight: Weight = 1024; + pub const MaximumBlockLength: u32 = 2 * 1024; + pub const AvailableBlockRatio: Perbill = Perbill::one(); + } + impl frame_system::Trait for Test { + type Origin = Origin; + type Index = u64; + type BlockNumber = u64; + type Hash = H256; + type Call = (); + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = (); + type BlockHashCount = BlockHashCount; + type MaximumBlockWeight = MaximumBlockWeight; + type MaximumBlockLength = MaximumBlockLength; + type AvailableBlockRatio = AvailableBlockRatio; + type Version = (); + type ModuleToIndex = (); + } + parameter_types! { + pub const CreationFee: u64 = 0; + } + impl pallet_balances::Trait for Test { + type Balance = u64; + type OnReapAccount = System; + type OnNewAccount = (); + type Event = (); + type TransferPayment = (); + type DustRemoval = (); + type ExistentialDeposit = ExistentialDeposit; + type CreationFee = CreationFee; + } + impl Trait for Test { + type Event = (); + type Currency = Balances; + type BlockNumberToBalance = Identity; + } + type System = frame_system::Module; + type Balances = pallet_balances::Module; + type Vesting = Module; + + thread_local! { + static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); + } + pub struct ExistentialDeposit; + impl Get for ExistentialDeposit { + fn get() -> u64 { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) } + } + + pub struct ExtBuilder { + existential_deposit: u64, + } + impl Default for ExtBuilder { + fn default() -> Self { + Self { + existential_deposit: 1, + } + } + } + impl ExtBuilder { + pub fn existential_deposit(mut self, existential_deposit: u64) -> Self { + self.existential_deposit = existential_deposit; + self + } + pub fn build(self) -> sp_io::TestExternalities { + EXISTENTIAL_DEPOSIT.with(|v| *v.borrow_mut() = self.existential_deposit); + let mut t = frame_system::GenesisConfig::default().build_storage::().unwrap(); + pallet_balances::GenesisConfig:: { + balances: 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) + ], + }.assimilate_storage(&mut t).unwrap(); + GenesisConfig:: { + vesting: vec![ + (1, 0, 10, 5 * self.existential_deposit), + (2, 10, 20, 0), + (12, 10, 20, 5 * self.existential_deposit) + ], + }.assimilate_storage(&mut t).unwrap(); + t.into() + } + } + + #[test] + fn vesting_info_via_migration_should_work() { + let mut s = Storage::default(); + use hex_literal::hex; + // A dump of data from the previous version for which we know account 6 vests 30 of its 60 + // over 5 blocks from block 3. + let data = vec![ + (hex!["26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac"].to_vec(), hex!["0100000000000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef70a98fdbe9ce6c55837576c60c7af3850"].to_vec(), hex!["02000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7"].to_vec(), hex!["08000000000000000000000000"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef78a42f33323cb5ced3b44dd825fda9fcc"].to_vec(), hex!["4545454545454545454545454545454545454545454545454545454545454545"].to_vec()), + (hex!["26aa394eea5630e07c48ae0c9558cef7a44704b568d21667356a5a050c11874681e47a19e6b29b0a65b9591762ce5143ed30d0261e5d24a3201752506b20f15c"].to_vec(), hex!["4545454545454545454545454545454545454545454545454545454545454545"].to_vec()), + (hex!["3a636f6465"].to_vec(), hex![""].to_vec()), + (hex!["3a65787472696e7369635f696e646578"].to_vec(), hex!["00000000"].to_vec()), + (hex!["3a686561707061676573"].to_vec(), hex!["0800000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc61dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["046d697363202020200300000000000000ffffffffffffffff04"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc66cddb367afbd583bb48f9bbd7d5ba3b1d0738b4881b1cddd38169526d8158137"].to_vec(), hex!["0474786665657320200300000000000000ffffffffffffffff01"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f218f26c73add634897550b4003b26bc6e88b43fded6323ef02ffeffbd8c40846ee09bf316271bd22369659c959dd733a"].to_vec(), hex!["08616c6c20202020200300000000000000ffffffffffffffff1f64656d6f63726163ffffffffffffffff030000000000000002"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f3c22813def93ef32c365b55cb92f10f91dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["0500000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f57c875e4cff74148e4628f264b974c80"].to_vec(), hex!["d200000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f5f27b51b5ec208ee9cb25b55d8728243b8788bb218b185b63e3e92653953f29b6b143fb8cf5159fc908632e6fe490501"].to_vec(), hex!["1e0000000000000006000000000000000200000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b41dbd7d0b561a41d23c2a469ad42fbd70d5438bae826f6fd607413190c37c363b"].to_vec(), hex!["0500000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b46cddb367afbd583bb48f9bbd7d5ba3b1d0738b4881b1cddd38169526d8158137"].to_vec(), hex!["1e00000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4b8788bb218b185b63e3e92653953f29b6b143fb8cf5159fc908632e6fe490501"].to_vec(), hex!["3c00000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4e88b43fded6323ef02ffeffbd8c40846ee09bf316271bd22369659c959dd733a"].to_vec(), hex!["1400000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4e96760d274653a39b429a87ebaae9d3aa4fdf58b9096cf0bebc7c4e5a4c2ed8d"].to_vec(), hex!["2800000000000000"].to_vec()), + (hex!["c2261276cc9d1f8598ea4b6a74b15c2f6482b9ade7bc6657aaca787ba1add3b4effb728943197fd12e694cbf3f3ede28fbf7498b0370c6dfa0013874b417c178"].to_vec(), hex!["3200000000000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911b7f17cdfbfa73331856cca0acddd7842e"].to_vec(), hex!["00000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911bbdcb0c5143a8617ed38ae3810dd45bc6"].to_vec(), hex!["00000000"].to_vec()), + (hex!["f2794c22e353e9a839f12faab03a911be2f6cb0456905c189bcb0458f9440f13"].to_vec(), hex!["00000000"].to_vec()), + ]; + s.top = data.into_iter().collect(); + sp_io::TestExternalities::new(s).execute_with(|| { + Balances::on_initialize(1); + assert_eq!(Balances::free_balance(6), 60); + assert_eq!(Balances::usable_balance(&6), 30); + System::set_block_number(2); + assert_ok!(Vesting::vest(Origin::signed(6))); + assert_eq!(Balances::usable_balance(&6), 30); + System::set_block_number(3); + assert_ok!(Vesting::vest(Origin::signed(6))); + assert_eq!(Balances::usable_balance(&6), 36); + System::set_block_number(4); + assert_ok!(Vesting::vest(Origin::signed(6))); + assert_eq!(Balances::usable_balance(&6), 42); + }); + } + + #[test] + fn check_vesting_status() { + ExtBuilder::default() + .existential_deposit(256) + .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 = VestingInfo { + locked: 256 * 5, + per_block: 128, // Vesting over 10 blocks + starting_block: 0, + }; + let user2_vesting_schedule = VestingInfo { + locked: 256 * 20, + per_block: 256, // Vesting over 20 blocks + starting_block: 10, + }; + let user12_vesting_schedule = VestingInfo { + locked: 256 * 5, + per_block: 64, // Vesting over 20 blocks + starting_block: 10, + }; + assert_eq!(Vesting::vesting(&1), Some(user1_vesting_schedule)); // Account 1 has a vesting schedule + assert_eq!(Vesting::vesting(&2), Some(user2_vesting_schedule)); // Account 2 has a vesting schedule + assert_eq!(Vesting::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!(Vesting::vesting_balance(&1), 128 * 9); + // Account 2 has their full balance locked + assert_eq!(Vesting::vesting_balance(&2), user2_free_balance); + // Account 12 has only their illiquid funds locked + assert_eq!(Vesting::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!(Vesting::vesting_balance(&1), 0); + // Account 2 has started vesting by block 10 + assert_eq!(Vesting::vesting_balance(&2), user2_free_balance); + // Account 12 has started vesting by block 10 + assert_eq!(Vesting::vesting_balance(&12), user12_free_balance - 256 * 5); + + System::set_block_number(30); + assert_eq!(System::block_number(), 30); + + assert_eq!(Vesting::vesting_balance(&1), 0); // Account 1 is still fully vested, and not negative + assert_eq!(Vesting::vesting_balance(&2), 0); // Account 2 has fully vested by block 30 + assert_eq!(Vesting::vesting_balance(&12), 0); // Account 2 has fully vested by block 30 + + }); + } + + #[test] + fn unvested_balance_should_not_transfer() { + ExtBuilder::default() + .existential_deposit(10) + .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!(Vesting::vesting_balance(&1), 45); + assert_noop!( + Balances::transfer(Some(1).into(), 2, 56), + pallet_balances::Error::::LiquidityRestrictions, + ); // Account 1 cannot send more than vested amount + }); + } + + #[test] + fn vested_balance_should_transfer() { + ExtBuilder::default() + .existential_deposit(10) + .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!(Vesting::vesting_balance(&1), 45); + assert_ok!(Vesting::vest(Some(1).into())); + assert_ok!(Balances::transfer(Some(1).into(), 2, 55)); + }); + } + + #[test] + fn vested_balance_should_transfer_using_vest_other() { + ExtBuilder::default() + .existential_deposit(10) + .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!(Vesting::vesting_balance(&1), 45); + assert_ok!(Vesting::vest_other(Some(2).into(), 1)); + assert_ok!(Balances::transfer(Some(1).into(), 2, 55)); + }); + } + + #[test] + fn extra_balance_should_transfer() { + ExtBuilder::default() + .existential_deposit(10) + .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!(Vesting::vesting_balance(&1), 45); + assert_ok!(Vesting::vest(Some(1).into())); + 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!(Vesting::vesting_balance(&2), 200); + assert_ok!(Vesting::vest(Some(2).into())); + 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) + .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!(Vesting::vesting_balance(&12), user12_free_balance - 256 * 5); + + // Account 12 has delayed vesting + let user12_vesting_schedule = VestingInfo { + locked: 256 * 5, + per_block: 64, // Vesting over 20 blocks + starting_block: 10, + }; + assert_eq!(Vesting::vesting(&12), Some(user12_vesting_schedule)); + + // Account 12 can still send liquid funds + assert_ok!(Balances::transfer(Some(12).into(), 3, 256 * 5)); + }); + } +}