Refactor the balances module (#4649)

* Initially scoping out of the problem

* Remove need for exiry in balance locks.

* Remove expiry from locks.

* Remove supefluous balance test

* Amalgamate pieces of balance module

* Split out vesting

* Fix tests

* Fixes for vesting.

* Docs.

* Weight docs.

* Refactor things in terms of set_balances.

* Switch out ED to be free + reserved.

* Remove on_free_balance_zero and some docs.

* Build fixes

* Update frame/vesting/src/lib.rs

Co-Authored-By: Xiliang Chen <xlchen1291@gmail.com>

* Update frame/vesting/src/lib.rs

Co-Authored-By: Xiliang Chen <xlchen1291@gmail.com>

* Migration

* Remove superfluous code.

* Test fixes

* Fix some tests

* Fix repatriate reserve

* Fixes

* Add test for migration

* Final cleanups

* Fix

* Indentation.

* Undo unneeded referencing

* Bump runtime version

* Fixes

Co-authored-by: Xiliang Chen <xlchen1291@gmail.com>
This commit is contained in:
Gavin Wood
2020-02-01 13:20:16 +00:00
committed by GitHub
parent b6e8fba179
commit d52d8692f9
46 changed files with 1748 additions and 1171 deletions
+1 -1
View File
@@ -149,7 +149,7 @@ impl<T: Trait> AccountDb<T> 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;
+1 -1
View File
@@ -573,7 +573,7 @@ impl<T: Trait> Token<T> for TransferFeeToken<BalanceOf<T>> {
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)
}
+7 -11
View File
@@ -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<BalanceOf<Self>>;
/// The fee required to make a transfer.
type TransferFee: Get<BalanceOf<Self>>;
/// The fee required to create an account.
type CreationFee: Get<BalanceOf<Self>>;
@@ -520,9 +517,6 @@ decl_module! {
/// to removal of a contract.
const SurchargeReward: BalanceOf<T> = T::SurchargeReward::get();
/// The fee required to make a transfer.
const TransferFee: BalanceOf<T> = T::TransferFee::get();
/// The fee required to create an account.
const CreationFee: BalanceOf<T> = T::CreationFee::get();
@@ -953,8 +947,8 @@ decl_storage! {
}
}
impl<T: Trait> OnFreeBalanceZero<T::AccountId> for Module<T> {
fn on_free_balance_zero(who: &T::AccountId) {
impl<T: Trait> OnReapAccount<T::AccountId> for Module<T> {
fn on_reap_account(who: &T::AccountId) {
if let Some(ContractInfo::Alive(info)) = <ContractInfoOf<T>>::take(who) {
child::kill_storage(&info.trie_id, info.child_trie_unique_id());
}
@@ -973,7 +967,6 @@ pub struct Config<T: Trait> {
pub max_value_size: u32,
pub contract_account_instantiate_fee: BalanceOf<T>,
pub account_create_fee: BalanceOf<T>,
pub transfer_fee: BalanceOf<T>,
}
impl<T: Trait> Config<T> {
@@ -986,7 +979,6 @@ impl<T: Trait> Config<T> {
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,
+12 -16
View File
@@ -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::<Test>().unwrap();
pallet_balances::GenesisConfig::<Test> {
balances: vec![],
vesting: vec![],
}.assimilate_storage(&mut t).unwrap();
GenesisConfig::<Test> {
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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::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::<Test>::get(BOB).is_none());
assert_eq!(Balances::free_balance(&BOB), Balances::minimum_balance());
assert_eq!(Balances::free_balance(BOB), Balances::minimum_balance());
});
}