diff --git a/node/cli/src/chain_spec.rs b/node/cli/src/chain_spec.rs index d5db35409..acd13a68b 100644 --- a/node/cli/src/chain_spec.rs +++ b/node/cli/src/chain_spec.rs @@ -109,9 +109,9 @@ fn staging_testnet_config_genesis() -> GenesisConfig { vesting: vec![], }), kton: Some(KtonConfig { - ring_balances: endowed_accounts.iter().cloned() - .map(|k| (k, ENDOWMENT, 12)) - .chain(initial_authorities.iter().map(|x| (x.0.clone(), ENDOWMENT, 12))) + balances: endowed_accounts.iter().cloned() + .map(|k| (k, ENDOWMENT)) + .chain(initial_authorities.iter().map(|x| (x.0.clone(), ENDOWMENT))) .collect(), vesting: vec![], sys_acc: hex!["984d592d15d930ac36e6716407fbed3f7d1e2e62bc11f8429345f8b8b0dfc107"].unchecked_into(), @@ -242,9 +242,9 @@ pub fn testnet_genesis( vesting: vec![], }), kton: Some(KtonConfig { - ring_balances: endowed_accounts.iter().cloned() - .map(|k| (k, ENDOWMENT, 12)) - .chain(initial_authorities.iter().map(|x| (x.0.clone(), ENDOWMENT, 12))) + balances: endowed_accounts.iter().cloned() + .map(|k| (k, ENDOWMENT)) + .chain(initial_authorities.iter().map(|x| (x.0.clone(), ENDOWMENT))) .collect(), vesting: vec![], sys_acc: hex!["984d592d15d930ac36e6716407fbed3f7d1e2e62bc11f8429345f8b8b0dfc107"].unchecked_into(), diff --git a/node/runtime/wasm/Cargo.lock b/node/runtime/wasm/Cargo.lock index 667b5c065..4a9814c1b 100644 --- a/node/runtime/wasm/Cargo.lock +++ b/node/runtime/wasm/Cargo.lock @@ -516,7 +516,6 @@ dependencies = [ "parity-codec 4.1.1 (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.94 (registry+https://github.com/rust-lang/crates.io-index)", - "sr-io 2.0.0 (git+https://github.com/paritytech/substrate.git)", "sr-primitives 2.0.0 (git+https://github.com/paritytech/substrate.git)", "sr-std 2.0.0 (git+https://github.com/paritytech/substrate.git)", "srml-support 2.0.0 (git+https://github.com/paritytech/substrate.git)", diff --git a/srml/kton/Cargo.toml b/srml/kton/Cargo.toml index 7da047c3e..bf9ef04aa 100644 --- a/srml/kton/Cargo.toml +++ b/srml/kton/Cargo.toml @@ -16,9 +16,10 @@ system = { package = "srml-system", git = 'https://github.com/paritytech/substra timestamp = { package = "srml-timestamp", git = 'https://github.com/paritytech/substrate.git', default-features = false } substrate-primitives = { git = 'https://github.com/paritytech/substrate.git', default-features = false } dsupport = { package = "evo-support", path = "../support", default-features = false } -runtime_io = { package = "sr-io", git = 'https://github.com/paritytech/substrate.git', default-features = false } + [dev-dependencies] +runtime_io = { package = "sr-io", git = 'https://github.com/paritytech/substrate.git' } substrate-primitives = { git = 'https://github.com/paritytech/substrate.git' } balances = { package = "srml-balances", git = 'https://github.com/paritytech/substrate.git' } node-runtime = { path = "../../node/runtime" } @@ -36,6 +37,5 @@ std = [ "system/std", "timestamp/std", "substrate-primitives/std", - "runtime_io/std", "dsupport/std", ] diff --git a/srml/kton/src/lib.rs b/srml/kton/src/lib.rs index 875df73d0..e6ce4deef 100644 --- a/srml/kton/src/lib.rs +++ b/srml/kton/src/lib.rs @@ -7,6 +7,7 @@ use primitives::traits::{ }; use rstd::{cmp, convert::{TryFrom, TryInto}, result}; use rstd::prelude::*; + use srml_support::{decl_event, decl_module, decl_storage, ensure, Parameter, StorageMap, StorageValue}; use srml_support::dispatch::Result; use srml_support::traits::{ @@ -17,9 +18,6 @@ use srml_support::traits::{ use substrate_primitives::U256; use system::ensure_signed; -#[cfg(feature = "std")] -use runtime_io::with_storage; - // customed use dsupport::traits::SystemCurrency; use imbalance::{NegativeImbalance, PositiveImbalance}; @@ -143,9 +141,12 @@ decl_storage! { // like `existential_deposit`, but always set to 0 pub MinimumBalance get(minimum_balance): T::Balance = 0.into(); - pub TotalIssuance get(total_issuance) : T::Balance; + pub TotalIssuance get(total_issuance) build(|config: &GenesisConfig| { + config.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n) + }): T::Balance; - pub FreeBalance get(free_balance) : map T::AccountId => T::Balance; + pub FreeBalance get(free_balance) build(|config: &GenesisConfig| config.balances.clone()): + map T::AccountId => T::Balance; pub ReservedBalance get(reserved_balance): map T::AccountId => T::Balance; @@ -153,30 +154,29 @@ decl_storage! { pub TotalLock get(total_lock): T::Balance; - pub Vesting get(vesting): map T::AccountId => Option>; + pub Vesting get(vesting) build(|config: &GenesisConfig| { + config.vesting.iter().filter_map(|&(ref who, begin, length)| { + let begin = >::from(begin); + let length = >::from(length); + + config.balances.iter() + .find(|&&(ref w, _)| w == who) + .map(|&(_, balance)| { + // <= begin it should be >= balance + // >= begin+length it should be <= 0 + + let per_block = balance / length.max(primitives::traits::One::one()); + let offset = begin * per_block + balance; + + (who.clone(), VestingSchedule { offset, per_block }) + }) + }).collect::>() + }): map T::AccountId => Option>; } add_extra_genesis { - // for ring - config(ring_balances): Vec<(T::AccountId, CurrencyOf, u32)>; - config(vesting): Vec <(T::AccountId, T::BlockNumber, T::BlockNumber)>; - build( | - storage: & mut primitives::StorageOverlay, - _: & mut primitives::ChildrenStorageOverlay, - config: & GenesisConfig - | { - with_storage(storage, || { - for &(ref depositor, balance, months) in &config.ring_balances { - assert!(T::Currency::free_balance(&depositor) >= balance); - let _ = >::deposit( - T::Origin::from(Some(depositor.clone()).into()), - balance, - months - ); - - } - }); - }); - } + config(balances): Vec<(T::AccountId, T::Balance)>; + config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber)>; // begin, length +} } decl_module! { @@ -267,7 +267,7 @@ impl Module { deposit.total, // u32::max_value().into(), T::BlockNumber::max_value(), - WithdrawReasons::all() + WithdrawReasons::all(), ); >::insert(who, deposit); } @@ -453,7 +453,7 @@ impl Currency for Module { fn slash( who: &T::AccountId, - value: Self::Balance + value: Self::Balance, ) -> (Self::NegativeImbalance, Self::Balance) { let free_balance = Self::free_balance(who); let free_slash = cmp::min(free_balance, value); diff --git a/srml/kton/src/mock.rs b/srml/kton/src/mock.rs index be56b28fc..cff4e6f8c 100644 --- a/srml/kton/src/mock.rs +++ b/srml/kton/src/mock.rs @@ -182,7 +182,7 @@ impl ExtBuilder { let _ = GenesisConfig:: { sys_acc: 42, - ring_balances: vec![ + balances: vec![ (1, 10 * balance_factor, 12), (2, 20 * balance_factor, 12), (3, 300 * balance_factor, 12), diff --git a/srml/kton/src/tests.rs b/srml/kton/src/tests.rs index 555ea5a99..0efd73042 100644 --- a/srml/kton/src/tests.rs +++ b/srml/kton/src/tests.rs @@ -29,20 +29,6 @@ fn deposit_with_decimals_pre() { Kton::deposit(Origin::signed(21), 1_000_000_000 * COIN, 36); } -#[test] -fn build_genesis_storage_should_work() { - with_externalities(&mut ExtBuilder::default() - .existential_deposit(1).build(), || { - - assert_eq!(Kton::free_balance(&1), 1 * COIN); - assert_eq!(Kton::free_balance(&2), 2 * COIN); - assert_eq!(Kton::free_balance(&3), 30 * COIN); - assert_eq!(Kton::free_balance(&4), 40 * COIN); - - assert_eq!(Kton::total_issuance(), (40 + 30 + 2 + 1) * COIN); - }); - -} #[test] fn ext_builer_should_work() { diff --git a/srml/staking/src/mock.rs b/srml/staking/src/mock.rs index 3dde921d8..212bed923 100644 --- a/srml/staking/src/mock.rs +++ b/srml/staking/src/mock.rs @@ -249,7 +249,7 @@ impl ExtBuilder { vesting: vec![], }.assimilate_storage(&mut t, &mut c); let _ = kton::GenesisConfig:: { - ring_balances : vec![ + balances : vec![ (1, 10 * balance_factor, 12), (2, 20 * balance_factor, 12), (3, 300 * balance_factor, 12), diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index c3ab46691..5d610ee3b 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -53,6 +53,9 @@ fn test_env_build() { // initial build storage should work // controller in session.validators assert_eq!(Session::validators(), vec![10, 1, 20]); + // 21 - the minimum bonded + assert_eq!(Staking::stakers(&21), Exposure { total: 1000, own: 1000, others: vec![IndividualExposure {who: 101, value: 0}]}); + assert_eq!(Staking::stakers(&11), Exposure { total: 100 * COIN, own: 100 * COIN, others: vec![]}); // stash in staking.current_elected assert_eq!(Staking::current_elected(), vec![11, 2, 21]); @@ -83,8 +86,12 @@ fn offline_should_slash_and_disable() { Staking::on_offline_validator(90, 4); start_era(2); + // acc 21-20 will not be a validator because it failed to meet the standard assert_eq!(Staking::current_elected(), vec![11, 91, 81]); -// assert_eq!(Session::validators(), vec![11, 91, 81]); + assert_eq!(Staking::stakers(&91), Exposure {total: 21 * COIN, own: 21 * COIN, others: vec![]}); + // out of validator set, status related will be cleared + assert_eq!(Staking::stakers(&21), Exposure { total: 0, own: 0, others: vec![]}); + assert_eq!(Session::validators(), vec![10, 90, 80]); }); }