Browse Source

runtime: Remove StakingHandler mocks copies.

Shamil Gadelshin 4 years ago
parent
commit
efca0eeaf3

+ 3 - 0
Cargo.lock

@@ -4060,6 +4060,7 @@ dependencies = [
  "sp-runtime",
  "sp-staking",
  "sp-std",
+ "staking-handler",
  "strum 0.19.5",
 ]
 
@@ -4152,6 +4153,7 @@ dependencies = [
  "sp-io",
  "sp-runtime",
  "sp-std",
+ "staking-handler",
 ]
 
 [[package]]
@@ -4261,6 +4263,7 @@ dependencies = [
  "sp-io",
  "sp-runtime",
  "sp-std",
+ "staking-handler",
 ]
 
 [[package]]

+ 1 - 0
runtime-modules/proposals/codex/Cargo.toml

@@ -33,6 +33,7 @@ sp-staking = { package = 'sp-staking', default-features = false, git = 'https://
 pallet-staking-reward-curve = { package = 'pallet-staking-reward-curve', default-features = false, git = 'https://github.com/paritytech/substrate.git', rev = 'a200cdb93c6af5763b9c7bf313fa708764ac88ca'}
 recurring-rewards = { package = 'pallet-recurring-reward', default-features = false, path = '../../recurring-reward'}
 strum = {version = "0.19", default-features = false}
+staking-handler = { package = 'staking-handler', default-features = false, path = '../../staking-handler'}
 
 [features]
 default = ['std']

+ 0 - 2
runtime-modules/proposals/codex/src/tests/mock/mod.rs → runtime-modules/proposals/codex/src/tests/mock.rs

@@ -16,8 +16,6 @@ use crate::{ProposalDetailsOf, ProposalEncoder, ProposalParameters};
 use proposals_engine::VotersParameters;
 use sp_runtime::testing::TestXt;
 
-mod staking_handler;
-
 impl_outer_origin! {
     pub enum Origin for Test {}
 }

+ 0 - 98
runtime-modules/proposals/codex/src/tests/mock/staking_handler.rs

@@ -1,98 +0,0 @@
-use frame_support::dispatch::{DispatchError, DispatchResult};
-use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
-use membership::staking_handler::{BalanceOf, MemberId, StakingHandler};
-use sp_arithmetic::traits::Zero;
-use sp_std::marker::PhantomData;
-
-/// Implementation of the StakingHandler.
-pub struct StakingManager<
-    T: frame_system::Trait + membership::Trait + balances::Trait,
-    LockId: Get<LockIdentifier>,
-> {
-    trait_marker: PhantomData<T>,
-    lock_id_marker: PhantomData<LockId>,
-}
-
-impl<T: frame_system::Trait + membership::Trait + balances::Trait, LockId: Get<LockIdentifier>>
-    StakingHandler<T> for StakingManager<T, LockId>
-{
-    fn lock(account_id: &T::AccountId, amount: BalanceOf<T>) {
-        <balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, WithdrawReasons::all())
-    }
-
-    fn unlock(account_id: &T::AccountId) {
-        T::Currency::remove_lock(LockId::get(), &account_id);
-    }
-
-    fn slash(account_id: &T::AccountId, amount: Option<BalanceOf<T>>) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        let mut actually_slashed_balance = Default::default();
-        if let Some(existing_lock) = existing_lock {
-            Self::unlock(&account_id);
-
-            let mut slashable_amount = existing_lock.amount;
-            if let Some(amount) = amount {
-                if existing_lock.amount > amount {
-                    let new_amount = existing_lock.amount - amount;
-                    Self::lock(&account_id, new_amount);
-
-                    slashable_amount = amount;
-                }
-            }
-
-            let _ = <balances::Module<T>>::slash(&account_id, slashable_amount);
-
-            actually_slashed_balance = slashable_amount
-        }
-
-        actually_slashed_balance
-    }
-
-    fn set_stake(account_id: &T::AccountId, new_stake: BalanceOf<T>) -> DispatchResult {
-        let current_stake = Self::current_stake(account_id);
-
-        //Unlock previous stake if its not zero.
-        if current_stake > Zero::zero() {
-            Self::unlock(account_id);
-        }
-
-        if !Self::is_enough_balance_for_stake(account_id, new_stake) {
-            //Restore previous stake if its not zero.
-            if current_stake > Zero::zero() {
-                Self::lock(account_id, current_stake);
-            }
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
-
-        Self::lock(account_id, new_stake);
-
-        Ok(())
-    }
-
-    fn is_member_staking_account(_member_id: &MemberId<T>, _account_id: &T::AccountId) -> bool {
-        true
-    }
-
-    fn is_account_free_of_conflicting_stakes(account_id: &T::AccountId) -> bool {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.is_none()
-    }
-
-    fn is_enough_balance_for_stake(account_id: &T::AccountId, amount: BalanceOf<T>) -> bool {
-        <balances::Module<T>>::usable_balance(account_id) >= amount
-    }
-
-    fn current_stake(account_id: &T::AccountId) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.map_or(Zero::zero(), |lock| lock.amount)
-    }
-}

+ 0 - 1
runtime-modules/proposals/engine/src/tests/mock/mod.rs

@@ -17,7 +17,6 @@ use sp_runtime::{
 };
 
 pub(crate) mod proposals;
-pub(crate) mod staking_handler;
 
 use crate::ProposalObserver;
 pub use proposals::*;

+ 0 - 98
runtime-modules/proposals/engine/src/tests/mock/staking_handler.rs

@@ -1,98 +0,0 @@
-use frame_support::dispatch::{DispatchError, DispatchResult};
-use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
-use membership::staking_handler::{BalanceOf, MemberId, StakingHandler};
-use sp_arithmetic::traits::Zero;
-use sp_std::marker::PhantomData;
-
-/// Implementation of the StakingHandler.
-pub struct StakingManager<
-    T: frame_system::Trait + membership::Trait + balances::Trait,
-    LockId: Get<LockIdentifier>,
-> {
-    trait_marker: PhantomData<T>,
-    lock_id_marker: PhantomData<LockId>,
-}
-
-impl<T: frame_system::Trait + membership::Trait + balances::Trait, LockId: Get<LockIdentifier>>
-    StakingHandler<T> for StakingManager<T, LockId>
-{
-    fn lock(account_id: &T::AccountId, amount: BalanceOf<T>) {
-        <balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, WithdrawReasons::all())
-    }
-
-    fn unlock(account_id: &T::AccountId) {
-        T::Currency::remove_lock(LockId::get(), &account_id);
-    }
-
-    fn slash(account_id: &T::AccountId, amount: Option<BalanceOf<T>>) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        let mut actually_slashed_balance = Default::default();
-        if let Some(existing_lock) = existing_lock {
-            Self::unlock(&account_id);
-
-            let mut slashable_amount = existing_lock.amount;
-            if let Some(amount) = amount {
-                if existing_lock.amount > amount {
-                    let new_amount = existing_lock.amount - amount;
-                    Self::lock(&account_id, new_amount);
-
-                    slashable_amount = amount;
-                }
-            }
-
-            let _ = <balances::Module<T>>::slash(&account_id, slashable_amount);
-
-            actually_slashed_balance = slashable_amount
-        }
-
-        actually_slashed_balance
-    }
-
-    fn set_stake(account_id: &T::AccountId, new_stake: BalanceOf<T>) -> DispatchResult {
-        let current_stake = Self::current_stake(account_id);
-
-        //Unlock previous stake if its not zero.
-        if current_stake > Zero::zero() {
-            Self::unlock(account_id);
-        }
-
-        if !Self::is_enough_balance_for_stake(account_id, new_stake) {
-            //Restore previous stake if its not zero.
-            if current_stake > Zero::zero() {
-                Self::lock(account_id, current_stake);
-            }
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
-
-        Self::lock(account_id, new_stake);
-
-        Ok(())
-    }
-
-    fn is_member_staking_account(_member_id: &MemberId<T>, _account_id: &T::AccountId) -> bool {
-        true
-    }
-
-    fn is_account_free_of_conflicting_stakes(account_id: &T::AccountId) -> bool {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.is_none()
-    }
-
-    fn is_enough_balance_for_stake(account_id: &T::AccountId, amount: BalanceOf<T>) -> bool {
-        <balances::Module<T>>::usable_balance(account_id) >= amount
-    }
-
-    fn current_stake(account_id: &T::AccountId) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.map_or(Zero::zero(), |lock| lock.amount)
-    }
-}

+ 1 - 0
runtime-modules/service-discovery/Cargo.toml

@@ -25,6 +25,7 @@ hiring = { package = 'pallet-hiring', default-features = false, path = '../hirin
 minting = { package = 'pallet-token-mint', default-features = false, path = '../token-minting'}
 recurringrewards = { package = 'pallet-recurring-reward', default-features = false, path = '../recurring-reward'}
 common = { package = 'pallet-common', default-features = false, path = '../common'}
+staking-handler = { package = 'staking-handler', default-features = false, path = '../staking-handler'}
 
 [features]
 default = ['std']

+ 0 - 2
runtime-modules/service-discovery/src/mock/mod.rs → runtime-modules/service-discovery/src/mock.rs

@@ -13,8 +13,6 @@ use sp_runtime::{
 // The storage working group instance alias.
 pub type StorageWorkingGroupInstance = working_group::Instance2;
 
-mod staking_handler;
-
 mod working_group_mod {
     pub use super::StorageWorkingGroupInstance;
     pub use working_group::Event;

+ 0 - 98
runtime-modules/service-discovery/src/mock/staking_handler.rs

@@ -1,98 +0,0 @@
-use frame_support::dispatch::{DispatchError, DispatchResult};
-use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
-use membership::staking_handler::{BalanceOf, MemberId, StakingHandler};
-use sp_arithmetic::traits::Zero;
-use sp_std::marker::PhantomData;
-
-/// Implementation of the StakingHandler.
-pub struct StakingManager<
-    T: frame_system::Trait + membership::Trait + balances::Trait,
-    LockId: Get<LockIdentifier>,
-> {
-    trait_marker: PhantomData<T>,
-    lock_id_marker: PhantomData<LockId>,
-}
-
-impl<T: frame_system::Trait + membership::Trait + balances::Trait, LockId: Get<LockIdentifier>>
-    StakingHandler<T> for StakingManager<T, LockId>
-{
-    fn lock(account_id: &T::AccountId, amount: BalanceOf<T>) {
-        <balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, WithdrawReasons::all())
-    }
-
-    fn unlock(account_id: &T::AccountId) {
-        T::Currency::remove_lock(LockId::get(), &account_id);
-    }
-
-    fn slash(account_id: &T::AccountId, amount: Option<BalanceOf<T>>) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        let mut actually_slashed_balance = Default::default();
-        if let Some(existing_lock) = existing_lock {
-            Self::unlock(&account_id);
-
-            let mut slashable_amount = existing_lock.amount;
-            if let Some(amount) = amount {
-                if existing_lock.amount > amount {
-                    let new_amount = existing_lock.amount - amount;
-                    Self::lock(&account_id, new_amount);
-
-                    slashable_amount = amount;
-                }
-            }
-
-            let _ = <balances::Module<T>>::slash(&account_id, slashable_amount);
-
-            actually_slashed_balance = slashable_amount
-        }
-
-        actually_slashed_balance
-    }
-
-    fn set_stake(account_id: &T::AccountId, new_stake: BalanceOf<T>) -> DispatchResult {
-        let current_stake = Self::current_stake(account_id);
-
-        //Unlock previous stake if its not zero.
-        if current_stake > Zero::zero() {
-            Self::unlock(account_id);
-        }
-
-        if !Self::is_enough_balance_for_stake(account_id, new_stake) {
-            //Restore previous stake if its not zero.
-            if current_stake > Zero::zero() {
-                Self::lock(account_id, current_stake);
-            }
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
-
-        Self::lock(account_id, new_stake);
-
-        Ok(())
-    }
-
-    fn is_member_staking_account(_member_id: &MemberId<T>, _account_id: &T::AccountId) -> bool {
-        true
-    }
-
-    fn is_account_free_of_conflicting_stakes(account_id: &T::AccountId) -> bool {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.is_none()
-    }
-
-    fn is_enough_balance_for_stake(account_id: &T::AccountId, amount: BalanceOf<T>) -> bool {
-        <balances::Module<T>>::usable_balance(account_id) >= amount
-    }
-
-    fn current_stake(account_id: &T::AccountId) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.map_or(Zero::zero(), |lock| lock.amount)
-    }
-}

+ 1 - 0
runtime-modules/storage/Cargo.toml

@@ -25,6 +25,7 @@ stake = { package = 'pallet-stake', default-features = false, path = '../stake'}
 hiring = { package = 'pallet-hiring', default-features = false, path = '../hiring'}
 minting = { package = 'pallet-token-mint', default-features = false, path = '../token-minting'}
 recurringrewards = { package = 'pallet-recurring-reward', default-features = false, path = '../recurring-reward'}
+staking-handler = { package = 'staking-handler', default-features = false, path = '../staking-handler'}
 
 [features]
 default = ['std']

+ 0 - 2
runtime-modules/storage/src/tests/mock/mod.rs → runtime-modules/storage/src/tests/mock.rs

@@ -17,8 +17,6 @@ pub use crate::{data_directory, data_object_storage_registry, data_object_type_r
 use common::currency::GovernanceCurrency;
 use membership;
 
-mod staking_handler;
-
 mod working_group_mod {
     pub use super::StorageWorkingGroupInstance;
     pub use working_group::Event;

+ 0 - 98
runtime-modules/storage/src/tests/mock/staking_handler.rs

@@ -1,98 +0,0 @@
-use frame_support::dispatch::{DispatchError, DispatchResult};
-use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
-use membership::staking_handler::{BalanceOf, MemberId, StakingHandler};
-use sp_arithmetic::traits::Zero;
-use sp_std::marker::PhantomData;
-
-/// Implementation of the StakingHandler.
-pub struct StakingManager<
-    T: frame_system::Trait + membership::Trait + balances::Trait,
-    LockId: Get<LockIdentifier>,
-> {
-    trait_marker: PhantomData<T>,
-    lock_id_marker: PhantomData<LockId>,
-}
-
-impl<T: frame_system::Trait + membership::Trait + balances::Trait, LockId: Get<LockIdentifier>>
-    StakingHandler<T> for StakingManager<T, LockId>
-{
-    fn lock(account_id: &T::AccountId, amount: BalanceOf<T>) {
-        <balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, WithdrawReasons::all())
-    }
-
-    fn unlock(account_id: &T::AccountId) {
-        T::Currency::remove_lock(LockId::get(), &account_id);
-    }
-
-    fn slash(account_id: &T::AccountId, amount: Option<BalanceOf<T>>) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        let mut actually_slashed_balance = Default::default();
-        if let Some(existing_lock) = existing_lock {
-            Self::unlock(&account_id);
-
-            let mut slashable_amount = existing_lock.amount;
-            if let Some(amount) = amount {
-                if existing_lock.amount > amount {
-                    let new_amount = existing_lock.amount - amount;
-                    Self::lock(&account_id, new_amount);
-
-                    slashable_amount = amount;
-                }
-            }
-
-            let _ = <balances::Module<T>>::slash(&account_id, slashable_amount);
-
-            actually_slashed_balance = slashable_amount
-        }
-
-        actually_slashed_balance
-    }
-
-    fn set_stake(account_id: &T::AccountId, new_stake: BalanceOf<T>) -> DispatchResult {
-        let current_stake = Self::current_stake(account_id);
-
-        //Unlock previous stake if its not zero.
-        if current_stake > Zero::zero() {
-            Self::unlock(account_id);
-        }
-
-        if !Self::is_enough_balance_for_stake(account_id, new_stake) {
-            //Restore previous stake if its not zero.
-            if current_stake > Zero::zero() {
-                Self::lock(account_id, current_stake);
-            }
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
-
-        Self::lock(account_id, new_stake);
-
-        Ok(())
-    }
-
-    fn is_member_staking_account(_member_id: &MemberId<T>, _account_id: &T::AccountId) -> bool {
-        true
-    }
-
-    fn is_account_free_of_conflicting_stakes(account_id: &T::AccountId) -> bool {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.is_none()
-    }
-
-    fn is_enough_balance_for_stake(account_id: &T::AccountId, amount: BalanceOf<T>) -> bool {
-        <balances::Module<T>>::usable_balance(account_id) >= amount
-    }
-
-    fn current_stake(account_id: &T::AccountId) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.map_or(Zero::zero(), |lock| lock.amount)
-    }
-}

+ 0 - 2
runtime-modules/working-group/src/tests/mock/mod.rs → runtime-modules/working-group/src/tests/mock.rs

@@ -14,8 +14,6 @@ impl_outer_origin! {
     pub enum Origin for Test {}
 }
 
-mod staking_handler;
-
 mod working_group {
     pub use crate::Event;
 }

+ 0 - 98
runtime-modules/working-group/src/tests/mock/staking_handler.rs

@@ -1,98 +0,0 @@
-use frame_support::dispatch::{DispatchError, DispatchResult};
-use frame_support::traits::{Currency, Get, LockIdentifier, LockableCurrency, WithdrawReasons};
-use membership::staking_handler::{BalanceOf, MemberId, StakingHandler};
-use sp_arithmetic::traits::Zero;
-use sp_std::marker::PhantomData;
-
-/// Implementation of the StakingHandler.
-pub struct StakingManager<
-    T: frame_system::Trait + membership::Trait + balances::Trait,
-    LockId: Get<LockIdentifier>,
-> {
-    trait_marker: PhantomData<T>,
-    lock_id_marker: PhantomData<LockId>,
-}
-
-impl<T: frame_system::Trait + membership::Trait + balances::Trait, LockId: Get<LockIdentifier>>
-    StakingHandler<T> for StakingManager<T, LockId>
-{
-    fn lock(account_id: &T::AccountId, amount: BalanceOf<T>) {
-        <balances::Module<T>>::set_lock(LockId::get(), &account_id, amount, WithdrawReasons::all())
-    }
-
-    fn unlock(account_id: &T::AccountId) {
-        T::Currency::remove_lock(LockId::get(), &account_id);
-    }
-
-    fn slash(account_id: &T::AccountId, amount: Option<BalanceOf<T>>) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        let mut actually_slashed_balance = Default::default();
-        if let Some(existing_lock) = existing_lock {
-            Self::unlock(&account_id);
-
-            let mut slashable_amount = existing_lock.amount;
-            if let Some(amount) = amount {
-                if existing_lock.amount > amount {
-                    let new_amount = existing_lock.amount - amount;
-                    Self::lock(&account_id, new_amount);
-
-                    slashable_amount = amount;
-                }
-            }
-
-            let _ = <balances::Module<T>>::slash(&account_id, slashable_amount);
-
-            actually_slashed_balance = slashable_amount
-        }
-
-        actually_slashed_balance
-    }
-
-    fn set_stake(account_id: &T::AccountId, new_stake: BalanceOf<T>) -> DispatchResult {
-        let current_stake = Self::current_stake(account_id);
-
-        //Unlock previous stake if its not zero.
-        if current_stake > Zero::zero() {
-            Self::unlock(account_id);
-        }
-
-        if !Self::is_enough_balance_for_stake(account_id, new_stake) {
-            //Restore previous stake if its not zero.
-            if current_stake > Zero::zero() {
-                Self::lock(account_id, current_stake);
-            }
-            return Err(DispatchError::Other("Not enough balance for a new stake."));
-        }
-
-        Self::lock(account_id, new_stake);
-
-        Ok(())
-    }
-
-    fn is_member_staking_account(_member_id: &MemberId<T>, _account_id: &T::AccountId) -> bool {
-        true
-    }
-
-    fn is_account_free_of_conflicting_stakes(account_id: &T::AccountId) -> bool {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.is_none()
-    }
-
-    fn is_enough_balance_for_stake(account_id: &T::AccountId, amount: BalanceOf<T>) -> bool {
-        <balances::Module<T>>::usable_balance(account_id) >= amount
-    }
-
-    fn current_stake(account_id: &T::AccountId) -> BalanceOf<T> {
-        let locks = <balances::Module<T>>::locks(&account_id);
-
-        let existing_lock = locks.iter().find(|lock| lock.id == LockId::get());
-
-        existing_lock.map_or(Zero::zero(), |lock| lock.amount)
-    }
-}