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
+25 -22
View File
@@ -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<E: Encode>(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<u8>, Hash) {
fn panic_execution_with_foreign_code_gives_error() {
let mut t = TestExternalities::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
69_u128.encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(69u128, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
69_u128.encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(69u128, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS).encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS).encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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::<Blake2Hasher>::new_with_code(BLOATY_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
0_u128.encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(0_u128, 0_u128, 0_u128, 0_u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS).encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(111 * DOLLARS, 0u128, 0u128, 0u128).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::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);
});
}
+6 -8
View File
@@ -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::<Blake2Hasher>::new_with_code(COMPACT_CODE, Storage {
top: map![
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(alice()) => {
(100 * DOLLARS).encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(alice()) => {
(100 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
},
<pallet_balances::FreeBalance<Runtime>>::hashed_key_for(bob()) => {
(10 * DOLLARS).encode()
<pallet_balances::Account<Runtime>>::hashed_key_for(bob()) => {
(10 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
},
<pallet_balances::TotalIssuance<Runtime>>::hashed_key().to_vec() => {
(110 * DOLLARS).encode()
(110 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS, 0 * DOLLARS).encode()
},
<pallet_indices::NextEnumSet<Runtime>>::hashed_key().to_vec() => vec![0u8; 16],
<frame_system::BlockHash<Runtime>>::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);
});
@@ -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();
<pallet_balances::FreeBalance<Runtime, _>>::insert(&address, 5_000_000_000_000);
let account = pallet_balances::AccountData { free: 5_000_000_000_000, ..Default::default() };
<pallet_balances::Account<Runtime, _>>::insert(&address, account);
// check validity
let res = Executive::validate_transaction(extrinsic);