From e717c4d6d8af8b8901375881b2ad3a66c8846bf8 Mon Sep 17 00:00:00 2001 From: h4x3rotab Date: Sun, 25 Jun 2023 10:09:37 +0000 Subject: [PATCH 1/4] wpha: base impl of council election --- Cargo.lock | 1 + pallets/phala/Cargo.toml | 5 +- pallets/phala/src/compute/base_pool.rs | 19 +- pallets/phala/src/compute/stake_pool_v2.rs | 10 +- pallets/phala/src/compute/vault.rs | 1 + pallets/phala/src/compute/wrapped_balances.rs | 334 +++++++++++++++++- pallets/phala/src/lib.rs | 3 + standalone/runtime/src/lib.rs | 2 +- 8 files changed, 348 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f48cf1b05..0a2a3c8900 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8171,6 +8171,7 @@ dependencies = [ "pallet-balances", "pallet-collective", "pallet-democracy", + "pallet-elections-phragmen", "pallet-insecure-randomness-collective-flip", "pallet-preimage", "pallet-rmrk-core", diff --git a/pallets/phala/Cargo.toml b/pallets/phala/Cargo.toml index 0dba9e4e64..290f8ff227 100644 --- a/pallets/phala/Cargo.toml +++ b/pallets/phala/Cargo.toml @@ -27,6 +27,7 @@ rmrk-traits = { git = "https://github.com/Phala-Network/rmrk-substrate", branch frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } pallet-insecure-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } +pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } @@ -73,6 +74,7 @@ std = [ "frame-system/std", "pallet-assets/std", "pallet-democracy/std", + "pallet-elections-phragmen/std", "sp-io/std", "sp-std/std", "sp-core/std", @@ -91,7 +93,8 @@ std = [ "pallet-preimage/std", ] runtime-benchmarks = [ - "frame-benchmarking/runtime-benchmarks" + "frame-benchmarking/runtime-benchmarks", + "pallet-elections-phragmen/runtime-benchmarks", ] try-runtime = ["frame-support/try-runtime"] native = [ diff --git a/pallets/phala/src/compute/base_pool.rs b/pallets/phala/src/compute/base_pool.rs index 94fa1ba723..a7fefeea96 100644 --- a/pallets/phala/src/compute/base_pool.rs +++ b/pallets/phala/src/compute/base_pool.rs @@ -293,7 +293,10 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + wrapped_balances::Config + vault::Config, + T: Config + + wrapped_balances::Config + + vault::Config + + pallet_elections_phragmen::Config, { Pallet::::set_nft_attr(self.cid, self.nftid, &self.attr)?; Ok(()) @@ -336,7 +339,10 @@ pub mod pallet { where T: pallet_assets::Config, T: Config, - T: Config + wrapped_balances::Config + vault::Config, + T: Config + + wrapped_balances::Config + + vault::Config + + pallet_elections_phragmen::Config, { pallet_assets::Pallet::::balance( ::WPhaAssetId::get(), @@ -359,7 +365,10 @@ pub mod pallet { sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, T: Config, - T: Config + wrapped_balances::Config + vault::Config, + T: Config + + wrapped_balances::Config + + vault::Config + + pallet_elections_phragmen::Config, { self.total_value += rewards; for vault_staker in &self.value_subscribers { @@ -422,7 +431,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { /// Adds a staker accountid to contribution whitelist. /// @@ -588,7 +597,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + wrapped_balances::Config + vault::Config, + T: Config + wrapped_balances::Config + vault::Config + pallet_elections_phragmen::Config, { /// Returns a [`NftGuard`] object that can read or write to the nft attributes /// diff --git a/pallets/phala/src/compute/stake_pool_v2.rs b/pallets/phala/src/compute/stake_pool_v2.rs index da949e8aa8..2570b67ce2 100644 --- a/pallets/phala/src/compute/stake_pool_v2.rs +++ b/pallets/phala/src/compute/stake_pool_v2.rs @@ -319,7 +319,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { /// Creates a new stake pool #[pallet::call_index(0)] @@ -994,7 +994,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { pub fn do_start_computing( owner: &T::AccountId, @@ -1206,7 +1206,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { /// Called when gk send new payout information. /// Append specific worker's reward balance of current round, @@ -1239,7 +1239,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { fn on_unbound(worker: &WorkerPublicKey, _force: bool) { // Usually called on worker force unbinding (force == true), but it's also possible @@ -1261,7 +1261,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { fn on_stopped( _worker: &WorkerPublicKey, diff --git a/pallets/phala/src/compute/vault.rs b/pallets/phala/src/compute/vault.rs index 9d5db6753d..40bb79a9ea 100644 --- a/pallets/phala/src/compute/vault.rs +++ b/pallets/phala/src/compute/vault.rs @@ -147,6 +147,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, + T: Config + pallet_elections_phragmen::Config, { /// Creates a new vault #[pallet::call_index(0)] diff --git a/pallets/phala/src/compute/wrapped_balances.rs b/pallets/phala/src/compute/wrapped_balances.rs index a2d551d456..bfc7805ce8 100644 --- a/pallets/phala/src/compute/wrapped_balances.rs +++ b/pallets/phala/src/compute/wrapped_balances.rs @@ -8,16 +8,18 @@ pub mod pallet { use crate::pool_proxy::PoolProxy; use crate::registry; use crate::vault; - use crate::{BalanceOf, NegativeImbalanceOf, PhalaConfig}; + use crate::{BalanceOf, NegativeImbalanceOf, PhalaConfig, PositiveImbalanceOf}; use frame_support::traits::tokens::{Fortitude, Precision}; use frame_support::{ pallet_prelude::*, traits::{ - tokens::fungibles::{Inspect, Mutate}, tokens::nonfungibles::InspectEnumerable, - Currency, - ExistenceRequirement::{AllowDeath, KeepAlive}, - OnUnbalanced, StorageVersion, + tokens::{ + fungibles::{Inspect, Mutate}, + BalanceStatus, + }, + Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, + OnUnbalanced, ReservableCurrency, SignedImbalance, StorageVersion, WithdrawReasons, }, }; use frame_system::{pallet_prelude::*, RawOrigin}; @@ -26,6 +28,7 @@ pub mod pallet { use scale_info::TypeInfo; use sp_runtime::traits::Zero; use sp_std::{fmt::Display, prelude::*, result::Result}; + #[pallet::config] pub trait Config: frame_system::Config @@ -88,6 +91,21 @@ pub mod pallet { pub type StakerAccounts = StorageMap<_, Twox64Concat, T::AccountId, FinanceAccount>>; + #[pallet::storage] + #[pallet::getter(fn election_locks)] + pub type ElectionLocks = + StorageMap<_, Twox64Concat, T::AccountId, BalanceOf, ValueQuery>; + + /// The WPHA a user owes the system because of a lack of liquid token. Wills be settled by `slash_reserved()` in the future. + #[pallet::storage] + #[pallet::getter(fn slash_debts)] + pub type SlashDebts = + StorageMap<_, Twox64Concat, T::AccountId, BalanceOf, ValueQuery>; + + #[pallet::storage] + #[pallet::getter(fn election_reserves)] + pub type ElectionReserves = + StorageMap<_, Twox64Concat, T::AccountId, BalanceOf, ValueQuery>; /// Collect the unmintable dust // TODO: since this is the imbalance, consider to mint it in the future. #[pallet::storage] @@ -121,6 +139,11 @@ pub mod pallet { aye_amount: BalanceOf, nay_amount: BalanceOf, }, + ReserveSlashed { + user: T::AccountId, + slash: BalanceOf, + new_debt: BalanceOf, + }, } #[pallet::error] @@ -137,6 +160,241 @@ pub mod pallet { ReferendumOngoing, /// The Iteration exceed the max limitaion IterationsIsNotVaild, + /// The amount of lock is larger than the total balances you owned + LiquidityRestrictions, + /// There is a debt to settle and before it is done you can not unwrap WPha to Pha + WphaNotSettled, + } + + impl Currency for Pallet + where + BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, + T: pallet_assets::Config>, + T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: pallet_balances::Config>, + { + type Balance = BalanceOf; + type PositiveImbalance = PositiveImbalanceOf; + type NegativeImbalance = NegativeImbalanceOf; + fn total_balance(who: &T::AccountId) -> Self::Balance { + Self::get_net_value(who.clone()).expect("Get net value should success; qed.") + } + + fn free_balance(who: &T::AccountId) -> Self::Balance { + Self::total_balance(who) + } + + fn can_slash(who: &T::AccountId, value: Self::Balance) -> bool { + if value.is_zero() { + return true; + } + Self::free_balance(who) >= value + } + + fn total_issuance() -> Self::Balance { + as Inspect>::total_issuance( + T::WPhaAssetId::get(), + ) + } + + fn active_issuance() -> Self::Balance { + as Inspect>::total_issuance( + T::WPhaAssetId::get(), + ) + } + + fn deactivate(_amount: Self::Balance) { + unimplemented!() + } + + fn reactivate(_amount: Self::Balance) { + unimplemented!() + } + + fn minimum_balance() -> Self::Balance { + as Inspect>::minimum_balance( + T::WPhaAssetId::get(), + ) + } + + fn burn(mut _amount: Self::Balance) -> Self::PositiveImbalance { + unimplemented!() + } + + fn issue(mut _amount: Self::Balance) -> Self::NegativeImbalance { + unimplemented!() + } + + fn ensure_can_withdraw( + who: &T::AccountId, + amount: Self::Balance, + _reasons: WithdrawReasons, + new_balance: Self::Balance, + ) -> DispatchResult { + if amount.is_zero() { + return Ok(()); + } + let lock = Self::get_lock(who); + ensure!(new_balance >= lock, Error::::LiquidityRestrictions); + Ok(()) + } + + fn transfer( + _transactor: &T::AccountId, + _dest: &T::AccountId, + _value: Self::Balance, + _existence_requirement: ExistenceRequirement, + ) -> DispatchResult { + unimplemented!() + } + + fn slash( + _who: &T::AccountId, + _value: Self::Balance, + ) -> (Self::NegativeImbalance, Self::Balance) { + unimplemented!() + } + + fn deposit_into_existing( + _who: &T::AccountId, + _value: Self::Balance, + ) -> Result { + unimplemented!() + } + + fn deposit_creating(_who: &T::AccountId, _value: Self::Balance) -> Self::PositiveImbalance { + unimplemented!() + } + + fn withdraw( + _who: &T::AccountId, + _value: Self::Balance, + _reasons: WithdrawReasons, + _liveness: ExistenceRequirement, + ) -> Result { + unimplemented!() + } + + fn make_free_balance_be( + _who: &T::AccountId, + _value: Self::Balance, + ) -> SignedImbalance { + unimplemented!() + } + } + + impl ReservableCurrency for Pallet + where + BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, + T: pallet_assets::Config>, + T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: pallet_balances::Config>, + { + fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool { + Self::total_balance(who) >= value + } + fn reserved_balance(who: &T::AccountId) -> Self::Balance { + ElectionReserves::::get(who) + } + fn reserve(who: &T::AccountId, value: Self::Balance) -> DispatchResult { + let actual = value.min(Self::total_balance(who)); + ElectionReserves::::mutate(who, |reserve| { + *reserve += actual; + }); + Ok(()) + } + fn unreserve(who: &T::AccountId, value: Self::Balance) -> Self::Balance { + let actual = value.min(ElectionReserves::::get(who)); + ElectionReserves::::mutate(who, |reserve| { + *reserve -= actual; + }); + value - actual + } + fn slash_reserved( + who: &T::AccountId, + value: Self::Balance, + ) -> (Self::NegativeImbalance, Self::Balance) { + if value == Zero::zero() { + return (NegativeImbalanceOf::::zero(), Zero::zero()); + } + let mut actual = value.min(ElectionReserves::::get(who)); + let free_wpha: BalanceOf = as Inspect< + T::AccountId, + >>::balance(T::WPhaAssetId::get(), who); + let mut new_debt = Zero::zero(); + if free_wpha < actual { + new_debt = actual - free_wpha; + actual = free_wpha; + SlashDebts::::mutate(who, |debt| { + *debt += new_debt; + }); + } + ElectionReserves::::mutate(who, |reserve| { + *reserve -= actual; + }); + Self::burn_from(who, actual).expect("there are enough WPHA to burn; qed."); + let imbalance = ::Currency::withdraw( + &T::WrappedBalancesAccountId::get(), + actual, + WithdrawReasons::all(), + ExistenceRequirement::AllowDeath, + ) + .expect("slash imbalance should success; qed."); + Self::deposit_event(Event::::ReserveSlashed { + user: who.clone(), + slash: actual, + new_debt, + }); + (imbalance, value - actual) + } + fn repatriate_reserved( + _slashed: &T::AccountId, + _beneficiary: &T::AccountId, + _value: Self::Balance, + _status: BalanceStatus, + ) -> Result { + unimplemented!() + } + } + impl LockableCurrency for Pallet + where + BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, + T: pallet_assets::Config>, + T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: pallet_balances::Config>, + { + type Moment = T::BlockNumber; + type MaxLocks = T::MaxLocks; + fn set_lock( + id: LockIdentifier, + who: &T::AccountId, + amount: Self::Balance, + _reasons: WithdrawReasons, + ) { + Self::ensure_lock_id_supported(id); + if amount == Zero::zero() { + return; + } + ElectionLocks::::insert(who, amount); + } + fn extend_lock( + id: LockIdentifier, + who: &T::AccountId, + amount: Self::Balance, + _reasons: WithdrawReasons, + ) { + Self::ensure_lock_id_supported(id); + if amount == Zero::zero() { + return; + } + ElectionLocks::::mutate(who, |locks| { + *locks += amount; + }); + } + fn remove_lock(id: LockIdentifier, who: &T::AccountId) { + Self::ensure_lock_id_supported(id); + ElectionLocks::::remove(who); + } } impl rmrk_traits::TransferHooks for Pallet @@ -145,7 +403,7 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { fn pre_check( _sender: &T::AccountId, @@ -186,7 +444,8 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + vault::Config, + T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: pallet_balances::Config>, { /// Wraps some pha and gain equal amount of W-PHA /// @@ -200,7 +459,7 @@ pub mod pallet { &user, &T::WrappedBalancesAccountId::get(), amount, - KeepAlive, + ExistenceRequirement::KeepAlive, )?; Self::mint_into(&user, amount)?; if !StakerAccounts::::contains_key(&user) { @@ -224,18 +483,21 @@ pub mod pallet { #[frame_support::transactional] pub fn unwrap_all(origin: OriginFor) -> DispatchResult { let user = ensure_signed(origin)?; + ensure!( + SlashDebts::::get(&user) > Zero::zero(), + Error::::WphaNotSettled, + ); let active_stakes = Self::get_net_value(user.clone())?; let free_stakes: BalanceOf = as Inspect< T::AccountId, >>::balance(T::WPhaAssetId::get(), &user); - let locked = - StakerAccounts::::get(&user).map_or(Zero::zero(), |status| status.locked); + let locked = Self::get_lock(&user); let withdraw_amount = (active_stakes - locked).min(free_stakes); ::Currency::transfer( &T::WrappedBalancesAccountId::get(), &user, withdraw_amount, - AllowDeath, + ExistenceRequirement::AllowDeath, )?; Self::burn_from(&user, withdraw_amount)?; Ok(()) @@ -249,6 +511,10 @@ pub mod pallet { #[frame_support::transactional] pub fn unwrap(origin: OriginFor, amount: BalanceOf) -> DispatchResult { let user = ensure_signed(origin)?; + ensure!( + SlashDebts::::get(&user) > Zero::zero(), + Error::::WphaNotSettled, + ); let free_stakes: BalanceOf = as Inspect< T::AccountId, >>::balance(T::WPhaAssetId::get(), &user); @@ -257,8 +523,7 @@ pub mod pallet { Error::::UnwrapAmountExceedsAvaliableStake ); let active_stakes = Self::get_net_value(user.clone())?; - let locked = - StakerAccounts::::get(&user).map_or(Zero::zero(), |status| status.locked); + let locked = Self::get_lock(&user); ensure!( amount + locked <= active_stakes, Error::::UnwrapAmountExceedsAvaliableStake, @@ -267,7 +532,7 @@ pub mod pallet { &T::WrappedBalancesAccountId::get(), &user, amount, - AllowDeath, + ExistenceRequirement::AllowDeath, )?; Self::burn_from(&user, amount)?; Self::deposit_event(Event::::Unwrapped { user, amount }); @@ -366,13 +631,40 @@ pub mod pallet { } Ok(()) } + #[pallet::call_index(6)] + #[pallet::weight({0})] + #[frame_support::transactional] + pub fn settle_balance_debt(origin: OriginFor) -> DispatchResult { + let who = ensure_signed(origin)?; + let debt = SlashDebts::::get(&who); + if debt == Zero::zero() { + return Ok(()); + } + let free_wpha = as Inspect>::balance( + T::WPhaAssetId::get(), + &who, + ); + let slash = debt.min(free_wpha); + let mut new_debt = Zero::zero(); + if debt > free_wpha { + new_debt = debt - free_wpha; + } + let (imbalance, _) = Self::slash_reserved(&who, slash); + T::OnSlashed::on_unbalanced(imbalance); + if new_debt != Zero::zero() { + SlashDebts::::insert(&who, new_debt); + } else { + SlashDebts::::remove(&who); + } + Ok(()) + } } impl Pallet where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: Config + vault::Config + pallet_elections_phragmen::Config, { /// Gets W-PHA's asset id pub fn get_asset_id() -> u32 { @@ -518,6 +810,18 @@ pub mod pallet { matches!(vote_info, Some(ReferendumInfo::Ongoing(_))) } + fn get_lock(who: &T::AccountId) -> BalanceOf { + let lock = StakerAccounts::::get(who).map_or(Zero::zero(), |status| status.locked); + let election_lock = ElectionLocks::::get(who); + let election_reserve = ElectionReserves::::get(who); + lock.max(election_lock).max(election_reserve) + } + + fn ensure_lock_id_supported(id: LockIdentifier) { + if id != ::PalletId::get() { + panic!("LockIdentifier is not supported."); + } + } /// Returns the minimum balance of WPHA pub fn min_balance() -> BalanceOf { if ! as Inspect>::asset_exists( diff --git a/pallets/phala/src/lib.rs b/pallets/phala/src/lib.rs index cff335a980..5d379ad6da 100644 --- a/pallets/phala/src/lib.rs +++ b/pallets/phala/src/lib.rs @@ -39,6 +39,9 @@ type NegativeImbalanceOf = <::Currency as frame_support::tr ::AccountId, >>::NegativeImbalance; +type PositiveImbalanceOf = <::Currency as frame_support::traits::Currency< + ::AccountId, +>>::PositiveImbalance; // Alias pub use compute::base_pool as pallet_base_pool; pub use compute::computation as pallet_computation; diff --git a/standalone/runtime/src/lib.rs b/standalone/runtime/src/lib.rs index a383cbdfa4..3c72da1d31 100644 --- a/standalone/runtime/src/lib.rs +++ b/standalone/runtime/src/lib.rs @@ -931,7 +931,7 @@ const_assert!(DesiredMembers::get() <= CouncilMaxMembers::get()); impl pallet_elections_phragmen::Config for Runtime { type RuntimeEvent = RuntimeEvent; type PalletId = ElectionsPhragmenPalletId; - type Currency = Balances; + type Currency = PhalaWrappedBalances; type ChangeMembers = Council; // NOTE: this implies that council's genesis members cannot be set directly and must come from // this module. From 0b82403d5466518b34f8587f784bde817869c69a Mon Sep 17 00:00:00 2001 From: h4x3rotab Date: Sun, 25 Jun 2023 11:25:41 +0000 Subject: [PATCH 2/4] simplify pallet deps --- pallets/phala/src/compute/base_pool.rs | 13 +++++-------- pallets/phala/src/compute/stake_pool_v2.rs | 10 +++++----- pallets/phala/src/compute/vault.rs | 2 +- pallets/phala/src/compute/wrapped_balances.rs | 19 ++++++++++--------- pallets/phala/src/mock.rs | 2 ++ standalone/runtime/src/lib.rs | 1 + 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/pallets/phala/src/compute/base_pool.rs b/pallets/phala/src/compute/base_pool.rs index a7fefeea96..9b48c81a24 100644 --- a/pallets/phala/src/compute/base_pool.rs +++ b/pallets/phala/src/compute/base_pool.rs @@ -295,8 +295,7 @@ pub mod pallet { T: pallet_assets::Config>, T: Config + wrapped_balances::Config - + vault::Config - + pallet_elections_phragmen::Config, + + vault::Config, { Pallet::::set_nft_attr(self.cid, self.nftid, &self.attr)?; Ok(()) @@ -341,8 +340,7 @@ pub mod pallet { T: Config, T: Config + wrapped_balances::Config - + vault::Config - + pallet_elections_phragmen::Config, + + vault::Config, { pallet_assets::Pallet::::balance( ::WPhaAssetId::get(), @@ -367,8 +365,7 @@ pub mod pallet { T: Config, T: Config + wrapped_balances::Config - + vault::Config - + pallet_elections_phragmen::Config, + + vault::Config, { self.total_value += rewards; for vault_staker in &self.value_subscribers { @@ -431,7 +428,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { /// Adds a staker accountid to contribution whitelist. /// @@ -597,7 +594,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + wrapped_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + wrapped_balances::Config + vault::Config, { /// Returns a [`NftGuard`] object that can read or write to the nft attributes /// diff --git a/pallets/phala/src/compute/stake_pool_v2.rs b/pallets/phala/src/compute/stake_pool_v2.rs index 2570b67ce2..da949e8aa8 100644 --- a/pallets/phala/src/compute/stake_pool_v2.rs +++ b/pallets/phala/src/compute/stake_pool_v2.rs @@ -319,7 +319,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { /// Creates a new stake pool #[pallet::call_index(0)] @@ -994,7 +994,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { pub fn do_start_computing( owner: &T::AccountId, @@ -1206,7 +1206,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { /// Called when gk send new payout information. /// Append specific worker's reward balance of current round, @@ -1239,7 +1239,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { fn on_unbound(worker: &WorkerPublicKey, _force: bool) { // Usually called on worker force unbinding (force == true), but it's also possible @@ -1261,7 +1261,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { fn on_stopped( _worker: &WorkerPublicKey, diff --git a/pallets/phala/src/compute/vault.rs b/pallets/phala/src/compute/vault.rs index 40bb79a9ea..04ebe29a7a 100644 --- a/pallets/phala/src/compute/vault.rs +++ b/pallets/phala/src/compute/vault.rs @@ -147,7 +147,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + pallet_elections_phragmen::Config, + T: Config, { /// Creates a new vault #[pallet::call_index(0)] diff --git a/pallets/phala/src/compute/wrapped_balances.rs b/pallets/phala/src/compute/wrapped_balances.rs index bfc7805ce8..ab451e5b34 100644 --- a/pallets/phala/src/compute/wrapped_balances.rs +++ b/pallets/phala/src/compute/wrapped_balances.rs @@ -9,14 +9,13 @@ pub mod pallet { use crate::registry; use crate::vault; use crate::{BalanceOf, NegativeImbalanceOf, PhalaConfig, PositiveImbalanceOf}; - use frame_support::traits::tokens::{Fortitude, Precision}; use frame_support::{ pallet_prelude::*, traits::{ tokens::nonfungibles::InspectEnumerable, tokens::{ fungibles::{Inspect, Mutate}, - BalanceStatus, + BalanceStatus, Fortitude, Precision, }, Currency, ExistenceRequirement, Imbalance, LockIdentifier, LockableCurrency, OnUnbalanced, ReservableCurrency, SignedImbalance, StorageVersion, WithdrawReasons, @@ -50,6 +49,8 @@ pub mod pallet { type WrappedBalancesAccountId: Get; /// The handler to absorb the slashed amount. type OnSlashed: OnUnbalanced>; + /// Council election call origin + type ElectionPalletId: Get; } /// User's asset status proxy @@ -170,7 +171,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + pallet_balances::Config + vault::Config, T: pallet_balances::Config>, { type Balance = BalanceOf; @@ -287,7 +288,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + pallet_balances::Config + vault::Config, T: pallet_balances::Config>, { fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool { @@ -360,7 +361,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + pallet_balances::Config + vault::Config, T: pallet_balances::Config>, { type Moment = T::BlockNumber; @@ -403,7 +404,7 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { fn pre_check( _sender: &T::AccountId, @@ -444,7 +445,7 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + pallet_balances::Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + pallet_balances::Config + vault::Config, T: pallet_balances::Config>, { /// Wraps some pha and gain equal amount of W-PHA @@ -664,7 +665,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config + pallet_elections_phragmen::Config, + T: Config + vault::Config, { /// Gets W-PHA's asset id pub fn get_asset_id() -> u32 { @@ -818,7 +819,7 @@ pub mod pallet { } fn ensure_lock_id_supported(id: LockIdentifier) { - if id != ::PalletId::get() { + if id != T::ElectionPalletId::get() { panic!("LockIdentifier is not supported."); } } diff --git a/pallets/phala/src/mock.rs b/pallets/phala/src/mock.rs index e2fbde8c1d..592c7ff3b6 100644 --- a/pallets/phala/src/mock.rs +++ b/pallets/phala/src/mock.rs @@ -263,6 +263,7 @@ impl computation::Config for Test { parameter_types! { pub const WPhaAssetId: u32 = 1; + pub const ElectionPalletId: LockIdentifier = b"phrelect"; } impl wrapped_balances::Config for Test { @@ -270,6 +271,7 @@ impl wrapped_balances::Config for Test { type WPhaAssetId = WPhaAssetId; type WrappedBalancesAccountId = ConstU64<1234>; type OnSlashed = (); + type ElectionPalletId = ElectionPalletId; } parameter_types! { diff --git a/standalone/runtime/src/lib.rs b/standalone/runtime/src/lib.rs index 3c72da1d31..f9efb1976c 100644 --- a/standalone/runtime/src/lib.rs +++ b/standalone/runtime/src/lib.rs @@ -1454,6 +1454,7 @@ impl pallet_wrapped_balances::Config for Runtime { type WPhaAssetId = ConstU32<10000>; type WrappedBalancesAccountId = WrappedBalancesPalletAccount; type OnSlashed = Treasury; + type ElectionPalletId = ElectionsPhragmenPalletId; } pub struct MigrationAccount; From 3be8fd8a8ecd66a29b9f41818d509190273b2623 Mon Sep 17 00:00:00 2001 From: h4x3rotab Date: Sun, 25 Jun 2023 11:31:54 +0000 Subject: [PATCH 3/4] remove unused deps --- Cargo.lock | 1 - pallets/phala/Cargo.toml | 3 --- 2 files changed, 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0a2a3c8900..2f48cf1b05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8171,7 +8171,6 @@ dependencies = [ "pallet-balances", "pallet-collective", "pallet-democracy", - "pallet-elections-phragmen", "pallet-insecure-randomness-collective-flip", "pallet-preimage", "pallet-rmrk-core", diff --git a/pallets/phala/Cargo.toml b/pallets/phala/Cargo.toml index 290f8ff227..bc51b71a11 100644 --- a/pallets/phala/Cargo.toml +++ b/pallets/phala/Cargo.toml @@ -27,7 +27,6 @@ rmrk-traits = { git = "https://github.com/Phala-Network/rmrk-substrate", branch frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } pallet-insecure-randomness-collective-flip = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } -pallet-elections-phragmen = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.42", default-features = false } @@ -74,7 +73,6 @@ std = [ "frame-system/std", "pallet-assets/std", "pallet-democracy/std", - "pallet-elections-phragmen/std", "sp-io/std", "sp-std/std", "sp-core/std", @@ -94,7 +92,6 @@ std = [ ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", - "pallet-elections-phragmen/runtime-benchmarks", ] try-runtime = ["frame-support/try-runtime"] native = [ From 17f23cd59c73f0f87a88ee923a1412854f941869 Mon Sep 17 00:00:00 2001 From: h4x3rotab Date: Sun, 25 Jun 2023 11:41:29 +0000 Subject: [PATCH 4/4] remove unused bounds --- pallets/phala/src/compute/base_pool.rs | 16 +++++----------- pallets/phala/src/compute/stake_pool_v2.rs | 10 +++++----- pallets/phala/src/compute/vault.rs | 1 - pallets/phala/src/compute/wrapped_balances.rs | 16 ++++++---------- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/pallets/phala/src/compute/base_pool.rs b/pallets/phala/src/compute/base_pool.rs index 9b48c81a24..8287b65269 100644 --- a/pallets/phala/src/compute/base_pool.rs +++ b/pallets/phala/src/compute/base_pool.rs @@ -293,9 +293,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config - + wrapped_balances::Config - + vault::Config, + T: wrapped_balances::Config + vault::Config, { Pallet::::set_nft_attr(self.cid, self.nftid, &self.attr)?; Ok(()) @@ -338,9 +336,7 @@ pub mod pallet { where T: pallet_assets::Config, T: Config, - T: Config - + wrapped_balances::Config - + vault::Config, + T: wrapped_balances::Config + vault::Config, { pallet_assets::Pallet::::balance( ::WPhaAssetId::get(), @@ -363,9 +359,7 @@ pub mod pallet { sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, T: Config, - T: Config - + wrapped_balances::Config - + vault::Config, + T: wrapped_balances::Config + vault::Config, { self.total_value += rewards; for vault_staker in &self.value_subscribers { @@ -428,7 +422,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { /// Adds a staker accountid to contribution whitelist. /// @@ -594,7 +588,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + wrapped_balances::Config + vault::Config, + T: wrapped_balances::Config + vault::Config, { /// Returns a [`NftGuard`] object that can read or write to the nft attributes /// diff --git a/pallets/phala/src/compute/stake_pool_v2.rs b/pallets/phala/src/compute/stake_pool_v2.rs index da949e8aa8..ccf8a983b4 100644 --- a/pallets/phala/src/compute/stake_pool_v2.rs +++ b/pallets/phala/src/compute/stake_pool_v2.rs @@ -319,7 +319,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { /// Creates a new stake pool #[pallet::call_index(0)] @@ -994,7 +994,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { pub fn do_start_computing( owner: &T::AccountId, @@ -1206,7 +1206,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { /// Called when gk send new payout information. /// Append specific worker's reward balance of current round, @@ -1239,7 +1239,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { fn on_unbound(worker: &WorkerPublicKey, _force: bool) { // Usually called on worker force unbinding (force == true), but it's also possible @@ -1261,7 +1261,7 @@ pub mod pallet { BalanceOf: FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { fn on_stopped( _worker: &WorkerPublicKey, diff --git a/pallets/phala/src/compute/vault.rs b/pallets/phala/src/compute/vault.rs index 04ebe29a7a..9d5db6753d 100644 --- a/pallets/phala/src/compute/vault.rs +++ b/pallets/phala/src/compute/vault.rs @@ -147,7 +147,6 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config, { /// Creates a new vault #[pallet::call_index(0)] diff --git a/pallets/phala/src/compute/wrapped_balances.rs b/pallets/phala/src/compute/wrapped_balances.rs index ab451e5b34..508120c5c2 100644 --- a/pallets/phala/src/compute/wrapped_balances.rs +++ b/pallets/phala/src/compute/wrapped_balances.rs @@ -171,8 +171,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config, - T: pallet_balances::Config>, + T: pallet_balances::Config> + vault::Config, { type Balance = BalanceOf; type PositiveImbalance = PositiveImbalanceOf; @@ -288,8 +287,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config, - T: pallet_balances::Config>, + T: pallet_balances::Config> + vault::Config, { fn can_reserve(who: &T::AccountId, value: Self::Balance) -> bool { Self::total_balance(who) >= value @@ -361,8 +359,7 @@ pub mod pallet { where BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_assets::Config>, - T: Config + pallet_balances::Config + vault::Config, - T: pallet_balances::Config>, + T: pallet_balances::Config> + vault::Config, { type Moment = T::BlockNumber; type MaxLocks = T::MaxLocks; @@ -404,7 +401,7 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + vault::Config, + T: vault::Config, { fn pre_check( _sender: &T::AccountId, @@ -445,8 +442,7 @@ pub mod pallet { T: pallet_uniques::Config, T: pallet_assets::Config>, T: pallet_democracy::Config::Currency>, - T: Config + pallet_balances::Config + vault::Config, - T: pallet_balances::Config>, + T: pallet_balances::Config> + vault::Config, { /// Wraps some pha and gain equal amount of W-PHA /// @@ -665,7 +661,7 @@ pub mod pallet { BalanceOf: sp_runtime::traits::AtLeast32BitUnsigned + Copy + FixedPointConvert + Display, T: pallet_uniques::Config, T: pallet_assets::Config>, - T: Config + vault::Config, + T: vault::Config, { /// Gets W-PHA's asset id pub fn get_asset_id() -> u32 {