Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Refactor the balances module #4649

Merged
merged 38 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
eb622d4
Initially scoping out of the problem
gavofyork Jan 16, 2020
d64eb64
Remove need for exiry in balance locks.
gavofyork Jan 19, 2020
1d7d881
Remove expiry from locks.
gavofyork Jan 19, 2020
726de0e
Remove supefluous balance test
gavofyork Jan 19, 2020
73ad7ec
Amalgamate pieces of balance module
gavofyork Jan 20, 2020
281ae43
Split out vesting
gavofyork Jan 22, 2020
878a2b9
Fix tests
gavofyork Jan 22, 2020
efa0e35
Fixes for vesting.
gavofyork Jan 22, 2020
ade6077
Merge remote-tracking branch 'origin/master' into gav-refactor-balances
gavofyork Jan 22, 2020
9f47c32
Docs.
gavofyork Jan 23, 2020
370bdbe
Weight docs.
gavofyork Jan 23, 2020
fb4e793
Refactor things in terms of set_balances.
gavofyork Jan 23, 2020
63cc7e7
Switch out ED to be free + reserved.
gavofyork Jan 23, 2020
cca0c11
Remove on_free_balance_zero and some docs.
gavofyork Jan 23, 2020
e4a9651
Build fixes
gavofyork Jan 23, 2020
2603170
Update frame/vesting/src/lib.rs
gavofyork Jan 23, 2020
16eb4fe
Update frame/vesting/src/lib.rs
gavofyork Jan 23, 2020
875073d
Migration
gavofyork Jan 23, 2020
ffff9a9
Merge remote-tracking branch 'origin/gav-refactor-balances' into gav-…
gavofyork Jan 23, 2020
99d86e8
Remove superfluous code.
gavofyork Jan 23, 2020
4827931
Test fixes
gavofyork Jan 23, 2020
21687dd
Fix some tests
gavofyork Jan 24, 2020
4774820
Fix repatriate reserve
gavofyork Jan 24, 2020
3d8b2c1
Fixes
gavofyork Jan 26, 2020
348edec
Merge remote-tracking branch 'origin/master' into gav-refactor-balances
gavofyork Jan 26, 2020
2735132
Add test for migration
gavofyork Jan 26, 2020
746b630
Final cleanups
gavofyork Jan 26, 2020
7843d6b
Fix
gavofyork Jan 26, 2020
89dcdc9
Indentation.
gavofyork Jan 27, 2020
050d44c
Undo unneeded referencing
gavofyork Jan 27, 2020
b0a02ac
Merge remote-tracking branch 'origin/master' into gav-refactor-balances
gavofyork Jan 29, 2020
b60ac8a
Bump runtime version
gavofyork Jan 29, 2020
735b31f
Fixes
gavofyork Jan 29, 2020
9b19152
Merge branch 'master' into gav-refactor-balances
gavofyork Jan 29, 2020
2a19cd3
Merge branch 'master' into gav-refactor-balances
gavofyork Jan 29, 2020
d5183db
Merge branch 'master' into gav-refactor-balances
gavofyork Jan 31, 2020
4d4e343
Merge remote-tracking branch 'origin/master' into gav-refactor-balances
gavofyork Jan 31, 2020
730e9c2
Merge branch 'master' into gav-refactor-balances
gavofyork Jan 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 0 additions & 4 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -214,7 +211,6 @@ impl balances::Trait for Runtime {
type DustRemoval = ();
type TransferPayment = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
}

Expand Down
1 change: 0 additions & 1 deletion bin/node-template/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
47 changes: 25 additions & 22 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
});
}
Expand All @@ -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()
Expand Down Expand Up @@ -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);
});
}
Expand All @@ -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![
Expand All @@ -362,7 +364,7 @@ fn full_native_block_import_works() {
alice().into(),
bob().into(),
69 * DOLLARS,
1 * CENTS,
0,
)),
topics: vec![],
},
Expand Down Expand Up @@ -416,7 +418,7 @@ fn full_native_block_import_works() {
bob().into(),
alice().into(),
5 * DOLLARS,
1 * CENTS,
0,
)
),
topics: vec![],
Expand All @@ -440,7 +442,7 @@ fn full_native_block_import_works() {
alice().into(),
bob().into(),
15 * DOLLARS,
1 * CENTS,
0,
)
),
topics: vec![],
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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);
});
}
Expand Down
14 changes: 6 additions & 8 deletions bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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);
});
Expand Down
3 changes: 2 additions & 1 deletion bin/node/executor/tests/submit_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 3 additions & 7 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// and set impl_version to 0. If only runtime
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 210,
impl_version: 1,
spec_version: 211,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
};

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion bin/node/testing/src/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
4 changes: 3 additions & 1 deletion frame/balances/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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",
]
Loading