diff --git a/node/cli/src/lib.rs b/node/cli/src/lib.rs index db7962f78..6f3e38fcf 100644 --- a/node/cli/src/lib.rs +++ b/node/cli/src/lib.rs @@ -172,7 +172,7 @@ where info!("{}", version.name); info!(" version {}", config.full_version()); info!(" _____ _ _ "); - info!(" | __ \\ (_) (_) "); + info!(" | __ \\ (_) (_) "); info!(" | | | | __ _ _ ____ ___ _ __ _ __ _ "); info!(" | | | |/ _` | '__\\ \\ /\\ / / | '_ \\| |/ _` |"); info!(" | |__| | (_| | | \\ V V /| | | | | | (_| |"); diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 8c116ed6e..4dd732922 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -32,7 +32,9 @@ use sr_api::impl_runtime_apis; use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Moment, Signature}; use rstd::prelude::*; -use sr_primitives::traits::{self, BlakeTwo256, Block as BlockT, NumberFor, SaturatedConversion, StaticLookup}; +use sr_primitives::traits::{ + self, BlakeTwo256, Block as BlockT, NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup, +}; use sr_primitives::transaction_validity::TransactionValidity; use sr_primitives::weights::Weight; #[cfg(any(feature = "std", test))] @@ -245,7 +247,6 @@ impl authorship::Trait for Runtime { // TODO: Introduce some structure to tie these together to make it a bit less of a footgun. This // should be easy, since OneSessionHandler trait provides the `Key` as an associated type. #2858 -type SessionHandlers = (Grandpa, Babe, ImOnline, AuthorityDiscovery); parameter_types! { pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(17); } @@ -255,7 +256,7 @@ impl session::Trait for Runtime { type ValidatorIdOf = staking::StashOf; type ShouldEndSession = Babe; type OnSessionEnding = Staking; - type SessionHandler = SessionHandlers; + type SessionHandler = ::KeyTypeIdProviders; type Keys = SessionKeys; type DisabledValidatorsThreshold = DisabledValidatorsThreshold; type SelectInitialValidators = Staking; diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 6bcfe86de..cbcc5ee0c 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -1141,13 +1141,13 @@ impl Module { CurrentEraStartSessionIndex::mutate(|v| { *v = start_session_index; }); - let bonding_duration = { + let bonding_era = { const BONDING_DURATION_ERA_TO_SECS_RATIO: TimeStamp = 300; (T::BondingDuration::get() / BONDING_DURATION_ERA_TO_SECS_RATIO) as _ }; - if current_era > bonding_duration { - let first_kept = current_era - bonding_duration; + if current_era > bonding_era { + let first_kept = current_era - bonding_era; BondedEras::mutate(|bonded| { bonded.push((current_era, start_session_index)); diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 5d103da2b..8094fe061 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -1,11 +1,11 @@ use srml_support::{ assert_err, assert_ok, - traits::{Currency, WithdrawReasons}, + traits::{Currency, WithdrawReason, WithdrawReasons}, }; use super::*; use crate::mock::*; -use darwinia_support::types::{CompositeLock, Lock, Locks}; +use darwinia_support::types::{CompositeLock, Locks}; // gen_paired_account!(a(1), b(2), m(12)); // will create stash `a` and controller `b` @@ -76,12 +76,12 @@ fn test_env_build() { assert_eq!(Staking::bonded(&11), Some(10)); assert_eq!( - Staking::ledger(&10), - Some(StakingLedger { + Staking::ledger(&10).unwrap(), + StakingLedger { stash: 11, total_ring: 100 * COIN, - active_deposit_ring: 100 * COIN, active_ring: 100 * COIN, + active_deposit_ring: 100 * COIN, total_kton: 0, active_kton: 0, deposit_items: vec![TimeDepositItem { @@ -89,7 +89,7 @@ fn test_env_build() { start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }], - }) + } ); assert_eq!(Kton::free_balance(&11), COIN / 100); @@ -103,12 +103,12 @@ fn test_env_build() { 13 )); assert_eq!( - Staking::ledger(&10), - Some(StakingLedger { + Staking::ledger(&10).unwrap(), + StakingLedger { stash: 11, total_ring: origin_ledger.total_ring + 20 * COIN, - active_deposit_ring: origin_ledger.active_deposit_ring + 20 * COIN, active_ring: origin_ledger.active_ring + 20 * COIN, + active_deposit_ring: origin_ledger.active_deposit_ring + 20 * COIN, total_kton: 0, active_kton: 0, deposit_items: vec![ @@ -123,203 +123,160 @@ fn test_env_build() { expire_time: 13 * MONTH_IN_SECONDS as u64 } ], - }) + } ); }); } -//#[test] -//fn normal_kton_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// Kton::deposit_creating(&1001, 10 * COIN); -// assert_ok!(Staking::bond( -// Origin::signed(1001), -// 1000, -// StakingBalance::Kton(10 * COIN), -// RewardDestination::Stash, -// 0 -// )); -// assert_eq!( -// Staking::ledger(&1000), -// Some(StakingLedger { -// stash: 1001, -// total_ring: 0, -// total_deposit_ring: 0, -// active_deposit_ring: 0, -// active_ring: 0, -// total_kton: 10 * COIN, -// active_kton: 10 * COIN, -// deposit_items: vec![], -// unlocking: vec![] -// }) -// ); -// -// assert_eq!( -// Kton::locks(&1001), -// vec![kton::BalanceLock { -// id: STAKING_ID, -// amount: 10 * COIN, -// until: u64::max_value(), -// reasons: WithdrawReasons::all() -// }] -// ); -// -// // promise_month should not work for kton -// Kton::deposit_creating(&2001, 10 * COIN); -// assert_ok!(Staking::bond( -// Origin::signed(2001), -// 2000, -// StakingBalance::Kton(10 * COIN), -// RewardDestination::Stash, -// 12 -// )); -// assert_eq!( -// Staking::ledger(&2000), -// Some(StakingLedger { -// stash: 2001, -// total_ring: 0, -// total_deposit_ring: 0, -// active_deposit_ring: 0, -// active_ring: 0, -// total_kton: 10 * COIN, -// active_kton: 10 * COIN, -// deposit_items: vec![], -// unlocking: vec![] -// }) -// ); -// }); -//} -// -//#[test] -//fn time_deposit_ring_unbond_and_withdraw_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// Timestamp::set_timestamp(13 * MONTH_IN_SECONDS as u64); -// -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); -// assert_eq!( -// Staking::ledger(&10), -// Some(StakingLedger { -// stash: 11, -// total_ring: 100 * COIN, -// total_deposit_ring: 100 * COIN, -// active_deposit_ring: 90 * COIN, -// active_ring: 90 * COIN, -// total_kton: 0, -// active_kton: 0, -// deposit_items: vec![TimeDepositItem { -// value: 90 * COIN, -// start_time: 0, -// expire_time: 12 * MONTH_IN_SECONDS as u64 -// }], -// unlocking: vec![UnlockChunk { -// value: StakingBalance::Ring(10 * COIN), -// era: 3, -// is_time_deposit: true -// }] -// }) -// ); -// -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(20 * COIN))); -// assert_eq!( -// Staking::ledger(&10), -// Some(StakingLedger { -// stash: 11, -// total_ring: 100 * COIN, -// total_deposit_ring: 100 * COIN, -// active_deposit_ring: 70 * COIN, -// active_ring: 70 * COIN, -// total_kton: 0, -// active_kton: 0, -// deposit_items: vec![TimeDepositItem { -// value: 70 * COIN, -// start_time: 0, -// expire_time: 12 * MONTH_IN_SECONDS as u64 -// }], -// unlocking: vec![ -// UnlockChunk { -// value: StakingBalance::Ring(10 * COIN), -// era: 3, -// is_time_deposit: true -// }, -// UnlockChunk { -// value: StakingBalance::Ring(20 * COIN), -// era: 3, -// is_time_deposit: true -// } -// ] -// }) -// ); -// -// // more than active ring -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(120 * COIN))); -// assert_eq!( -// Staking::ledger(&10), -// Some(StakingLedger { -// stash: 11, -// total_ring: 100 * COIN, -// total_deposit_ring: 100 * COIN, -// active_deposit_ring: 0, -// active_ring: 0, -// total_kton: 0, -// active_kton: 0, -// deposit_items: vec![], // should be cleared -// unlocking: vec![ -// UnlockChunk { -// value: StakingBalance::Ring(10 * COIN), -// era: 3, -// is_time_deposit: true -// }, -// UnlockChunk { -// value: StakingBalance::Ring(20 * COIN), -// era: 3, -// is_time_deposit: true -// }, -// UnlockChunk { -// value: StakingBalance::Ring(70 * COIN), -// era: 3, -// is_time_deposit: true -// }, -// ] -// }) -// ); -// -// start_era(3); -// -// assert_ok!(Staking::withdraw_unbonded(Origin::signed(10))); -// assert_eq!( -// Staking::ledger(&10), -// Some(StakingLedger { -// stash: 11, -// total_ring: 0, -// total_deposit_ring: 0, -// active_deposit_ring: 0, -// active_ring: 0, -// total_kton: 0, -// active_kton: 0, -// deposit_items: vec![], // should be cleared -// unlocking: vec![] -// }) -// ); -// -// let free_balance = Ring::free_balance(&11); -// assert_eq!( -// Ring::locks(&11), -// vec![balances::BalanceLock { -// id: STAKING_ID, -// amount: 0, -// until: u64::max_value(), -// reasons: WithdrawReasons::all() -// }] -// ); -// assert_ok!(Ring::ensure_can_withdraw( -// &11, -// free_balance, -// WithdrawReason::Transfer, -// 0 -// )); -// }); -//} -// +#[test] +fn normal_kton_should_work() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + Kton::deposit_creating(&1001, 10 * COIN); + assert_ok!(Staking::bond( + Origin::signed(1001), + 1000, + StakingBalance::Kton(10 * COIN), + RewardDestination::Stash, + 0 + )); + assert_eq!( + Staking::ledger(&1000).unwrap(), + StakingLedger { + stash: 1001, + total_ring: 0, + active_ring: 0, + active_deposit_ring: 0, + total_kton: 10 * COIN, + active_kton: 10 * COIN, + deposit_items: vec![], + } + ); + assert_eq!(Kton::locks(&1001), Locks(vec![CompositeLock::Staking(10 * COIN)])); + + // promise_month should not work for kton + Kton::deposit_creating(&2001, 10 * COIN); + assert_ok!(Staking::bond( + Origin::signed(2001), + 2000, + StakingBalance::Kton(10 * COIN), + RewardDestination::Stash, + 12 + )); + assert_eq!( + Staking::ledger(&2000).unwrap(), + StakingLedger { + stash: 2001, + total_ring: 0, + active_ring: 0, + active_deposit_ring: 0, + total_kton: 10 * COIN, + active_kton: 10 * COIN, + deposit_items: vec![], + } + ); + }); +} + +#[test] +fn time_deposit_ring_unbond_and_withdraw_should_work() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + Timestamp::set_timestamp(13 * MONTH_IN_SECONDS as u64); + + let ledger = Staking::ledger(&10).unwrap(); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); + // Only active normal ring can be unbond + assert_eq!(&Staking::ledger(&10).unwrap(), &ledger,); + assert_eq!(Ring::locks(11), Locks(vec![CompositeLock::Staking(100 * COIN)])); + + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(20 * COIN))); + assert_eq!( + Staking::ledger(&10).unwrap(), + StakingLedger { + stash: 11, + total_ring: 100 * COIN, + active_ring: 100 * COIN, + active_deposit_ring: 70 * COIN, + total_kton: 0, + active_kton: 0, + deposit_items: vec![TimeDepositItem { + value: 70 * COIN, + start_time: 0, + expire_time: 12 * MONTH_IN_SECONDS as u64 + }], + } + ); + // unlocking: vec![ + // UnlockChunk { + // value: StakingBalance::Ring(10 * COIN), + // era: 3, + // is_time_deposit: true + // }, + // UnlockChunk { + // value: StakingBalance::Ring(20 * COIN), + // era: 3, + // is_time_deposit: true + // } + // ] + + // more than active ring + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(120 * COIN))); + assert_eq!( + Staking::ledger(&10).unwrap(), + StakingLedger { + stash: 11, + total_ring: 100 * COIN, + active_ring: 100 * COIN, + active_deposit_ring: 0, + total_kton: 0, + active_kton: 0, + deposit_items: vec![], + } + ); + // unlocking: vec![ + // UnlockChunk { + // value: StakingBalance::Ring(10 * COIN), + // era: 3, + // is_time_deposit: true + // }, + // UnlockChunk { + // value: StakingBalance::Ring(20 * COIN), + // era: 3, + // is_time_deposit: true + // }, + // UnlockChunk { + // value: StakingBalance::Ring(70 * COIN), + // era: 3, + // is_time_deposit: true + // }, + // ] + + Timestamp::set_timestamp(BondingDuration::get()); + + assert_eq!( + Staking::ledger(&10).unwrap(), + StakingLedger { + stash: 11, + total_ring: 0, + active_ring: 0, + active_deposit_ring: 0, + total_kton: 0, + active_kton: 0, + deposit_items: vec![], + } + ); + // unlocking: vec![] + + let free_balance = Ring::free_balance(&11); + assert_eq!(Ring::locks(&11), Locks(vec![CompositeLock::Staking(0)])); + assert_ok!(Ring::ensure_can_withdraw( + &11, + free_balance, + WithdrawReason::Transfer.into(), + 0 + )); + }); +} + //#[test] //fn normal_unbond_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| {