From 8c800d146b375287f00d3bdbe5518a056be53c9b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 12:12:45 +0800 Subject: [PATCH 01/45] update: sync tests and runtime primitives --- Cargo.lock | 3 + node/cli/src/chain_spec.rs | 14 +- node/runtime/src/constants.rs | 5 +- node/runtime/src/lib.rs | 99 +++++----- srml/kton/Cargo.toml | 3 + srml/kton/src/mock.rs | 26 +-- srml/staking/Cargo.toml | 3 +- srml/staking/src/mock.rs | 45 +++-- srml/staking/src/tests.rs | 329 +++++++++++++++++----------------- 9 files changed, 270 insertions(+), 257 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 800fd8123..c69013b0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -718,6 +718,8 @@ name = "darwinia-kton" version = "0.1.0" dependencies = [ "darwinia-support 0.1.0", + "node-primitives 2.0.0", + "node-runtime 0.1.0", "parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "safe-mix 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)", @@ -738,6 +740,7 @@ dependencies = [ "darwinia-balances 2.0.0", "darwinia-kton 0.1.0", "darwinia-support 0.1.0", + "node-primitives 2.0.0", "node-runtime 0.1.0", "parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/node/cli/src/chain_spec.rs b/node/cli/src/chain_spec.rs index bb9a82001..b6fb9af20 100644 --- a/node/cli/src/chain_spec.rs +++ b/node/cli/src/chain_spec.rs @@ -25,9 +25,9 @@ use node_runtime::constants::currency::*; use node_runtime::Block; pub use node_runtime::GenesisConfig; use node_runtime::{ - AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, ContractsConfig, GrandpaConfig, ImOnlineConfig, - IndicesConfig, KtonConfig, SessionConfig, SessionKeys, StakerStatus, StakingConfig, SudoConfig, SystemConfig, COIN, - WASM_BINARY, + constants::currency::MILLICENTS, AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, ContractsConfig, + GrandpaConfig, ImOnlineConfig, IndicesConfig, KtonConfig, SessionConfig, SessionKeys, StakerStatus, StakingConfig, + SudoConfig, SystemConfig, WASM_BINARY, }; use primitives::{crypto::UncheckedInto, Pair, Public}; use serde::{Deserialize, Serialize}; @@ -249,7 +249,7 @@ pub fn testnet_genesis( }), staking: Some(StakingConfig { current_era: 0, - // current_era_total_reward: 80_000_000 * COIN / 63720, + // current_era_total_reward: 80_000_000 * MILLICENTS / 63720, // offline_slash: Perbill::from_parts(1_000_000), session_reward: Perbill::from_percent(90), validator_count: 7, @@ -350,8 +350,8 @@ pub fn darwinia_genesis_verbose( ] }); - const ENDOWMENT: Balance = 100_000_000 * COIN; - const STASH: Balance = 100 * COIN; + const ENDOWMENT: Balance = 100_000_000 * MILLICENTS; + const STASH: Balance = 100 * MILLICENTS; GenesisConfig { system: Some(SystemConfig { @@ -404,7 +404,7 @@ pub fn darwinia_genesis_verbose( }), staking: Some(StakingConfig { current_era: 0, - // current_era_total_reward: 80_000_000 * COIN / 63720, + // current_era_total_reward: 80_000_000 * MILLICENTS / 63720, // offline_slash: Perbill::from_parts(1_000_000), session_reward: Perbill::from_percent(90), validator_count: 7, diff --git a/node/runtime/src/constants.rs b/node/runtime/src/constants.rs index 489934045..39dc0dc7b 100644 --- a/node/runtime/src/constants.rs +++ b/node/runtime/src/constants.rs @@ -20,7 +20,10 @@ pub mod currency { use node_primitives::Balance; - pub const MILLICENTS: Balance = 1_000_000_000; + pub const NANO: Balance = 1; + pub const MICRO: Balance = 1_000 * NANO; + pub const MILLI: Balance = 1_000 * MICRO; + pub const MILLICENTS: Balance = 1_000 * MILLI; pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent. pub const DOLLARS: Balance = 100 * CENTS; } diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 29eda15b1..5b0de4faf 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -19,60 +19,52 @@ #![cfg_attr(not(feature = "std"), no_std)] #![recursion_limit = "256"] -use authority_discovery_primitives::{AuthorityId as EncodedAuthorityId, Signature as EncodedSignature}; -use babe_primitives::{AuthorityId as BabeId, AuthoritySignature as BabeSignature}; -pub use balances::Call as BalancesCall; -use codec::{Decode, Encode}; pub use contracts::Gas; -use sr_api::impl_runtime_apis; +pub use timestamp::Call as TimestampCall; -//use grandpa::fg_primitives; -//use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight}; -//use im_online::sr25519::AuthorityId as ImOnlineId; +pub use balances::Call as BalancesCall; +pub use staking::StakerStatus; +use authority_discovery_primitives::{AuthorityId as EncodedAuthorityId, Signature as EncodedSignature}; +use babe_primitives::{AuthorityId as BabeId, AuthoritySignature as BabeSignature}; +use codec::{Decode, Encode}; +use grandpa::{fg_primitives, AuthorityList as GrandpaAuthorityList}; +use im_online::sr25519::AuthorityId as ImOnlineId; use node_primitives::{AccountId, AccountIndex, Balance, BlockNumber, Hash, Index, Moment, Signature}; use rstd::prelude::*; -use sr_primitives::traits::{ - self, BlakeTwo256, Block as BlockT, NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup, +use sr_api::impl_runtime_apis; +use sr_primitives::{ + create_runtime_str, generic, impl_opaque_keys, + traits::{self, BlakeTwo256, Block as BlockT, NumberFor, OpaqueKeys, SaturatedConversion, StaticLookup}, + transaction_validity::TransactionValidity, + weights::Weight, + ApplyResult, Perbill, }; -use sr_primitives::transaction_validity::TransactionValidity; -use sr_primitives::weights::Weight; -#[cfg(any(feature = "std", test))] -pub use sr_primitives::BuildStorage; - -use sr_primitives::{create_runtime_str, generic, impl_opaque_keys, ApplyResult, Perbill}; use substrate_primitives::u32_trait::{_1, _4}; - -use support::traits::OnUnbalanced; -pub use support::StorageValue; +use substrate_primitives::OpaqueMetadata; use support::{ construct_runtime, parameter_types, - traits::{Currency, Randomness, SplitTwoWays}, + traits::{Currency, OnUnbalanced, Randomness, SplitTwoWays}, }; - -pub use timestamp::Call as TimestampCall; +use system::offchain::TransactionSubmitter; +use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; #[cfg(any(feature = "std", test))] use version::NativeVersion; use version::RuntimeVersion; - -use grandpa::fg_primitives; -use grandpa::AuthorityList as GrandpaAuthorityList; -use im_online::sr25519::AuthorityId as ImOnlineId; -use substrate_primitives::OpaqueMetadata; -use system::offchain::TransactionSubmitter; -use transaction_payment_rpc_runtime_api::RuntimeDispatchInfo; +//use grandpa::fg_primitives; +//use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight}; +//use im_online::sr25519::AuthorityId as ImOnlineId; use darwinia_support::TimeStamp; use staking::EraIndex; -pub use staking::StakerStatus; +/// Constant values used within the runtime. +pub mod constants; /// Implementations of some helper traits passed into runtime modules as associated types. pub mod impls; -use impls::{Author, CurrencyToVoteHandler, LinearWeightToFee, TargetedFeeAdjustment}; -/// Constant values used within the runtime. -pub mod constants; -use constants::time::*; +use constants::{currency::*, time::*}; +use impls::{Author, CurrencyToVoteHandler, LinearWeightToFee, TargetedFeeAdjustment}; // Make the WASM binary available. #[cfg(feature = "std")] @@ -96,12 +88,15 @@ pub fn native_version() -> NativeVersion { } } -pub const NANO: Balance = 1; -pub const MICRO: Balance = 1_000 * NANO; -pub const MILLI: Balance = 1_000 * MICRO; -pub const COIN: Balance = 1_000 * MILLI; - type NegativeImbalance = >::NegativeImbalance; +type DealWithFees = SplitTwoWays< + Balance, + NegativeImbalance, + _4, + MockTreasury, // 4 parts (80%) goes to the treasury. + _1, + Author, // 1 part (20%) goes to the block author. +>; //pub struct Author; // @@ -118,20 +113,6 @@ impl OnUnbalanced for MockTreasury { } } -pub type DealWithFees = SplitTwoWays< - Balance, - NegativeImbalance, - _4, - MockTreasury, // 4 parts (80%) goes to the treasury. - _1, - Author, // 1 part (20%) goes to the block author. ->; - -pub const SECS_PER_BLOCK: BlockNumber = 6; -pub const MINUTES: BlockNumber = 60 / SECS_PER_BLOCK; -pub const HOURS: BlockNumber = MINUTES * 60; -pub const DAYS: BlockNumber = HOURS * 24; - parameter_types! { pub const BlockHashCount: BlockNumber = 250; pub const MaximumBlockWeight: Weight = 1_000_000_000; @@ -170,7 +151,7 @@ impl indices::Trait for Runtime { } parameter_types! { - pub const ExistentialDeposit: Balance = 1 * COIN; + pub const ExistentialDeposit: Balance = 1 * MILLICENTS; pub const TransferFee: Balance = 1 * MILLI; pub const CreationFee: Balance = 1 * MILLI; } @@ -314,10 +295,10 @@ parameter_types! { pub const ContractTransactionBaseFee: Balance = 1 * MILLI; pub const ContractTransactionByteFee: Balance = 10 * MICRO; pub const ContractFee: Balance = 1 * MILLI; - pub const TombstoneDeposit: Balance = 1 * COIN; - pub const RentByteFee: Balance = 1 * COIN; - pub const RentDepositOffset: Balance = 1000 * COIN; - pub const SurchargeReward: Balance = 150 * COIN; + pub const TombstoneDeposit: Balance = 1 * MILLICENTS; + pub const RentByteFee: Balance = 1 * MILLICENTS; + pub const RentDepositOffset: Balance = 1000 * MILLICENTS; + pub const SurchargeReward: Balance = 150 * MILLICENTS; } impl contracts::Trait for Runtime { type Currency = Balances; @@ -394,7 +375,7 @@ parameter_types! { // 365 days * 24 hours * 60 minutes / 5 minutes pub const ErasPerEpoch: EraIndex = 105120; // decimal 9 - pub const HardCap: Balance = 10_000_000_000 * COIN; + pub const HardCap: Balance = 10_000_000_000 * MILLICENTS; pub const GenesisTime: Moment = 1_574_156_000_000; } impl staking::Trait for Runtime { diff --git a/srml/kton/Cargo.toml b/srml/kton/Cargo.toml index a63398c03..af999e3f0 100644 --- a/srml/kton/Cargo.toml +++ b/srml/kton/Cargo.toml @@ -22,6 +22,9 @@ darwinia-support = { path = "../support", default-features = false } runtime_io = { package = "sr-io", git = 'https://github.com/darwinia-network/substrate.git' } substrate-primitives = { git = 'https://github.com/darwinia-network/substrate.git' } +node-runtime = { path = "../../node/runtime" } +node-primitives = { path = "../../node/primitives" } + [features] default = ["std"] std = [ diff --git a/srml/kton/src/mock.rs b/srml/kton/src/mock.rs index 60aed4901..1b24b4f1c 100644 --- a/srml/kton/src/mock.rs +++ b/srml/kton/src/mock.rs @@ -1,8 +1,11 @@ +pub use node_runtime::constants::currency::MILLICENTS; + use std::{cell::RefCell, collections::HashSet}; use sr_primitives::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, + weights::Weight, Perbill, }; use srml_support::{impl_outer_origin, parameter_types}; @@ -10,18 +13,21 @@ use substrate_primitives::H256; use super::*; use crate::{GenesisConfig, Module}; - -pub const COIN: u64 = 1_000_000_000; +use node_primitives::Balance; thread_local! { static SESSION: RefCell<(Vec, HashSet)> = RefCell::new(Default::default()); - static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); + static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); } /// The AccountId alias in this test module. pub type AccountId = u64; +// FIXME: +// replace +// testing::Header.number: u64 +// with +// node_primitives::BlockNumber pub type BlockNumber = u64; -pub type Balance = u64; impl_outer_origin! { pub enum Origin for Test {} @@ -31,8 +37,8 @@ impl_outer_origin! { #[derive(Clone, PartialEq, Eq, Debug)] pub struct Test; parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: u32 = 1024; + pub const BlockHashCount: BlockNumber = 250; + pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } @@ -71,7 +77,7 @@ impl Trait for Test { } pub struct ExtBuilder { - existential_deposit: u64, + existential_deposit: Balance, } impl Default for ExtBuilder { @@ -81,7 +87,7 @@ impl Default for ExtBuilder { } impl ExtBuilder { - pub fn existential_deposit(mut self, existential_deposit: u64) -> Self { + pub fn existential_deposit(mut self, existential_deposit: Balance) -> Self { self.existential_deposit = existential_deposit; self } @@ -94,9 +100,9 @@ impl ExtBuilder { self.set_associated_consts(); let mut t = system::GenesisConfig::default().build_storage::().unwrap(); let balance_factor = if self.existential_deposit > 0 { - 1_000 * COIN + 1_000 * MILLICENTS } else { - 1 * COIN + 1 * MILLICENTS }; let _ = GenesisConfig:: { diff --git a/srml/staking/Cargo.toml b/srml/staking/Cargo.toml index 193e0f687..a79aaba12 100644 --- a/srml/staking/Cargo.toml +++ b/srml/staking/Cargo.toml @@ -29,9 +29,10 @@ substrate-primitives = { git = 'https://github.com/darwinia-network/substrate.gi timestamp = { package = "srml-timestamp", git = 'https://github.com/darwinia-network/substrate.git' } rand = "0.7.2" -balances = { package = "darwinia-balances", path = '../balances', default-features = false } +balances = { package = "darwinia-balances", path = '../balances' } kton = { package = "darwinia-kton", path = "../kton" } node-runtime = { path = "../../node/runtime" } +node-primitives = { path = "../../node/primitives" } [features] equalize = [] diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 019e4f16f..55d1b2c2d 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -1,8 +1,11 @@ +pub use node_runtime::constants::currency::MILLICENTS; + use std::{cell::RefCell, collections::HashSet}; use sr_primitives::{ testing::{Header, UintAuthorityId}, traits::{BlakeTwo256, Convert, IdentityLookup, OnInitialize, OpaqueKeys}, + weights::Weight, KeyTypeId, Perbill, }; use sr_staking_primitives::SessionIndex; @@ -15,12 +18,17 @@ use substrate_primitives::{crypto::key_types, H256}; use crate::{EraIndex, GenesisConfig, Module, Nominators, RewardDestination, StakerStatus, StakingBalance, Trait}; use darwinia_support::TimeStamp; +use node_primitives::Balance; use phragmen::ExtendedBalance; /// The AccountId alias in this test module. pub type AccountId = u64; +// FIXME: +// replace +// testing::Header.number: u64 +// with +// node_primitives::BlockNumber pub type BlockNumber = u64; -pub type Balance = u64; /// Module alias pub type System = system::Module; @@ -50,7 +58,7 @@ impl Convert for CurrencyToVoteHandler { thread_local! { static SESSION: RefCell<(Vec, HashSet)> = RefCell::new(Default::default()); - static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); + static EXISTENTIAL_DEPOSIT: RefCell = RefCell::new(0); } pub struct TestSessionHandler; @@ -81,8 +89,8 @@ pub fn is_disabled(validator: AccountId) -> bool { } pub struct ExistentialDeposit; -impl Get for ExistentialDeposit { - fn get() -> u64 { +impl Get for ExistentialDeposit { + fn get() -> Balance { EXISTENTIAL_DEPOSIT.with(|v| *v.borrow()) } } @@ -95,8 +103,8 @@ impl_outer_origin! { #[derive(Clone, PartialEq, Eq, Debug)] pub struct Test; parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const MaximumBlockWeight: u32 = 1024; + pub const BlockHashCount: BlockNumber = 250; + pub const MaximumBlockWeight: Weight = 1024; pub const MaximumBlockLength: u32 = 2 * 1024; pub const AvailableBlockRatio: Perbill = Perbill::one(); } @@ -177,10 +185,9 @@ parameter_types! { pub const BondingDuration: TimeStamp = 60; pub const ErasPerEpoch: EraIndex = 10; } -pub const COIN: u64 = 1_000_000_000; parameter_types! { // decimal 9 - pub const CAP: Balance = 10_000_000_000 * COIN; + pub const CAP: Balance = 10_000_000_000 * MILLICENTS; } impl Trait for Test { type Ring = Ring; @@ -202,9 +209,9 @@ impl Trait for Test { } pub struct ExtBuilder { - existential_deposit: u64, + existential_deposit: Balance, current_era: EraIndex, - reward: u64, + reward: Balance, validator_pool: bool, nominate: bool, validator_count: u32, @@ -228,7 +235,7 @@ impl Default for ExtBuilder { } impl ExtBuilder { - pub fn existential_deposit(mut self, existential_deposit: u64) -> Self { + pub fn existential_deposit(mut self, existential_deposit: Balance) -> Self { self.existential_deposit = existential_deposit; self } @@ -263,9 +270,9 @@ impl ExtBuilder { self.set_associated_consts(); let mut storage = system::GenesisConfig::default().build_storage::().unwrap(); let balance_factor = if self.existential_deposit > 0 { - 1_000 * COIN + 1_000 * MILLICENTS } else { - 1 * COIN + 1 * MILLICENTS }; let validators = if self.validator_pool { vec![10, 20, 30, 40] @@ -315,10 +322,10 @@ impl ExtBuilder { let nominated = if self.nominate { vec![11, 21] } else { vec![] }; let _ = GenesisConfig:: { current_era: self.current_era, - // current_era_total_reward: 80_000_000 * COIN / ErasPerEpoch::get() as u64, + // current_era_total_reward: 80_000_000 * MILLICENTS / ErasPerEpoch::get() as u64, stakers: vec![ - // (2, 1, 1 * COIN, StakerStatus::::Validator), - (11, 10, 100 * COIN, StakerStatus::::Validator), + // (2, 1, 1 * MILLICENTS, StakerStatus::::Validator), + (11, 10, 100 * MILLICENTS, StakerStatus::::Validator), (21, 20, stake_21, StakerStatus::::Validator), (31, 30, stake_31, StakerStatus::::Validator), (41, 40, balance_factor * 1000, status_41), @@ -393,7 +400,7 @@ pub fn check_nominator_exposure(stash: u64) { ); } -pub fn assert_total_expo(stash: u64, val: u128) { +pub fn assert_total_expo(stash: u64, val: Balance) { let expo = Staking::stakers(&stash); assert_eq!(expo.total, val); } @@ -402,7 +409,7 @@ pub fn assert_is_stash(acc: u64) { assert!(Staking::bonded(&acc).is_some(), "Not a stash."); } -pub fn bond_validator(acc: u64, val: u64) { +pub fn bond_validator(acc: u64, val: Balance) { // a = controller // a + 1 = stash let _ = Ring::make_free_balance_be(&(acc + 1), val); @@ -421,7 +428,7 @@ pub fn bond_validator(acc: u64, val: u64) { )); } -pub fn bond_nominator(acc: u64, val: u64, target: Vec) { +pub fn bond_nominator(acc: u64, val: Balance, target: Vec) { // a = controller // a + 1 = stash let _ = Ring::make_free_balance_be(&(acc + 1), val); diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index aef213669..c5d7e1a3b 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -17,55 +17,55 @@ use darwinia_support::{BalanceLock, NormalLock, StakingLock, WithdrawLock}; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $promise_month:ident($how_long:expr)) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); +// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); +// Kton::deposit_creating(&$stash, 100 * MILLICENTS); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); +// let _ = Ring::deposit_creating(&$controller, MILLICENTS); // #[allow(non_snake_case, unused)] // let $promise_month = $how_long; // assert_ok!(Staking::bond( // Origin::signed($stash), // $controller, -// StakingBalance::Ring(50 * COIN), +// StakingBalance::Ring(50 * MILLICENTS), // RewardDestination::Stash, // $how_long // )); // assert_ok!(Staking::bond_extra( // Origin::signed($stash), -// StakingBalance::Kton(50 * COIN), +// StakingBalance::Kton(50 * MILLICENTS), // $how_long // )); // }; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $how_long:expr) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); +// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); +// Kton::deposit_creating(&$stash, 100 * MILLICENTS); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); +// let _ = Ring::deposit_creating(&$controller, MILLICENTS); // assert_ok!(Staking::bond( // Origin::signed($stash), // $controller, -// StakingBalance::Ring(50 * COIN), +// StakingBalance::Ring(50 * MILLICENTS), // RewardDestination::Stash, // $how_long // )); // assert_ok!(Staking::bond_extra( // Origin::signed($stash), -// StakingBalance::Kton(50 * COIN), +// StakingBalance::Kton(50 * MILLICENTS), // $how_long // )); // }; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr)) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); +// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); +// Kton::deposit_creating(&$stash, 100 * MILLICENTS); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); +// let _ = Ring::deposit_creating(&$controller, MILLICENTS); // }; //} @@ -79,16 +79,16 @@ fn test_env_build() { Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * COIN, - active_deposit_ring: 100 * COIN, + active_ring: 100 * MILLICENTS, + active_deposit_ring: 100 * MILLICENTS, active_kton: 0, deposit_items: vec![TimeDepositItem { - value: 100 * COIN, + value: 100 * MILLICENTS, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }], ring_staking_lock: StakingLock { - staking_amount: 100 * COIN, + staking_amount: 100 * MILLICENTS, unbondings: vec![] }, kton_staking_lock: StakingLock { @@ -98,37 +98,37 @@ fn test_env_build() { } ); - assert_eq!(Kton::free_balance(&11), COIN / 100); - assert_eq!(Kton::total_issuance(), 16 * COIN / 100); + assert_eq!(Kton::free_balance(&11), MILLICENTS / 100); + assert_eq!(Kton::total_issuance(), 16 * MILLICENTS / 100); let origin_ledger = Staking::ledger(&10).unwrap(); - let _ = Ring::deposit_creating(&11, 100 * COIN); + let _ = Ring::deposit_creating(&11, 100 * MILLICENTS); assert_ok!(Staking::bond_extra( Origin::signed(11), - StakingBalance::Ring(20 * COIN), + StakingBalance::Ring(20 * MILLICENTS), 13 )); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: origin_ledger.active_ring + 20 * COIN, - active_deposit_ring: origin_ledger.active_deposit_ring + 20 * COIN, + active_ring: origin_ledger.active_ring + 20 * MILLICENTS, + active_deposit_ring: origin_ledger.active_deposit_ring + 20 * MILLICENTS, active_kton: 0, deposit_items: vec![ TimeDepositItem { - value: 100 * COIN, + value: 100 * MILLICENTS, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }, TimeDepositItem { - value: 20 * COIN, + value: 20 * MILLICENTS, start_time: 0, expire_time: 13 * MONTH_IN_SECONDS as u64 } ], ring_staking_lock: StakingLock { - staking_amount: origin_ledger.active_ring + 20 * COIN, + staking_amount: origin_ledger.active_ring + 20 * MILLICENTS, unbondings: vec![] }, kton_staking_lock: StakingLock { @@ -143,11 +143,11 @@ fn test_env_build() { #[test] fn normal_kton_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - Kton::deposit_creating(&1001, 10 * COIN); + Kton::deposit_creating(&1001, 10 * MILLICENTS); assert_ok!(Staking::bond( Origin::signed(1001), 1000, - StakingBalance::Kton(10 * COIN), + StakingBalance::Kton(10 * MILLICENTS), RewardDestination::Stash, 0 )); @@ -157,14 +157,14 @@ fn normal_kton_should_work() { stash: 1001, active_ring: 0, active_deposit_ring: 0, - active_kton: 10 * COIN, + active_kton: 10 * MILLICENTS, deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 0, unbondings: vec![] }, kton_staking_lock: StakingLock { - staking_amount: 10 * COIN, + staking_amount: 10 * MILLICENTS, unbondings: vec![] }, } @@ -174,7 +174,7 @@ fn normal_kton_should_work() { vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 10 * COIN, + staking_amount: 10 * MILLICENTS, unbondings: vec![], }), reasons: WithdrawReasons::all() @@ -182,11 +182,11 @@ fn normal_kton_should_work() { ); // promise_month should not work for kton - Kton::deposit_creating(&2001, 10 * COIN); + Kton::deposit_creating(&2001, 10 * MILLICENTS); assert_ok!(Staking::bond( Origin::signed(2001), 2000, - StakingBalance::Kton(10 * COIN), + StakingBalance::Kton(10 * MILLICENTS), RewardDestination::Stash, 12 )); @@ -196,14 +196,14 @@ fn normal_kton_should_work() { stash: 2001, active_ring: 0, active_deposit_ring: 0, - active_kton: 10 * COIN, + active_kton: 10 * MILLICENTS, deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 0, unbondings: vec![] }, kton_staking_lock: StakingLock { - staking_amount: 10 * COIN, + staking_amount: 10 * MILLICENTS, unbondings: vec![] }, } @@ -217,7 +217,10 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { 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))); + assert_ok!(Staking::unbond( + Origin::signed(10), + StakingBalance::Ring(10 * MILLICENTS) + )); // Only active normal ring can be unbond assert_eq!(&Staking::ledger(&10).unwrap(), &ledger,); assert_eq!( @@ -225,28 +228,31 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * COIN, + staking_amount: 100 * MILLICENTS, unbondings: vec![], }), reasons: WithdrawReasons::all() }] ); - assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(20 * COIN))); + assert_ok!(Staking::unbond( + Origin::signed(10), + StakingBalance::Ring(20 * MILLICENTS) + )); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * COIN, - active_deposit_ring: 70 * COIN, + active_ring: 100 * MILLICENTS, + active_deposit_ring: 70 * MILLICENTS, active_kton: 0, deposit_items: vec![TimeDepositItem { - value: 70 * COIN, + value: 70 * MILLICENTS, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }], ring_staking_lock: StakingLock { - staking_amount: 100 * COIN, + staking_amount: 100 * MILLICENTS, unbondings: vec![] }, kton_staking_lock: StakingLock { @@ -257,24 +263,27 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { ); // unbondings: vec![ // NormalLock { - // value: StakingBalance::Ring(10 * COIN), + // value: StakingBalance::Ring(10 * MILLICENTS), // era: 3, // is_time_deposit: true // }, // NormalLock { - // value: StakingBalance::Ring(20 * COIN), + // value: StakingBalance::Ring(20 * MILLICENTS), // era: 3, // is_time_deposit: true // } // ] // more than active ring - assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(120 * COIN))); + assert_ok!(Staking::unbond( + Origin::signed(10), + StakingBalance::Ring(120 * MILLICENTS) + )); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * COIN, + active_ring: 100 * MILLICENTS, active_deposit_ring: 0, active_kton: 0, deposit_items: vec![], @@ -290,17 +299,17 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { ); // unbondings: vec![ // NormalLock { - // value: StakingBalance::Ring(10 * COIN), + // value: StakingBalance::Ring(10 * MILLICENTS), // era: 3, // is_time_deposit: true // }, // NormalLock { - // value: StakingBalance::Ring(20 * COIN), + // value: StakingBalance::Ring(20 * MILLICENTS), // era: 3, // is_time_deposit: true // }, // NormalLock { - // value: StakingBalance::Ring(70 * COIN), + // value: StakingBalance::Ring(70 * MILLICENTS), // era: 3, // is_time_deposit: true // }, @@ -354,10 +363,10 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // let stash = 11; // let controller = 10; -// let value = 200 * COIN; +// let value = 200 * MILLICENTS; // let promise_month = 12; // // unbond normal ring -// let _ = Ring::deposit_creating(&stash, 1000 * COIN); +// let _ = Ring::deposit_creating(&stash, 1000 * MILLICENTS); // // { // let kton_free_balance = Kton::free_balance(&stash); @@ -392,7 +401,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // bond += 0.03 // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Kton(COIN), +// StakingBalance::Kton(MILLICENTS), // 0 // )); // ledger.active_kton += kton_free_balance; @@ -420,15 +429,15 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // let controller = 1000; // let promise_month = 36; // -// let _ = Ring::deposit_creating(&stash, 100 * COIN); -// Kton::deposit_creating(&stash, COIN / 100000); +// let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); +// Kton::deposit_creating(&stash, MILLICENTS / 100000); // // // timestamp now is 0. // // free balance of kton is too low to work // assert_ok!(Staking::bond( // Origin::signed(stash), // controller, -// StakingBalance::Ring(10 * COIN), +// StakingBalance::Ring(10 * MILLICENTS), // RewardDestination::Stash, // promise_month // )); @@ -436,12 +445,12 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Staking::ledger(&controller), // Some(StakingLedger { // stash, -// total_deposit_ring: 10 * COIN, -// active_deposit_ring: 10 * COIN, -// active_ring: 10 * COIN, +// total_deposit_ring: 10 * MILLICENTS, +// active_deposit_ring: 10 * MILLICENTS, +// active_ring: 10 * MILLICENTS, // active_kton: 0, // deposit_items: vec![TimeDepositItem { -// value: 10 * COIN, +// value: 10 * MILLICENTS, // start_time: 0, // expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64 // }], // should be cleared @@ -453,16 +462,16 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // kton is 0, skip unbond_with_punish // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 10 * COIN, +// 10 * MILLICENTS, // promise_month as u64 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); // assert_eq!(Kton::free_balance(&stash), kton_free_balance); // // // set more kton balance to make it work -// Kton::deposit_creating(&stash, 10 * COIN); +// Kton::deposit_creating(&stash, 10 * MILLICENTS); // let kton_free_balance = Kton::free_balance(&stash); -// let unbond_value = 5 * COIN; +// let unbond_value = 5 * MILLICENTS; // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), // unbond_value, @@ -485,7 +494,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // the whole item should be be dropped // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 5 * COIN, +// 5 * MILLICENTS, // promise_month as u64 * MONTH_IN_SECONDS as u64 // )); // assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); @@ -495,29 +504,29 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { //#[test] //fn transform_to_promised_ring_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); +// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(10 * COIN), +// StakingBalance::Ring(10 * MILLICENTS), // RewardDestination::Stash, // 0 // )); // let origin_ledger = Staking::ledger(&1000).unwrap(); // let kton_free_balance = Kton::free_balance(&1001); // -// assert_ok!(Staking::promise_extra(Origin::signed(1000), 5 * COIN, 12)); +// assert_ok!(Staking::promise_extra(Origin::signed(1000), 5 * MILLICENTS, 12)); // // assert_eq!( // Staking::ledger(&1000), // Some(StakingLedger { // stash: 1001, -// total_deposit_ring: origin_ledger.total_deposit_ring + 5 * COIN, -// active_deposit_ring: origin_ledger.active_deposit_ring + 5 * COIN, +// total_deposit_ring: origin_ledger.total_deposit_ring + 5 * MILLICENTS, +// active_deposit_ring: origin_ledger.active_deposit_ring + 5 * MILLICENTS, // active_ring: origin_ledger.active_ring, // active_kton: origin_ledger.active_kton, // deposit_items: vec![TimeDepositItem { -// value: 5 * COIN, +// value: 5 * MILLICENTS, // start_time: 0, // expire_time: 12 * MONTH_IN_SECONDS as u64 // }], @@ -525,24 +534,24 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // }) // ); // -// assert_eq!(Kton::free_balance(&1001), kton_free_balance + (5 * COIN / 10000)); +// assert_eq!(Kton::free_balance(&1001), kton_free_balance + (5 * MILLICENTS / 10000)); // }); //} // //#[test] //fn expired_ring_should_capable_to_promise_again() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); +// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(10 * COIN), +// StakingBalance::Ring(10 * MILLICENTS), // RewardDestination::Stash, // 12 // )); // let mut ledger = Staking::ledger(&1000).unwrap(); // let ts = 13 * MONTH_IN_SECONDS as u64; -// let promise_extra_value = 5 * COIN; +// let promise_extra_value = 5 * MILLICENTS; // Timestamp::set_timestamp(ts); // assert_ok!(Staking::promise_extra(Origin::signed(1000), promise_extra_value, 13)); // ledger.total_deposit_ring = promise_extra_value; @@ -560,14 +569,14 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { ////#[test] ////fn inflation_should_be_correct() { //// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// let initial_issuance = 1_200_000_000 * COIN; +//// let initial_issuance = 1_200_000_000 * MILLICENTS; //// let surplus_needed = initial_issuance - Ring::total_issuance(); //// let _ = Ring::deposit_into_existing(&11, surplus_needed); //// assert_eq!(Ring::total_issuance(), initial_issuance); -//// // assert_eq!(Staking::current_era_total_reward(), 80000000 * COIN / 10); +//// // assert_eq!(Staking::current_era_total_reward(), 80000000 * MILLICENTS / 10); //// start_era(11); //// // ErasPerEpoch = 10 -//// // assert_eq!(Staking::current_era_total_reward(), 88000000 * COIN / 10); +//// // assert_eq!(Staking::current_era_total_reward(), 88000000 * MILLICENTS / 10); //// }); ////} // @@ -575,44 +584,44 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { //fn reward_should_work_correctly() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // // create controller account -// let _ = Ring::deposit_creating(&2000, COIN); -// let _ = Ring::deposit_creating(&1000, COIN); -// let _ = Ring::deposit_creating(&200, COIN); +// let _ = Ring::deposit_creating(&2000, MILLICENTS); +// let _ = Ring::deposit_creating(&1000, MILLICENTS); +// let _ = Ring::deposit_creating(&200, MILLICENTS); // // new validator -// let _ = Ring::deposit_creating(&2001, 2000 * COIN); -// Kton::deposit_creating(&2001, 10 * COIN); +// let _ = Ring::deposit_creating(&2001, 2000 * MILLICENTS); +// Kton::deposit_creating(&2001, 10 * MILLICENTS); // // new validator -// let _ = Ring::deposit_creating(&1001, 300 * COIN); -// Kton::deposit_creating(&1001, 1 * COIN); +// let _ = Ring::deposit_creating(&1001, 300 * MILLICENTS); +// Kton::deposit_creating(&1001, 1 * MILLICENTS); // // handle some dirty work -// let _ = Ring::deposit_creating(&201, 2000 * COIN); -// Kton::deposit_creating(&201, 10 * COIN); -// assert_eq!(Kton::free_balance(&201), 10 * COIN); +// let _ = Ring::deposit_creating(&201, 2000 * MILLICENTS); +// Kton::deposit_creating(&201, 10 * MILLICENTS); +// assert_eq!(Kton::free_balance(&201), 10 * MILLICENTS); // // // 2001-2000 // assert_ok!(Staking::bond( // Origin::signed(2001), // 2000, -// StakingBalance::Ring(300 * COIN), +// StakingBalance::Ring(300 * MILLICENTS), // RewardDestination::Controller, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(2001), -// StakingBalance::Kton(1 * COIN), +// StakingBalance::Kton(1 * MILLICENTS), // 0 // )); // // 1001-1000 // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(300 * COIN), +// StakingBalance::Ring(300 * MILLICENTS), // RewardDestination::Controller, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(1001), -// StakingBalance::Kton(1 * COIN), +// StakingBalance::Kton(1 * MILLICENTS), // 0 // )); // let ring_pool = Staking::ring_pool(); @@ -621,29 +630,29 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // assert_ok!(Staking::bond( // Origin::signed(201), // 200, -// StakingBalance::Ring(3000 * COIN - ring_pool), +// StakingBalance::Ring(3000 * MILLICENTS - ring_pool), // RewardDestination::Stash, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(201), -// StakingBalance::Kton(10 * COIN - kton_pool), +// StakingBalance::Kton(10 * MILLICENTS - kton_pool), // 0, // )); // // ring_pool and kton_pool -// assert_eq!(Staking::ring_pool(), 3000 * COIN); -// assert_eq!(Staking::kton_pool(), 10 * COIN); +// assert_eq!(Staking::ring_pool(), 3000 * MILLICENTS); +// assert_eq!(Staking::kton_pool(), 10 * MILLICENTS); // // 1/5 ring_pool and 1/5 kton_pool // assert_ok!(Staking::validate(Origin::signed(2000), [0; 8].to_vec(), 0, 3)); // assert_ok!(Staking::nominate(Origin::signed(1000), vec![2001])); // -// assert_eq!(Staking::ledger(&2000).unwrap().active_kton, 1 * COIN); -// assert_eq!(Staking::ledger(&2000).unwrap().active_ring, 300 * COIN); +// assert_eq!(Staking::ledger(&2000).unwrap().active_kton, 1 * MILLICENTS); +// assert_eq!(Staking::ledger(&2000).unwrap().active_ring, 300 * MILLICENTS); // assert_eq!(Staking::power_of(&2001), 1_000_000_000 / 10 as u128); // // 600COIN for rewarding ring bond-er // // 600COIN for rewarding kton bond-er // Staking::select_validators(); -// Staking::reward_validator(&2001, 1200 * COIN); +// Staking::reward_validator(&2001, 1200 * MILLICENTS); // // assert_eq!( // Staking::stakers(2001), @@ -656,33 +665,33 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // }] // } // ); -// assert_eq!(Ring::free_balance(&2000), 601 * COIN); -// assert_eq!(Ring::free_balance(&1000), 601 * COIN); +// assert_eq!(Ring::free_balance(&2000), 601 * MILLICENTS); +// assert_eq!(Ring::free_balance(&1000), 601 * MILLICENTS); // }); //} // //#[test] //fn slash_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); -// Kton::deposit_creating(&1001, 100 * COIN); +// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); +// Kton::deposit_creating(&1001, 100 * MILLICENTS); // // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(50 * COIN), +// StakingBalance::Ring(50 * MILLICENTS), // RewardDestination::Controller, // 0, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(1001), -// StakingBalance::Kton(50 * COIN), +// StakingBalance::Kton(50 * MILLICENTS), // 0 // )); // assert_ok!(Staking::validate(Origin::signed(1000), [0; 8].to_vec(), 0, 3)); // // // slash 1% -// let slash_value = 5 * COIN / 10; +// let slash_value = 5 * MILLICENTS / 10; // let mut ledger = Staking::ledger(&1000).unwrap(); // let ring_free_balance = Ring::free_balance(&1001); // let kton_free_balance = Kton::free_balance(&1001); @@ -698,10 +707,10 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { //#[test] ////fn test_inflation() { //// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// assert_eq!(Staking::current_era_total_reward(), 80_000_000 * COIN / 10); +//// assert_eq!(Staking::current_era_total_reward(), 80_000_000 * MILLICENTS / 10); //// start_era(20); //// assert_eq!(Staking::epoch_index(), 2); -//// assert_eq!(Staking::current_era_total_reward(), 9_999_988_266 * COIN / 1000); +//// assert_eq!(Staking::current_era_total_reward(), 9_999_988_266 * MILLICENTS / 1000); //// }); ////} //#[test] @@ -722,9 +731,9 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { //#[test] //fn set_controller_should_not_change_ledger() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// assert_eq!(Staking::ledger(&10).unwrap().active_ring, 100 * COIN); +// assert_eq!(Staking::ledger(&10).unwrap().active_ring, 100 * MILLICENTS); // assert_ok!(Staking::set_controller(Origin::signed(11), 12)); -// assert_eq!(Staking::ledger(&12).unwrap().active_ring, 100 * COIN); +// assert_eq!(Staking::ledger(&12).unwrap().active_ring, 100 * MILLICENTS); // }); //} // @@ -738,29 +747,29 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // old_ledger.active_ring, // old_ledger.active_deposit_ring // ), -// (100 * COIN, 100 * COIN) +// (100 * MILLICENTS, 100 * MILLICENTS) // ); // // assert_ok!(Staking::bond_extra( // Origin::signed(11), -// StakingBalance::Ring(100 * COIN), +// StakingBalance::Ring(100 * MILLICENTS), // 0 // )); -// Kton::deposit_creating(&11, 10 * COIN); +// Kton::deposit_creating(&11, 10 * MILLICENTS); // assert_ok!(Staking::bond_extra( // Origin::signed(11), -// StakingBalance::Kton(10 * COIN), +// StakingBalance::Kton(10 * MILLICENTS), // 0 // )); // -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); +// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * MILLICENTS))); // let new_ledger = Staking::ledger(&10).unwrap(); // assert_eq!( // ( // new_ledger.active_ring, // new_ledger.active_deposit_ring // ), -// (190 * COIN, 100 * COIN) +// (190 * MILLICENTS, 100 * MILLICENTS) // ); // // // slash 100% @@ -772,7 +781,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // 10Ring in unbondings // (0, 0) // ); -// assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * COIN)); +// assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * MILLICENTS)); // }); //} // @@ -784,7 +793,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Staking::bond( // Origin::signed(stash), // controller, -// StakingBalance::Ring(COIN), +// StakingBalance::Ring(MILLICENTS), // RewardDestination::Stash, // 37 // ), @@ -793,7 +802,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // gen_paired_account!(stash(123), controller(456), promise_month(12)); // assert_err!( -// Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(COIN), 37), +// Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(MILLICENTS), 37), // "months at most is 36." // ); // }); @@ -807,7 +816,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Staking::bond( // Origin::signed(11), // unpaired_controller, -// StakingBalance::Ring(COIN), +// StakingBalance::Ring(MILLICENTS), // RewardDestination::Stash, // 0 // ), @@ -817,7 +826,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Staking::bond( // Origin::signed(unpaired_stash), // 10, -// StakingBalance::Ring(COIN), +// StakingBalance::Ring(MILLICENTS), // RewardDestination::Stash, // 0 // ), @@ -835,54 +844,54 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // bond: 100COIN // gen_paired_account!(stash_1(111), controller_1(222), 0); // gen_paired_account!(stash_2(333), controller_2(444), promise_month(12)); -// ring_pool += 100 * COIN; -// kton_pool += 100 * COIN; +// ring_pool += 100 * MILLICENTS; +// kton_pool += 100 * MILLICENTS; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // // // unbond: 50Ring 50Kton // assert_ok!(Staking::unbond( // Origin::signed(controller_1), -// StakingBalance::Ring(50 * COIN) +// StakingBalance::Ring(50 * MILLICENTS) // )); // assert_ok!(Staking::unbond( // Origin::signed(controller_1), -// StakingBalance::Kton(25 * COIN) +// StakingBalance::Kton(25 * MILLICENTS) // )); // // not yet expired: promise for 12 months // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Ring(50 * COIN) +// StakingBalance::Ring(50 * MILLICENTS) // )); // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Kton(25 * COIN) +// StakingBalance::Kton(25 * MILLICENTS) // )); -// ring_pool -= 50 * COIN; -// kton_pool -= 50 * COIN; +// ring_pool -= 50 * MILLICENTS; +// kton_pool -= 50 * MILLICENTS; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // // // unbond with punish: 12.5Ring // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller_2), -// 125 * COIN / 10, +// 125 * MILLICENTS / 10, // promise_month * MONTH_IN_SECONDS as u64 // )); // // unbond deposit items: 12.5Ring // Timestamp::set_timestamp(promise_month * MONTH_IN_SECONDS as u64); // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Ring(125 * COIN / 10) +// StakingBalance::Ring(125 * MILLICENTS / 10) // )); -// ring_pool -= 25 * COIN; +// ring_pool -= 25 * MILLICENTS; // assert_eq!(Staking::ring_pool(), ring_pool); // // // slash: 25Ring 50Kton // Staking::slash_validator(&stash_1, 1_000_000_000); // Staking::slash_validator(&stash_2, 1_000_000_000); -// ring_pool -= 25 * COIN; -// kton_pool -= 50 * COIN; +// ring_pool -= 25 * MILLICENTS; +// kton_pool -= 50 * MILLICENTS; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // }); @@ -897,7 +906,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // for _ in 1..deposit_items_len { // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(COIN), +// StakingBalance::Ring(MILLICENTS), // promise_month // )); // } @@ -910,7 +919,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Timestamp::set_timestamp(promise_month as u64 * MONTH_IN_SECONDS as u64); // // for _ in 1..deposit_items_len { -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); // } // { // let ledger = Staking::ledger(&controller).unwrap(); @@ -920,7 +929,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // assert_err!( // Staking::unbond( // Origin::signed(controller), -// StakingBalance::Ring((deposit_items_len - 1) as u64 * COIN) +// StakingBalance::Ring((deposit_items_len - 1) as u64 * MILLICENTS) // ), // "can not schedule more unlock chunks" // ); @@ -934,34 +943,34 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // { // let stash = 444; // let controller = 555; -// let _ = Ring::deposit_creating(&stash, 100 * COIN); -// Kton::deposit_creating(&stash, 100 * COIN); +// let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); +// Kton::deposit_creating(&stash, 100 * MILLICENTS); // // assert_ok!(Staking::bond( // Origin::signed(stash), // controller, -// StakingBalance::Ring(50 * COIN), +// StakingBalance::Ring(50 * MILLICENTS), // RewardDestination::Stash, // 0 // )); // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Kton(50 * COIN), +// StakingBalance::Kton(50 * MILLICENTS), // 0 // )); // // let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; // -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(COIN), +// value: StakingBalance::Ring(MILLICENTS), // era: 3, // is_time_deposit: false, // }); // assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(COIN))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(MILLICENTS))); // unbondings.push(NormalLock { -// value: StakingBalance::Kton(COIN), +// value: StakingBalance::Kton(MILLICENTS), // era: 3, // is_time_deposit: false, // }); @@ -989,13 +998,13 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(50 * COIN), +// StakingBalance::Ring(50 * MILLICENTS), // 36 // )); // // let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; // -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); // unbondings.push(NormalLock { // value: StakingBalance::Ring(0), // era: 3, @@ -1006,11 +1015,11 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // for month in [12, 36].iter() { // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 20 * COIN, +// 20 * MILLICENTS, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(20 * COIN), +// value: StakingBalance::Ring(20 * MILLICENTS), // era: 3, // is_time_deposit: true, // }); @@ -1018,11 +1027,11 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 29 * COIN, +// 29 * MILLICENTS, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(29 * COIN), +// value: StakingBalance::Ring(29 * MILLICENTS), // era: 3, // is_time_deposit: true, // }); @@ -1030,11 +1039,11 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 50 * COIN, +// 50 * MILLICENTS, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(1 * COIN), +// value: StakingBalance::Ring(1 * MILLICENTS), // era: 3, // is_time_deposit: true, // }); @@ -1062,17 +1071,17 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(5 * COIN), +// StakingBalance::Ring(5 * MILLICENTS), // 0 // )); // for _ in 0..expired_item_len { -// assert_ok!(Staking::promise_extra(Origin::signed(controller), COIN, promise_month)); +// assert_ok!(Staking::promise_extra(Origin::signed(controller), MILLICENTS, promise_month)); // } // // Timestamp::set_timestamp(expiry_date - 1); // assert_ok!(Staking::promise_extra( // Origin::signed(controller), -// 2 * COIN, +// 2 * MILLICENTS, // promise_month // )); // assert_eq!( @@ -1083,7 +1092,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // Timestamp::set_timestamp(expiry_date); // assert_ok!(Staking::promise_extra( // Origin::signed(controller), -// 2 * COIN, +// 2 * MILLICENTS, // promise_month // )); // assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 2); @@ -1094,7 +1103,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { //fn unbond_zero_before_expiry() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // let expiry_date = 12 * MONTH_IN_SECONDS as u64; -// let unbond_value = StakingBalance::Ring(COIN); +// let unbond_value = StakingBalance::Ring(MILLICENTS); // // Timestamp::set_timestamp(expiry_date - 1); // assert_ok!(Staking::unbond(Origin::signed(10), unbond_value.clone())); @@ -1143,7 +1152,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 10_000 * COIN, +// 10_000 * MILLICENTS, // 12 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(Kton::free_balance(&stash), 1); @@ -1167,7 +1176,7 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // // not enough Kton to unbond // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 10_000 * COIN, +// 10_000 * MILLICENTS, // 36 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); @@ -1207,8 +1216,8 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { // if with_new_era { // start_era(2); // } -// Staking::reward_validator(&validator_1_stash, 1000 * COIN); -// Staking::reward_validator(&validator_2_stash, 1000 * COIN); +// Staking::reward_validator(&validator_1_stash, 1000 * MILLICENTS); +// Staking::reward_validator(&validator_2_stash, 1000 * MILLICENTS); // // balance = Ring::free_balance(&nominator_stash); // }); From a6a3ef185d09ad3a5a8539accc938e09aef7ae85 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 16:13:37 +0800 Subject: [PATCH 02/45] fix: `clear_mature_deposits` logic --- srml/staking/src/lib.rs | 42 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index c7e921c49..4cc451ca7 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -765,32 +765,26 @@ decl_module! { } impl Module { - pub fn clear_mature_deposits(who: &T::AccountId) { - let mut ledger = if let Some(l) = Self::ledger(&who) { - l - } else { - return; - }; - - // let mut ledger = Self::ledger(who).ok_or("not a controller")?; - - let StakingLedger { - active_deposit_ring, - deposit_items, - .. - } = &mut ledger; - - let now = >::now(); - - deposit_items.retain(|item| { - if item.expire_time > now { - return true; - } + pub fn clear_mature_deposits(controller: &T::AccountId) { + if let Some(mut ledger) = Self::ledger(&controller) { + let now = >::now(); + let StakingLedger { + active_deposit_ring, + deposit_items, + .. + } = &mut ledger; - *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); + deposit_items.retain(|item| { + if item.expire_time > now { + true + } else { + *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); + false + } + }); - false - }); + >::insert(controller, ledger); + }; } fn bond_helper_in_ring( From ec53209f9f6b7deb9ab9cd7fcec5ac703dc203bd Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 16:13:59 +0800 Subject: [PATCH 03/45] hanging: bakcup --- srml/staking/src/tests.rs | 187 ++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 109 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index c5d7e1a3b..2dbeac88a 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -11,7 +11,7 @@ use darwinia_support::{BalanceLock, NormalLock, StakingLock, WithdrawLock}; // will create stash `a` and controller `b` // `a` has 100 Ring and 100 Kton // promise for `m` month with 50 Ring and 50 Kton -// `m` can be ignore, and it wont perfrom `bond` action +// `m` can be ignore, and it wont perform `bond` action // gen_paired_account!(a(1), b(2)); //macro_rules! gen_paired_account { // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $promise_month:ident($how_long:expr)) => { @@ -212,73 +212,61 @@ fn normal_kton_should_work() { } #[test] -fn time_deposit_ring_unbond_and_withdraw_should_work() { +fn time_deposit_ring_can_not_be_unbond() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let locks = vec![BalanceLock { + id: STAKING_ID, + withdraw_lock: WithdrawLock::WithStaking(StakingLock { + staking_amount: 100 * MILLICENTS, + unbondings: vec![], + }), + reasons: WithdrawReasons::all(), + }]; + let ledger = StakingLedger { + stash: 11, + active_ring: 100 * MILLICENTS, + active_deposit_ring: 100 * MILLICENTS, + active_kton: 0, + deposit_items: vec![TimeDepositItem { + value: 100 * MILLICENTS, + start_time: 0, + expire_time: 12 * MONTH_IN_SECONDS as u64, + }], + ring_staking_lock: StakingLock { + staking_amount: 100 * MILLICENTS, + unbondings: vec![], + }, + kton_staking_lock: StakingLock { + staking_amount: 0, + unbondings: vec![], + }, + }; + 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 * MILLICENTS) )); - // Only active normal ring can be unbond - assert_eq!(&Staking::ledger(&10).unwrap(), &ledger,); - assert_eq!( - Ring::locks(11), - vec![BalanceLock { - id: STAKING_ID, - withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * MILLICENTS, - unbondings: vec![], - }), - reasons: WithdrawReasons::all() - }] - ); + assert_eq!(Ring::locks(11), locks); + assert_eq!(Staking::ledger(&10).unwrap(), ledger,); assert_ok!(Staking::unbond( Origin::signed(10), StakingBalance::Ring(20 * MILLICENTS) )); - assert_eq!( - Staking::ledger(&10).unwrap(), - StakingLedger { - stash: 11, - active_ring: 100 * MILLICENTS, - active_deposit_ring: 70 * MILLICENTS, - active_kton: 0, - deposit_items: vec![TimeDepositItem { - value: 70 * MILLICENTS, - start_time: 0, - expire_time: 12 * MONTH_IN_SECONDS as u64 - }], - ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS, - unbondings: vec![] - }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, - } - ); - // unbondings: vec![ - // NormalLock { - // value: StakingBalance::Ring(10 * MILLICENTS), - // era: 3, - // is_time_deposit: true - // }, - // NormalLock { - // value: StakingBalance::Ring(20 * MILLICENTS), - // era: 3, - // is_time_deposit: true - // } - // ] + assert_eq!(Ring::locks(11), locks); + assert_eq!(Staking::ledger(&10).unwrap(), ledger); - // more than active ring assert_ok!(Staking::unbond( Origin::signed(10), StakingBalance::Ring(120 * MILLICENTS) )); + assert_eq!(Ring::locks(11), locks); + assert_eq!(Staking::ledger(&10).unwrap(), ledger); + + assert_ok!(Staking::claim_mature_deposits(Origin::signed(10))); + // assert_eq!(Ring::locks(11), locks); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { @@ -288,73 +276,54 @@ fn time_deposit_ring_unbond_and_withdraw_should_work() { active_kton: 0, deposit_items: vec![], ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] + staking_amount: 100 * MILLICENTS, + unbondings: vec![], }, kton_staking_lock: StakingLock { staking_amount: 0, - unbondings: vec![] + unbondings: vec![], }, } ); - // unbondings: vec![ - // NormalLock { - // value: StakingBalance::Ring(10 * MILLICENTS), - // era: 3, - // is_time_deposit: true - // }, - // NormalLock { - // value: StakingBalance::Ring(20 * MILLICENTS), - // era: 3, - // is_time_deposit: true - // }, - // NormalLock { - // value: StakingBalance::Ring(70 * MILLICENTS), - // era: 3, - // is_time_deposit: true - // }, - // ] - - Timestamp::set_timestamp(BondingDuration::get()); - assert_eq!( - Staking::ledger(&10).unwrap(), - StakingLedger { - stash: 11, - active_ring: 0, - active_deposit_ring: 0, - active_kton: 0, - deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, - } - ); + // assert_eq!( + // Staking::ledger(&10).unwrap(), + // StakingLedger { + // stash: 11, + // active_ring: 0, + // active_deposit_ring: 0, + // active_kton: 0, + // deposit_items: vec![], + // ring_staking_lock: StakingLock { + // staking_amount: 0, + // unbondings: vec![] + // }, + // kton_staking_lock: StakingLock { + // staking_amount: 0, + // unbondings: vec![] + // }, + // } + // ); // unbondings: vec![] - let free_balance = Ring::free_balance(&11); - assert_eq!( - Ring::locks(&11), - vec![BalanceLock { - id: STAKING_ID, - withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 0, - unbondings: vec![], - }), - reasons: WithdrawReasons::all() - }] - ); - assert_ok!(Ring::ensure_can_withdraw( - &11, - free_balance, - WithdrawReason::Transfer.into(), - 0 - )); + // let free_balance = Ring::free_balance(&11); + // assert_eq!( + // Ring::locks(&11), + // vec![BalanceLock { + // id: STAKING_ID, + // withdraw_lock: WithdrawLock::WithStaking(StakingLock { + // staking_amount: 0, + // unbondings: vec![], + // }), + // reasons: WithdrawReasons::all() + // }] + // ); + // assert_ok!(Ring::ensure_can_withdraw( + // &11, + // free_balance, + // WithdrawReason::Transfer.into(), + // 0 + // )); }); } From 77852bb52be9de4b904b867254bf6bfe5baf950f Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 17:13:31 +0800 Subject: [PATCH 04/45] hanging: backup --- srml/staking/src/tests.rs | 116 ++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 50 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 2dbeac88a..1c6c490e0 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -212,72 +212,88 @@ fn normal_kton_should_work() { } #[test] -fn time_deposit_ring_can_not_be_unbond() { +fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - let locks = vec![BalanceLock { - id: STAKING_ID, - withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * MILLICENTS, - unbondings: vec![], - }), - reasons: WithdrawReasons::all(), - }]; - let ledger = StakingLedger { - stash: 11, - active_ring: 100 * MILLICENTS, - active_deposit_ring: 100 * MILLICENTS, - active_kton: 0, - deposit_items: vec![TimeDepositItem { - value: 100 * MILLICENTS, - start_time: 0, - expire_time: 12 * MONTH_IN_SECONDS as u64, - }], - ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS, - unbondings: vec![], - }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![], - }, - }; + { + let locks = vec![BalanceLock { + id: STAKING_ID, + withdraw_lock: WithdrawLock::WithStaking(StakingLock { + staking_amount: 100 * MILLICENTS, + unbondings: vec![], + }), + reasons: WithdrawReasons::all(), + }]; + let ledger = StakingLedger { + stash: 11, + active_ring: 100 * MILLICENTS, + active_deposit_ring: 100 * MILLICENTS, + active_kton: 0, + deposit_items: vec![TimeDepositItem { + value: 100 * MILLICENTS, + start_time: 0, + expire_time: 12 * MONTH_IN_SECONDS as u64, + }], + ring_staking_lock: StakingLock { + staking_amount: 100 * MILLICENTS, + unbondings: vec![], + }, + kton_staking_lock: StakingLock { + staking_amount: 0, + unbondings: vec![], + }, + }; - Timestamp::set_timestamp(13 * MONTH_IN_SECONDS as u64); + assert_ok!(Staking::unbond( + Origin::signed(10), + StakingBalance::Ring(10 * MILLICENTS) + )); + assert_eq!(Ring::locks(11), locks); + assert_eq!(Staking::ledger(&10).unwrap(), ledger,); - assert_ok!(Staking::unbond( - Origin::signed(10), - StakingBalance::Ring(10 * MILLICENTS) - )); - assert_eq!(Ring::locks(11), locks); - assert_eq!(Staking::ledger(&10).unwrap(), ledger,); + assert_ok!(Staking::unbond( + Origin::signed(10), + StakingBalance::Ring(120 * MILLICENTS) + )); + assert_eq!(Ring::locks(11), locks); + assert_eq!(Staking::ledger(&10).unwrap(), ledger); + } - assert_ok!(Staking::unbond( - Origin::signed(10), - StakingBalance::Ring(20 * MILLICENTS) - )); - assert_eq!(Ring::locks(11), locks); - assert_eq!(Staking::ledger(&10).unwrap(), ledger); + let mut ts = 13 * MONTH_IN_SECONDS as u64; + Timestamp::set_timestamp(ts); assert_ok!(Staking::unbond( Origin::signed(10), - StakingBalance::Ring(120 * MILLICENTS) + StakingBalance::Ring(10 * MILLICENTS) )); - assert_eq!(Ring::locks(11), locks); - assert_eq!(Staking::ledger(&10).unwrap(), ledger); - - assert_ok!(Staking::claim_mature_deposits(Origin::signed(10))); - // assert_eq!(Ring::locks(11), locks); + ts += BondingDuration::get(); + assert_eq!( + Ring::locks(11), + vec![BalanceLock { + id: STAKING_ID, + withdraw_lock: WithdrawLock::WithStaking(StakingLock { + staking_amount: 90 * MILLICENTS, + unbondings: vec![NormalLock { + amount: 10 * MILLICENTS, + until: ts, + }], + }), + reasons: WithdrawReasons::all() + }] + ); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * MILLICENTS, + active_ring: 90 * MILLICENTS, active_deposit_ring: 0, active_kton: 0, deposit_items: vec![], ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS, - unbondings: vec![], + staking_amount: 90 * MILLICENTS, + unbondings: vec![NormalLock { + amount: 10 * MILLICENTS, + until: ts, + }], }, kton_staking_lock: StakingLock { staking_amount: 0, From fe248b79070287eca1970d93520a1d41eb52b23c Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 18:03:36 +0800 Subject: [PATCH 05/45] hanging: backup --- srml/staking/src/tests.rs | 211 +++++++++++++++++--------------------- 1 file changed, 92 insertions(+), 119 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 1c6c490e0..ba4b158e4 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -258,23 +258,19 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { assert_eq!(Staking::ledger(&10).unwrap(), ledger); } - let mut ts = 13 * MONTH_IN_SECONDS as u64; - Timestamp::set_timestamp(ts); + let (unbond_start, unbond_value) = (13 * MONTH_IN_SECONDS as u64, 10 * MILLICENTS); + Timestamp::set_timestamp(unbond_start); - assert_ok!(Staking::unbond( - Origin::signed(10), - StakingBalance::Ring(10 * MILLICENTS) - )); - ts += BondingDuration::get(); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(unbond_value))); assert_eq!( Ring::locks(11), vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 90 * MILLICENTS, + staking_amount: 100 * MILLICENTS - unbond_value, unbondings: vec![NormalLock { - amount: 10 * MILLICENTS, - until: ts, + amount: unbond_value, + until: unbond_start + BondingDuration::get(), }], }), reasons: WithdrawReasons::all() @@ -284,15 +280,15 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 90 * MILLICENTS, + active_ring: 100 * MILLICENTS - unbond_value, active_deposit_ring: 0, active_kton: 0, deposit_items: vec![], ring_staking_lock: StakingLock { - staking_amount: 90 * MILLICENTS, + staking_amount: 100 * MILLICENTS - unbond_value, unbondings: vec![NormalLock { - amount: 10 * MILLICENTS, - until: ts, + amount: unbond_value, + until: unbond_start + BondingDuration::get(), }], }, kton_staking_lock: StakingLock { @@ -302,111 +298,88 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { } ); - // assert_eq!( - // Staking::ledger(&10).unwrap(), - // StakingLedger { - // stash: 11, - // active_ring: 0, - // active_deposit_ring: 0, - // active_kton: 0, - // deposit_items: vec![], - // ring_staking_lock: StakingLock { - // staking_amount: 0, - // unbondings: vec![] - // }, - // kton_staking_lock: StakingLock { - // staking_amount: 0, - // unbondings: vec![] - // }, - // } - // ); - // unbondings: vec![] + Timestamp::set_timestamp(unbond_start + BondingDuration::get()); + assert_err!( + Ring::ensure_can_withdraw( + &11, + unbond_value, + WithdrawReason::Transfer.into(), + 100 * MILLICENTS - unbond_value - 1, + ), + "account liquidity restrictions prevent withdrawal" + ); + assert_ok!(Ring::ensure_can_withdraw( + &11, + unbond_value, + WithdrawReason::Transfer.into(), + 100 * MILLICENTS - unbond_value, + )); + }); +} - // let free_balance = Ring::free_balance(&11); - // assert_eq!( - // Ring::locks(&11), - // vec![BalanceLock { - // id: STAKING_ID, - // withdraw_lock: WithdrawLock::WithStaking(StakingLock { - // staking_amount: 0, - // unbondings: vec![], - // }), - // reasons: WithdrawReasons::all() - // }] - // ); - // 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(|| { + let stash = 11; + let controller = 10; + let value = 200 * MILLICENTS; + let promise_month = 12; + let _ = Ring::deposit_creating(&stash, 1000 * MILLICENTS); + + { + let kton_free_balance = Kton::free_balance(&stash); + let mut ledger = Staking::ledger(&controller).unwrap(); + + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Ring(value), + promise_month, + )); + assert_eq!( + Kton::free_balance(&stash), + kton_free_balance + inflation::compute_kton_return::(value, promise_month) + ); + ledger.active_ring += value; + ledger.active_deposit_ring += value; + ledger.deposit_items.push(TimeDepositItem { + value, + start_time: 0, + expire_time: (promise_month * MONTH_IN_SECONDS) as u64, + }); + ledger.ring_staking_lock.staking_amount += value; + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + } + + { + let kton_free_balance = Kton::free_balance(&stash); + let mut ledger = Staking::ledger(&controller).unwrap(); + + // we try to bond 1 kton, but stash only has 0.03 Kton + // extra = 1.min(0.03) + // bond += 0.03 + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Kton(MILLICENTS), + 0 + )); + ledger.active_kton += kton_free_balance; + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + + assert_ok!(Staking::unbond( + Origin::signed(controller), + StakingBalance::Kton(kton_free_balance) + )); + ledger.active_kton = 0; + ledger.unbondings = vec![NormalLock { + value: StakingBalance::Kton(kton_free_balance), + era: 3, + is_time_deposit: false, + }]; + assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); + } }); } -//#[test] -//fn normal_unbond_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let stash = 11; -// let controller = 10; -// let value = 200 * MILLICENTS; -// let promise_month = 12; -// // unbond normal ring -// let _ = Ring::deposit_creating(&stash, 1000 * MILLICENTS); -// -// { -// let kton_free_balance = Kton::free_balance(&stash); -// let mut ledger = Staking::ledger(&controller).unwrap(); -// -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(value), -// promise_month, -// )); -// assert_eq!( -// Kton::free_balance(&stash), -// kton_free_balance + inflation::compute_kton_return::(value, promise_month) -// ); -// ledger.total_deposit_ring += value; -// ledger.active_ring += value; -// ledger.active_deposit_ring += value; -// ledger.deposit_items.push(TimeDepositItem { -// value: value, -// start_time: 0, -// expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// } -// -// { -// let kton_free_balance = Kton::free_balance(&stash); -// let mut ledger = Staking::ledger(&controller).unwrap(); -// -// // we try to bond 1 kton, but stash only has 0.03 Kton -// // extra = 1.min(0.03) -// // bond += 0.03 -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Kton(MILLICENTS), -// 0 -// )); -// ledger.active_kton += kton_free_balance; -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// -// assert_ok!(Staking::unbond( -// Origin::signed(controller), -// StakingBalance::Kton(kton_free_balance) -// )); -// ledger.active_kton = 0; -// ledger.unbondings = vec![NormalLock { -// value: StakingBalance::Kton(kton_free_balance), -// era: 3, -// is_time_deposit: false, -// }]; -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// } -// }); -//} -// //#[test] //fn punished_unbond_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -1577,7 +1550,7 @@ fn xavier_q2() { // println!("Ok Bond Extra - Kton Locks: {:#?}", Kton::locks(stash)); // println!(); - let (unbond_value_1, unbond_start_1) = (2, 2); + let (unbond_start_1, unbond_value_1) = (2, 2); Timestamp::set_timestamp(unbond_start_1); assert_ok!(Staking::unbond( Origin::signed(controller), @@ -1603,7 +1576,7 @@ fn xavier_q2() { // println!("Ok Unbond - Kton Locks: {:#?}", Kton::locks(stash)); // println!(); - let (unbond_value_2, unbond_start_2) = (6, 3); + let (unbond_start_2, unbond_value_2) = (3, 6); Timestamp::set_timestamp(unbond_start_2); assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(6))); assert_eq!(Timestamp::get(), unbond_start_2); @@ -1780,7 +1753,7 @@ fn xavier_q2() { // println!("Ok Bond Extra - Ring Locks: {:#?}", Ring::locks(stash)); // println!(); - let (unbond_value_1, unbond_start_1) = (2, 2); + let (unbond_start_1, unbond_value_1) = (2, 2); Timestamp::set_timestamp(unbond_start_1); assert_ok!(Staking::unbond( Origin::signed(controller), @@ -1806,7 +1779,7 @@ fn xavier_q2() { // println!("Ok Unbond - Ring Locks: {:#?}", Ring::locks(stash)); // println!(); - let (unbond_value_2, unbond_start_2) = (6, 3); + let (unbond_start_2, unbond_value_2) = (3, 6); Timestamp::set_timestamp(unbond_start_2); assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(6))); assert_eq!(Timestamp::get(), unbond_start_2); From ecafdd0f638adce2f1594341002e6b545c0e8726 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 18:20:43 +0800 Subject: [PATCH 06/45] fix: `normal_unbond_should_work` --- srml/staking/src/tests.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index ba4b158e4..ad025b34c 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -363,6 +363,7 @@ fn normal_unbond_should_work() { 0 )); ledger.active_kton += kton_free_balance; + ledger.kton_staking_lock.staking_amount += kton_free_balance; assert_eq!(Staking::ledger(&controller).unwrap(), ledger); assert_ok!(Staking::unbond( @@ -370,12 +371,12 @@ fn normal_unbond_should_work() { StakingBalance::Kton(kton_free_balance) )); ledger.active_kton = 0; - ledger.unbondings = vec![NormalLock { - value: StakingBalance::Kton(kton_free_balance), - era: 3, - is_time_deposit: false, - }]; - assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); + ledger.kton_staking_lock.staking_amount = 0; + ledger.kton_staking_lock.unbondings.push(NormalLock { + amount: kton_free_balance, + until: BondingDuration::get(), + }); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); } }); } @@ -458,7 +459,7 @@ fn normal_unbond_should_work() { // assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); // }); //} -// + //#[test] //fn transform_to_promised_ring_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From a30ff732c0aad3f03f407ac256d6ce8048fe9788 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 22 Nov 2019 18:30:38 +0800 Subject: [PATCH 07/45] hanging: backup --- srml/staking/src/tests.rs | 155 +++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 78 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index ad025b34c..111ccf727 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -381,84 +381,83 @@ fn normal_unbond_should_work() { }); } -//#[test] -//fn punished_unbond_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let stash = 1001; -// let controller = 1000; -// let promise_month = 36; -// -// let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); -// Kton::deposit_creating(&stash, MILLICENTS / 100000); -// -// // timestamp now is 0. -// // free balance of kton is too low to work -// assert_ok!(Staking::bond( -// Origin::signed(stash), -// controller, -// StakingBalance::Ring(10 * MILLICENTS), -// RewardDestination::Stash, -// promise_month -// )); -// assert_eq!( -// Staking::ledger(&controller), -// Some(StakingLedger { -// stash, -// total_deposit_ring: 10 * MILLICENTS, -// active_deposit_ring: 10 * MILLICENTS, -// active_ring: 10 * MILLICENTS, -// active_kton: 0, -// deposit_items: vec![TimeDepositItem { -// value: 10 * MILLICENTS, -// start_time: 0, -// expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64 -// }], // should be cleared -// unbondings: vec![] -// }) -// ); -// let mut ledger = Staking::ledger(&controller).unwrap(); -// let kton_free_balance = Kton::free_balance(&stash); -// // kton is 0, skip unbond_with_punish -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 10 * MILLICENTS, -// promise_month as u64 * MONTH_IN_SECONDS as u64 -// )); -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// assert_eq!(Kton::free_balance(&stash), kton_free_balance); -// -// // set more kton balance to make it work -// Kton::deposit_creating(&stash, 10 * MILLICENTS); -// let kton_free_balance = Kton::free_balance(&stash); -// let unbond_value = 5 * MILLICENTS; -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// unbond_value, -// promise_month as u64 * MONTH_IN_SECONDS as u64 -// )); -// ledger.active_ring -= unbond_value; -// ledger.active_deposit_ring -= unbond_value; -// ledger.deposit_items[0].value -= unbond_value; -// ledger.unbondings = vec![NormalLock { -// value: StakingBalance::Ring(unbond_value), -// era: 3, -// is_time_deposit: true, -// }]; -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// -// let kton_punishment = inflation::compute_kton_return::(unbond_value, promise_month); -// assert_eq!(Kton::free_balance(&stash), kton_free_balance - 3 * kton_punishment); -// -// // if deposit_item.value == 0 -// // the whole item should be be dropped -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 5 * MILLICENTS, -// promise_month as u64 * MONTH_IN_SECONDS as u64 -// )); -// assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); -// }); -//} +#[test] +fn punished_unbond_should_work() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let stash = 1001; + let controller = 1000; + let promise_month = 36; + let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); + Kton::deposit_creating(&stash, MILLICENTS / 100000); + + // timestamp now is 0. + // free balance of kton is too low to work + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(10 * MILLICENTS), + RewardDestination::Stash, + promise_month, + )); + // assert_eq!( + // Staking::ledger(&controller), + // Some(StakingLedger { + // stash, + // total_deposit_ring: 10 * MILLICENTS, + // active_deposit_ring: 10 * MILLICENTS, + // active_ring: 10 * MILLICENTS, + // active_kton: 0, + // deposit_items: vec![TimeDepositItem { + // value: 10 * MILLICENTS, + // start_time: 0, + // expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64 + // }], // should be cleared + // unbondings: vec![] + // }) + // ); + // let mut ledger = Staking::ledger(&controller).unwrap(); + // let kton_free_balance = Kton::free_balance(&stash); + // // kton is 0, skip unbond_with_punish + // assert_ok!(Staking::unbond_with_punish( + // Origin::signed(controller), + // 10 * MILLICENTS, + // promise_month as u64 * MONTH_IN_SECONDS as u64 + // )); + // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); + // assert_eq!(Kton::free_balance(&stash), kton_free_balance); + // + // // set more kton balance to make it work + // Kton::deposit_creating(&stash, 10 * MILLICENTS); + // let kton_free_balance = Kton::free_balance(&stash); + // let unbond_value = 5 * MILLICENTS; + // assert_ok!(Staking::unbond_with_punish( + // Origin::signed(controller), + // unbond_value, + // promise_month as u64 * MONTH_IN_SECONDS as u64 + // )); + // ledger.active_ring -= unbond_value; + // ledger.active_deposit_ring -= unbond_value; + // ledger.deposit_items[0].value -= unbond_value; + // ledger.unbondings = vec![NormalLock { + // value: StakingBalance::Ring(unbond_value), + // era: 3, + // is_time_deposit: true, + // }]; + // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); + // + // let kton_punishment = inflation::compute_kton_return::(unbond_value, promise_month); + // assert_eq!(Kton::free_balance(&stash), kton_free_balance - 3 * kton_punishment); + // + // // if deposit_item.value == 0 + // // the whole item should be be dropped + // assert_ok!(Staking::unbond_with_punish( + // Origin::signed(controller), + // 5 * MILLICENTS, + // promise_month as u64 * MONTH_IN_SECONDS as u64 + // )); + // assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); + }); +} //#[test] //fn transform_to_promised_ring_should_work() { From 9f80a33ca9128cd674d4a24c3c37fb64aeee7365 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 08:07:06 +0800 Subject: [PATCH 08/45] update: currency primitives --- node/cli/src/chain_spec.rs | 27 ++- node/runtime/src/constants.rs | 4 +- node/runtime/src/lib.rs | 41 +++-- srml/kton/src/mock.rs | 6 +- srml/staking/src/mock.rs | 14 +- srml/staking/src/tests.rs | 324 +++++++++++++++++----------------- 6 files changed, 202 insertions(+), 214 deletions(-) diff --git a/node/cli/src/chain_spec.rs b/node/cli/src/chain_spec.rs index b6fb9af20..1d59faccd 100644 --- a/node/cli/src/chain_spec.rs +++ b/node/cli/src/chain_spec.rs @@ -15,26 +15,23 @@ // along with Substrate. If not, see . //! Substrate chain configurations. +pub use node_runtime::GenesisConfig; + use babe_primitives::AuthorityId as BabeId; use chain_spec::ChainSpecExtension; use grandpa_primitives::AuthorityId as GrandpaId; use hex_literal::hex; use im_online::sr25519::AuthorityId as ImOnlineId; use node_primitives::{AccountId, Balance}; -use node_runtime::constants::currency::*; -use node_runtime::Block; -pub use node_runtime::GenesisConfig; use node_runtime::{ - constants::currency::MILLICENTS, AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, ContractsConfig, + constants::currency::*, AuthorityDiscoveryConfig, BabeConfig, BalancesConfig, Block, ContractsConfig, GrandpaConfig, ImOnlineConfig, IndicesConfig, KtonConfig, SessionConfig, SessionKeys, StakerStatus, StakingConfig, SudoConfig, SystemConfig, WASM_BINARY, }; use primitives::{crypto::UncheckedInto, Pair, Public}; use serde::{Deserialize, Serialize}; -use serde_json::de::ParserNumber; -use serde_json::Number; +use serde_json::{de::ParserNumber, Number}; use sr_primitives::Perbill; -use substrate_service; use substrate_service::Properties; use substrate_telemetry::TelemetryEndpoints; @@ -195,8 +192,8 @@ pub fn testnet_genesis( ] }); - const ENDOWMENT: Balance = 10_000_000 * DOLLARS; - const STASH: Balance = 100 * DOLLARS; + const ENDOWMENT: Balance = 10_000_000 * COIN; + const STASH: Balance = 100 * COIN; GenesisConfig { system: Some(SystemConfig { @@ -230,7 +227,7 @@ pub fn testnet_genesis( enable_println, // this should only be enabled on development chains ..Default::default() }, - gas_price: 1 * MILLICENTS, + gas_price: 1 * MICRO, }), sudo: Some(SudoConfig { key: root_key }), babe: Some(BabeConfig { authorities: vec![] }), @@ -249,7 +246,7 @@ pub fn testnet_genesis( }), staking: Some(StakingConfig { current_era: 0, - // current_era_total_reward: 80_000_000 * MILLICENTS / 63720, + // current_era_total_reward: 80_000_000 * COIN / 63720, // offline_slash: Perbill::from_parts(1_000_000), session_reward: Perbill::from_percent(90), validator_count: 7, @@ -350,8 +347,8 @@ pub fn darwinia_genesis_verbose( ] }); - const ENDOWMENT: Balance = 100_000_000 * MILLICENTS; - const STASH: Balance = 100 * MILLICENTS; + const ENDOWMENT: Balance = 100_000_000 * COIN; + const STASH: Balance = 100 * COIN; GenesisConfig { system: Some(SystemConfig { @@ -385,7 +382,7 @@ pub fn darwinia_genesis_verbose( enable_println, // this should only be enabled on development chains ..Default::default() }, - gas_price: 1 * MILLICENTS, + gas_price: 1 * MICRO, }), sudo: Some(SudoConfig { key: root_key }), babe: Some(BabeConfig { authorities: vec![] }), @@ -404,7 +401,7 @@ pub fn darwinia_genesis_verbose( }), staking: Some(StakingConfig { current_era: 0, - // current_era_total_reward: 80_000_000 * MILLICENTS / 63720, + // current_era_total_reward: 80_000_000 * COIN / 63720, // offline_slash: Perbill::from_parts(1_000_000), session_reward: Perbill::from_percent(90), validator_count: 7, diff --git a/node/runtime/src/constants.rs b/node/runtime/src/constants.rs index 39dc0dc7b..c6271bc12 100644 --- a/node/runtime/src/constants.rs +++ b/node/runtime/src/constants.rs @@ -23,9 +23,7 @@ pub mod currency { pub const NANO: Balance = 1; pub const MICRO: Balance = 1_000 * NANO; pub const MILLI: Balance = 1_000 * MICRO; - pub const MILLICENTS: Balance = 1_000 * MILLI; - pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent. - pub const DOLLARS: Balance = 100 * CENTS; + pub const COIN: Balance = 1_000 * MILLI; } /// Time. diff --git a/node/runtime/src/lib.rs b/node/runtime/src/lib.rs index 5b0de4faf..9eaf3f9a7 100644 --- a/node/runtime/src/lib.rs +++ b/node/runtime/src/lib.rs @@ -19,6 +19,11 @@ #![cfg_attr(not(feature = "std"), no_std)] #![recursion_limit = "256"] +/// Constant values used within the runtime. +pub mod constants; +/// Implementations of some helper traits passed into runtime modules as associated types. +pub mod impls; + pub use contracts::Gas; pub use timestamp::Call as TimestampCall; @@ -55,16 +60,10 @@ use version::RuntimeVersion; //use grandpa::{AuthorityId as GrandpaId, AuthorityWeight as GrandpaWeight}; //use im_online::sr25519::AuthorityId as ImOnlineId; -use darwinia_support::TimeStamp; -use staking::EraIndex; - -/// Constant values used within the runtime. -pub mod constants; -/// Implementations of some helper traits passed into runtime modules as associated types. -pub mod impls; - use constants::{currency::*, time::*}; +use darwinia_support::TimeStamp; use impls::{Author, CurrencyToVoteHandler, LinearWeightToFee, TargetedFeeAdjustment}; +use staking::EraIndex; // Make the WASM binary available. #[cfg(feature = "std")] @@ -151,9 +150,9 @@ impl indices::Trait for Runtime { } parameter_types! { - pub const ExistentialDeposit: Balance = 1 * MILLICENTS; - pub const TransferFee: Balance = 1 * MILLI; - pub const CreationFee: Balance = 1 * MILLI; + pub const ExistentialDeposit: Balance = 1 * COIN; + pub const TransferFee: Balance = 1 * MICRO; + pub const CreationFee: Balance = 1 * MICRO; } impl balances::Trait for Runtime { type Balance = Balance; @@ -168,7 +167,7 @@ impl balances::Trait for Runtime { } parameter_types! { - pub const TransactionBaseFee: Balance = 1 * MILLI; + pub const TransactionBaseFee: Balance = 1 * MICRO; pub const TransactionByteFee: Balance = 10 * MICRO; // setting this to zero will disable the weight fee. pub const WeightFeeCoefficient: Balance = 1_000; @@ -290,15 +289,15 @@ impl finality_tracker::Trait for Runtime { } parameter_types! { - pub const ContractTransferFee: Balance = 1 * MILLI; - pub const ContractCreationFee: Balance = 1 * MILLI; - pub const ContractTransactionBaseFee: Balance = 1 * MILLI; + pub const ContractTransferFee: Balance = 1 * MICRO; + pub const ContractCreationFee: Balance = 1 * MICRO; + pub const ContractTransactionBaseFee: Balance = 1 * MICRO; pub const ContractTransactionByteFee: Balance = 10 * MICRO; - pub const ContractFee: Balance = 1 * MILLI; - pub const TombstoneDeposit: Balance = 1 * MILLICENTS; - pub const RentByteFee: Balance = 1 * MILLICENTS; - pub const RentDepositOffset: Balance = 1000 * MILLICENTS; - pub const SurchargeReward: Balance = 150 * MILLICENTS; + pub const ContractFee: Balance = 1 * MICRO; + pub const TombstoneDeposit: Balance = 1 * COIN; + pub const RentByteFee: Balance = 1 * COIN; + pub const RentDepositOffset: Balance = 1000 * COIN; + pub const SurchargeReward: Balance = 150 * COIN; } impl contracts::Trait for Runtime { type Currency = Balances; @@ -375,7 +374,7 @@ parameter_types! { // 365 days * 24 hours * 60 minutes / 5 minutes pub const ErasPerEpoch: EraIndex = 105120; // decimal 9 - pub const HardCap: Balance = 10_000_000_000 * MILLICENTS; + pub const HardCap: Balance = 10_000_000_000 * COIN; pub const GenesisTime: Moment = 1_574_156_000_000; } impl staking::Trait for Runtime { diff --git a/srml/kton/src/mock.rs b/srml/kton/src/mock.rs index 1b24b4f1c..b51141bef 100644 --- a/srml/kton/src/mock.rs +++ b/srml/kton/src/mock.rs @@ -1,4 +1,4 @@ -pub use node_runtime::constants::currency::MILLICENTS; +pub use node_runtime::constants::currency::COIN; use std::{cell::RefCell, collections::HashSet}; @@ -100,9 +100,9 @@ impl ExtBuilder { self.set_associated_consts(); let mut t = system::GenesisConfig::default().build_storage::().unwrap(); let balance_factor = if self.existential_deposit > 0 { - 1_000 * MILLICENTS + 1_000 * COIN } else { - 1 * MILLICENTS + 1 * COIN }; let _ = GenesisConfig:: { diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 55d1b2c2d..a9c373727 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -1,4 +1,4 @@ -pub use node_runtime::constants::currency::MILLICENTS; +pub use node_runtime::constants::currency::COIN; use std::{cell::RefCell, collections::HashSet}; @@ -187,7 +187,7 @@ parameter_types! { } parameter_types! { // decimal 9 - pub const CAP: Balance = 10_000_000_000 * MILLICENTS; + pub const CAP: Balance = 10_000_000_000 * COIN; } impl Trait for Test { type Ring = Ring; @@ -270,9 +270,9 @@ impl ExtBuilder { self.set_associated_consts(); let mut storage = system::GenesisConfig::default().build_storage::().unwrap(); let balance_factor = if self.existential_deposit > 0 { - 1_000 * MILLICENTS + 1_000 * COIN } else { - 1 * MILLICENTS + 1 * COIN }; let validators = if self.validator_pool { vec![10, 20, 30, 40] @@ -322,10 +322,10 @@ impl ExtBuilder { let nominated = if self.nominate { vec![11, 21] } else { vec![] }; let _ = GenesisConfig:: { current_era: self.current_era, - // current_era_total_reward: 80_000_000 * MILLICENTS / ErasPerEpoch::get() as u64, + // current_era_total_reward: 80_000_000 * COIN / ErasPerEpoch::get() as u64, stakers: vec![ - // (2, 1, 1 * MILLICENTS, StakerStatus::::Validator), - (11, 10, 100 * MILLICENTS, StakerStatus::::Validator), + // (2, 1, 1 * COIN, StakerStatus::::Validator), + (11, 10, 100 * COIN, StakerStatus::::Validator), (21, 20, stake_21, StakerStatus::::Validator), (31, 30, stake_31, StakerStatus::::Validator), (41, 40, balance_factor * 1000, status_41), diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 111ccf727..6b0a8701e 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -17,55 +17,55 @@ use darwinia_support::{BalanceLock, NormalLock, StakingLock, WithdrawLock}; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $promise_month:ident($how_long:expr)) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); -// Kton::deposit_creating(&$stash, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&$stash, 100 * COIN); +// Kton::deposit_creating(&$stash, 100 * COIN); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, MILLICENTS); +// let _ = Ring::deposit_creating(&$controller, COIN); // #[allow(non_snake_case, unused)] // let $promise_month = $how_long; // assert_ok!(Staking::bond( // Origin::signed($stash), // $controller, -// StakingBalance::Ring(50 * MILLICENTS), +// StakingBalance::Ring(50 * COIN), // RewardDestination::Stash, // $how_long // )); // assert_ok!(Staking::bond_extra( // Origin::signed($stash), -// StakingBalance::Kton(50 * MILLICENTS), +// StakingBalance::Kton(50 * COIN), // $how_long // )); // }; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $how_long:expr) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); -// Kton::deposit_creating(&$stash, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&$stash, 100 * COIN); +// Kton::deposit_creating(&$stash, 100 * COIN); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, MILLICENTS); +// let _ = Ring::deposit_creating(&$controller, COIN); // assert_ok!(Staking::bond( // Origin::signed($stash), // $controller, -// StakingBalance::Ring(50 * MILLICENTS), +// StakingBalance::Ring(50 * COIN), // RewardDestination::Stash, // $how_long // )); // assert_ok!(Staking::bond_extra( // Origin::signed($stash), -// StakingBalance::Kton(50 * MILLICENTS), +// StakingBalance::Kton(50 * COIN), // $how_long // )); // }; // ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr)) => { // #[allow(non_snake_case, unused)] // let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * MILLICENTS); -// Kton::deposit_creating(&$stash, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&$stash, 100 * COIN); +// Kton::deposit_creating(&$stash, 100 * COIN); // #[allow(non_snake_case, unused)] // let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, MILLICENTS); +// let _ = Ring::deposit_creating(&$controller, COIN); // }; //} @@ -79,16 +79,16 @@ fn test_env_build() { Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * MILLICENTS, - active_deposit_ring: 100 * MILLICENTS, + active_ring: 100 * COIN, + active_deposit_ring: 100 * COIN, active_kton: 0, deposit_items: vec![TimeDepositItem { - value: 100 * MILLICENTS, + value: 100 * COIN, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }], ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS, + staking_amount: 100 * COIN, unbondings: vec![] }, kton_staking_lock: StakingLock { @@ -98,37 +98,37 @@ fn test_env_build() { } ); - assert_eq!(Kton::free_balance(&11), MILLICENTS / 100); - assert_eq!(Kton::total_issuance(), 16 * MILLICENTS / 100); + assert_eq!(Kton::free_balance(&11), COIN / 100); + assert_eq!(Kton::total_issuance(), 16 * COIN / 100); let origin_ledger = Staking::ledger(&10).unwrap(); - let _ = Ring::deposit_creating(&11, 100 * MILLICENTS); + let _ = Ring::deposit_creating(&11, 100 * COIN); assert_ok!(Staking::bond_extra( Origin::signed(11), - StakingBalance::Ring(20 * MILLICENTS), + StakingBalance::Ring(20 * COIN), 13 )); assert_eq!( Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: origin_ledger.active_ring + 20 * MILLICENTS, - active_deposit_ring: origin_ledger.active_deposit_ring + 20 * MILLICENTS, + active_ring: origin_ledger.active_ring + 20 * COIN, + active_deposit_ring: origin_ledger.active_deposit_ring + 20 * COIN, active_kton: 0, deposit_items: vec![ TimeDepositItem { - value: 100 * MILLICENTS, + value: 100 * COIN, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64 }, TimeDepositItem { - value: 20 * MILLICENTS, + value: 20 * COIN, start_time: 0, expire_time: 13 * MONTH_IN_SECONDS as u64 } ], ring_staking_lock: StakingLock { - staking_amount: origin_ledger.active_ring + 20 * MILLICENTS, + staking_amount: origin_ledger.active_ring + 20 * COIN, unbondings: vec![] }, kton_staking_lock: StakingLock { @@ -143,11 +143,11 @@ fn test_env_build() { #[test] fn normal_kton_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - Kton::deposit_creating(&1001, 10 * MILLICENTS); + Kton::deposit_creating(&1001, 10 * COIN); assert_ok!(Staking::bond( Origin::signed(1001), 1000, - StakingBalance::Kton(10 * MILLICENTS), + StakingBalance::Kton(10 * COIN), RewardDestination::Stash, 0 )); @@ -157,14 +157,14 @@ fn normal_kton_should_work() { stash: 1001, active_ring: 0, active_deposit_ring: 0, - active_kton: 10 * MILLICENTS, + active_kton: 10 * COIN, deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 0, unbondings: vec![] }, kton_staking_lock: StakingLock { - staking_amount: 10 * MILLICENTS, + staking_amount: 10 * COIN, unbondings: vec![] }, } @@ -174,7 +174,7 @@ fn normal_kton_should_work() { vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 10 * MILLICENTS, + staking_amount: 10 * COIN, unbondings: vec![], }), reasons: WithdrawReasons::all() @@ -182,11 +182,11 @@ fn normal_kton_should_work() { ); // promise_month should not work for kton - Kton::deposit_creating(&2001, 10 * MILLICENTS); + Kton::deposit_creating(&2001, 10 * COIN); assert_ok!(Staking::bond( Origin::signed(2001), 2000, - StakingBalance::Kton(10 * MILLICENTS), + StakingBalance::Kton(10 * COIN), RewardDestination::Stash, 12 )); @@ -196,14 +196,14 @@ fn normal_kton_should_work() { stash: 2001, active_ring: 0, active_deposit_ring: 0, - active_kton: 10 * MILLICENTS, + active_kton: 10 * COIN, deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 0, unbondings: vec![] }, kton_staking_lock: StakingLock { - staking_amount: 10 * MILLICENTS, + staking_amount: 10 * COIN, unbondings: vec![] }, } @@ -218,23 +218,23 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { let locks = vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * MILLICENTS, + staking_amount: 100 * COIN, unbondings: vec![], }), reasons: WithdrawReasons::all(), }]; let ledger = StakingLedger { stash: 11, - active_ring: 100 * MILLICENTS, - active_deposit_ring: 100 * MILLICENTS, + active_ring: 100 * COIN, + active_deposit_ring: 100 * COIN, active_kton: 0, deposit_items: vec![TimeDepositItem { - value: 100 * MILLICENTS, + value: 100 * COIN, start_time: 0, expire_time: 12 * MONTH_IN_SECONDS as u64, }], ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS, + staking_amount: 100 * COIN, unbondings: vec![], }, kton_staking_lock: StakingLock { @@ -243,22 +243,16 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { }, }; - assert_ok!(Staking::unbond( - Origin::signed(10), - StakingBalance::Ring(10 * MILLICENTS) - )); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); assert_eq!(Ring::locks(11), locks); assert_eq!(Staking::ledger(&10).unwrap(), ledger,); - assert_ok!(Staking::unbond( - Origin::signed(10), - StakingBalance::Ring(120 * MILLICENTS) - )); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(120 * COIN))); assert_eq!(Ring::locks(11), locks); assert_eq!(Staking::ledger(&10).unwrap(), ledger); } - let (unbond_start, unbond_value) = (13 * MONTH_IN_SECONDS as u64, 10 * MILLICENTS); + let (unbond_start, unbond_value) = (13 * MONTH_IN_SECONDS as u64, 10 * COIN); Timestamp::set_timestamp(unbond_start); assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(unbond_value))); @@ -267,7 +261,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { vec![BalanceLock { id: STAKING_ID, withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * MILLICENTS - unbond_value, + staking_amount: 100 * COIN - unbond_value, unbondings: vec![NormalLock { amount: unbond_value, until: unbond_start + BondingDuration::get(), @@ -280,12 +274,12 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { Staking::ledger(&10).unwrap(), StakingLedger { stash: 11, - active_ring: 100 * MILLICENTS - unbond_value, + active_ring: 100 * COIN - unbond_value, active_deposit_ring: 0, active_kton: 0, deposit_items: vec![], ring_staking_lock: StakingLock { - staking_amount: 100 * MILLICENTS - unbond_value, + staking_amount: 100 * COIN - unbond_value, unbondings: vec![NormalLock { amount: unbond_value, until: unbond_start + BondingDuration::get(), @@ -304,7 +298,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { &11, unbond_value, WithdrawReason::Transfer.into(), - 100 * MILLICENTS - unbond_value - 1, + 100 * COIN - unbond_value - 1, ), "account liquidity restrictions prevent withdrawal" ); @@ -312,7 +306,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { &11, unbond_value, WithdrawReason::Transfer.into(), - 100 * MILLICENTS - unbond_value, + 100 * COIN - unbond_value, )); }); } @@ -322,9 +316,9 @@ fn normal_unbond_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { let stash = 11; let controller = 10; - let value = 200 * MILLICENTS; + let value = 200 * COIN; let promise_month = 12; - let _ = Ring::deposit_creating(&stash, 1000 * MILLICENTS); + let _ = Ring::deposit_creating(&stash, 1000 * COIN); { let kton_free_balance = Kton::free_balance(&stash); @@ -359,7 +353,7 @@ fn normal_unbond_should_work() { // bond += 0.03 assert_ok!(Staking::bond_extra( Origin::signed(stash), - StakingBalance::Kton(MILLICENTS), + StakingBalance::Kton(COIN), 0 )); ledger.active_kton += kton_free_balance; @@ -387,15 +381,15 @@ fn punished_unbond_should_work() { let stash = 1001; let controller = 1000; let promise_month = 36; - let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); - Kton::deposit_creating(&stash, MILLICENTS / 100000); + let _ = Ring::deposit_creating(&stash, 100 * COIN); + Kton::deposit_creating(&stash, COIN / 100000); // timestamp now is 0. // free balance of kton is too low to work assert_ok!(Staking::bond( Origin::signed(stash), controller, - StakingBalance::Ring(10 * MILLICENTS), + StakingBalance::Ring(10 * COIN), RewardDestination::Stash, promise_month, )); @@ -403,12 +397,12 @@ fn punished_unbond_should_work() { // Staking::ledger(&controller), // Some(StakingLedger { // stash, - // total_deposit_ring: 10 * MILLICENTS, - // active_deposit_ring: 10 * MILLICENTS, - // active_ring: 10 * MILLICENTS, + // total_deposit_ring: 10 * COIN, + // active_deposit_ring: 10 * COIN, + // active_ring: 10 * COIN, // active_kton: 0, // deposit_items: vec![TimeDepositItem { - // value: 10 * MILLICENTS, + // value: 10 * COIN, // start_time: 0, // expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64 // }], // should be cleared @@ -420,16 +414,16 @@ fn punished_unbond_should_work() { // // kton is 0, skip unbond_with_punish // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), - // 10 * MILLICENTS, + // 10 * COIN, // promise_month as u64 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); // assert_eq!(Kton::free_balance(&stash), kton_free_balance); // // // set more kton balance to make it work - // Kton::deposit_creating(&stash, 10 * MILLICENTS); + // Kton::deposit_creating(&stash, 10 * COIN); // let kton_free_balance = Kton::free_balance(&stash); - // let unbond_value = 5 * MILLICENTS; + // let unbond_value = 5 * COIN; // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), // unbond_value, @@ -452,7 +446,7 @@ fn punished_unbond_should_work() { // // the whole item should be be dropped // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), - // 5 * MILLICENTS, + // 5 * COIN, // promise_month as u64 * MONTH_IN_SECONDS as u64 // )); // assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); @@ -462,29 +456,29 @@ fn punished_unbond_should_work() { //#[test] //fn transform_to_promised_ring_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&1001, 100 * COIN); // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(10 * MILLICENTS), +// StakingBalance::Ring(10 * COIN), // RewardDestination::Stash, // 0 // )); // let origin_ledger = Staking::ledger(&1000).unwrap(); // let kton_free_balance = Kton::free_balance(&1001); // -// assert_ok!(Staking::promise_extra(Origin::signed(1000), 5 * MILLICENTS, 12)); +// assert_ok!(Staking::promise_extra(Origin::signed(1000), 5 * COIN, 12)); // // assert_eq!( // Staking::ledger(&1000), // Some(StakingLedger { // stash: 1001, -// total_deposit_ring: origin_ledger.total_deposit_ring + 5 * MILLICENTS, -// active_deposit_ring: origin_ledger.active_deposit_ring + 5 * MILLICENTS, +// total_deposit_ring: origin_ledger.total_deposit_ring + 5 * COIN, +// active_deposit_ring: origin_ledger.active_deposit_ring + 5 * COIN, // active_ring: origin_ledger.active_ring, // active_kton: origin_ledger.active_kton, // deposit_items: vec![TimeDepositItem { -// value: 5 * MILLICENTS, +// value: 5 * COIN, // start_time: 0, // expire_time: 12 * MONTH_IN_SECONDS as u64 // }], @@ -492,24 +486,24 @@ fn punished_unbond_should_work() { // }) // ); // -// assert_eq!(Kton::free_balance(&1001), kton_free_balance + (5 * MILLICENTS / 10000)); +// assert_eq!(Kton::free_balance(&1001), kton_free_balance + (5 * COIN / 10000)); // }); //} // //#[test] //fn expired_ring_should_capable_to_promise_again() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&1001, 100 * COIN); // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(10 * MILLICENTS), +// StakingBalance::Ring(10 * COIN), // RewardDestination::Stash, // 12 // )); // let mut ledger = Staking::ledger(&1000).unwrap(); // let ts = 13 * MONTH_IN_SECONDS as u64; -// let promise_extra_value = 5 * MILLICENTS; +// let promise_extra_value = 5 * COIN; // Timestamp::set_timestamp(ts); // assert_ok!(Staking::promise_extra(Origin::signed(1000), promise_extra_value, 13)); // ledger.total_deposit_ring = promise_extra_value; @@ -527,14 +521,14 @@ fn punished_unbond_should_work() { ////#[test] ////fn inflation_should_be_correct() { //// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// let initial_issuance = 1_200_000_000 * MILLICENTS; +//// let initial_issuance = 1_200_000_000 * COIN; //// let surplus_needed = initial_issuance - Ring::total_issuance(); //// let _ = Ring::deposit_into_existing(&11, surplus_needed); //// assert_eq!(Ring::total_issuance(), initial_issuance); -//// // assert_eq!(Staking::current_era_total_reward(), 80000000 * MILLICENTS / 10); +//// // assert_eq!(Staking::current_era_total_reward(), 80000000 * COIN / 10); //// start_era(11); //// // ErasPerEpoch = 10 -//// // assert_eq!(Staking::current_era_total_reward(), 88000000 * MILLICENTS / 10); +//// // assert_eq!(Staking::current_era_total_reward(), 88000000 * COIN / 10); //// }); ////} // @@ -542,44 +536,44 @@ fn punished_unbond_should_work() { //fn reward_should_work_correctly() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // // create controller account -// let _ = Ring::deposit_creating(&2000, MILLICENTS); -// let _ = Ring::deposit_creating(&1000, MILLICENTS); -// let _ = Ring::deposit_creating(&200, MILLICENTS); +// let _ = Ring::deposit_creating(&2000, COIN); +// let _ = Ring::deposit_creating(&1000, COIN); +// let _ = Ring::deposit_creating(&200, COIN); // // new validator -// let _ = Ring::deposit_creating(&2001, 2000 * MILLICENTS); -// Kton::deposit_creating(&2001, 10 * MILLICENTS); +// let _ = Ring::deposit_creating(&2001, 2000 * COIN); +// Kton::deposit_creating(&2001, 10 * COIN); // // new validator -// let _ = Ring::deposit_creating(&1001, 300 * MILLICENTS); -// Kton::deposit_creating(&1001, 1 * MILLICENTS); +// let _ = Ring::deposit_creating(&1001, 300 * COIN); +// Kton::deposit_creating(&1001, 1 * COIN); // // handle some dirty work -// let _ = Ring::deposit_creating(&201, 2000 * MILLICENTS); -// Kton::deposit_creating(&201, 10 * MILLICENTS); -// assert_eq!(Kton::free_balance(&201), 10 * MILLICENTS); +// let _ = Ring::deposit_creating(&201, 2000 * COIN); +// Kton::deposit_creating(&201, 10 * COIN); +// assert_eq!(Kton::free_balance(&201), 10 * COIN); // // // 2001-2000 // assert_ok!(Staking::bond( // Origin::signed(2001), // 2000, -// StakingBalance::Ring(300 * MILLICENTS), +// StakingBalance::Ring(300 * COIN), // RewardDestination::Controller, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(2001), -// StakingBalance::Kton(1 * MILLICENTS), +// StakingBalance::Kton(1 * COIN), // 0 // )); // // 1001-1000 // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(300 * MILLICENTS), +// StakingBalance::Ring(300 * COIN), // RewardDestination::Controller, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(1001), -// StakingBalance::Kton(1 * MILLICENTS), +// StakingBalance::Kton(1 * COIN), // 0 // )); // let ring_pool = Staking::ring_pool(); @@ -588,29 +582,29 @@ fn punished_unbond_should_work() { // assert_ok!(Staking::bond( // Origin::signed(201), // 200, -// StakingBalance::Ring(3000 * MILLICENTS - ring_pool), +// StakingBalance::Ring(3000 * COIN - ring_pool), // RewardDestination::Stash, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(201), -// StakingBalance::Kton(10 * MILLICENTS - kton_pool), +// StakingBalance::Kton(10 * COIN - kton_pool), // 0, // )); // // ring_pool and kton_pool -// assert_eq!(Staking::ring_pool(), 3000 * MILLICENTS); -// assert_eq!(Staking::kton_pool(), 10 * MILLICENTS); +// assert_eq!(Staking::ring_pool(), 3000 * COIN); +// assert_eq!(Staking::kton_pool(), 10 * COIN); // // 1/5 ring_pool and 1/5 kton_pool // assert_ok!(Staking::validate(Origin::signed(2000), [0; 8].to_vec(), 0, 3)); // assert_ok!(Staking::nominate(Origin::signed(1000), vec![2001])); // -// assert_eq!(Staking::ledger(&2000).unwrap().active_kton, 1 * MILLICENTS); -// assert_eq!(Staking::ledger(&2000).unwrap().active_ring, 300 * MILLICENTS); +// assert_eq!(Staking::ledger(&2000).unwrap().active_kton, 1 * COIN); +// assert_eq!(Staking::ledger(&2000).unwrap().active_ring, 300 * COIN); // assert_eq!(Staking::power_of(&2001), 1_000_000_000 / 10 as u128); // // 600COIN for rewarding ring bond-er // // 600COIN for rewarding kton bond-er // Staking::select_validators(); -// Staking::reward_validator(&2001, 1200 * MILLICENTS); +// Staking::reward_validator(&2001, 1200 * COIN); // // assert_eq!( // Staking::stakers(2001), @@ -623,33 +617,33 @@ fn punished_unbond_should_work() { // }] // } // ); -// assert_eq!(Ring::free_balance(&2000), 601 * MILLICENTS); -// assert_eq!(Ring::free_balance(&1000), 601 * MILLICENTS); +// assert_eq!(Ring::free_balance(&2000), 601 * COIN); +// assert_eq!(Ring::free_balance(&1000), 601 * COIN); // }); //} // //#[test] //fn slash_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * MILLICENTS); -// Kton::deposit_creating(&1001, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&1001, 100 * COIN); +// Kton::deposit_creating(&1001, 100 * COIN); // // assert_ok!(Staking::bond( // Origin::signed(1001), // 1000, -// StakingBalance::Ring(50 * MILLICENTS), +// StakingBalance::Ring(50 * COIN), // RewardDestination::Controller, // 0, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(1001), -// StakingBalance::Kton(50 * MILLICENTS), +// StakingBalance::Kton(50 * COIN), // 0 // )); // assert_ok!(Staking::validate(Origin::signed(1000), [0; 8].to_vec(), 0, 3)); // // // slash 1% -// let slash_value = 5 * MILLICENTS / 10; +// let slash_value = 5 * COIN / 10; // let mut ledger = Staking::ledger(&1000).unwrap(); // let ring_free_balance = Ring::free_balance(&1001); // let kton_free_balance = Kton::free_balance(&1001); @@ -665,10 +659,10 @@ fn punished_unbond_should_work() { //#[test] ////fn test_inflation() { //// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// assert_eq!(Staking::current_era_total_reward(), 80_000_000 * MILLICENTS / 10); +//// assert_eq!(Staking::current_era_total_reward(), 80_000_000 * COIN / 10); //// start_era(20); //// assert_eq!(Staking::epoch_index(), 2); -//// assert_eq!(Staking::current_era_total_reward(), 9_999_988_266 * MILLICENTS / 1000); +//// assert_eq!(Staking::current_era_total_reward(), 9_999_988_266 * COIN / 1000); //// }); ////} //#[test] @@ -689,9 +683,9 @@ fn punished_unbond_should_work() { //#[test] //fn set_controller_should_not_change_ledger() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// assert_eq!(Staking::ledger(&10).unwrap().active_ring, 100 * MILLICENTS); +// assert_eq!(Staking::ledger(&10).unwrap().active_ring, 100 * COIN); // assert_ok!(Staking::set_controller(Origin::signed(11), 12)); -// assert_eq!(Staking::ledger(&12).unwrap().active_ring, 100 * MILLICENTS); +// assert_eq!(Staking::ledger(&12).unwrap().active_ring, 100 * COIN); // }); //} // @@ -705,29 +699,29 @@ fn punished_unbond_should_work() { // old_ledger.active_ring, // old_ledger.active_deposit_ring // ), -// (100 * MILLICENTS, 100 * MILLICENTS) +// (100 * COIN, 100 * COIN) // ); // // assert_ok!(Staking::bond_extra( // Origin::signed(11), -// StakingBalance::Ring(100 * MILLICENTS), +// StakingBalance::Ring(100 * COIN), // 0 // )); -// Kton::deposit_creating(&11, 10 * MILLICENTS); +// Kton::deposit_creating(&11, 10 * COIN); // assert_ok!(Staking::bond_extra( // Origin::signed(11), -// StakingBalance::Kton(10 * MILLICENTS), +// StakingBalance::Kton(10 * COIN), // 0 // )); // -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * MILLICENTS))); +// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); // let new_ledger = Staking::ledger(&10).unwrap(); // assert_eq!( // ( // new_ledger.active_ring, // new_ledger.active_deposit_ring // ), -// (190 * MILLICENTS, 100 * MILLICENTS) +// (190 * COIN, 100 * COIN) // ); // // // slash 100% @@ -739,7 +733,7 @@ fn punished_unbond_should_work() { // // 10Ring in unbondings // (0, 0) // ); -// assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * MILLICENTS)); +// assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * COIN)); // }); //} // @@ -751,7 +745,7 @@ fn punished_unbond_should_work() { // Staking::bond( // Origin::signed(stash), // controller, -// StakingBalance::Ring(MILLICENTS), +// StakingBalance::Ring(COIN), // RewardDestination::Stash, // 37 // ), @@ -760,7 +754,7 @@ fn punished_unbond_should_work() { // // gen_paired_account!(stash(123), controller(456), promise_month(12)); // assert_err!( -// Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(MILLICENTS), 37), +// Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(COIN), 37), // "months at most is 36." // ); // }); @@ -774,7 +768,7 @@ fn punished_unbond_should_work() { // Staking::bond( // Origin::signed(11), // unpaired_controller, -// StakingBalance::Ring(MILLICENTS), +// StakingBalance::Ring(COIN), // RewardDestination::Stash, // 0 // ), @@ -784,7 +778,7 @@ fn punished_unbond_should_work() { // Staking::bond( // Origin::signed(unpaired_stash), // 10, -// StakingBalance::Ring(MILLICENTS), +// StakingBalance::Ring(COIN), // RewardDestination::Stash, // 0 // ), @@ -802,54 +796,54 @@ fn punished_unbond_should_work() { // // bond: 100COIN // gen_paired_account!(stash_1(111), controller_1(222), 0); // gen_paired_account!(stash_2(333), controller_2(444), promise_month(12)); -// ring_pool += 100 * MILLICENTS; -// kton_pool += 100 * MILLICENTS; +// ring_pool += 100 * COIN; +// kton_pool += 100 * COIN; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // // // unbond: 50Ring 50Kton // assert_ok!(Staking::unbond( // Origin::signed(controller_1), -// StakingBalance::Ring(50 * MILLICENTS) +// StakingBalance::Ring(50 * COIN) // )); // assert_ok!(Staking::unbond( // Origin::signed(controller_1), -// StakingBalance::Kton(25 * MILLICENTS) +// StakingBalance::Kton(25 * COIN) // )); // // not yet expired: promise for 12 months // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Ring(50 * MILLICENTS) +// StakingBalance::Ring(50 * COIN) // )); // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Kton(25 * MILLICENTS) +// StakingBalance::Kton(25 * COIN) // )); -// ring_pool -= 50 * MILLICENTS; -// kton_pool -= 50 * MILLICENTS; +// ring_pool -= 50 * COIN; +// kton_pool -= 50 * COIN; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // // // unbond with punish: 12.5Ring // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller_2), -// 125 * MILLICENTS / 10, +// 125 * COIN / 10, // promise_month * MONTH_IN_SECONDS as u64 // )); // // unbond deposit items: 12.5Ring // Timestamp::set_timestamp(promise_month * MONTH_IN_SECONDS as u64); // assert_ok!(Staking::unbond( // Origin::signed(controller_2), -// StakingBalance::Ring(125 * MILLICENTS / 10) +// StakingBalance::Ring(125 * COIN / 10) // )); -// ring_pool -= 25 * MILLICENTS; +// ring_pool -= 25 * COIN; // assert_eq!(Staking::ring_pool(), ring_pool); // // // slash: 25Ring 50Kton // Staking::slash_validator(&stash_1, 1_000_000_000); // Staking::slash_validator(&stash_2, 1_000_000_000); -// ring_pool -= 25 * MILLICENTS; -// kton_pool -= 50 * MILLICENTS; +// ring_pool -= 25 * COIN; +// kton_pool -= 50 * COIN; // assert_eq!(Staking::ring_pool(), ring_pool); // assert_eq!(Staking::kton_pool(), kton_pool); // }); @@ -864,7 +858,7 @@ fn punished_unbond_should_work() { // for _ in 1..deposit_items_len { // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(MILLICENTS), +// StakingBalance::Ring(COIN), // promise_month // )); // } @@ -877,7 +871,7 @@ fn punished_unbond_should_work() { // Timestamp::set_timestamp(promise_month as u64 * MONTH_IN_SECONDS as u64); // // for _ in 1..deposit_items_len { -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); // } // { // let ledger = Staking::ledger(&controller).unwrap(); @@ -887,7 +881,7 @@ fn punished_unbond_should_work() { // assert_err!( // Staking::unbond( // Origin::signed(controller), -// StakingBalance::Ring((deposit_items_len - 1) as u64 * MILLICENTS) +// StakingBalance::Ring((deposit_items_len - 1) as u64 * COIN) // ), // "can not schedule more unlock chunks" // ); @@ -901,34 +895,34 @@ fn punished_unbond_should_work() { // { // let stash = 444; // let controller = 555; -// let _ = Ring::deposit_creating(&stash, 100 * MILLICENTS); -// Kton::deposit_creating(&stash, 100 * MILLICENTS); +// let _ = Ring::deposit_creating(&stash, 100 * COIN); +// Kton::deposit_creating(&stash, 100 * COIN); // // assert_ok!(Staking::bond( // Origin::signed(stash), // controller, -// StakingBalance::Ring(50 * MILLICENTS), +// StakingBalance::Ring(50 * COIN), // RewardDestination::Stash, // 0 // )); // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Kton(50 * MILLICENTS), +// StakingBalance::Kton(50 * COIN), // 0 // )); // // let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; // -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(MILLICENTS), +// value: StakingBalance::Ring(COIN), // era: 3, // is_time_deposit: false, // }); // assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(MILLICENTS))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(COIN))); // unbondings.push(NormalLock { -// value: StakingBalance::Kton(MILLICENTS), +// value: StakingBalance::Kton(COIN), // era: 3, // is_time_deposit: false, // }); @@ -956,13 +950,13 @@ fn punished_unbond_should_work() { // // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(50 * MILLICENTS), +// StakingBalance::Ring(50 * COIN), // 36 // )); // // let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; // -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(MILLICENTS))); +// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); // unbondings.push(NormalLock { // value: StakingBalance::Ring(0), // era: 3, @@ -973,11 +967,11 @@ fn punished_unbond_should_work() { // for month in [12, 36].iter() { // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 20 * MILLICENTS, +// 20 * COIN, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(20 * MILLICENTS), +// value: StakingBalance::Ring(20 * COIN), // era: 3, // is_time_deposit: true, // }); @@ -985,11 +979,11 @@ fn punished_unbond_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 29 * MILLICENTS, +// 29 * COIN, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(29 * MILLICENTS), +// value: StakingBalance::Ring(29 * COIN), // era: 3, // is_time_deposit: true, // }); @@ -997,11 +991,11 @@ fn punished_unbond_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 50 * MILLICENTS, +// 50 * COIN, // month * MONTH_IN_SECONDS as u64 // )); // unbondings.push(NormalLock { -// value: StakingBalance::Ring(1 * MILLICENTS), +// value: StakingBalance::Ring(1 * COIN), // era: 3, // is_time_deposit: true, // }); @@ -1029,17 +1023,17 @@ fn punished_unbond_should_work() { // // assert_ok!(Staking::bond_extra( // Origin::signed(stash), -// StakingBalance::Ring(5 * MILLICENTS), +// StakingBalance::Ring(5 * COIN), // 0 // )); // for _ in 0..expired_item_len { -// assert_ok!(Staking::promise_extra(Origin::signed(controller), MILLICENTS, promise_month)); +// assert_ok!(Staking::promise_extra(Origin::signed(controller), COIN, promise_month)); // } // // Timestamp::set_timestamp(expiry_date - 1); // assert_ok!(Staking::promise_extra( // Origin::signed(controller), -// 2 * MILLICENTS, +// 2 * COIN, // promise_month // )); // assert_eq!( @@ -1050,7 +1044,7 @@ fn punished_unbond_should_work() { // Timestamp::set_timestamp(expiry_date); // assert_ok!(Staking::promise_extra( // Origin::signed(controller), -// 2 * MILLICENTS, +// 2 * COIN, // promise_month // )); // assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 2); @@ -1061,7 +1055,7 @@ fn punished_unbond_should_work() { //fn unbond_zero_before_expiry() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // let expiry_date = 12 * MONTH_IN_SECONDS as u64; -// let unbond_value = StakingBalance::Ring(MILLICENTS); +// let unbond_value = StakingBalance::Ring(COIN); // // Timestamp::set_timestamp(expiry_date - 1); // assert_ok!(Staking::unbond(Origin::signed(10), unbond_value.clone())); @@ -1110,7 +1104,7 @@ fn punished_unbond_should_work() { // // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 10_000 * MILLICENTS, +// 10_000 * COIN, // 12 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(Kton::free_balance(&stash), 1); @@ -1134,7 +1128,7 @@ fn punished_unbond_should_work() { // // not enough Kton to unbond // assert_ok!(Staking::unbond_with_punish( // Origin::signed(controller), -// 10_000 * MILLICENTS, +// 10_000 * COIN, // 36 * MONTH_IN_SECONDS as u64 // )); // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); @@ -1174,8 +1168,8 @@ fn punished_unbond_should_work() { // if with_new_era { // start_era(2); // } -// Staking::reward_validator(&validator_1_stash, 1000 * MILLICENTS); -// Staking::reward_validator(&validator_2_stash, 1000 * MILLICENTS); +// Staking::reward_validator(&validator_1_stash, 1000 * COIN); +// Staking::reward_validator(&validator_2_stash, 1000 * COIN); // // balance = Ring::free_balance(&nominator_stash); // }); From af5d88da9934054745445c7663dafc6d7a9b5223 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 09:53:33 +0800 Subject: [PATCH 09/45] remove: useless import --- srml/kton/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/srml/kton/src/lib.rs b/srml/kton/src/lib.rs index 4c5a6f10a..ac4281aaf 100644 --- a/srml/kton/src/lib.rs +++ b/srml/kton/src/lib.rs @@ -1,8 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std)] use codec::{Codec, Decode, Encode}; -#[cfg(not(feature = "std"))] -use rstd::borrow::ToOwned; use rstd::{cmp, fmt::Debug, prelude::*, result}; #[cfg(feature = "std")] use sr_primitives::traits::One; From e7064cc591218b2ddc451ad00de658105901612a Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 09:53:59 +0800 Subject: [PATCH 10/45] remove: useless lifetime bound --- srml/staking/src/inflation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/srml/staking/src/inflation.rs b/srml/staking/src/inflation.rs index 92e0be19b..ef378aae6 100644 --- a/srml/staking/src/inflation.rs +++ b/srml/staking/src/inflation.rs @@ -9,7 +9,7 @@ use substrate_primitives::U256; // 1 - (99 /100)^sqrt(year) // () -> RingBalanceOf -pub fn compute_total_payout( +pub fn compute_total_payout( era_duration: u64, living_time: u64, total_left: u128, @@ -48,7 +48,7 @@ pub fn compute_total_payout( // consistent with the formula in smart contract in evolution land which can be found in // https://github.com/evolutionlandorg/bank/blob/master/contracts/GringottsBank.sol#L280 -pub fn compute_kton_return(value: RingBalanceOf, months: u32) -> KtonBalanceOf { +pub fn compute_kton_return(value: RingBalanceOf, months: u32) -> KtonBalanceOf { let value = value.saturated_into::(); let no = U256::from(67).pow(U256::from(months)); let de = U256::from(66).pow(U256::from(months)); From 2f6ff7517421f25f5307b87205b4b71af3d9133b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 09:54:08 +0800 Subject: [PATCH 11/45] hanging: backup --- srml/staking/src/lib.rs | 84 ++++++++--------- srml/staking/src/tests.rs | 194 ++++++++++++++------------------------ 2 files changed, 111 insertions(+), 167 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 4cc451ca7..7a21a7d46 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -24,7 +24,7 @@ extern crate test; pub mod inflation; use codec::{Decode, Encode, HasCompact}; -use rstd::{prelude::*, result}; +use rstd::{convert::TryInto, prelude::*, result}; use session::{historical::OnSessionEnding, SelectInitialValidators}; use sr_primitives::{ traits::{CheckedSub, Convert, One, SaturatedConversion, Saturating, StaticLookup, Zero}, @@ -45,8 +45,6 @@ use system::{ensure_root, ensure_signed}; use darwinia_support::{LockIdentifier, LockableCurrency, NormalLock, StakingLock, TimeStamp, WithdrawLock}; use phragmen::{build_support_map, elect, equalize, ExtendedBalance, PhragmenStakedAssignment}; -use core::convert::TryInto; - #[allow(unused)] #[cfg(any(feature = "bench", test))] mod mock; @@ -627,6 +625,10 @@ decl_module! { fn claim_deposits_with_punish(origin, expire_time: T::Moment) { let controller = ensure_signed(origin)?; let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; + + let now = >::now(); + ensure!(expire_time > now, "use unbond instead."); + let StakingLedger { stash, active_deposit_ring, @@ -634,49 +636,47 @@ decl_module! { .. } = &mut ledger; - let now = >::now(); - - ensure!(expire_time > now, "use unbond instead."); - deposit_items.retain(|item| { - if item.expire_time == expire_time { - let passed_duration = - (now - item.start_time).saturated_into::() - / MONTH_IN_SECONDS - ; - - let plan_duration = - (item.expire_time - item.start_time).saturated_into::() - / MONTH_IN_SECONDS - ; - - let kton_slash = (inflation::compute_kton_return::(item.value, plan_duration) - inflation::compute_kton_return::(item.value, passed_duration)) * 3.into(); - - // check total free balance and locked one - // strict on punishing in kton - if T::Kton::free_balance(stash).checked_sub(&kton_slash).and_then( - |new_balance| { - T::Kton::ensure_can_withdraw( - stash, - kton_slash, - WithdrawReason::Transfer.into(), - new_balance - ).ok() - } - ) - .is_some() - { - *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); - - let (imbalance, _) = T::Kton::slash(stash, kton_slash); - T::KtonSlash::on_unbalanced(imbalance); - - return false; - } + if item.expire_time != expire_time { + return true; } - true + let kton_slash = { + let passed_duration = (now - item.start_time).saturated_into::() / MONTH_IN_SECONDS; + let plan_duration = (item.expire_time - item.start_time).saturated_into::() / MONTH_IN_SECONDS; + + ( + inflation::compute_kton_return::(item.value, plan_duration) + - + inflation::compute_kton_return::(item.value, passed_duration) + ) * 3.into() + }; + // check total free balance and locked one + // strict on punishing in kton + if T::Kton::free_balance(stash) + .checked_sub(&kton_slash) + .and_then(|new_balance| { + T::Kton::ensure_can_withdraw( + stash, + kton_slash, + WithdrawReason::Transfer.into(), + new_balance + ).ok() + }) + .is_some() + { + *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); + + let (imbalance, _) = T::Kton::slash(stash, kton_slash); + T::KtonSlash::on_unbalanced(imbalance); + + false + } else { + true + } }); + + >::insert(&controller, ledger); } fn validate(origin, name: Vec, ratio: u32, unstake_threshold: u32) { diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 6b0a8701e..9b3ab2512 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -85,16 +85,13 @@ fn test_env_build() { deposit_items: vec![TimeDepositItem { value: 100 * COIN, start_time: 0, - expire_time: 12 * MONTH_IN_SECONDS as u64 + expire_time: (12 * MONTH_IN_SECONDS) as _, }], ring_staking_lock: StakingLock { staking_amount: 100 * COIN, - unbondings: vec![] - }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] + unbondings: vec![], }, + kton_staking_lock: Default::default(), } ); @@ -106,7 +103,7 @@ fn test_env_build() { assert_ok!(Staking::bond_extra( Origin::signed(11), StakingBalance::Ring(20 * COIN), - 13 + 13, )); assert_eq!( Staking::ledger(&10).unwrap(), @@ -119,22 +116,19 @@ fn test_env_build() { TimeDepositItem { value: 100 * COIN, start_time: 0, - expire_time: 12 * MONTH_IN_SECONDS as u64 + expire_time: (12 * MONTH_IN_SECONDS) as _, }, TimeDepositItem { value: 20 * COIN, start_time: 0, - expire_time: 13 * MONTH_IN_SECONDS as u64 - } + expire_time: (13 * MONTH_IN_SECONDS) as _, + }, ], ring_staking_lock: StakingLock { staking_amount: origin_ledger.active_ring + 20 * COIN, - unbondings: vec![] - }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] + unbondings: vec![], }, + kton_staking_lock: Default::default(), } ); }); @@ -149,7 +143,7 @@ fn normal_kton_should_work() { 1000, StakingBalance::Kton(10 * COIN), RewardDestination::Stash, - 0 + 0, )); assert_eq!( Staking::ledger(&1000).unwrap(), @@ -159,13 +153,10 @@ fn normal_kton_should_work() { active_deposit_ring: 0, active_kton: 10 * COIN, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 10 * COIN, - unbondings: vec![] + unbondings: vec![], }, } ); @@ -177,7 +168,7 @@ fn normal_kton_should_work() { staking_amount: 10 * COIN, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); @@ -188,7 +179,7 @@ fn normal_kton_should_work() { 2000, StakingBalance::Kton(10 * COIN), RewardDestination::Stash, - 12 + 12, )); assert_eq!( Staking::ledger(&2000).unwrap(), @@ -198,13 +189,10 @@ fn normal_kton_should_work() { active_deposit_ring: 0, active_kton: 10 * COIN, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 10 * COIN, - unbondings: vec![] + unbondings: vec![], }, } ); @@ -212,7 +200,7 @@ fn normal_kton_should_work() { } #[test] -fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { +fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { { let locks = vec![BalanceLock { @@ -231,16 +219,13 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { deposit_items: vec![TimeDepositItem { value: 100 * COIN, start_time: 0, - expire_time: 12 * MONTH_IN_SECONDS as u64, + expire_time: (12 * MONTH_IN_SECONDS) as _, }], ring_staking_lock: StakingLock { staking_amount: 100 * COIN, unbondings: vec![], }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![], - }, + kton_staking_lock: Default::default(), }; assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); @@ -252,7 +237,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { assert_eq!(Staking::ledger(&10).unwrap(), ledger); } - let (unbond_start, unbond_value) = (13 * MONTH_IN_SECONDS as u64, 10 * COIN); + let (unbond_start, unbond_value) = ((13 * MONTH_IN_SECONDS) as _, 10 * COIN); Timestamp::set_timestamp(unbond_start); assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(unbond_value))); @@ -267,7 +252,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { until: unbond_start + BondingDuration::get(), }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); assert_eq!( @@ -285,10 +270,7 @@ fn time_deposit_ring_unbond_and_withdraw_automactically_should_work() { until: unbond_start + BondingDuration::get(), }], }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![], - }, + kton_staking_lock: Default::default(), } ); @@ -338,7 +320,7 @@ fn normal_unbond_should_work() { ledger.deposit_items.push(TimeDepositItem { value, start_time: 0, - expire_time: (promise_month * MONTH_IN_SECONDS) as u64, + expire_time: (promise_month * MONTH_IN_SECONDS) as _, }); ledger.ring_staking_lock.staking_amount += value; assert_eq!(Staking::ledger(&controller).unwrap(), ledger); @@ -376,16 +358,30 @@ fn normal_unbond_should_work() { } #[test] -fn punished_unbond_should_work() { +fn punished_claim_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { let stash = 1001; let controller = 1000; let promise_month = 36; let _ = Ring::deposit_creating(&stash, 100 * COIN); Kton::deposit_creating(&stash, COIN / 100000); + let mut ledger = StakingLedger { + stash, + active_ring: 10 * COIN, + active_deposit_ring: 10 * COIN, + active_kton: 0, + deposit_items: vec![TimeDepositItem { + value: 10 * COIN, + start_time: 0, + expire_time: (promise_month * MONTH_IN_SECONDS) as _, + }], + ring_staking_lock: StakingLock { + staking_amount: 10 * COIN, + unbondings: vec![], + }, + kton_staking_lock: Default::default(), + }; - // timestamp now is 0. - // free balance of kton is too low to work assert_ok!(Staking::bond( Origin::signed(stash), controller, @@ -393,55 +389,27 @@ fn punished_unbond_should_work() { RewardDestination::Stash, promise_month, )); - // assert_eq!( - // Staking::ledger(&controller), - // Some(StakingLedger { - // stash, - // total_deposit_ring: 10 * COIN, - // active_deposit_ring: 10 * COIN, - // active_ring: 10 * COIN, - // active_kton: 0, - // deposit_items: vec![TimeDepositItem { - // value: 10 * COIN, - // start_time: 0, - // expire_time: promise_month as u64 * MONTH_IN_SECONDS as u64 - // }], // should be cleared - // unbondings: vec![] - // }) - // ); - // let mut ledger = Staking::ledger(&controller).unwrap(); - // let kton_free_balance = Kton::free_balance(&stash); - // // kton is 0, skip unbond_with_punish - // assert_ok!(Staking::unbond_with_punish( - // Origin::signed(controller), - // 10 * COIN, - // promise_month as u64 * MONTH_IN_SECONDS as u64 - // )); - // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); - // assert_eq!(Kton::free_balance(&stash), kton_free_balance); - // - // // set more kton balance to make it work - // Kton::deposit_creating(&stash, 10 * COIN); - // let kton_free_balance = Kton::free_balance(&stash); - // let unbond_value = 5 * COIN; - // assert_ok!(Staking::unbond_with_punish( - // Origin::signed(controller), - // unbond_value, - // promise_month as u64 * MONTH_IN_SECONDS as u64 - // )); - // ledger.active_ring -= unbond_value; - // ledger.active_deposit_ring -= unbond_value; - // ledger.deposit_items[0].value -= unbond_value; - // ledger.unbondings = vec![NormalLock { - // value: StakingBalance::Ring(unbond_value), - // era: 3, - // is_time_deposit: true, - // }]; - // assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); - // - // let kton_punishment = inflation::compute_kton_return::(unbond_value, promise_month); - // assert_eq!(Kton::free_balance(&stash), kton_free_balance - 3 * kton_punishment); - // + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + // kton is 0, skip unbond_with_punish + assert_ok!(Staking::claim_deposits_with_punish( + Origin::signed(controller), + (promise_month * MONTH_IN_SECONDS) as _, + )); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + + // set more kton balance to make it work + Kton::deposit_creating(&stash, 10 * COIN); + let kton_free_balance = Kton::free_balance(&stash); + let kton_punishment = inflation::compute_kton_return::(10 * COIN, promise_month); + assert_ok!(Staking::claim_deposits_with_punish( + Origin::signed(controller), + (promise_month * MONTH_IN_SECONDS) as _, + )); + ledger.active_deposit_ring -= 10 * COIN; + ledger.deposit_items.clear(); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + assert_eq!(Kton::free_balance(&stash), kton_free_balance - 3 * kton_punishment); + // // if deposit_item.value == 0 // // the whole item should be be dropped // assert_ok!(Staking::unbond_with_punish( @@ -1318,10 +1286,7 @@ fn xavier_q1() { active_deposit_ring: 0, active_kton: 20, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 20, unbondings: vec![NormalLock { @@ -1478,10 +1443,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }] }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + kton_staking_lock: Default::default(), } ); // println!("Unlocking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1927,10 +1889,7 @@ fn xavier_q3() { active_deposit_ring: 0, active_kton: 5, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 5, unbondings: vec![] @@ -1950,10 +1909,7 @@ fn xavier_q3() { active_deposit_ring: 0, active_kton: 0, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 0, unbondings: vec![NormalLock { amount: 5, until: 61 }] @@ -1975,10 +1931,7 @@ fn xavier_q3() { active_deposit_ring: 0, active_kton: 1, deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 1, unbondings: vec![NormalLock { amount: 5, until: 61 }] @@ -2016,10 +1969,7 @@ fn xavier_q3() { staking_amount: 5, unbondings: vec![] }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + kton_staking_lock: Default::default(), } ); // println!("Locks: {:#?}", Ring::locks(stash)); @@ -2039,10 +1989,7 @@ fn xavier_q3() { staking_amount: 0, unbondings: vec![NormalLock { amount: 5, until: 61 }] }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + kton_staking_lock: Default::default(), } ); // println!("Locks: {:#?}", Ring::locks(stash)); @@ -2064,10 +2011,7 @@ fn xavier_q3() { staking_amount: 1, unbondings: vec![NormalLock { amount: 5, until: 61 }] }, - kton_staking_lock: StakingLock { - staking_amount: 0, - unbondings: vec![] - }, + kton_staking_lock: Default::default(), } ); // println!("Locks: {:#?}", Ring::locks(stash)); From 7202e661a0cbad743dcea1f727ead49338dffc20 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 10:14:12 +0800 Subject: [PATCH 12/45] fix: `punished_claim_should_work` --- srml/staking/src/tests.rs | 9 --------- 1 file changed, 9 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 9b3ab2512..9c003f3fd 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -409,15 +409,6 @@ fn punished_claim_should_work() { ledger.deposit_items.clear(); assert_eq!(Staking::ledger(&controller).unwrap(), ledger); assert_eq!(Kton::free_balance(&stash), kton_free_balance - 3 * kton_punishment); - - // // if deposit_item.value == 0 - // // the whole item should be be dropped - // assert_ok!(Staking::unbond_with_punish( - // Origin::signed(controller), - // 5 * COIN, - // promise_month as u64 * MONTH_IN_SECONDS as u64 - // )); - // assert!(Staking::ledger(&controller).unwrap().deposit_items.is_empty()); }); } From 928a1aa60516811a35ce4ea9c053c3e7c149937d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 10:34:36 +0800 Subject: [PATCH 13/45] remove: clone on copy, useless field --- srml/staking/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 7a21a7d46..b8ed0b8bf 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -529,7 +529,6 @@ decl_module! { let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; let StakingLedger { - stash: _, active_ring, active_deposit_ring, active_kton, @@ -911,7 +910,7 @@ impl Module { journal.push(SlashJournalEntry { who: stash.clone(), - own_slash: own_slash.clone(), + own_slash, amount: slash, }); From 3f67e3a85ee774bfc15feb0232f9894ce1193396 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 12:02:28 +0800 Subject: [PATCH 14/45] hanging: bakcup --- srml/staking/src/lib.rs | 60 ++++--- srml/staking/src/tests.rs | 337 ++++++++++++++++++++------------------ 2 files changed, 203 insertions(+), 194 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index b8ed0b8bf..d01a836bd 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -450,25 +450,25 @@ decl_module! { fn deposit_event() = default; - fn bond(origin, + fn bond( + origin, controller: ::Source, value: StakingBalance, KtonBalanceOf>, payee: RewardDestination, promise_month: u32 ) { let stash = ensure_signed(origin)?; - ensure!(promise_month <= 36, "months at most is 36."); - if >::exists(&stash) { return Err("stash already bonded") } let controller = T::Lookup::lookup(controller)?; - if >::exists(&controller) { return Err("controller already paired") } + ensure!(promise_month <= 36, "months at most is 36."); + >::insert(&stash, &controller); >::insert(&stash, payee); @@ -491,14 +491,17 @@ decl_module! { } } - fn bond_extra(origin, + fn bond_extra( + origin, value: StakingBalance, KtonBalanceOf>, promise_month: u32 ) { let stash = ensure_signed(origin)?; - ensure!(promise_month <= 36, "months at most is 36."); let controller = Self::bonded(&stash).ok_or("not a stash")?; let ledger = Self::ledger(&controller).ok_or("not a controller")?; + + ensure!(promise_month <= 36, "months at most is 36."); + match value { StakingBalance::Ring(r) => { let stash_balance = T::Ring::free_balance(&stash); @@ -580,40 +583,35 @@ decl_module! { /// called by controller fn deposit_extra(origin, value: RingBalanceOf, promise_month: u32) { let controller = ensure_signed(origin)?; - - ensure!(promise_month <= 36, "months at most is 36."); let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; + + ensure!(promise_month >= 3 && promise_month <= 36, "months at least is 3 and at most is 36."); + + Self::clear_mature_deposits(&controller); + + let now = >::now(); let StakingLedger { + stash, active_ring, active_deposit_ring, deposit_items, - stash, .. } = &mut ledger; - - Self::clear_mature_deposits(&controller); - let value = value.min(*active_ring - *active_deposit_ring); // active_normal_ring + // for now, kton_return is free + // mint kton + let kton_return = inflation::compute_kton_return::(value, promise_month); + let kton_positive_imbalance = T::Kton::deposit_creating(stash, kton_return); + T::KtonReward::on_unbalanced(kton_positive_imbalance); - let now = >::now(); + *active_deposit_ring += value; + deposit_items.push(TimeDepositItem { + value, + start_time: now, + expire_time: now + (MONTH_IN_SECONDS * promise_month).into(), + }); - if promise_month >= 3 { - // update active_deposit_ring - *active_deposit_ring += value; - - // for now, kton_return is free - // mint kton - let kton_return = inflation::compute_kton_return::(value, promise_month); - let kton_positive_imbalance = T::Kton::deposit_creating(stash, kton_return); - T::KtonReward::on_unbalanced(kton_positive_imbalance); - - let expire_time = now + (MONTH_IN_SECONDS * promise_month).into(); - deposit_items.push(TimeDepositItem { - value, - start_time: now, - expire_time, - }); - } + >::insert(&controller, ledger); } fn claim_mature_deposits(origin) { @@ -688,7 +686,7 @@ decl_module! { ); // at most 100% let ratio = Perbill::from_percent(ratio.min(100)); - let prefs = ValidatorPrefs {unstake_threshold: unstake_threshold, validator_payment_ratio: ratio }; + let prefs = ValidatorPrefs { unstake_threshold: unstake_threshold, validator_payment_ratio: ratio }; >::remove(stash); >::insert(stash, prefs); diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 9c003f3fd..271716bae 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -74,11 +74,13 @@ fn test_env_build() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { check_exposure_all(); - assert_eq!(Staking::bonded(&11), Some(10)); + let (stash, controller) = (11, 10); + + assert_eq!(Staking::bonded(&stash), Some(controller)); assert_eq!( - Staking::ledger(&10).unwrap(), + Staking::ledger(&controller).unwrap(), StakingLedger { - stash: 11, + stash, active_ring: 100 * COIN, active_deposit_ring: 100 * COIN, active_kton: 0, @@ -98,17 +100,17 @@ fn test_env_build() { assert_eq!(Kton::free_balance(&11), COIN / 100); assert_eq!(Kton::total_issuance(), 16 * COIN / 100); - let origin_ledger = Staking::ledger(&10).unwrap(); - let _ = Ring::deposit_creating(&11, 100 * COIN); + let origin_ledger = Staking::ledger(&controller).unwrap(); + let _ = Ring::deposit_creating(&stash, 100 * COIN); assert_ok!(Staking::bond_extra( - Origin::signed(11), + Origin::signed(stash), StakingBalance::Ring(20 * COIN), 13, )); assert_eq!( - Staking::ledger(&10).unwrap(), + Staking::ledger(&controller).unwrap(), StakingLedger { - stash: 11, + stash, active_ring: origin_ledger.active_ring + 20 * COIN, active_deposit_ring: origin_ledger.active_deposit_ring + 20 * COIN, active_kton: 0, @@ -137,71 +139,81 @@ fn test_env_build() { #[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, - active_ring: 0, - active_deposit_ring: 0, - active_kton: 10 * COIN, - deposit_items: vec![], - ring_staking_lock: Default::default(), - kton_staking_lock: StakingLock { - staking_amount: 10 * COIN, - unbondings: vec![], - }, - } - ); - assert_eq!( - Kton::locks(&1001), - vec![BalanceLock { - id: STAKING_ID, - withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 10 * COIN, - unbondings: vec![], - }), - reasons: WithdrawReasons::all(), - }] - ); + { + let (stash, controller) = (1001, 1000); - // 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, - active_ring: 0, - active_deposit_ring: 0, - active_kton: 10 * COIN, - deposit_items: vec![], - ring_staking_lock: Default::default(), - kton_staking_lock: StakingLock { - staking_amount: 10 * COIN, - unbondings: vec![], - }, - } - ); + Kton::deposit_creating(&stash, 10 * COIN); + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Kton(10 * COIN), + RewardDestination::Stash, + 0, + )); + assert_eq!( + Staking::ledger(&controller).unwrap(), + StakingLedger { + stash, + active_ring: 0, + active_deposit_ring: 0, + active_kton: 10 * COIN, + deposit_items: vec![], + ring_staking_lock: Default::default(), + kton_staking_lock: StakingLock { + staking_amount: 10 * COIN, + unbondings: vec![], + }, + } + ); + assert_eq!( + Kton::locks(&stash), + vec![BalanceLock { + id: STAKING_ID, + withdraw_lock: WithdrawLock::WithStaking(StakingLock { + staking_amount: 10 * COIN, + unbondings: vec![], + }), + reasons: WithdrawReasons::all(), + }] + ); + } + + { + let (stash, controller) = (2001, 2000); + + // promise_month should not work for kton + Kton::deposit_creating(&stash, 10 * COIN); + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Kton(10 * COIN), + RewardDestination::Stash, + 12, + )); + assert_eq!( + Staking::ledger(&controller).unwrap(), + StakingLedger { + stash, + active_ring: 0, + active_deposit_ring: 0, + active_kton: 10 * COIN, + deposit_items: vec![], + ring_staking_lock: Default::default(), + kton_staking_lock: StakingLock { + staking_amount: 10 * COIN, + unbondings: vec![], + }, + } + ); + } }); } #[test] fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let (stash, controller) = (11, 10); + { let locks = vec![BalanceLock { id: STAKING_ID, @@ -212,7 +224,7 @@ fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() { reasons: WithdrawReasons::all(), }]; let ledger = StakingLedger { - stash: 11, + stash, active_ring: 100 * COIN, active_deposit_ring: 100 * COIN, active_kton: 0, @@ -228,76 +240,86 @@ fn time_deposit_ring_unbond_and_withdraw_automatically_should_work() { kton_staking_lock: Default::default(), }; - assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); - assert_eq!(Ring::locks(11), locks); - assert_eq!(Staking::ledger(&10).unwrap(), ledger,); + assert_ok!(Staking::unbond( + Origin::signed(controller), + StakingBalance::Ring(10 * COIN) + )); + assert_eq!(Ring::locks(stash), locks); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger,); - assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(120 * COIN))); - assert_eq!(Ring::locks(11), locks); - assert_eq!(Staking::ledger(&10).unwrap(), ledger); + assert_ok!(Staking::unbond( + Origin::signed(controller), + StakingBalance::Ring(120 * COIN) + )); + assert_eq!(Ring::locks(stash), locks); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); } - let (unbond_start, unbond_value) = ((13 * MONTH_IN_SECONDS) as _, 10 * COIN); - Timestamp::set_timestamp(unbond_start); + { + let (unbond_start, unbond_value) = ((13 * MONTH_IN_SECONDS) as _, 10 * COIN); + Timestamp::set_timestamp(unbond_start); - assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(unbond_value))); - assert_eq!( - Ring::locks(11), - vec![BalanceLock { - id: STAKING_ID, - withdraw_lock: WithdrawLock::WithStaking(StakingLock { - staking_amount: 100 * COIN - unbond_value, - unbondings: vec![NormalLock { - amount: unbond_value, - until: unbond_start + BondingDuration::get(), - }], - }), - reasons: WithdrawReasons::all(), - }] - ); - assert_eq!( - Staking::ledger(&10).unwrap(), - StakingLedger { - stash: 11, - active_ring: 100 * COIN - unbond_value, - active_deposit_ring: 0, - active_kton: 0, - deposit_items: vec![], - ring_staking_lock: StakingLock { - staking_amount: 100 * COIN - unbond_value, - unbondings: vec![NormalLock { - amount: unbond_value, - until: unbond_start + BondingDuration::get(), - }], - }, - kton_staking_lock: Default::default(), - } - ); + assert_ok!(Staking::unbond( + Origin::signed(controller), + StakingBalance::Ring(unbond_value) + )); + assert_eq!( + Ring::locks(stash), + vec![BalanceLock { + id: STAKING_ID, + withdraw_lock: WithdrawLock::WithStaking(StakingLock { + staking_amount: 100 * COIN - unbond_value, + unbondings: vec![NormalLock { + amount: unbond_value, + until: unbond_start + BondingDuration::get(), + }], + }), + reasons: WithdrawReasons::all(), + }] + ); + assert_eq!( + Staking::ledger(&controller).unwrap(), + StakingLedger { + stash, + active_ring: 100 * COIN - unbond_value, + active_deposit_ring: 0, + active_kton: 0, + deposit_items: vec![], + ring_staking_lock: StakingLock { + staking_amount: 100 * COIN - unbond_value, + unbondings: vec![NormalLock { + amount: unbond_value, + until: unbond_start + BondingDuration::get(), + }], + }, + kton_staking_lock: Default::default(), + } + ); - Timestamp::set_timestamp(unbond_start + BondingDuration::get()); - assert_err!( - Ring::ensure_can_withdraw( - &11, + Timestamp::set_timestamp(unbond_start + BondingDuration::get()); + assert_err!( + Ring::ensure_can_withdraw( + &stash, + unbond_value, + WithdrawReason::Transfer.into(), + 100 * COIN - unbond_value - 1, + ), + "account liquidity restrictions prevent withdrawal" + ); + assert_ok!(Ring::ensure_can_withdraw( + &stash, unbond_value, WithdrawReason::Transfer.into(), - 100 * COIN - unbond_value - 1, - ), - "account liquidity restrictions prevent withdrawal" - ); - assert_ok!(Ring::ensure_can_withdraw( - &11, - unbond_value, - WithdrawReason::Transfer.into(), - 100 * COIN - unbond_value, - )); + 100 * COIN - unbond_value, + )); + } }); } #[test] fn normal_unbond_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - let stash = 11; - let controller = 10; + let (stash, controller) = (11, 10); let value = 200 * COIN; let promise_month = 12; let _ = Ring::deposit_creating(&stash, 1000 * COIN); @@ -360,8 +382,7 @@ fn normal_unbond_should_work() { #[test] fn punished_claim_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - let stash = 1001; - let controller = 1000; + let (stash, controller) = (1001, 1000); let promise_month = 36; let _ = Ring::deposit_creating(&stash, 100 * COIN); Kton::deposit_creating(&stash, COIN / 100000); @@ -412,43 +433,33 @@ fn punished_claim_should_work() { }); } -//#[test] -//fn transform_to_promised_ring_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); -// assert_ok!(Staking::bond( -// Origin::signed(1001), -// 1000, -// StakingBalance::Ring(10 * COIN), -// RewardDestination::Stash, -// 0 -// )); -// let origin_ledger = Staking::ledger(&1000).unwrap(); -// let kton_free_balance = Kton::free_balance(&1001); -// -// assert_ok!(Staking::promise_extra(Origin::signed(1000), 5 * COIN, 12)); -// -// assert_eq!( -// Staking::ledger(&1000), -// Some(StakingLedger { -// stash: 1001, -// total_deposit_ring: origin_ledger.total_deposit_ring + 5 * COIN, -// active_deposit_ring: origin_ledger.active_deposit_ring + 5 * COIN, -// active_ring: origin_ledger.active_ring, -// active_kton: origin_ledger.active_kton, -// deposit_items: vec![TimeDepositItem { -// value: 5 * COIN, -// start_time: 0, -// expire_time: 12 * MONTH_IN_SECONDS as u64 -// }], -// unbondings: vec![] -// }) -// ); -// -// assert_eq!(Kton::free_balance(&1001), kton_free_balance + (5 * COIN / 10000)); -// }); -//} -// +#[test] +fn transform_to_deposited_ring_should_work() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let (stash, controller) = (1001, 1000); + let _ = Ring::deposit_creating(&stash, 100 * COIN); + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(10 * COIN), + RewardDestination::Stash, + 0, + )); + let kton_free_balance = Kton::free_balance(&stash); + let mut ledger = Staking::ledger(&controller).unwrap(); + + assert_ok!(Staking::deposit_extra(Origin::signed(controller), 5 * COIN, 12)); + ledger.active_deposit_ring += 5 * COIN; + ledger.deposit_items.push(TimeDepositItem { + value: 5 * COIN, + start_time: 0, + expire_time: (12 * MONTH_IN_SECONDS) as u64, + }); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + assert_eq!(Kton::free_balance(&stash), kton_free_balance + (5 * COIN / 10000)); + }); +} + //#[test] //fn expired_ring_should_capable_to_promise_again() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From 1b780cf1f1622902002612e30d5e2c050417fd5d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 12:09:34 +0800 Subject: [PATCH 15/45] fix: `transform_to_deposited_ring_should_work` --- srml/staking/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index d01a836bd..ac5f449c8 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -602,8 +602,8 @@ decl_module! { // mint kton let kton_return = inflation::compute_kton_return::(value, promise_month); let kton_positive_imbalance = T::Kton::deposit_creating(stash, kton_return); - T::KtonReward::on_unbalanced(kton_positive_imbalance); + T::KtonReward::on_unbalanced(kton_positive_imbalance); *active_deposit_ring += value; deposit_items.push(TimeDepositItem { value, From 1c7ea9034a69e095a08b3042e7fbec9900e0835d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 12:39:02 +0800 Subject: [PATCH 16/45] hanging: backup --- srml/staking/src/tests.rs | 61 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 271716bae..e650b556a 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -460,34 +460,39 @@ fn transform_to_deposited_ring_should_work() { }); } -//#[test] -//fn expired_ring_should_capable_to_promise_again() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); -// assert_ok!(Staking::bond( -// Origin::signed(1001), -// 1000, -// StakingBalance::Ring(10 * COIN), -// RewardDestination::Stash, -// 12 -// )); -// let mut ledger = Staking::ledger(&1000).unwrap(); -// let ts = 13 * MONTH_IN_SECONDS as u64; -// let promise_extra_value = 5 * COIN; -// Timestamp::set_timestamp(ts); -// assert_ok!(Staking::promise_extra(Origin::signed(1000), promise_extra_value, 13)); -// ledger.total_deposit_ring = promise_extra_value; -// ledger.active_deposit_ring = promise_extra_value; -// // old deposit_item with 12 months promised removed -// ledger.deposit_items = vec![TimeDepositItem { -// value: promise_extra_value, -// start_time: ts, -// expire_time: 2 * ts, -// }]; -// assert_eq!(&Staking::ledger(&1000).unwrap(), &ledger); -// }); -//} -// +#[test] +fn expired_ring_should_capable_to_promise_again() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let (stash, controller) = (1001, 1000); + let _ = Ring::deposit_creating(&stash, 100 * COIN); + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(10 * COIN), + RewardDestination::Stash, + 12, + )); + let mut ledger = Staking::ledger(&controller).unwrap(); + let ts = (13 * MONTH_IN_SECONDS) as u64; + let promise_extra_value = 5 * COIN; + + Timestamp::set_timestamp(ts); + assert_ok!(Staking::deposit_extra( + Origin::signed(controller), + promise_extra_value, + 13, + )); + ledger.active_deposit_ring = promise_extra_value; + // old deposit_item with 12 months promised removed + ledger.deposit_items = vec![TimeDepositItem { + value: promise_extra_value, + start_time: ts, + expire_time: 2 * ts, + }]; + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + }); +} + ////#[test] ////fn inflation_should_be_correct() { //// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From e9bd44853adcf910dbf8510168ed384b7066dd08 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 12:50:04 +0800 Subject: [PATCH 17/45] fix: `expired_ring_should_capable_to_promise_again` --- srml/staking/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index ac5f449c8..fa97234a6 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -583,13 +583,14 @@ decl_module! { /// called by controller fn deposit_extra(origin, value: RingBalanceOf, promise_month: u32) { let controller = ensure_signed(origin)?; - let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; + if Self::ledger(&controller).is_none() { return Err("not a controller"); } ensure!(promise_month >= 3 && promise_month <= 36, "months at least is 3 and at most is 36."); Self::clear_mature_deposits(&controller); let now = >::now(); + let mut ledger = Self::ledger(&controller).unwrap(); let StakingLedger { stash, active_ring, From 48ba6c2bb9de4bb9c461065ae7a9005f89d31949 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 16:54:35 +0800 Subject: [PATCH 18/45] fix: `inflation_should_be_correct` --- srml/staking/src/tests.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index e650b556a..438ddef07 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -493,20 +493,23 @@ fn expired_ring_should_capable_to_promise_again() { }); } -////#[test] -////fn inflation_should_be_correct() { -//// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// let initial_issuance = 1_200_000_000 * COIN; -//// let surplus_needed = initial_issuance - Ring::total_issuance(); -//// let _ = Ring::deposit_into_existing(&11, surplus_needed); -//// assert_eq!(Ring::total_issuance(), initial_issuance); -//// // assert_eq!(Staking::current_era_total_reward(), 80000000 * COIN / 10); -//// start_era(11); -//// // ErasPerEpoch = 10 -//// // assert_eq!(Staking::current_era_total_reward(), 88000000 * COIN / 10); -//// }); -////} -// +#[test] +fn inflation_should_be_correct() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let initial_issuance = 1_200_000_000 * COIN; + let surplus_needed = initial_issuance - Ring::total_issuance(); + let _ = Ring::deposit_into_existing(&11, surplus_needed); + + assert_eq!(Ring::total_issuance(), initial_issuance); + // assert_eq!(Staking::current_era_total_reward(), 80000000 * COIN / 10); + // + // start_era(11); + // + // // ErasPerEpoch = 10 + // assert_eq!(Staking::current_era_total_reward(), 88000000 * COIN / 10); + }); +} + //#[test] //fn reward_should_work_correctly() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From 93cff5d72bfb50cad6bde88ac39074fad8c350e8 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Sat, 23 Nov 2019 16:55:32 +0800 Subject: [PATCH 19/45] add: TODO --- srml/staking/src/tests.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 438ddef07..48b822410 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -501,10 +501,9 @@ fn inflation_should_be_correct() { let _ = Ring::deposit_into_existing(&11, surplus_needed); assert_eq!(Ring::total_issuance(), initial_issuance); + // TODO // assert_eq!(Staking::current_era_total_reward(), 80000000 * COIN / 10); - // // start_era(11); - // // // ErasPerEpoch = 10 // assert_eq!(Staking::current_era_total_reward(), 88000000 * COIN / 10); }); From ab0cb5a6cf8553bbdeec5b4537260362c50721d9 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 14:04:19 +0800 Subject: [PATCH 20/45] add: comment for `reward_validator` --- srml/staking/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index fa97234a6..8948ad9be 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -1127,6 +1127,9 @@ impl Module { maybe_new_validators } + /// Reward a given validator by a specific amount. Add the reward to the validator's, and its + /// nominators' balance, pro-rata based on their exposure, after having removed the validator's + /// pre-payout cut. fn reward_validator(stash: &T::AccountId, reward: RingBalanceOf) -> RingPositiveImbalanceOf { let off_the_table = Self::validators(stash).validator_payment_ratio * reward; let reward = reward - off_the_table; From 2bb337f3be0cd5867cfd04519ac1b0fed0eb0eb5 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:19:35 +0800 Subject: [PATCH 21/45] fix: `set_controller_should_not_change_ledger` --- srml/staking/src/tests.rs | 74 ++++++++++++++------------------------- 1 file changed, 26 insertions(+), 48 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 48b822410..3eb57ffa3 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -510,8 +510,8 @@ fn inflation_should_be_correct() { } //#[test] -//fn reward_should_work_correctly() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { +//fn reward_should_work() { +// ExtBuilder::default().nominate(false).build().execute_with(|| { // // create controller account // let _ = Ring::deposit_creating(&2000, COIN); // let _ = Ring::deposit_creating(&1000, COIN); @@ -521,11 +521,10 @@ fn inflation_should_be_correct() { // Kton::deposit_creating(&2001, 10 * COIN); // // new validator // let _ = Ring::deposit_creating(&1001, 300 * COIN); -// Kton::deposit_creating(&1001, 1 * COIN); +// Kton::deposit_creating(&1001, COIN); // // handle some dirty work // let _ = Ring::deposit_creating(&201, 2000 * COIN); // Kton::deposit_creating(&201, 10 * COIN); -// assert_eq!(Kton::free_balance(&201), 10 * COIN); // // // 2001-2000 // assert_ok!(Staking::bond( @@ -535,11 +534,7 @@ fn inflation_should_be_correct() { // RewardDestination::Controller, // 12, // )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(2001), -// StakingBalance::Kton(1 * COIN), -// 0 -// )); +// assert_ok!(Staking::bond_extra(Origin::signed(2001), StakingBalance::Kton(COIN), 0)); // // 1001-1000 // assert_ok!(Staking::bond( // Origin::signed(1001), @@ -548,40 +543,32 @@ fn inflation_should_be_correct() { // RewardDestination::Controller, // 12, // )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(1001), -// StakingBalance::Kton(1 * COIN), -// 0 -// )); -// let ring_pool = Staking::ring_pool(); -// let kton_pool = Staking::kton_pool(); +// assert_ok!(Staking::bond_extra(Origin::signed(1001), StakingBalance::Kton(COIN), 0)); // // 201-200 // assert_ok!(Staking::bond( // Origin::signed(201), // 200, -// StakingBalance::Ring(3000 * COIN - ring_pool), +// StakingBalance::Ring(3000 * COIN - Staking::ring_pool()), // RewardDestination::Stash, // 12, // )); // assert_ok!(Staking::bond_extra( // Origin::signed(201), -// StakingBalance::Kton(10 * COIN - kton_pool), +// StakingBalance::Kton(10 * COIN - Staking::kton_pool()), // 0, // )); -// // ring_pool and kton_pool // assert_eq!(Staking::ring_pool(), 3000 * COIN); // assert_eq!(Staking::kton_pool(), 10 * COIN); +// // // 1/5 ring_pool and 1/5 kton_pool -// assert_ok!(Staking::validate(Origin::signed(2000), [0; 8].to_vec(), 0, 3)); +// assert_ok!(Staking::validate(Origin::signed(2000), vec![0; 8], 0, 3)); // assert_ok!(Staking::nominate(Origin::signed(1000), vec![2001])); // -// assert_eq!(Staking::ledger(&2000).unwrap().active_kton, 1 * COIN); -// assert_eq!(Staking::ledger(&2000).unwrap().active_ring, 300 * COIN); -// assert_eq!(Staking::power_of(&2001), 1_000_000_000 / 10 as u128); +// assert_eq!(Staking::power_of(&2001), 100_000_000); // // 600COIN for rewarding ring bond-er // // 600COIN for rewarding kton bond-er -// Staking::select_validators(); -// Staking::reward_validator(&2001, 1200 * COIN); +// Staking::new_era(Session::current_index()); +// Staking::reward_validator(&2001, 200); // // assert_eq!( // Staking::stakers(2001), @@ -590,7 +577,7 @@ fn inflation_should_be_correct() { // own: 600000000000, // others: vec![IndividualExposure { // who: 1001, -// value: 600000000000 +// value: 600000000000, // }] // } // ); @@ -598,7 +585,7 @@ fn inflation_should_be_correct() { // assert_eq!(Ring::free_balance(&1000), 601 * COIN); // }); //} -// + //#[test] //fn slash_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -632,16 +619,7 @@ fn inflation_should_be_correct() { // assert_eq!(Kton::free_balance(&1001), kton_free_balance - slash_value); // }); //} -// -//#[test] -////fn test_inflation() { -//// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -//// assert_eq!(Staking::current_era_total_reward(), 80_000_000 * COIN / 10); -//// start_era(20); -//// assert_eq!(Staking::epoch_index(), 2); -//// assert_eq!(Staking::current_era_total_reward(), 9_999_988_266 * COIN / 1000); -//// }); -////} + //#[test] //fn set_controller_should_remove_old_ledger() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -656,16 +634,17 @@ fn inflation_should_be_correct() { // assert!(Staking::ledger(&old_controller).is_none()); // }); //} -// -//#[test] -//fn set_controller_should_not_change_ledger() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// assert_eq!(Staking::ledger(&10).unwrap().active_ring, 100 * COIN); -// assert_ok!(Staking::set_controller(Origin::signed(11), 12)); -// assert_eq!(Staking::ledger(&12).unwrap().active_ring, 100 * COIN); -// }); -//} -// + +#[test] +fn set_controller_should_not_change_ledger() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let ledger = Staking::ledger(&10).unwrap(); + assert_ok!(Staking::set_controller(Origin::signed(11), 12)); + assert_eq!(Staking::ledger(&10), None); + assert_eq!(Staking::ledger(&12).unwrap(), ledger); + }); +} + //#[test] //fn slash_should_not_touch_unbondingss() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -1161,7 +1140,6 @@ fn inflation_should_be_correct() { // assert_ne!(free_balance_with_new_era, 0); // assert!(free_balance > free_balance_with_new_era); //} - #[test] fn xavier_q1() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From 77219c0d8c15e1b47e90840e832070b442e38128 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:20:10 +0800 Subject: [PATCH 22/45] fix: remove dangling comma --- types.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types.json b/types.json index 7eacd5184..ad552bf39 100644 --- a/types.json +++ b/types.json @@ -21,7 +21,7 @@ }, "NormalLock": { "amount": "u128", - "util": "Moment", + "util": "Moment" }, "StakingLock": { "staking_amount": "u128", From 5d728eb2fe765ef66a21b97c9e3ecd1bcdf8fb65 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:22:05 +0800 Subject: [PATCH 23/45] rename: `set_controller_should_remove_old_ledger` to `set_controller_should_work` --- srml/staking/src/tests.rs | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 3eb57ffa3..901d792f3 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -620,23 +620,8 @@ fn inflation_should_be_correct() { // }); //} -//#[test] -//fn set_controller_should_remove_old_ledger() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let stash = 11; -// let old_controller = 10; -// let new_controller = 12; -// -// assert!(Staking::ledger(&old_controller).is_some()); -// assert_eq!(Staking::bonded(&stash), Some(old_controller)); -// -// assert_ok!(Staking::set_controller(Origin::signed(stash), new_controller)); -// assert!(Staking::ledger(&old_controller).is_none()); -// }); -//} - #[test] -fn set_controller_should_not_change_ledger() { +fn set_controller_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { let ledger = Staking::ledger(&10).unwrap(); assert_ok!(Staking::set_controller(Origin::signed(11), 12)); From f82d4b0fcfd26f389bd4bbfac5441024f7e56df2 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:25:33 +0800 Subject: [PATCH 24/45] fix: `check_stash_already_bonded_and_controller_already_paired` --- srml/staking/src/tests.rs | 170 +++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 901d792f3..21e01a905 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -13,61 +13,61 @@ use darwinia_support::{BalanceLock, NormalLock, StakingLock, WithdrawLock}; // promise for `m` month with 50 Ring and 50 Kton // `m` can be ignore, and it wont perform `bond` action // gen_paired_account!(a(1), b(2)); -//macro_rules! gen_paired_account { -// ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $promise_month:ident($how_long:expr)) => { -// #[allow(non_snake_case, unused)] -// let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); -// #[allow(non_snake_case, unused)] -// let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); -// #[allow(non_snake_case, unused)] -// let $promise_month = $how_long; -// assert_ok!(Staking::bond( -// Origin::signed($stash), -// $controller, -// StakingBalance::Ring(50 * COIN), -// RewardDestination::Stash, -// $how_long -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed($stash), -// StakingBalance::Kton(50 * COIN), -// $how_long -// )); -// }; -// ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $how_long:expr) => { -// #[allow(non_snake_case, unused)] -// let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); -// #[allow(non_snake_case, unused)] -// let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); -// assert_ok!(Staking::bond( -// Origin::signed($stash), -// $controller, -// StakingBalance::Ring(50 * COIN), -// RewardDestination::Stash, -// $how_long -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed($stash), -// StakingBalance::Kton(50 * COIN), -// $how_long -// )); -// }; -// ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr)) => { -// #[allow(non_snake_case, unused)] -// let $stash = $stash_id; -// let _ = Ring::deposit_creating(&$stash, 100 * COIN); -// Kton::deposit_creating(&$stash, 100 * COIN); -// #[allow(non_snake_case, unused)] -// let $controller = $controller_id; -// let _ = Ring::deposit_creating(&$controller, COIN); -// }; -//} +macro_rules! gen_paired_account { + ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $promise_month:ident($how_long:expr)) => { + #[allow(non_snake_case, unused)] + let $stash = $stash_id; + let _ = Ring::deposit_creating(&$stash, 100 * COIN); + Kton::deposit_creating(&$stash, 100 * COIN); + #[allow(non_snake_case, unused)] + let $controller = $controller_id; + let _ = Ring::deposit_creating(&$controller, COIN); + #[allow(non_snake_case, unused)] + let $promise_month = $how_long; + assert_ok!(Staking::bond( + Origin::signed($stash), + $controller, + StakingBalance::Ring(50 * COIN), + RewardDestination::Stash, + $how_long + )); + assert_ok!(Staking::bond_extra( + Origin::signed($stash), + StakingBalance::Kton(50 * COIN), + $how_long + )); + }; + ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr), $how_long:expr) => { + #[allow(non_snake_case, unused)] + let $stash = $stash_id; + let _ = Ring::deposit_creating(&$stash, 100 * COIN); + Kton::deposit_creating(&$stash, 100 * COIN); + #[allow(non_snake_case, unused)] + let $controller = $controller_id; + let _ = Ring::deposit_creating(&$controller, COIN); + assert_ok!(Staking::bond( + Origin::signed($stash), + $controller, + StakingBalance::Ring(50 * COIN), + RewardDestination::Stash, + $how_long + )); + assert_ok!(Staking::bond_extra( + Origin::signed($stash), + StakingBalance::Kton(50 * COIN), + $how_long + )); + }; + ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr)) => { + #[allow(non_snake_case, unused)] + let $stash = $stash_id; + let _ = Ring::deposit_creating(&$stash, 100 * COIN); + Kton::deposit_creating(&$stash, 100 * COIN); + #[allow(non_snake_case, unused)] + let $controller = $controller_id; + let _ = Ring::deposit_creating(&$controller, COIN); + }; +} #[test] fn test_env_build() { @@ -631,7 +631,7 @@ fn set_controller_should_work() { } //#[test] -//fn slash_should_not_touch_unbondingss() { +//fn slash_should_not_touch_unbondings() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { // let old_ledger = Staking::ledger(&10).unwrap(); // // only deposit_ring, no normal_ring @@ -677,7 +677,7 @@ fn set_controller_should_work() { // assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * COIN)); // }); //} -// + //#[test] //fn bond_over_max_promise_month_should_fail() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -700,34 +700,34 @@ fn set_controller_should_work() { // ); // }); //} -// -//#[test] -//fn stash_already_bonded_and_controller_already_paired_should_fail() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// gen_paired_account!(unpaired_stash(123), unpaired_controller(456)); -// assert_err!( -// Staking::bond( -// Origin::signed(11), -// unpaired_controller, -// StakingBalance::Ring(COIN), -// RewardDestination::Stash, -// 0 -// ), -// "stash already bonded" -// ); -// assert_err!( -// Staking::bond( -// Origin::signed(unpaired_stash), -// 10, -// StakingBalance::Ring(COIN), -// RewardDestination::Stash, -// 0 -// ), -// "controller already paired" -// ); -// }); -//} -// + +#[test] +fn check_stash_already_bonded_and_controller_already_paired() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(unpaired_stash(123), unpaired_controller(456)); + assert_err!( + Staking::bond( + Origin::signed(11), + unpaired_controller, + StakingBalance::Ring(COIN), + RewardDestination::Stash, + 0 + ), + "stash already bonded" + ); + assert_err!( + Staking::bond( + Origin::signed(unpaired_stash), + 10, + StakingBalance::Ring(COIN), + RewardDestination::Stash, + 0 + ), + "controller already paired" + ); + }); +} + //#[test] //fn pool_should_be_increased_and_decreased_correctly() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From 333db3a44c35e171fc8a2c321843763145656d8f Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:27:12 +0800 Subject: [PATCH 25/45] fix: `bond_over_max_promise_month_should_fail` --- srml/staking/src/tests.rs | 44 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 21e01a905..78b248417 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -678,28 +678,28 @@ fn set_controller_should_work() { // }); //} -//#[test] -//fn bond_over_max_promise_month_should_fail() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// gen_paired_account!(stash(123), controller(456)); -// assert_err!( -// Staking::bond( -// Origin::signed(stash), -// controller, -// StakingBalance::Ring(COIN), -// RewardDestination::Stash, -// 37 -// ), -// "months at most is 36." -// ); -// -// gen_paired_account!(stash(123), controller(456), promise_month(12)); -// assert_err!( -// Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(COIN), 37), -// "months at most is 36." -// ); -// }); -//} +#[test] +fn bond_over_max_promise_month_should_fail() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(stash(123), controller(456)); + assert_err!( + Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(COIN), + RewardDestination::Stash, + 37 + ), + "months at most is 36." + ); + + gen_paired_account!(stash(123), controller(456), promise_month(12)); + assert_err!( + Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(COIN), 37), + "months at most is 36." + ); + }); +} #[test] fn check_stash_already_bonded_and_controller_already_paired() { From a93aba2b78a10123803745dd835e12a951b824e2 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 15:52:21 +0800 Subject: [PATCH 26/45] add: comment for `slash_validator` --- srml/staking/src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 8948ad9be..a06e63d15 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -856,6 +856,13 @@ impl Module { >::insert(controller, ledger); } + /// Slash a given validator by a specific amount with given (historical) exposure. + /// + /// Removes the slash from the validator's balance by preference, + /// and reduces the nominators' balance if needed. + /// + /// Returns the resulting `NegativeImbalance` to allow distributing the slashed amount and + /// pushes an entry onto the slash journal. fn slash_validator( stash: &T::AccountId, slash: ExtendedBalance, From ac33c71e9ed8653a5d9df44f7ee7ae38677eb4e3 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 16:21:19 +0800 Subject: [PATCH 27/45] fix: typo update: use `to_owned` for reference --- srml/staking/src/lib.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index a06e63d15..e6b05dbd6 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -24,7 +24,7 @@ extern crate test; pub mod inflation; use codec::{Decode, Encode, HasCompact}; -use rstd::{convert::TryInto, prelude::*, result}; +use rstd::{borrow::ToOwned, convert::TryInto, prelude::*, result}; use session::{historical::OnSessionEnding, SelectInitialValidators}; use sr_primitives::{ traits::{CheckedSub, Convert, One, SaturatedConversion, Saturating, StaticLookup, Zero}, @@ -889,7 +889,7 @@ impl Module { // The amount we'll slash from the validator's stash directly. let own_slash = own_remaining.min(slash); - let (mut ring_imbalance, mut kton_imblance, missing) = + let (mut ring_imbalance, mut kton_imbalance, missing) = Self::slash_individual(stash, Perbill::from_rational_approximation(own_slash, exposure.own)); // T::Currency::slash(stash, own_slash); let own_slash = own_slash - missing; // The amount remaining that we can't slash from the validator, @@ -909,21 +909,21 @@ impl Module { ); ring_imbalance.subsume(r); - kton_imblance.subsume(k); + kton_imbalance.subsume(k); } } } journal.push(SlashJournalEntry { - who: stash.clone(), + who: stash.to_owned(), own_slash, amount: slash, }); // trigger the event - Self::deposit_event(RawEvent::Slash(stash.clone(), slash)); + Self::deposit_event(RawEvent::Slash(stash.to_owned(), slash)); - (ring_imbalance, kton_imblance) + (ring_imbalance, kton_imbalance) } // TODO: there is reserve balance in Balance.Slash, we assuming it is zero for now. @@ -942,7 +942,6 @@ impl Module { } else { (>::zero(), Zero::zero()) }; - let (kton_imbalance, _) = if !ledger.active_kton.is_zero() { let slashable_kton = slash_ratio * ledger.active_kton; let value_slashed = Self::slash_helper(&controller, &mut ledger, StakingBalance::Kton(slashable_kton)); From a8a9b988bcc670edc77a907c0fb9a4858688a4cb Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 16:21:43 +0800 Subject: [PATCH 28/45] update: `set_controller_should_work` --- srml/staking/src/tests.rs | 69 +++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 78b248417..3d45557aa 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -623,58 +623,71 @@ fn inflation_should_be_correct() { #[test] fn set_controller_should_work() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - let ledger = Staking::ledger(&10).unwrap(); - assert_ok!(Staking::set_controller(Origin::signed(11), 12)); - assert_eq!(Staking::ledger(&10), None); - assert_eq!(Staking::ledger(&12).unwrap(), ledger); + let (stash, old_controller, new_controller) = (11, 10, 12); + let ledger = Staking::ledger(&old_controller).unwrap(); + + assert_ok!(Staking::set_controller(Origin::signed(stash), new_controller)); + assert_eq!(Staking::ledger(&old_controller), None); + assert_eq!(Staking::ledger(&new_controller).unwrap(), ledger); }); } //#[test] //fn slash_should_not_touch_unbondings() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let old_ledger = Staking::ledger(&10).unwrap(); +// let (stash, controller) = (11, 10); +// let ledger = Staking::ledger(&controller).unwrap(); +// let unbondings = ( +// ledger.ring_staking_lock.unbondings.clone(), +// ledger.kton_staking_lock.unbondings.clone(), +// ); +// // // only deposit_ring, no normal_ring // assert_eq!( -// ( -// old_ledger.active_ring, -// old_ledger.active_deposit_ring -// ), +// (ledger.active_ring, ledger.active_deposit_ring), // (100 * COIN, 100 * COIN) // ); // // assert_ok!(Staking::bond_extra( -// Origin::signed(11), +// Origin::signed(stash), // StakingBalance::Ring(100 * COIN), -// 0 +// 0, // )); -// Kton::deposit_creating(&11, 10 * COIN); +// Kton::deposit_creating(&stash, 10 * COIN); // assert_ok!(Staking::bond_extra( -// Origin::signed(11), +// Origin::signed(stash), // StakingBalance::Kton(10 * COIN), -// 0 +// 0, // )); // -// assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(10 * COIN))); -// let new_ledger = Staking::ledger(&10).unwrap(); +// assert_ok!(Staking::unbond( +// Origin::signed(controller), +// StakingBalance::Ring(10 * COIN) +// )); +// let ledger = Staking::ledger(&controller).unwrap(); // assert_eq!( -// ( -// new_ledger.active_ring, -// new_ledger.active_deposit_ring -// ), +// (ledger.active_ring, ledger.active_deposit_ring), // (190 * COIN, 100 * COIN) // ); // -// // slash 100% -// Staking::slash_validator(&11, 1_000_000_000); -// -// let ledger = Staking::ledger(&10).unwrap(); +// // slash all +// println!("{:#?}", Staking::ledger(&controller).unwrap()); +// let _ = Staking::slash_validator( +// &stash, +// ExtendedBalance::max_value(), +// &Staking::stakers(&stash), +// &mut vec![], +// ); +// println!("{:#?}", Staking::ledger(&controller).unwrap()); +// let ledger = Staking::ledger(&controller).unwrap(); +// assert_eq!((ledger.active_ring, ledger.active_deposit_ring), (0, 0)); // assert_eq!( -// (ledger.active_ring, ledger.active_deposit_ring), -// // 10Ring in unbondings -// (0, 0) +// ( +// ledger.ring_staking_lock.unbondings.clone(), +// ledger.kton_staking_lock.unbondings.clone(), +// ), +// unbondings // ); -// assert_eq!(ledger.unbondings[0].value, StakingBalance::Ring(10 * COIN)); // }); //} From 08f95cc407c2a955fa41b7c1462d7457d54c5e0b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 16:35:56 +0800 Subject: [PATCH 29/45] fix: `pool_should_be_increased_and_decreased_correctly` --- srml/staking/src/tests.rs | 140 +++++++++++++++++++++----------------- 1 file changed, 78 insertions(+), 62 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 3d45557aa..22d29da87 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -509,6 +509,7 @@ fn inflation_should_be_correct() { }); } +// TODO //#[test] //fn reward_should_work() { // ExtBuilder::default().nominate(false).build().execute_with(|| { @@ -586,6 +587,7 @@ fn inflation_should_be_correct() { // }); //} +// TODO //#[test] //fn slash_should_work() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -632,6 +634,7 @@ fn set_controller_should_work() { }); } +// TODO //#[test] //fn slash_should_not_touch_unbondings() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -741,68 +744,78 @@ fn check_stash_already_bonded_and_controller_already_paired() { }); } -//#[test] -//fn pool_should_be_increased_and_decreased_correctly() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let mut ring_pool = Staking::ring_pool(); -// let mut kton_pool = Staking::kton_pool(); -// -// // bond: 100COIN -// gen_paired_account!(stash_1(111), controller_1(222), 0); -// gen_paired_account!(stash_2(333), controller_2(444), promise_month(12)); -// ring_pool += 100 * COIN; -// kton_pool += 100 * COIN; -// assert_eq!(Staking::ring_pool(), ring_pool); -// assert_eq!(Staking::kton_pool(), kton_pool); -// -// // unbond: 50Ring 50Kton -// assert_ok!(Staking::unbond( -// Origin::signed(controller_1), -// StakingBalance::Ring(50 * COIN) -// )); -// assert_ok!(Staking::unbond( -// Origin::signed(controller_1), -// StakingBalance::Kton(25 * COIN) -// )); -// // not yet expired: promise for 12 months -// assert_ok!(Staking::unbond( -// Origin::signed(controller_2), -// StakingBalance::Ring(50 * COIN) -// )); -// assert_ok!(Staking::unbond( -// Origin::signed(controller_2), -// StakingBalance::Kton(25 * COIN) -// )); -// ring_pool -= 50 * COIN; -// kton_pool -= 50 * COIN; -// assert_eq!(Staking::ring_pool(), ring_pool); -// assert_eq!(Staking::kton_pool(), kton_pool); -// -// // unbond with punish: 12.5Ring -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller_2), -// 125 * COIN / 10, -// promise_month * MONTH_IN_SECONDS as u64 -// )); -// // unbond deposit items: 12.5Ring -// Timestamp::set_timestamp(promise_month * MONTH_IN_SECONDS as u64); -// assert_ok!(Staking::unbond( -// Origin::signed(controller_2), -// StakingBalance::Ring(125 * COIN / 10) -// )); -// ring_pool -= 25 * COIN; -// assert_eq!(Staking::ring_pool(), ring_pool); -// -// // slash: 25Ring 50Kton -// Staking::slash_validator(&stash_1, 1_000_000_000); -// Staking::slash_validator(&stash_2, 1_000_000_000); -// ring_pool -= 25 * COIN; -// kton_pool -= 50 * COIN; -// assert_eq!(Staking::ring_pool(), ring_pool); -// assert_eq!(Staking::kton_pool(), kton_pool); -// }); -//} -// +#[test] +fn pool_should_be_increased_and_decreased_correctly() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let mut ring_pool = Staking::ring_pool(); + let mut kton_pool = Staking::kton_pool(); + + // bond: 100COIN + gen_paired_account!(stash_1(111), controller_1(222), 0); + gen_paired_account!(stash_2(333), controller_2(444), promise_month(12)); + ring_pool += 100 * COIN; + kton_pool += 100 * COIN; + assert_eq!(Staking::ring_pool(), ring_pool); + assert_eq!(Staking::kton_pool(), kton_pool); + + // unbond: 50Ring 50Kton + assert_ok!(Staking::unbond( + Origin::signed(controller_1), + StakingBalance::Ring(50 * COIN) + )); + assert_ok!(Staking::unbond( + Origin::signed(controller_1), + StakingBalance::Kton(25 * COIN) + )); + // not yet expired: promise for 12 months + assert_ok!(Staking::unbond( + Origin::signed(controller_2), + StakingBalance::Ring(50 * COIN) + )); + assert_ok!(Staking::unbond( + Origin::signed(controller_2), + StakingBalance::Kton(25 * COIN) + )); + ring_pool -= 50 * COIN; + kton_pool -= 50 * COIN; + assert_eq!(Staking::ring_pool(), ring_pool); + assert_eq!(Staking::kton_pool(), kton_pool); + + // unbond with punish: 12.5Ring + assert_ok!(Staking::claim_deposits_with_punish( + Origin::signed(controller_2), + (promise_month * MONTH_IN_SECONDS) as u64 + )); + // unbond deposit items: 12.5Ring + Timestamp::set_timestamp((promise_month * MONTH_IN_SECONDS) as u64); + assert_ok!(Staking::unbond( + Origin::signed(controller_2), + StakingBalance::Ring(125 * COIN / 10) + )); + ring_pool -= 125 * COIN / 10; + assert_eq!(Staking::ring_pool(), ring_pool); + + // TODO + // slash: 25Ring 50Kton + // let _ = Staking::slash_validator( + // &stash_1, + // ExtendedBalance::max_value(), + // &Staking::stakers(&stash_1), + // &mut vec![], + // ); + // let _ = Staking::slash_validator( + // &stash_2, + // ExtendedBalance::max_value(), + // &Staking::stakers(&stash_2), + // &mut vec![], + // ); + // ring_pool -= 25 * COIN; + // kton_pool -= 50 * COIN; + // assert_eq!(Staking::ring_pool(), ring_pool); + // assert_eq!(Staking::kton_pool(), kton_pool); + }); +} + //#[test] //fn unbond_over_max_unbondings_chunks_should_fail() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { @@ -1024,6 +1037,7 @@ fn check_stash_already_bonded_and_controller_already_paired() { // }); //} // +// TODO //// bond 10_000 Ring for 12 months, gain 1 Kton //// bond extra 10_000 Ring for 36 months, gain 3 Kton //// bond extra 1 Kton @@ -1089,6 +1103,7 @@ fn check_stash_already_bonded_and_controller_already_paired() { // }); //} // +// TODO //// how to balance the power and calculate the reward if some validators have been chilled //#[test] //fn yakio_q2() { @@ -1138,6 +1153,7 @@ fn check_stash_already_bonded_and_controller_already_paired() { // assert_ne!(free_balance_with_new_era, 0); // assert!(free_balance > free_balance_with_new_era); //} + #[test] fn xavier_q1() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From eaab535ea49ef161a7c861cb4abb4efe63723155 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 16:51:24 +0800 Subject: [PATCH 30/45] fix: typo --- types.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types.json b/types.json index ad552bf39..2f3f71259 100644 --- a/types.json +++ b/types.json @@ -21,7 +21,7 @@ }, "NormalLock": { "amount": "u128", - "util": "Moment" + "until": "Moment" }, "StakingLock": { "staking_amount": "u128", From d76be29bac5fbe889433019635a6fdc239e50993 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 17:24:29 +0800 Subject: [PATCH 31/45] hanging: backup --- srml/staking/src/tests.rs | 55 ++++++------------- types.json | 108 ++++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 85 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 22d29da87..8b8556a70 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -816,45 +816,22 @@ fn pool_should_be_increased_and_decreased_correctly() { }); } -//#[test] -//fn unbond_over_max_unbondings_chunks_should_fail() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// gen_paired_account!(stash(123), controller(456), promise_month(12)); -// let deposit_items_len = MAX_UNLOCKING_CHUNKS + 1; -// -// for _ in 1..deposit_items_len { -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(COIN), -// promise_month -// )); -// } -// { -// let ledger = Staking::ledger(&controller).unwrap(); -// assert_eq!(ledger.deposit_items.len(), deposit_items_len); -// assert_eq!(ledger.unbondings.len(), 0); -// } -// -// Timestamp::set_timestamp(promise_month as u64 * MONTH_IN_SECONDS as u64); -// -// for _ in 1..deposit_items_len { -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); -// } -// { -// let ledger = Staking::ledger(&controller).unwrap(); -// assert_eq!(ledger.deposit_items.len(), 1); -// assert_eq!(ledger.unbondings.len(), deposit_items_len - 1); -// } -// assert_err!( -// Staking::unbond( -// Origin::signed(controller), -// StakingBalance::Ring((deposit_items_len - 1) as u64 * COIN) -// ), -// "can not schedule more unlock chunks" -// ); -// }); -//} -// +#[test] +fn unbond_over_max_unbondings_chunks_should_fail() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(stash(123), controller(456), promise_month(12)); + + for i in timestamp + assert_err!( + Staking::unbond( + Origin::signed(controller), + StakingBalance::Ring((deposit_items_len - 1) as u128 * COIN), + ), + "can not schedule more unlock chunks" + ); + }); +} + //#[test] //fn unlock_value_should_be_increased_and_decreased_correctly() { // ExtBuilder::default().existential_deposit(0).build().execute_with(|| { diff --git a/types.json b/types.json index 2f3f71259..e7c4f5357 100644 --- a/types.json +++ b/types.json @@ -1,48 +1,64 @@ { - "EpochDuration": "u64", - "EraIndex": "u32", - "RingBalanceOf": "u128", - "KtonBalanceOf": "u128", - "ExtendedBalance": "u128", - "ValidatorPrefs": { - "unstake_threshold": "Compact", - "validator_payment_ratio": "Perbill" - }, - "StakingBalance": { - "_enum": { - "Ring": "RingBalanceOf", - "Kton": "KtonBalanceOf" - } - }, - "TimeDepositItem": { - "value": "Compact", - "start_time": "Compact", - "expire_time": "Compact" - }, - "NormalLock": { - "amount": "u128", - "until": "Moment" - }, - "StakingLock": { - "staking_amount": "u128", - "unbondings": "Vec" - }, - "StakingLedger": { - "stash": "AccountId", - "active_ring": "Compact", - "active_deposit_ring": "Compact", - "active_kton": "Compact", - "deposit_items": "Vec", - "ring_staking_lock": "StakingLock", - "kton_staking_lock": "StakingLock" - }, - "IndividualExposure": { - "who": "AccountId", - "value": "ExtendedBalance" - }, - "Exposure": { - "total": "ExtendedBalance", - "own": "ExtendedBalance", - "others": "Vec" - } + "EpochDuration": "u64", + "EraIndex": "u32", + + "RingBalanceOf": "u128", + "KtonBalanceOf": "u128", + "ExtendedBalance": "u128", + "StakingBalance": { + "_enum": { + "Ring": "RingBalanceOf", + "Kton": "KtonBalanceOf" + } + }, + + "IndividualExposure": { + "who": "AccountId", + "value": "ExtendedBalance" + }, + "Exposure": { + "total": "ExtendedBalance", + "own": "ExtendedBalance", + "others": "Vec" + }, + + "ValidatorPrefs": { + "unstake_threshold": "Compact", + "validator_payment_ratio": "Perbill" + }, + + "StakingLedger": { + "stash": "AccountId", + "active_ring": "Compact", + "active_deposit_ring": "Compact", + "active_kton": "Compact", + "deposit_items": "Vec", + "ring_staking_lock": "StakingLock", + "kton_staking_lock": "StakingLock" + }, + "TimeDepositItem": { + "value": "Compact", + "start_time": "Compact", + "expire_time": "Compact" + }, + + "BalanceLock": { + "id": "Vec", + "withdraw_lock": "WithdrawLock", + "reasons": "WithdrawReasons" + }, + "WithdrawLock": { + "_enum": { + "Normal": "NormalLock", + "WithStaking": "StakingLock" + } + }, + "NormalLock": { + "amount": "u128", + "until": "Moment" + }, + "StakingLock": { + "staking_amount": "u128", + "unbondings": "Vec" + } } \ No newline at end of file From 8619dfcafb55fb4757616c7ec3481a64b2cf13c9 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 18:40:56 +0800 Subject: [PATCH 32/45] fix: `unbond_over_max_unbondings_chunks_should_fail` --- srml/staking/src/tests.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 8b8556a70..0d912e1d2 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -819,15 +819,23 @@ fn pool_should_be_increased_and_decreased_correctly() { #[test] fn unbond_over_max_unbondings_chunks_should_fail() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { - gen_paired_account!(stash(123), controller(456), promise_month(12)); + gen_paired_account!(stash(123), controller(456)); + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(COIN), + RewardDestination::Stash, + 0, + )); + + for ts in 0..MAX_UNLOCKING_CHUNKS { + Timestamp::set_timestamp(ts as u64); + assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(1))); + } - for i in timestamp assert_err!( - Staking::unbond( - Origin::signed(controller), - StakingBalance::Ring((deposit_items_len - 1) as u128 * COIN), - ), - "can not schedule more unlock chunks" + Staking::unbond(Origin::signed(controller), StakingBalance::Ring(1)), + "can not schedule more unlock chunks", ); }); } From 920b3380b2c6bb621d0e0eae6d110469ea0fe748 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Mon, 25 Nov 2019 18:41:47 +0800 Subject: [PATCH 33/45] fix: bug with `ensure!` parser --- srml/staking/src/lib.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index e6b05dbd6..93a383381 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -24,7 +24,7 @@ extern crate test; pub mod inflation; use codec::{Decode, Encode, HasCompact}; -use rstd::{borrow::ToOwned, convert::TryInto, prelude::*, result}; +use rstd::{borrow::ToOwned, prelude::*, result}; use session::{historical::OnSessionEnding, SelectInitialValidators}; use sr_primitives::{ traits::{CheckedSub, Convert, One, SaturatedConversion, Saturating, StaticLookup, Zero}, @@ -540,9 +540,17 @@ decl_module! { .. } = &mut ledger; + // due to the macro parser, we've to add a bracket + // actually, this's totally wrong: + // `a as u32 + b as u32 < c` + // workaround: + // 1. `(a as u32 + b as u32) < c` + // 2. `let c_ = a as u32 + b as u32; c_ < c` ensure!( - ring_staking_lock.unbondings.len() + kton_staking_lock.unbondings.len() < MAX_UNLOCKING_CHUNKS.try_into().unwrap(), - "can not schedule more unlock chunks" + (ring_staking_lock.unbondings.len() as u32 + kton_staking_lock.unbondings.len() as u32) + < + MAX_UNLOCKING_CHUNKS, + "can not schedule more unlock chunks", ); let at = >::now().saturated_into::() + T::BondingDuration::get(); From 9dcdcef4c9d80dadaf8cf017373696944c96c038 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 11:29:25 +0800 Subject: [PATCH 34/45] add: `unbond_zero` --- srml/staking/src/tests.rs | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 0d912e1d2..ddfec8cd2 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -1002,26 +1002,20 @@ fn unbond_over_max_unbondings_chunks_should_fail() { // assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 2); // }); //} -// -//#[test] -//fn unbond_zero_before_expiry() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let expiry_date = 12 * MONTH_IN_SECONDS as u64; -// let unbond_value = StakingBalance::Ring(COIN); -// -// Timestamp::set_timestamp(expiry_date - 1); -// assert_ok!(Staking::unbond(Origin::signed(10), unbond_value.clone())); -// assert_eq!( -// Staking::ledger(&10).unwrap().unbondings[0].value, -// StakingBalance::Ring(0) -// ); -// -// Timestamp::set_timestamp(expiry_date); -// assert_ok!(Staking::unbond(Origin::signed(10), unbond_value.clone())); -// assert_eq!(Staking::ledger(&10).unwrap().unbondings[1].value, unbond_value); -// }); -//} -// + +#[test] +fn unbond_zero() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(stash(123), controller(456), promise_month(12)); + let ledger = Staking::ledger(&controller).unwrap(); + + Timestamp::set_timestamp((promise_month * MONTH_IN_SECONDS) as u64); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Ring(0))); + assert_ok!(Staking::unbond(Origin::signed(10), StakingBalance::Kton(0))); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + }); +} + // TODO //// bond 10_000 Ring for 12 months, gain 1 Kton //// bond extra 10_000 Ring for 36 months, gain 3 Kton From bb332ba3b65e42571f42f0b876a0b4728a6e3096 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 11:38:09 +0800 Subject: [PATCH 35/45] fix: `promise_extra_should_not_remove_unexpired_items` --- srml/staking/src/tests.rs | 77 +++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 39 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index ddfec8cd2..274f1fed7 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -956,7 +956,7 @@ fn unbond_over_max_unbondings_chunks_should_fail() { // } // }); //} -// + //// #[test] //// fn total_deposit_should_be_increased_and_decreased_correctly() { //// with_externalities( @@ -964,44 +964,43 @@ fn unbond_over_max_unbondings_chunks_should_fail() { //// || body, //// ); //// } -// -//#[test] -//fn promise_extra_should_not_remove_unexpired_items() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// gen_paired_account!(stash(123), controller(456), promise_month(12)); -// -// let expired_item_len = 3; -// let expiry_date = promise_month as u64 * MONTH_IN_SECONDS as u64; -// -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(5 * COIN), -// 0 -// )); -// for _ in 0..expired_item_len { -// assert_ok!(Staking::promise_extra(Origin::signed(controller), COIN, promise_month)); -// } -// -// Timestamp::set_timestamp(expiry_date - 1); -// assert_ok!(Staking::promise_extra( -// Origin::signed(controller), -// 2 * COIN, -// promise_month -// )); -// assert_eq!( -// Staking::ledger(&controller).unwrap().deposit_items.len(), -// 2 + expired_item_len -// ); -// -// Timestamp::set_timestamp(expiry_date); -// assert_ok!(Staking::promise_extra( -// Origin::signed(controller), -// 2 * COIN, -// promise_month -// )); -// assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 2); -// }); -//} + +#[test] +fn promise_extra_should_not_remove_unexpired_items() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(stash(123), controller(456), promise_month(12)); + let expired_items_len = 3; + let expiry_date = (promise_month * MONTH_IN_SECONDS) as u64; + + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Ring(5 * COIN), + 0, + )); + for _ in 0..expired_items_len { + assert_ok!(Staking::deposit_extra(Origin::signed(controller), COIN, promise_month)); + } + + Timestamp::set_timestamp(expiry_date - 1); + assert_ok!(Staking::deposit_extra( + Origin::signed(controller), + 2 * COIN, + promise_month, + )); + assert_eq!( + Staking::ledger(&controller).unwrap().deposit_items.len(), + 2 + expired_items_len, + ); + + Timestamp::set_timestamp(expiry_date); + assert_ok!(Staking::deposit_extra( + Origin::signed(controller), + 2 * COIN, + promise_month, + )); + assert_eq!(Staking::ledger(&controller).unwrap().deposit_items.len(), 2); + }); +} #[test] fn unbond_zero() { From b0cdc1e6f89f945bfbee9f9ab755e106cd02ff4b Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 11:54:33 +0800 Subject: [PATCH 36/45] update: avoid unnecessary `insert` operation --- srml/staking/src/lib.rs | 52 +++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index 93a383381..a85cf6480 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -527,10 +527,7 @@ decl_module! { /// modify time_deposit_items and time_deposit_ring amount fn unbond(origin, value: StakingBalance, KtonBalanceOf>) { let controller = ensure_signed(origin)?; - - Self::clear_mature_deposits(&controller); - - let mut ledger = Self::ledger(&controller).ok_or("not a controller")?; + let mut ledger = Self::clear_mature_deposits(Self::ledger(&controller).ok_or("not a controller")?); let StakingLedger { active_ring, active_deposit_ring, @@ -591,14 +588,12 @@ decl_module! { /// called by controller fn deposit_extra(origin, value: RingBalanceOf, promise_month: u32) { let controller = ensure_signed(origin)?; - if Self::ledger(&controller).is_none() { return Err("not a controller"); } + let ledger = Self::ledger(&controller).ok_or("not a controller")?; ensure!(promise_month >= 3 && promise_month <= 36, "months at least is 3 and at most is 36."); - Self::clear_mature_deposits(&controller); - let now = >::now(); - let mut ledger = Self::ledger(&controller).unwrap(); + let mut ledger = Self::clear_mature_deposits(ledger); let StakingLedger { stash, active_ring, @@ -625,7 +620,8 @@ decl_module! { fn claim_mature_deposits(origin) { let controller = ensure_signed(origin)?; - Self::clear_mature_deposits(&controller); + let ledger = Self::clear_mature_deposits(Self::ledger(&controller).ok_or("not a controller")?); + >::insert(controller, ledger); } fn claim_deposits_with_punish(origin, expire_time: T::Moment) { @@ -771,26 +767,26 @@ decl_module! { } impl Module { - pub fn clear_mature_deposits(controller: &T::AccountId) { - if let Some(mut ledger) = Self::ledger(&controller) { - let now = >::now(); - let StakingLedger { - active_deposit_ring, - deposit_items, - .. - } = &mut ledger; - - deposit_items.retain(|item| { - if item.expire_time > now { - true - } else { - *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); - false - } - }); + pub fn clear_mature_deposits( + mut ledger: StakingLedger, KtonBalanceOf, T::Moment>, + ) -> StakingLedger, KtonBalanceOf, T::Moment> { + let now = >::now(); + let StakingLedger { + active_deposit_ring, + deposit_items, + .. + } = &mut ledger; + + deposit_items.retain(|item| { + if item.expire_time > now { + true + } else { + *active_deposit_ring = active_deposit_ring.saturating_sub(item.value); + false + } + }); - >::insert(controller, ledger); - }; + ledger } fn bond_helper_in_ring( From 46be1dd93908d07cb1ca0e67b06c47d48be6534f Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 12:02:39 +0800 Subject: [PATCH 37/45] remove: `total_deposit_should_be_increased_and_decreased_correctly` --- srml/staking/src/tests.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 274f1fed7..25f9af613 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -957,14 +957,6 @@ fn unbond_over_max_unbondings_chunks_should_fail() { // }); //} -//// #[test] -//// fn total_deposit_should_be_increased_and_decreased_correctly() { -//// with_externalities( -//// &mut ExtBuilder::default().existential_deposit(0).build(), -//// || body, -//// ); -//// } - #[test] fn promise_extra_should_not_remove_unexpired_items() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From d4b6eb24ef55812a5f23ad000cec076529e9d474 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 12:19:25 +0800 Subject: [PATCH 38/45] remove: `unlock_value_should_be_increased_and_decreased_correctly` --- srml/staking/src/tests.rs | 117 -------------------------------------- 1 file changed, 117 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 25f9af613..52d9c3090 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -840,123 +840,6 @@ fn unbond_over_max_unbondings_chunks_should_fail() { }); } -//#[test] -//fn unlock_value_should_be_increased_and_decreased_correctly() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// // normal Ring/Kton -// { -// let stash = 444; -// let controller = 555; -// let _ = Ring::deposit_creating(&stash, 100 * COIN); -// Kton::deposit_creating(&stash, 100 * COIN); -// -// assert_ok!(Staking::bond( -// Origin::signed(stash), -// controller, -// StakingBalance::Ring(50 * COIN), -// RewardDestination::Stash, -// 0 -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Kton(50 * COIN), -// 0 -// )); -// -// let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; -// -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(COIN), -// era: 3, -// is_time_deposit: false, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(COIN))); -// unbondings.push(NormalLock { -// value: StakingBalance::Kton(COIN), -// era: 3, -// is_time_deposit: false, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(0))); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(0), -// era: 3, -// is_time_deposit: true, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Kton(0))); -// unbondings.push(NormalLock { -// value: StakingBalance::Kton(0), -// era: 3, -// is_time_deposit: false, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// } -// -// // promise Ring -// { -// gen_paired_account!(stash(666), controller(777), promise_month(12)); -// -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(50 * COIN), -// 36 -// )); -// -// let mut unbondings = Staking::ledger(&controller).unwrap().unbondings; -// -// assert_ok!(Staking::unbond(Origin::signed(controller), StakingBalance::Ring(COIN))); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(0), -// era: 3, -// is_time_deposit: true, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// -// for month in [12, 36].iter() { -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 20 * COIN, -// month * MONTH_IN_SECONDS as u64 -// )); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(20 * COIN), -// era: 3, -// is_time_deposit: true, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 29 * COIN, -// month * MONTH_IN_SECONDS as u64 -// )); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(29 * COIN), -// era: 3, -// is_time_deposit: true, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 50 * COIN, -// month * MONTH_IN_SECONDS as u64 -// )); -// unbondings.push(NormalLock { -// value: StakingBalance::Ring(1 * COIN), -// era: 3, -// is_time_deposit: true, -// }); -// assert_eq!(&Staking::ledger(&controller).unwrap().unbondings, &unbondings); -// } -// } -// }); -//} - #[test] fn promise_extra_should_not_remove_unexpired_items() { ExtBuilder::default().existential_deposit(0).build().execute_with(|| { From 7ad23af98067248175b8514dcfbf3fe5d17dd7f1 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 13:30:22 +0800 Subject: [PATCH 39/45] fix: `yakio_q1` --- srml/staking/src/tests.rs | 113 ++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 66 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 52d9c3090..3b7d93c08 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -890,72 +890,53 @@ fn unbond_zero() { }); } -// TODO -//// bond 10_000 Ring for 12 months, gain 1 Kton -//// bond extra 10_000 Ring for 36 months, gain 3 Kton -//// bond extra 1 Kton -//// nominate -//// unlock the 12 months deposit item with punish -//// lost 3 Kton and 10_000 Ring's power for nominate -//#[test] -//fn yakio_q1() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let stash = 777; -// let controller = 888; -// let _ = Ring::deposit_creating(&stash, 20_000); -// -// assert_ok!(Staking::bond( -// Origin::signed(stash), -// controller, -// StakingBalance::Ring(10_000), -// RewardDestination::Stash, -// 12 -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(10_000), -// 36 -// )); -// assert_eq!(Kton::free_balance(&stash), 4); -// -// assert_ok!(Staking::bond_extra(Origin::signed(stash), StakingBalance::Kton(1), 36)); -// assert_eq!(Staking::ledger(&controller).unwrap().active_kton, 1); -// -// assert_ok!(Staking::nominate(Origin::signed(controller), vec![controller])); -// -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 10_000 * COIN, -// 12 * MONTH_IN_SECONDS as u64 -// )); -// assert_eq!(Kton::free_balance(&stash), 1); -// -// let ledger = StakingLedger { -// stash: 777, -// total_deposit_ring: 10_000, -// active_ring: 10_000, -// active_deposit_ring: 10_000, -// active_kton: 1, -// deposit_items: vec![TimeDepositItem { -// value: 10_000, -// start_time: 0, -// expire_time: 36 * MONTH_IN_SECONDS as u64, -// }], -// unbondings: vec![], -// }; -// start_era(3); -// assert_ok!(Staking::withdraw_unbonded(Origin::signed(controller))); -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// // not enough Kton to unbond -// assert_ok!(Staking::unbond_with_punish( -// Origin::signed(controller), -// 10_000 * COIN, -// 36 * MONTH_IN_SECONDS as u64 -// )); -// assert_eq!(&Staking::ledger(&controller).unwrap(), &ledger); -// }); -//} -// +// bond 10_000 Ring for 12 months, gain 1 Kton +// bond extra 10_000 Ring for 36 months, gain 3 Kton +// bond extra 1 Kton +// nominate +// unlock the 12 months deposit item with punish +// lost 3 Kton and 10_000 Ring's power for nominate +#[test] +fn yakio_q1() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let (stash, controller) = (777, 888); + let _ = Ring::deposit_creating(&stash, 20_000); + + assert_ok!(Staking::bond( + Origin::signed(stash), + controller, + StakingBalance::Ring(10_000), + RewardDestination::Stash, + 12 + )); + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Ring(10_000), + 36 + )); + assert_eq!(Kton::free_balance(&stash), 4); + + assert_ok!(Staking::bond_extra(Origin::signed(stash), StakingBalance::Kton(1), 36)); + assert_eq!(Staking::ledger(&controller).unwrap().active_kton, 1); + + assert_ok!(Staking::nominate(Origin::signed(controller), vec![controller])); + + assert_ok!(Staking::claim_deposits_with_punish( + Origin::signed(controller), + (12 * MONTH_IN_SECONDS) as u64, + )); + assert_eq!(Kton::free_balance(&stash), 1); + + let ledger = Staking::ledger(&controller).unwrap(); + // not enough Kton to unbond + assert_ok!(Staking::claim_deposits_with_punish( + Origin::signed(controller), + (36 * MONTH_IN_SECONDS) as u64, + )); + assert_eq!(Staking::ledger(&controller).unwrap(), ledger); + }); +} + // TODO //// how to balance the power and calculate the reward if some validators have been chilled //#[test] From 129416a2d4ec67e16a3e673f8dff297a861cfda4 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 15:31:21 +0800 Subject: [PATCH 40/45] update: `ValidatorPrefs` --- srml/staking/src/lib.rs | 64 ++++++++++++++++++++++------------------ srml/staking/src/mock.rs | 14 +++++---- types.json | 3 +- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index a85cf6480..5caa84235 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -105,19 +105,22 @@ pub enum StakerStatus { #[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)] pub struct ValidatorPrefs { + pub node_name: Vec, /// Validator should ensure this many more slashes than is necessary before being unstaked. #[codec(compact)] pub unstake_threshold: u32, /// percent of Reward that validator takes up-front; only the rest is split between themselves and /// nominators. - pub validator_payment_ratio: Perbill, + #[codec(compact)] + pub validator_payment_ratio: u32, } impl Default for ValidatorPrefs { fn default() -> Self { ValidatorPrefs { + node_name: vec![], unstake_threshold: 3, - validator_payment_ratio: Default::default(), + validator_payment_ratio: 0, } } } @@ -378,19 +381,15 @@ decl_storage! { /// The rest of the slashed value is handled by the `Slash`. pub SlashRewardFraction get(fn slash_reward_fraction) config(): Perbill; - /// A mapping from still-bonded eras to the first session index of that era. - BondedEras: Vec<(EraIndex, SessionIndex)>; - - /// All slashes that have occurred in a given era. - EraSlashJournal get(fn era_slash_journal): - map EraIndex => Vec>; - - pub NodeName get(node_name): map T::AccountId => Vec; - pub RingPool get(ring_pool): RingBalanceOf; pub KtonPool get(kton_pool): KtonBalanceOf; + /// A mapping from still-bonded eras to the first session index of that era. + BondedEras: Vec<(EraIndex, SessionIndex)>; + + /// All slashes that have occurred in a given era. + EraSlashJournal get(fn era_slash_journal): map EraIndex => Vec>; } add_extra_genesis { config(stakers): @@ -403,21 +402,22 @@ decl_storage! { T::Lookup::unlookup(controller.clone()), StakingBalance::Ring(balance), RewardDestination::Stash, - 12 + 12, ); let _ = match status { StakerStatus::Validator => { >::validate( T::Origin::from(Some(controller.clone()).into()), - [0;8].to_vec(), - 0, - 3 + ValidatorPrefs { + node_name: vec![0; 8], + ..Default::default() + }, ) }, StakerStatus::Nominator(votes) => { >::nominate( T::Origin::from(Some(controller.clone()).into()), - votes.iter().map(|l| {T::Lookup::unlookup(l.clone())}).collect() + votes.iter().map(|l| {T::Lookup::unlookup(l.clone())}).collect(), ) }, _ => Ok(()) }; @@ -681,24 +681,32 @@ decl_module! { >::insert(&controller, ledger); } - fn validate(origin, name: Vec, ratio: u32, unstake_threshold: u32) { + fn validate(origin, prefs: ValidatorPrefs) { let controller = ensure_signed(origin)?; let ledger = Self::ledger(&controller).ok_or("not a controller")?; - let stash = &ledger.stash; + + ensure!( + !prefs.node_name.is_empty(), + "node name can not be empty", + ); ensure!( - unstake_threshold <= MAX_UNSTAKE_THRESHOLD, - "unstake threshold too large" + prefs.unstake_threshold <= MAX_UNSTAKE_THRESHOLD, + "unstake threshold too large", ); + + let stash = &ledger.stash; + let mut prefs = prefs; // at most 100% - let ratio = Perbill::from_percent(ratio.min(100)); - let prefs = ValidatorPrefs { unstake_threshold: unstake_threshold, validator_payment_ratio: ratio }; + prefs.validator_payment_ratio = prefs.validator_payment_ratio.min(100); >::remove(stash); - >::insert(stash, prefs); - if !>::exists(&controller) { - >::insert(controller, name); - Self::deposit_event(RawEvent::NodeNameUpdated); - } + >::mutate(stash, |prefs_| { + let exists = !prefs_.node_name.is_empty(); + *prefs_ = prefs; + if exists { + Self::deposit_event(RawEvent::NodeNameUpdated); + } + }); } fn nominate(origin, targets: Vec<::Source>) { @@ -1141,7 +1149,7 @@ impl Module { /// nominators' balance, pro-rata based on their exposure, after having removed the validator's /// pre-payout cut. fn reward_validator(stash: &T::AccountId, reward: RingBalanceOf) -> RingPositiveImbalanceOf { - let off_the_table = Self::validators(stash).validator_payment_ratio * reward; + let off_the_table = Perbill::from_percent(Self::validators(stash).validator_payment_ratio) * reward; let reward = reward - off_the_table; let mut imbalance = >::zero(); let validator_cut = if reward.is_zero() { diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index a9c373727..77b5cc15e 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -16,7 +16,7 @@ use srml_support::{ }; use substrate_primitives::{crypto::key_types, H256}; -use crate::{EraIndex, GenesisConfig, Module, Nominators, RewardDestination, StakerStatus, StakingBalance, Trait}; +use crate::*; use darwinia_support::TimeStamp; use node_primitives::Balance; use phragmen::ExtendedBalance; @@ -418,13 +418,15 @@ pub fn bond_validator(acc: u64, val: Balance) { acc, StakingBalance::Ring(val), RewardDestination::Controller, - 0 + 0, )); assert_ok!(Staking::validate( Origin::signed(acc), - "test".as_bytes().to_owned(), - 0, - 0 + ValidatorPrefs { + node_name: "test".as_bytes().to_vec(), + unstake_threshold: 0, + validator_payment_ratio: 0, + } )); } @@ -437,7 +439,7 @@ pub fn bond_nominator(acc: u64, val: Balance, target: Vec) { acc, StakingBalance::Ring(val), RewardDestination::Controller, - 0 + 0, )); assert_ok!(Staking::nominate(Origin::signed(acc), target)); } diff --git a/types.json b/types.json index 081c517b0..0943c581f 100644 --- a/types.json +++ b/types.json @@ -23,8 +23,9 @@ }, "ValidatorPrefs": { + "node_name": "Vec", "unstake_threshold": "Compact", - "validator_payment_ratio": "Perbill" + "validator_payment_ratio": "Compact" }, "StakingLedger": { From b77c5050d06a6d3e3c2175cfc48e701ebd30c23d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 17:08:10 +0800 Subject: [PATCH 41/45] fix: `yakio_q2` --- srml/staking/src/mock.rs | 2 +- srml/staking/src/tests.rs | 232 +++++++++++++------------------------- 2 files changed, 80 insertions(+), 154 deletions(-) diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 77b5cc15e..730c13b4c 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -1,3 +1,4 @@ +pub use node_primitives::Balance; pub use node_runtime::constants::currency::COIN; use std::{cell::RefCell, collections::HashSet}; @@ -18,7 +19,6 @@ use substrate_primitives::{crypto::key_types, H256}; use crate::*; use darwinia_support::TimeStamp; -use node_primitives::Balance; use phragmen::ExtendedBalance; /// The AccountId alias in this test module. diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 3b7d93c08..c67efe772 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -511,114 +511,39 @@ fn inflation_should_be_correct() { // TODO //#[test] -//fn reward_should_work() { -// ExtBuilder::default().nominate(false).build().execute_with(|| { -// // create controller account -// let _ = Ring::deposit_creating(&2000, COIN); -// let _ = Ring::deposit_creating(&1000, COIN); -// let _ = Ring::deposit_creating(&200, COIN); -// // new validator -// let _ = Ring::deposit_creating(&2001, 2000 * COIN); -// Kton::deposit_creating(&2001, 10 * COIN); -// // new validator -// let _ = Ring::deposit_creating(&1001, 300 * COIN); -// Kton::deposit_creating(&1001, COIN); -// // handle some dirty work -// let _ = Ring::deposit_creating(&201, 2000 * COIN); -// Kton::deposit_creating(&201, 10 * COIN); +//fn reward_and_slash_should_work() { +// ExtBuilder::default().build().execute_with(|| { +// gen_paired_account!(stash_1(123), _c(456), 12); +// gen_paired_account!(stash_2(234), _c(567), 12); // -// // 2001-2000 -// assert_ok!(Staking::bond( -// Origin::signed(2001), -// 2000, -// StakingBalance::Ring(300 * COIN), -// RewardDestination::Controller, -// 12, -// )); -// assert_ok!(Staking::bond_extra(Origin::signed(2001), StakingBalance::Kton(COIN), 0)); -// // 1001-1000 -// assert_ok!(Staking::bond( -// Origin::signed(1001), -// 1000, -// StakingBalance::Ring(300 * COIN), -// RewardDestination::Controller, -// 12, -// )); -// assert_ok!(Staking::bond_extra(Origin::signed(1001), StakingBalance::Kton(COIN), 0)); -// // 201-200 -// assert_ok!(Staking::bond( -// Origin::signed(201), -// 200, -// StakingBalance::Ring(3000 * COIN - Staking::ring_pool()), -// RewardDestination::Stash, -// 12, -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(201), -// StakingBalance::Kton(10 * COIN - Staking::kton_pool()), -// 0, -// )); -// assert_eq!(Staking::ring_pool(), 3000 * COIN); -// assert_eq!(Staking::kton_pool(), 10 * COIN); -// -// // 1/5 ring_pool and 1/5 kton_pool -// assert_ok!(Staking::validate(Origin::signed(2000), vec![0; 8], 0, 3)); -// assert_ok!(Staking::nominate(Origin::signed(1000), vec![2001])); -// -// assert_eq!(Staking::power_of(&2001), 100_000_000); -// // 600COIN for rewarding ring bond-er -// // 600COIN for rewarding kton bond-er -// Staking::new_era(Session::current_index()); -// Staking::reward_validator(&2001, 200); +// >::insert( +// &stash_1, +// Exposure { +// total: 1, +// own: 1, +// others: vec![], +// }, +// ); +// assert_eq!(Ring::total_balance(&stash_1), 100 * COIN); +// let _ = Staking::reward_validator(&stash_1, 20 * COIN); +// assert_eq!(Ring::total_balance(&stash_1), 120 * COIN); // -// assert_eq!( -// Staking::stakers(2001), +// >::insert( +// &stash_1, // Exposure { -// total: 1200000000000, -// own: 600000000000, +// total: 100 * COIN, +// own: 1, // others: vec![IndividualExposure { -// who: 1001, -// value: 600000000000, -// }] -// } +// who: stash_2, +// value: 100 * COIN - 1, +// }], +// }, // ); -// assert_eq!(Ring::free_balance(&2000), 601 * COIN); -// assert_eq!(Ring::free_balance(&1000), 601 * COIN); -// }); -//} - -// TODO -//#[test] -//fn slash_should_work() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let _ = Ring::deposit_creating(&1001, 100 * COIN); -// Kton::deposit_creating(&1001, 100 * COIN); -// -// assert_ok!(Staking::bond( -// Origin::signed(1001), -// 1000, -// StakingBalance::Ring(50 * COIN), -// RewardDestination::Controller, -// 0, -// )); -// assert_ok!(Staking::bond_extra( -// Origin::signed(1001), -// StakingBalance::Kton(50 * COIN), -// 0 -// )); -// assert_ok!(Staking::validate(Origin::signed(1000), [0; 8].to_vec(), 0, 3)); -// -// // slash 1% -// let slash_value = 5 * COIN / 10; -// let mut ledger = Staking::ledger(&1000).unwrap(); -// let ring_free_balance = Ring::free_balance(&1001); -// let kton_free_balance = Kton::free_balance(&1001); -// Staking::slash_validator(&1001, 10_000_000); -// ledger.active_ring -= slash_value; -// ledger.active_kton -= slash_value; -// assert_eq!(&Staking::ledger(&1000).unwrap(), &ledger); -// assert_eq!(Ring::free_balance(&1001), ring_free_balance - slash_value); -// assert_eq!(Kton::free_balance(&1001), kton_free_balance - slash_value); +// println!("{:#?}", Ring::total_balance(&stash_1)); +// let _ = Staking::slash_validator(&stash_1, 1, &Staking::stakers(&stash_1), &mut Vec::new()); +// println!("{:#?}", Ring::total_balance(&stash_1)); +// // assert_eq!(Ring::total_balance(&stash_1), 120 * COIN - 1); +// // assert_eq!(Ring::total_balance(&stash_2), 1); // }); //} @@ -937,56 +862,57 @@ fn yakio_q1() { }); } -// TODO -//// how to balance the power and calculate the reward if some validators have been chilled -//#[test] -//fn yakio_q2() { -// fn run(with_new_era: bool) -> u64 { -// let mut balance = 0; -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// gen_paired_account!(validator_1_stash(123), validator_1_controller(456), 0); -// gen_paired_account!(validator_2_stash(234), validator_2_controller(567), 0); -// gen_paired_account!(nominator_stash(345), nominator_controller(678), 0); -// -// assert_ok!(Staking::validate( -// Origin::signed(validator_1_controller), -// vec![0; 8], -// 0, -// 3 -// )); -// assert_ok!(Staking::validate( -// Origin::signed(validator_2_controller), -// vec![1; 8], -// 0, -// 3 -// )); -// assert_ok!(Staking::nominate( -// Origin::signed(nominator_controller), -// vec![validator_1_stash, validator_2_stash] -// )); -// -// start_era(1); -// assert_ok!(Staking::chill(Origin::signed(validator_1_controller))); -// // assert_ok!(Staking::chill(Origin::signed(validator_2_controller))); -// if with_new_era { -// start_era(2); -// } -// Staking::reward_validator(&validator_1_stash, 1000 * COIN); -// Staking::reward_validator(&validator_2_stash, 1000 * COIN); -// -// balance = Ring::free_balance(&nominator_stash); -// }); -// -// balance -// } -// -// let free_balance = run(false); -// let free_balance_with_new_era = run(true); -// -// assert_ne!(free_balance, 0); -// assert_ne!(free_balance_with_new_era, 0); -// assert!(free_balance > free_balance_with_new_era); -//} +// how to balance the power and calculate the reward if some validators have been chilled +#[test] +fn yakio_q2() { + fn run(with_new_era: bool) -> Balance { + let mut balance = 0; + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + gen_paired_account!(validator_1_stash(123), validator_1_controller(456), 0); + gen_paired_account!(validator_2_stash(234), validator_2_controller(567), 0); + gen_paired_account!(nominator_stash(345), nominator_controller(678), 0); + + assert_ok!(Staking::validate( + Origin::signed(validator_1_controller), + ValidatorPrefs { + node_name: vec![0; 8], + ..Default::default() + }, + )); + assert_ok!(Staking::validate( + Origin::signed(validator_2_controller), + ValidatorPrefs { + node_name: vec![1; 8], + ..Default::default() + }, + )); + assert_ok!(Staking::nominate( + Origin::signed(nominator_controller), + vec![validator_1_stash, validator_2_stash] + )); + + start_era(1); + assert_ok!(Staking::chill(Origin::signed(validator_1_controller))); + // assert_ok!(Staking::chill(Origin::signed(validator_2_controller))); + if with_new_era { + start_era(2); + } + let _ = Staking::reward_validator(&validator_1_stash, 1000 * COIN); + let _ = Staking::reward_validator(&validator_2_stash, 1000 * COIN); + + balance = Ring::free_balance(&nominator_stash); + }); + + balance + } + + let free_balance = run(false); + let free_balance_with_new_era = run(true); + + assert_ne!(free_balance, 0); + assert_ne!(free_balance_with_new_era, 0); + assert!(free_balance > free_balance_with_new_era); +} #[test] fn xavier_q1() { From 8fefeb9050edb77f923d30997c6a01abafa4b628 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 17:52:42 +0800 Subject: [PATCH 42/45] fix: `pool_should_be_increased_and_decreased_correctly` --- srml/staking/src/tests.rs | 53 +++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index c67efe772..4b7704b4b 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -706,7 +706,7 @@ fn pool_should_be_increased_and_decreased_correctly() { assert_eq!(Staking::ring_pool(), ring_pool); assert_eq!(Staking::kton_pool(), kton_pool); - // unbond with punish: 12.5Ring + // claim: 50Ring assert_ok!(Staking::claim_deposits_with_punish( Origin::signed(controller_2), (promise_month * MONTH_IN_SECONDS) as u64 @@ -720,24 +720,39 @@ fn pool_should_be_increased_and_decreased_correctly() { ring_pool -= 125 * COIN / 10; assert_eq!(Staking::ring_pool(), ring_pool); - // TODO - // slash: 25Ring 50Kton - // let _ = Staking::slash_validator( - // &stash_1, - // ExtendedBalance::max_value(), - // &Staking::stakers(&stash_1), - // &mut vec![], - // ); - // let _ = Staking::slash_validator( - // &stash_2, - // ExtendedBalance::max_value(), - // &Staking::stakers(&stash_2), - // &mut vec![], - // ); - // ring_pool -= 25 * COIN; - // kton_pool -= 50 * COIN; - // assert_eq!(Staking::ring_pool(), ring_pool); - // assert_eq!(Staking::kton_pool(), kton_pool); + // slash: 37.5Ring 50Kton + >::insert( + &stash_1, + Exposure { + total: 1, + own: 1, + others: vec![], + }, + ); + >::insert( + &stash_2, + Exposure { + total: 1, + own: 1, + others: vec![], + }, + ); + let _ = Staking::slash_validator( + &stash_1, + ExtendedBalance::max_value(), + &Staking::stakers(&stash_1), + &mut vec![], + ); + let _ = Staking::slash_validator( + &stash_2, + ExtendedBalance::max_value(), + &Staking::stakers(&stash_2), + &mut vec![], + ); + ring_pool -= 375 * COIN / 10; + kton_pool -= 50 * COIN; + assert_eq!(Staking::ring_pool(), ring_pool); + assert_eq!(Staking::kton_pool(), kton_pool); }); } From 840be95db0e4b7683802da75f73d3f8edaf7ce81 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 18:04:27 +0800 Subject: [PATCH 43/45] fix: `slash_should_not_touch_unbondings` --- srml/staking/src/tests.rs | 196 +++++++++++++++++++------------------- 1 file changed, 100 insertions(+), 96 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 4b7704b4b..45c17a344 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -509,43 +509,43 @@ fn inflation_should_be_correct() { }); } -// TODO -//#[test] -//fn reward_and_slash_should_work() { -// ExtBuilder::default().build().execute_with(|| { -// gen_paired_account!(stash_1(123), _c(456), 12); -// gen_paired_account!(stash_2(234), _c(567), 12); -// -// >::insert( -// &stash_1, -// Exposure { -// total: 1, -// own: 1, -// others: vec![], -// }, -// ); -// assert_eq!(Ring::total_balance(&stash_1), 100 * COIN); -// let _ = Staking::reward_validator(&stash_1, 20 * COIN); -// assert_eq!(Ring::total_balance(&stash_1), 120 * COIN); -// -// >::insert( -// &stash_1, -// Exposure { -// total: 100 * COIN, -// own: 1, -// others: vec![IndividualExposure { -// who: stash_2, -// value: 100 * COIN - 1, -// }], -// }, -// ); -// println!("{:#?}", Ring::total_balance(&stash_1)); -// let _ = Staking::slash_validator(&stash_1, 1, &Staking::stakers(&stash_1), &mut Vec::new()); -// println!("{:#?}", Ring::total_balance(&stash_1)); -// // assert_eq!(Ring::total_balance(&stash_1), 120 * COIN - 1); -// // assert_eq!(Ring::total_balance(&stash_2), 1); -// }); -//} +#[test] +fn reward_and_slash_should_work() { + ExtBuilder::default().build().execute_with(|| { + gen_paired_account!(stash_1(123), _c(456), 12); + gen_paired_account!(stash_2(234), _c(567), 12); + + >::insert( + &stash_1, + Exposure { + total: 1, + own: 1, + others: vec![], + }, + ); + assert_eq!(Ring::total_balance(&stash_1), 100 * COIN); + let _ = Staking::reward_validator(&stash_1, 20 * COIN); + assert_eq!(Ring::total_balance(&stash_1), 120 * COIN); + + // TODO + // >::insert( + // &stash_1, + // Exposure { + // total: 100 * COIN, + // own: 1, + // others: vec![IndividualExposure { + // who: stash_2, + // value: 100 * COIN - 1, + // }], + // }, + // ); + // println!("{:#?}", Ring::total_balance(&stash_1)); + // let _ = Staking::slash_validator(&stash_1, 1, &Staking::stakers(&stash_1), &mut Vec::new()); + // println!("{:#?}", Ring::total_balance(&stash_1)); + // assert_eq!(Ring::total_balance(&stash_1), 120 * COIN - 1); + // assert_eq!(Ring::total_balance(&stash_2), 1); + }); +} #[test] fn set_controller_should_work() { @@ -559,65 +559,69 @@ fn set_controller_should_work() { }); } -// TODO -//#[test] -//fn slash_should_not_touch_unbondings() { -// ExtBuilder::default().existential_deposit(0).build().execute_with(|| { -// let (stash, controller) = (11, 10); -// let ledger = Staking::ledger(&controller).unwrap(); -// let unbondings = ( -// ledger.ring_staking_lock.unbondings.clone(), -// ledger.kton_staking_lock.unbondings.clone(), -// ); -// -// // only deposit_ring, no normal_ring -// assert_eq!( -// (ledger.active_ring, ledger.active_deposit_ring), -// (100 * COIN, 100 * COIN) -// ); -// -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Ring(100 * COIN), -// 0, -// )); -// Kton::deposit_creating(&stash, 10 * COIN); -// assert_ok!(Staking::bond_extra( -// Origin::signed(stash), -// StakingBalance::Kton(10 * COIN), -// 0, -// )); -// -// assert_ok!(Staking::unbond( -// Origin::signed(controller), -// StakingBalance::Ring(10 * COIN) -// )); -// let ledger = Staking::ledger(&controller).unwrap(); -// assert_eq!( -// (ledger.active_ring, ledger.active_deposit_ring), -// (190 * COIN, 100 * COIN) -// ); -// -// // slash all -// println!("{:#?}", Staking::ledger(&controller).unwrap()); -// let _ = Staking::slash_validator( -// &stash, -// ExtendedBalance::max_value(), -// &Staking::stakers(&stash), -// &mut vec![], -// ); -// println!("{:#?}", Staking::ledger(&controller).unwrap()); -// let ledger = Staking::ledger(&controller).unwrap(); -// assert_eq!((ledger.active_ring, ledger.active_deposit_ring), (0, 0)); -// assert_eq!( -// ( -// ledger.ring_staking_lock.unbondings.clone(), -// ledger.kton_staking_lock.unbondings.clone(), -// ), -// unbondings -// ); -// }); -//} +#[test] +fn slash_should_not_touch_unbondings() { + ExtBuilder::default().existential_deposit(0).build().execute_with(|| { + let (stash, controller) = (11, 10); + let ledger = Staking::ledger(&controller).unwrap(); + + // only deposit_ring, no normal_ring + assert_eq!( + (ledger.active_ring, ledger.active_deposit_ring), + (100 * COIN, 100 * COIN), + ); + + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Ring(100 * COIN), + 0, + )); + Kton::deposit_creating(&stash, 10 * COIN); + assert_ok!(Staking::bond_extra( + Origin::signed(stash), + StakingBalance::Kton(10 * COIN), + 0, + )); + + assert_ok!(Staking::unbond( + Origin::signed(controller), + StakingBalance::Ring(10 * COIN), + )); + let ledger = Staking::ledger(&controller).unwrap(); + let unbondings = ( + ledger.ring_staking_lock.unbondings.clone(), + ledger.kton_staking_lock.unbondings.clone(), + ); + assert_eq!( + (ledger.active_ring, ledger.active_deposit_ring), + (190 * COIN, 100 * COIN), + ); + + >::insert( + &stash, + Exposure { + total: 1, + own: 1, + others: vec![], + }, + ); + let _ = Staking::slash_validator( + &stash, + ExtendedBalance::max_value(), + &Staking::stakers(&stash), + &mut vec![], + ); + let ledger = Staking::ledger(&controller).unwrap(); + assert_eq!( + ( + ledger.ring_staking_lock.unbondings.clone(), + ledger.kton_staking_lock.unbondings.clone(), + ), + unbondings, + ); + assert_eq!((ledger.active_ring, ledger.active_deposit_ring), (0, 0)); + }); +} #[test] fn bond_over_max_promise_month_should_fail() { From 9fb732ffa2580cf30679303a5e1922253076750d Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 18:14:35 +0800 Subject: [PATCH 44/45] udpate: format --- srml/staking/src/tests.rs | 100 +++++++++++++++++++------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 45c17a344..4b92254f0 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -29,7 +29,7 @@ macro_rules! gen_paired_account { $controller, StakingBalance::Ring(50 * COIN), RewardDestination::Stash, - $how_long + $how_long, )); assert_ok!(Staking::bond_extra( Origin::signed($stash), @@ -50,12 +50,12 @@ macro_rules! gen_paired_account { $controller, StakingBalance::Ring(50 * COIN), RewardDestination::Stash, - $how_long + $how_long, )); assert_ok!(Staking::bond_extra( Origin::signed($stash), StakingBalance::Kton(50 * COIN), - $how_long + $how_long, )); }; ($stash:ident($stash_id:expr), $controller:ident($controller_id:expr)) => { @@ -633,15 +633,15 @@ fn bond_over_max_promise_month_should_fail() { controller, StakingBalance::Ring(COIN), RewardDestination::Stash, - 37 + 37, ), - "months at most is 36." + "months at most is 36.", ); gen_paired_account!(stash(123), controller(456), promise_month(12)); assert_err!( Staking::bond_extra(Origin::signed(stash), StakingBalance::Ring(COIN), 37), - "months at most is 36." + "months at most is 36.", ); }); } @@ -656,9 +656,9 @@ fn check_stash_already_bonded_and_controller_already_paired() { unpaired_controller, StakingBalance::Ring(COIN), RewardDestination::Stash, - 0 + 0, ), - "stash already bonded" + "stash already bonded", ); assert_err!( Staking::bond( @@ -666,9 +666,9 @@ fn check_stash_already_bonded_and_controller_already_paired() { 10, StakingBalance::Ring(COIN), RewardDestination::Stash, - 0 + 0, ), - "controller already paired" + "controller already paired", ); }); } @@ -713,13 +713,13 @@ fn pool_should_be_increased_and_decreased_correctly() { // claim: 50Ring assert_ok!(Staking::claim_deposits_with_punish( Origin::signed(controller_2), - (promise_month * MONTH_IN_SECONDS) as u64 + (promise_month * MONTH_IN_SECONDS) as u64, )); // unbond deposit items: 12.5Ring Timestamp::set_timestamp((promise_month * MONTH_IN_SECONDS) as u64); assert_ok!(Staking::unbond( Origin::signed(controller_2), - StakingBalance::Ring(125 * COIN / 10) + StakingBalance::Ring(125 * COIN / 10), )); ring_pool -= 125 * COIN / 10; assert_eq!(Staking::ring_pool(), ring_pool); @@ -851,12 +851,12 @@ fn yakio_q1() { controller, StakingBalance::Ring(10_000), RewardDestination::Stash, - 12 + 12, )); assert_ok!(Staking::bond_extra( Origin::signed(stash), StakingBalance::Ring(10_000), - 36 + 36, )); assert_eq!(Kton::free_balance(&stash), 4); @@ -907,7 +907,7 @@ fn yakio_q2() { )); assert_ok!(Staking::nominate( Origin::signed(nominator_controller), - vec![validator_1_stash, validator_2_stash] + vec![validator_1_stash, validator_2_stash], )); start_era(1); @@ -977,7 +977,7 @@ fn xavier_q1() { staking_amount: 10, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Bond Extra - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1000,7 +1000,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1037,7 +1037,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); @@ -1055,7 +1055,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); assert_eq!( @@ -1108,7 +1108,7 @@ fn xavier_q1() { staking_amount: 5, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Init - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1127,7 +1127,7 @@ fn xavier_q1() { staking_amount: 10, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Bond Extra - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1150,7 +1150,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1159,7 +1159,7 @@ fn xavier_q1() { assert_err!( Ring::transfer(Origin::signed(stash), controller, 1), - "account liquidity restrictions prevent withdrawal" + "account liquidity restrictions prevent withdrawal", ); // println!("Locking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); // println!("Locking Transfer - Ring Locks: {:#?}", Ring::locks(stash)); @@ -1187,7 +1187,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); @@ -1205,7 +1205,7 @@ fn xavier_q1() { until: BondingDuration::get() + unbond_start, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); assert_eq!( @@ -1260,7 +1260,7 @@ fn xavier_q2() { staking_amount: 5, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Init - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1279,7 +1279,7 @@ fn xavier_q2() { staking_amount: 9, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Bond Extra - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1290,7 +1290,7 @@ fn xavier_q2() { Timestamp::set_timestamp(unbond_start_1); assert_ok!(Staking::unbond( Origin::signed(controller), - StakingBalance::Kton(unbond_value_1) + StakingBalance::Kton(unbond_value_1), )); assert_eq!(Timestamp::get(), unbond_start_1); assert_eq!(Kton::free_balance(stash), 10); @@ -1305,7 +1305,7 @@ fn xavier_q2() { until: BondingDuration::get() + unbond_start_1, }], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1334,7 +1334,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1343,7 +1343,7 @@ fn xavier_q2() { assert_err!( Kton::transfer(Origin::signed(stash), controller, unbond_value_1), - "account liquidity restrictions prevent withdrawal" + "account liquidity restrictions prevent withdrawal", ); // println!("Locking Transfer - Kton Balance: {:?}", Kton::free_balance(stash)); // println!("Locking Transfer - Kton Locks: {:#?}", Kton::locks(stash)); @@ -1357,7 +1357,7 @@ fn xavier_q2() { Timestamp::set_timestamp(BondingDuration::get() + unbond_start_1); assert_err!( Kton::transfer(Origin::signed(stash), controller, unbond_value_1 + 1), - "account liquidity restrictions prevent withdrawal" + "account liquidity restrictions prevent withdrawal", ); // println!("Locking Transfer - Kton Balance: {:?}", Kton::free_balance(stash)); // println!("Locking Transfer - Kton Locks: {:#?}", Kton::locks(stash)); @@ -1382,7 +1382,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Unlocking Transfer - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1409,7 +1409,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Unlocking Transfer - Kton Balance: {:?}", Kton::free_balance(stash)); @@ -1436,7 +1436,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); }); @@ -1463,7 +1463,7 @@ fn xavier_q2() { staking_amount: 5, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Init - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1482,7 +1482,7 @@ fn xavier_q2() { staking_amount: 9, unbondings: vec![], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Bond Extra - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1508,7 +1508,7 @@ fn xavier_q2() { until: BondingDuration::get() + unbond_start_1, },], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1537,7 +1537,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Ok Unbond - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1546,7 +1546,7 @@ fn xavier_q2() { assert_err!( Ring::transfer(Origin::signed(stash), controller, unbond_value_1), - "account liquidity restrictions prevent withdrawal" + "account liquidity restrictions prevent withdrawal", ); // println!("Locking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); // println!("Locking Transfer - Ring Locks: {:#?}", Ring::locks(stash)); @@ -1560,7 +1560,7 @@ fn xavier_q2() { Timestamp::set_timestamp(BondingDuration::get() + unbond_start_1); assert_err!( Ring::transfer(Origin::signed(stash), controller, unbond_value_1 + 1), - "account liquidity restrictions prevent withdrawal" + "account liquidity restrictions prevent withdrawal", ); // println!("Locking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); // println!("Locking Transfer - Ring Locks: {:#?}", Ring::locks(stash)); @@ -1585,7 +1585,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Unlocking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1612,7 +1612,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); // println!("Unlocking Transfer - Ring Balance: {:?}", Ring::free_balance(stash)); @@ -1639,7 +1639,7 @@ fn xavier_q2() { } ], }), - reasons: WithdrawReasons::all() + reasons: WithdrawReasons::all(), }] ); }); @@ -1672,7 +1672,7 @@ fn xavier_q3() { ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 5, - unbondings: vec![] + unbondings: vec![], }, } ); @@ -1692,7 +1692,7 @@ fn xavier_q3() { ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 0, - unbondings: vec![NormalLock { amount: 5, until: 61 }] + unbondings: vec![NormalLock { amount: 5, until: 61 }], }, } ); @@ -1714,7 +1714,7 @@ fn xavier_q3() { ring_staking_lock: Default::default(), kton_staking_lock: StakingLock { staking_amount: 1, - unbondings: vec![NormalLock { amount: 5, until: 61 }] + unbondings: vec![NormalLock { amount: 5, until: 61 }], }, } ); @@ -1747,7 +1747,7 @@ fn xavier_q3() { deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 5, - unbondings: vec![] + unbondings: vec![], }, kton_staking_lock: Default::default(), } @@ -1767,7 +1767,7 @@ fn xavier_q3() { deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 0, - unbondings: vec![NormalLock { amount: 5, until: 61 }] + unbondings: vec![NormalLock { amount: 5, until: 61 }], }, kton_staking_lock: Default::default(), } @@ -1789,7 +1789,7 @@ fn xavier_q3() { deposit_items: vec![], ring_staking_lock: StakingLock { staking_amount: 1, - unbondings: vec![NormalLock { amount: 5, until: 61 }] + unbondings: vec![NormalLock { amount: 5, until: 61 }], }, kton_staking_lock: Default::default(), } From 5dc44fda18d9d80cdfc74f6b95056e5b8dd7ed2a Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Tue, 26 Nov 2019 19:35:35 +0800 Subject: [PATCH 45/45] add: `FIXME: slash strategy` --- srml/staking/src/tests.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 4b92254f0..59c98edb0 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -523,11 +523,11 @@ fn reward_and_slash_should_work() { others: vec![], }, ); - assert_eq!(Ring::total_balance(&stash_1), 100 * COIN); + assert_eq!(Ring::free_balance(&stash_1), 100 * COIN); let _ = Staking::reward_validator(&stash_1, 20 * COIN); - assert_eq!(Ring::total_balance(&stash_1), 120 * COIN); + assert_eq!(Ring::free_balance(&stash_1), 120 * COIN); - // TODO + // FIXME: slash strategy // >::insert( // &stash_1, // Exposure { @@ -539,11 +539,9 @@ fn reward_and_slash_should_work() { // }], // }, // ); - // println!("{:#?}", Ring::total_balance(&stash_1)); // let _ = Staking::slash_validator(&stash_1, 1, &Staking::stakers(&stash_1), &mut Vec::new()); - // println!("{:#?}", Ring::total_balance(&stash_1)); - // assert_eq!(Ring::total_balance(&stash_1), 120 * COIN - 1); - // assert_eq!(Ring::total_balance(&stash_2), 1); + // assert_eq!(Ring::free_balance(&stash_1), 120 * COIN - 1); + // assert_eq!(Ring::free_balance(&stash_2), 1); }); } @@ -605,6 +603,7 @@ fn slash_should_not_touch_unbondings() { others: vec![], }, ); + // FIXME: slash strategy let _ = Staking::slash_validator( &stash, ExtendedBalance::max_value(), @@ -741,12 +740,14 @@ fn pool_should_be_increased_and_decreased_correctly() { others: vec![], }, ); + // FIXME: slash strategy let _ = Staking::slash_validator( &stash_1, ExtendedBalance::max_value(), &Staking::stakers(&stash_1), &mut vec![], ); + // FIXME: slash strategy let _ = Staking::slash_validator( &stash_2, ExtendedBalance::max_value(),