From eaff0be6bf7bbedb2cb2f3c9e09d918a99c3ce2f Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 30 Nov 2018 14:22:39 +0800 Subject: [PATCH] Hotfix/ensure name is empty (#129) * Ensure name of intention is empty * Adjust PCX precision * Don't use expect() * Change PCX precision from 8 to 3 * Tweak ensures --- cxrml/mining/staking/src/lib.rs | 83 ++++++++++++--------------------- pool/src/pool.rs | 9 ++-- src/genesis_config.rs | 20 +++++--- 3 files changed, 49 insertions(+), 63 deletions(-) diff --git a/cxrml/mining/staking/src/lib.rs b/cxrml/mining/staking/src/lib.rs index 840f30fe1bc24..972da961a2a9f 100644 --- a/cxrml/mining/staking/src/lib.rs +++ b/cxrml/mining/staking/src/lib.rs @@ -480,7 +480,7 @@ impl Module { tokenbalances::is_valid_symbol(&url)?; ensure!( - !>::get(&intention).name.is_empty(), + >::get(&intention).name.is_empty(), "Cannot register if already registered." ); @@ -536,12 +536,13 @@ impl Module { cxsupport::Module::::handle_fee_before(&who, Self::activate_fee(), true, || Ok(()))?; ensure!( - Self::intentions().iter().find(|&t| t == &who).is_some(), - "Cannot activate if transactor is not an intention." + !>::get(&who).is_active, + "Cannot activate if already active." ); + ensure!( - !>::get(&who).is_active, - "Cannot activate if already activated." + Self::intentions().iter().find(|&t| t == &who).is_some(), + "Cannot activate if transactor is not an intention." ); let mut iprof = >::get(&who); @@ -558,20 +559,21 @@ impl Module { let who = ensure_signed(origin)?; cxsupport::Module::::handle_fee_before(&who, Self::deactivate_fee(), true, || Ok(()))?; + ensure!( + >::get(&who).is_active, + "Cannot deactivate if already inactive." + ); + ensure!( Self::intentions().iter().find(|&t| t == &who).is_some(), "Cannot deactivate if transactor is not an intention." ); + // deactivate fails in degenerate case of having too few existing staked parties if Self::intentions().len() <= Self::minimum_validator_count() as usize { return Err("cannot deactivate when there are too few staked participants"); } - ensure!( - >::get(&who).is_active, - "Cannot deactivate if already inactive." - ); - let mut iprof = >::get(&who); iprof.is_active = false; >::insert(who, iprof); @@ -585,21 +587,17 @@ impl Module { cxsupport::Module::::handle_fee_before(&who, Self::stake_fee(), true, || Ok(()))?; ensure!(value.as_() > 0, "Cannot stake zero."); - // TODO Take care of precision later - // ensure!( - // value.as_() % 10_u64.pow(>::chainx_precision() as u32) == 0, - // "Cannot stake decimal." - // ); - - // let value = T::Balance::sa( - // value.as_() / 10_u64.pow(>::chainx_precision() as u32), - // ); ensure!( value <= >::free_balance(&who), "Cannot stake if amount greater than your free balance." ); + ensure!( + Self::intentions().iter().find(|&t| t == &who).is_some(), + "Cannot stake if transactor is not an intention." + ); + Self::apply_stake(&who, value)?; Ok(()) @@ -610,25 +608,18 @@ impl Module { let who = ensure_signed(origin)?; cxsupport::Module::::handle_fee_before(&who, Self::unstake_fee(), true, || Ok(()))?; - ensure!( - Self::intentions().iter().find(|&t| t == &who).is_some(), - "Cannot unstake if transactor is not an intention." - ); - ensure!(value.as_() > 0, "Cannot unstake zero."); - // ensure!( - // value.as_() % 10_u64.pow(>::chainx_precision() as u32) == 0, - // "Cannot stake decimal." - // ); - - // let value = T::Balance::sa( - // value.as_() / 10_u64.pow(>::chainx_precision() as u32), - // ); let current_nomination = Self::nomination_record_of(&who, &who).nomination; + ensure!( value <= current_nomination, - "Cannot unstake if amount greater than your free balance." + "Cannot unstake if amount greater than your current nomination." + ); + + ensure!( + Self::intentions().iter().find(|&t| t == &who).is_some(), + "Cannot unstake if transactor is not an intention." ); let mut nprof = >::get(&who); @@ -656,19 +647,15 @@ impl Module { ensure!(value.as_() > 0, "Cannot stake zero."); + ensure!( + Self::intentions().iter().find(|&t| t == &target).is_some(), + "cannot nominate if target is not an intention." + ); + if Self::intentions().iter().find(|&t| t == &who).is_some() { ensure!(who != target, "cannot nominate per se as an intention."); } - // ensure!( - // value.as_() % 10_u64.pow(>::chainx_precision() as u32) == 0, - // "Cannot stake decimal." - // ); - - // let value = T::Balance::sa( - // value.as_() / 10_u64.pow(>::chainx_precision() as u32), - // ); - ensure!( value <= >::free_balance(&who), "Cannot nominate if greater than your avaliable free balance." @@ -744,6 +731,8 @@ impl Module { || Ok(()), )?; + ensure!(value.as_() > 0, "Cannot unnominate zero."); + let target = >::lookup(target)?; let nprof = >::get(&source); @@ -753,16 +742,6 @@ impl Module { "Cannot claim if target is not your nominee." ); - ensure!(value.as_() > 0, "Cannot unnominate zero."); - // ensure!( - // value.as_() % 10_u64.pow(>::chainx_precision() as u32) == 0, - // "Cannot stake decimal." - // ); - - // let value = T::Balance::sa( - // value.as_() / 10_u64.pow(>::chainx_precision() as u32), - // ); - let mut record = Self::nomination_record_of(&source, &target); let current_nomination = record.nomination; diff --git a/pool/src/pool.rs b/pool/src/pool.rs index fea44c4564b9e..086c5f85f3ac8 100644 --- a/pool/src/pool.rs +++ b/pool/src/pool.rs @@ -293,11 +293,10 @@ where Ok(xt) => Some(*xt.hash()), Err(e) => match e.into_pool_error() { Ok(e) => match e.kind() { - extrinsic_pool::ErrorKind::AlreadyImported(hash) => Some( - ::std::str::FromStr::from_str(&hash) - .map_err(|_| {}) - .expect("Hash string is always valid"), - ), + extrinsic_pool::ErrorKind::AlreadyImported(hash) => { + warn!("{:?} already imported", hash); + None + } _ => { debug!("Error adding transaction to the pool: {:?}", e); None diff --git a/src/genesis_config.rs b/src/genesis_config.rs index 52e64853e15b0..168138eb55a9a 100644 --- a/src/genesis_config.rs +++ b/src/genesis_config.rs @@ -53,6 +53,8 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { const HOURS: u64 = MINUTES * 60; const DAYS: u64 = HOURS * 24; + let pcx_precision = 3_u16; + let normalize = |n: u128| n * 10_u128.pow(pcx_precision as u32); let balances_config = BalancesConfig { transaction_base_fee: 1, transaction_byte_fee: 0, @@ -61,11 +63,17 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { creation_fee: 0, reclaim_rebate: 0, balances: vec![ - (Keyring::Alice.to_raw_public().into(), 1_000_000), - (Keyring::Bob.to_raw_public().into(), 1_000_000), - (Keyring::Charlie.to_raw_public().into(), 1_000_000), - (Keyring::Dave.to_raw_public().into(), 1_000_000), - (Keyring::Ferdie.to_raw_public().into(), 996_000_000), + (Keyring::Alice.to_raw_public().into(), normalize(1_000_000)), + (Keyring::Bob.to_raw_public().into(), normalize(1_000_000)), + ( + Keyring::Charlie.to_raw_public().into(), + normalize(1_000_000), + ), + (Keyring::Dave.to_raw_public().into(), normalize(1_000_000)), + ( + Keyring::Ferdie.to_raw_public().into(), + normalize(996_000_000), + ), ], }; let balances_config_copy = BalancesConfigCopy::create_from_src(&balances_config).src(); @@ -118,7 +126,7 @@ pub fn testnet_genesis(chainspec: ChainSpec) -> GenesisConfig { fee_buy_account: substrate_primitives::H256([1; 32]), }), tokenbalances: Some(TokenBalancesConfig { - chainx_precision: 8, + chainx_precision: pcx_precision, // token_list: Vec<(Token, Vec<(T::AccountId, T::TokenBalance)>)> // e.g. [("btc", [(account1, value), (account2, value)].to_vec()), ("eth", [(account1, value), (account2, value)].to_vec())] token_list: vec![