Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Fix up balances, remove balances stuff from staking
  • Loading branch information
gavofyork committed Aug 29, 2018
commit 318fda26ad8134a4893aa2dc154f240c2838cca7
3 changes: 1 addition & 2 deletions substrate/runtime/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ extern crate substrate_runtime_session as session;
extern crate substrate_runtime_system as system;
extern crate substrate_runtime_timestamp as timestamp;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This extern crate can be removed.


#[cfg(test)] use std::fmt::Debug;
use rstd::prelude::*;
use rstd::{cmp, result};
use codec::{Encode, Decode, Codec, Input, Output};
Expand Down Expand Up @@ -458,7 +457,7 @@ impl<T: Trait> Module<T> {
if b < value {
return Err("not enough free funds")
}
if T::IsAccountLiquid::is_account_liquid(who) {
if !T::IsAccountLiquid::is_account_liquid(who) {
return Err("free funds are still bonded")
}
Self::set_reserved_balance(who, Self::reserved_balance(who) + value);
Expand Down
28 changes: 3 additions & 25 deletions substrate/runtime/balances/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
#![cfg(test)]

use primitives::BuildStorage;
use primitives::traits::{HasPublicAux, Identity};
use primitives::traits::HasPublicAux;
use primitives::testing::{Digest, Header};
use substrate_primitives::{H256, KeccakHasher};
use runtime_io;
use {GenesisConfig, Module, Trait, consensus, session, system, timestamp};
use {GenesisConfig, Module, Trait, consensus, system};

// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
Expand All @@ -45,15 +45,6 @@ impl system::Trait for Test {
type Header = Header;
type Event = ();
}
impl session::Trait for Test {
type ConvertAccountIdToSessionKey = Identity;
type OnSessionChange = Staking;
type Event = ();
}
impl timestamp::Trait for Test {
const TIMESTAMP_SET_POSITION: u32 = 0;
type Moment = u64;
}
impl Trait for Test {
type Balance = u64;
type AccountIndex = u64;
Expand All @@ -73,17 +64,9 @@ pub fn new_test_ext(ext_deposit: u64, monied: bool) -> runtime_io::TestExternali
code: vec![],
authorities: vec![],
}.build_storage().unwrap());
t.extend(session::GenesisConfig::<Test>{
session_length,
validators: vec![10, 20],
}.build_storage().unwrap());
t.extend(GenesisConfig::<Test>{
balances: if monied {
if reward > 0 {
vec![(1, 10 * balance_factor), (2, 20 * balance_factor), (3, 30 * balance_factor), (4, 40 * balance_factor), (10, balance_factor), (20, balance_factor)]
} else {
vec![(1, 10 * balance_factor), (2, 20 * balance_factor), (3, 30 * balance_factor), (4, 40 * balance_factor)]
}
vec![(1, 10 * balance_factor), (2, 20 * balance_factor), (3, 30 * balance_factor), (4, 40 * balance_factor)]
} else {
vec![(10, balance_factor), (20, balance_factor)]
},
Expand All @@ -94,13 +77,8 @@ pub fn new_test_ext(ext_deposit: u64, monied: bool) -> runtime_io::TestExternali
creation_fee: 0,
reclaim_rebate: 0,
}.build_storage().unwrap());
t.extend(timestamp::GenesisConfig::<Test>{
period: 5
}.build_storage().unwrap());
t.into()
}

pub type System = system::Module<Test>;
pub type Session = session::Module<Test>;
pub type Timestamp = timestamp::Module<Test>;
pub type Balances = Module<Test>;
54 changes: 27 additions & 27 deletions substrate/runtime/balances/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@

use super::*;
use runtime_io::with_externalities;
use mock::{Session, Balances, System, Timestamp, Test, new_test_ext};
use mock::{Balances, System, Test, new_test_ext};

#[test]
fn reward_should_work() {
with_externalities(&mut new_test_ext(0, 3, 3, 0, true, 10), || {
assert_eq!(Balances::voting_balance(&10), 1);
assert_ok!(Balances::reward(&10, 10));
assert_eq!(Balances::voting_balance(&10), 11);
assert_eq!(<TotalStake<Test>>::get(), 112);
with_externalities(&mut new_test_ext(0, true), || {
assert_eq!(Balances::voting_balance(&1), 10);
assert_ok!(Balances::reward(&1, 10));
assert_eq!(Balances::voting_balance(&1), 20);
assert_eq!(<TotalStake<Test>>::get(), 110);
});
}

#[test]
fn indexing_lookup_should_work() {
with_externalities(&mut new_test_ext(10, 1, 2, 0, true, 0), || {
with_externalities(&mut new_test_ext(10, true), || {
assert_eq!(Balances::lookup_index(0), Some(1));
assert_eq!(Balances::lookup_index(1), Some(2));
assert_eq!(Balances::lookup_index(2), Some(3));
Expand All @@ -45,7 +45,7 @@ fn indexing_lookup_should_work() {

#[test]
fn default_indexing_on_new_accounts_should_work() {
with_externalities(&mut new_test_ext(10, 1, 2, 0, true, 0), || {
with_externalities(&mut new_test_ext(10, true), || {
assert_eq!(Balances::lookup_index(4), None);
assert_ok!(Balances::transfer(&1, 5.into(), 10));
assert_eq!(Balances::lookup_index(4), Some(5));
Expand All @@ -54,7 +54,7 @@ fn default_indexing_on_new_accounts_should_work() {

#[test]
fn dust_account_removal_should_work() {
with_externalities(&mut new_test_ext(256 * 10, 1, 2, 0, true, 0), || {
with_externalities(&mut new_test_ext(256 * 10, true), || {
System::inc_account_nonce(&2);
assert_eq!(System::account_nonce(&2), 1);
assert_eq!(Balances::voting_balance(&2), 256 * 20);
Expand All @@ -68,7 +68,7 @@ fn dust_account_removal_should_work() {

#[test]
fn reclaim_indexing_on_new_accounts_should_work() {
with_externalities(&mut new_test_ext(256 * 1, 1, 2, 0, true, 0), || {
with_externalities(&mut new_test_ext(256 * 1, true), || {
assert_eq!(Balances::lookup_index(1), Some(2));
assert_eq!(Balances::lookup_index(4), None);
assert_eq!(Balances::voting_balance(&2), 256 * 20);
Expand All @@ -84,7 +84,7 @@ fn reclaim_indexing_on_new_accounts_should_work() {

#[test]
fn reserved_balance_should_prevent_reclaim_count() {
with_externalities(&mut new_test_ext(256 * 1, 1, 2, 0, true, 0), || {
with_externalities(&mut new_test_ext(256 * 1, true), || {
System::inc_account_nonce(&2);
assert_eq!(Balances::lookup_index(1), Some(2));
assert_eq!(Balances::lookup_index(4), None);
Expand Down Expand Up @@ -112,7 +112,7 @@ fn reserved_balance_should_prevent_reclaim_count() {

#[test]
fn balance_works() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 42);
assert_eq!(Balances::free_balance(&1), 42);
assert_eq!(Balances::reserved_balance(&1), 0);
Expand All @@ -125,7 +125,7 @@ fn balance_works() {

#[test]
fn balance_transfer_works() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
Balances::increase_total_stake_by(111);
assert_ok!(Balances::transfer(&1, 2.into(), 69));
Expand All @@ -136,7 +136,7 @@ fn balance_transfer_works() {

#[test]
fn reserving_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);

assert_eq!(Balances::voting_balance(&1), 111);
Expand All @@ -153,7 +153,7 @@ fn reserving_balance_should_work() {

#[test]
fn balance_transfer_when_reserved_should_not_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
assert_ok!(Balances::reserve(&1, 69));
assert_noop!(Balances::transfer(&1, 2.into(), 69), "balance too low to send value");
Expand All @@ -162,7 +162,7 @@ fn balance_transfer_when_reserved_should_not_work() {

#[test]
fn deducting_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
assert_ok!(Balances::reserve(&1, 69));
assert_eq!(Balances::free_balance(&1), 42);
Expand All @@ -171,7 +171,7 @@ fn deducting_balance_should_work() {

#[test]
fn refunding_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 42);
Balances::set_reserved_balance(&1, 69);
Balances::unreserve(&1, 69);
Expand All @@ -182,7 +182,7 @@ fn refunding_balance_should_work() {

#[test]
fn slashing_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
Balances::increase_total_stake_by(111);
assert_ok!(Balances::reserve(&1, 69));
Expand All @@ -195,7 +195,7 @@ fn slashing_balance_should_work() {

#[test]
fn slashing_incomplete_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 42);
Balances::increase_total_stake_by(42);
assert_ok!(Balances::reserve(&1, 21));
Expand All @@ -208,7 +208,7 @@ fn slashing_incomplete_balance_should_work() {

#[test]
fn unreserving_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
Balances::unreserve(&1, 42);
Expand All @@ -219,7 +219,7 @@ fn unreserving_balance_should_work() {

#[test]
fn slashing_reserved_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
Balances::increase_total_stake_by(111);
assert_ok!(Balances::reserve(&1, 111));
Expand All @@ -232,7 +232,7 @@ fn slashing_reserved_balance_should_work() {

#[test]
fn slashing_incomplete_reserved_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
Balances::increase_total_stake_by(111);
assert_ok!(Balances::reserve(&1, 42));
Expand All @@ -245,7 +245,7 @@ fn slashing_incomplete_reserved_balance_should_work() {

#[test]
fn transferring_reserved_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 110);
Balances::set_free_balance(&2, 1);
assert_ok!(Balances::reserve(&1, 110));
Expand All @@ -259,7 +259,7 @@ fn transferring_reserved_balance_should_work() {

#[test]
fn transferring_reserved_balance_to_nonexistent_should_fail() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 111);
assert_ok!(Balances::reserve(&1, 111));
assert_noop!(Balances::transfer_reserved(&1, &2, 42), "beneficiary account must pre-exist");
Expand All @@ -268,7 +268,7 @@ fn transferring_reserved_balance_to_nonexistent_should_fail() {

#[test]
fn transferring_incomplete_reserved_balance_should_work() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
Balances::set_free_balance(&1, 110);
Balances::set_free_balance(&2, 1);
assert_ok!(Balances::reserve(&1, 41));
Expand All @@ -282,7 +282,7 @@ fn transferring_incomplete_reserved_balance_should_work() {

#[test]
fn transferring_too_high_value_should_not_panic() {
with_externalities(&mut new_test_ext(0, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(0, false), || {
<FreeBalance<Test>>::insert(1, u64::max_value());
<FreeBalance<Test>>::insert(2, 1);

Expand All @@ -298,7 +298,7 @@ fn transferring_too_high_value_should_not_panic() {

#[test]
fn account_removal_on_free_too_low() {
with_externalities(&mut new_test_ext(100, 1, 3, 1, false, 0), || {
with_externalities(&mut new_test_ext(100, false), || {
// Setup two accounts with free balance above the exsistential threshold.
{
Balances::set_free_balance(&1, 110);
Expand Down
3 changes: 2 additions & 1 deletion substrate/runtime/contract/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ extern crate substrate_runtime_sandbox as sandbox;
extern crate substrate_runtime_std as rstd;

extern crate substrate_runtime_consensus as consensus;
extern crate substrate_runtime_balances as balances;
extern crate substrate_runtime_staking as staking;
extern crate substrate_runtime_system as system;

Expand Down Expand Up @@ -269,7 +270,7 @@ impl<T: Trait> Module<T> {
}
}

impl<T: Trait> staking::OnFreeBalanceZero<T::AccountId> for Module<T> {
impl<T: Trait> balances::OnFreeBalanceZero<T::AccountId> for Module<T> {
fn on_free_balance_zero(who: &T::AccountId) {
<CodeOf<T>>::remove(who);
<StorageOf<T>>::remove_prefix(who.clone());
Expand Down
Loading