From 11959f468f224bd567d45f40a4c33d3b6973ddd1 Mon Sep 17 00:00:00 2001 From: Honza <115138297+Supremesource@users.noreply.github.com> Date: Wed, 18 Sep 2024 22:35:55 +0200 Subject: [PATCH] feat/removing vali blacklist (#227) * feat: removing validator blacklist * fix: removing validator blacklist and tranfering proposal rewards from treasury --- pallets/governance/src/proposal.rs | 15 ++--- .../src/subnet_consensus/yuma.rs | 19 ++---- pallets/subspace/src/benchmarking.rs | 18 ------ pallets/subspace/src/lib.rs | 29 --------- pallets/subspace/src/subnet.rs | 38 ----------- tests/src/subnet_emission.rs | 64 ------------------- 6 files changed, 13 insertions(+), 170 deletions(-) diff --git a/pallets/governance/src/proposal.rs b/pallets/governance/src/proposal.rs index 2ceddb133..d62ab3b29 100644 --- a/pallets/governance/src/proposal.rs +++ b/pallets/governance/src/proposal.rs @@ -618,6 +618,7 @@ fn distribute_proposal_rewards( ) { use frame_support::sp_runtime::traits::IntegerSquareRoot; + let dao_treasury_address = DaoTreasuryAddress::::get(); let account_sqrt_stakes: Vec<_> = account_stakes .into_iter() .map(|(acc_id, stake)| (acc_id, stake.integer_sqrt())) @@ -630,14 +631,12 @@ fn distribute_proposal_rewards( let percentage = I92F36::from_num(stake).checked_div(total_stake).unwrap_or_default(); let reward: u64 = total_allocation.checked_mul(percentage).unwrap_or_default().to_num(); - let reward = match pallet_subspace::Pallet::::u64_to_balance(reward) { - Some(balance) => balance, - None => { - log::error!("could not transform {reward} into T::Balance"); - continue; - } - }; - pallet_subspace::Pallet::::add_balance_to_account(&acc_id, reward); + // Transfer the proposal reward to the accounts from treasury + let _ = PalletSubspace::::transfer_balance_to_account( + &dao_treasury_address, + &acc_id, + reward, + ); } } diff --git a/pallets/subnet_emission/src/subnet_consensus/yuma.rs b/pallets/subnet_emission/src/subnet_consensus/yuma.rs index 6903234b2..1cfbfa8d4 100644 --- a/pallets/subnet_emission/src/subnet_consensus/yuma.rs +++ b/pallets/subnet_emission/src/subnet_consensus/yuma.rs @@ -4,8 +4,8 @@ use frame_support::{ensure, weights::Weight, DebugNoBound}; use pallet_subspace::{ math::*, Active, Bonds, BondsMovingAverage, Config, Consensus, Dividends, Emission, Founder, Incentive, Kappa, Keys, LastUpdate, MaxAllowedValidators, MaxWeightAge, - Pallet as PalletSubspace, PruningScores, Rank, Trust, Uids, ValidatorBlacklist, - ValidatorPermits, ValidatorTrust, Vec, Weights, N, + Pallet as PalletSubspace, PruningScores, Rank, Trust, Uids, ValidatorPermits, ValidatorTrust, + Vec, Weights, N, }; use sp_std::vec; use substrate_fixed::types::{I32F32, I64F64, I96F32}; @@ -115,8 +115,7 @@ impl YumaEpoch { let max_validators = self.max_allowed_validators; let mut new_permits = vec![false; stake.as_ref().len()]; - let mut sorted_indexed_stake: Vec<(u16, T::AccountId, u64)> = (0u16..(stake.as_ref().len() - as u16)) + let mut sorted_indexed_stake: Vec<(u16, u64)> = (0u16..(stake.as_ref().len() as u16)) .map(|idx| { self.weight_counter.read(1); let key = match PalletSubspace::::get_key_for_uid(self.netuid, idx) { @@ -126,28 +125,22 @@ impl YumaEpoch { self.weight_counter.read(1); let stake = PalletSubspace::::get_delegated_stake(&key); - Ok((idx, key, stake)) + Ok((idx, stake)) }) .collect::, EmissionError>>()?; - sorted_indexed_stake.sort_by_key(|(_, _, stake)| *stake); + sorted_indexed_stake.sort_by_key(|(_, stake)| *stake); sorted_indexed_stake.reverse(); - let blacklist = ValidatorBlacklist::::get(self.netuid); - self.weight_counter.read(1); let current_block = PalletSubspace::::get_current_block_number(); self.weight_counter.read(1); let min_stake = pallet_subspace::MinValidatorStake::::get(self.netuid); self.weight_counter.read(1); let mut validator_count = 0; - for (idx, key, stake) in sorted_indexed_stake { + for (idx, stake) in sorted_indexed_stake { if max_validators.is_some_and(|max| max <= validator_count) { break; } - if blacklist.contains(&key) { - continue; - } - if stake < min_stake { continue; } diff --git a/pallets/subspace/src/benchmarking.rs b/pallets/subspace/src/benchmarking.rs index 251d8a99a..425660e9a 100644 --- a/pallets/subspace/src/benchmarking.rs +++ b/pallets/subspace/src/benchmarking.rs @@ -251,22 +251,4 @@ benchmarks! { ); }: register_subnet(RawOrigin::Signed(key.clone()), "testnet".as_bytes().to_vec(), Some(b"testmetadata".to_vec())) - add_blacklist { - let owner: T::AccountId = account("Alice", 0, 1); - let stake = 100000000000000u64; - let blacklisted: T::AccountId = account("Module", 0, 2); - register_mock::(owner.clone(), owner.clone(), b"owner".to_vec()).unwrap(); - register_mock::(owner.clone(), owner.clone(), b"blacklisted".to_vec()).unwrap(); - let netuid = SubspaceMod::::get_netuid_for_name(b"testnet").unwrap(); - }: add_blacklist(RawOrigin::Signed(owner), netuid, blacklisted) - - remove_blacklist { - let owner: T::AccountId = account("Alice", 0, 1); - let stake = 100000000000000u64; - let blacklisted: T::AccountId = account("Module", 0, 2); - register_mock::(owner.clone(), owner.clone(), b"owner".to_vec()).unwrap(); - register_mock::(owner.clone(), owner.clone(), b"blacklisted".to_vec()).unwrap(); - let netuid = SubspaceMod::::get_netuid_for_name(b"testnet").unwrap(); - SubspaceMod::::add_blacklist(RawOrigin::Signed(owner.clone()).into(), netuid, blacklisted.clone()).unwrap(); - }: remove_blacklist(RawOrigin::Signed(owner), netuid, blacklisted) } diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index 47a48118b..40dc70d1e 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -421,10 +421,6 @@ pub mod pallet { FloorFounderShare::::get() as u16 } - #[pallet::storage] - pub type ValidatorBlacklist = - StorageMap<_, Identity, u16, BTreeSet, ValueQuery>; - // --------------------------------- // Module Variables // --------------------------------- @@ -716,10 +712,6 @@ pub mod pallet { InvalidMinValidatorStake, /// The maximum allowed validators value is invalid, minimum is 10. InvalidMaxAllowedValidators, - /// Module is already on the subnet blacklist - AlreadyBlacklisted, - /// Module is not on the subnet blacklist - NotBlacklisted, } // --------------------------------- @@ -1047,29 +1039,8 @@ pub mod pallet { ) -> DispatchResult { Self::do_register_subnet(origin, name, metadata) } - - #[pallet::call_index(13)] - #[pallet::weight((T::WeightInfo::delegate_rootnet_control(), DispatchClass::Normal, Pays::No))] - pub fn add_blacklist( - origin: OriginFor, - netuid: u16, - module: T::AccountId, - ) -> DispatchResult { - Self::do_add_blacklist(origin, netuid, module) - } - - #[pallet::call_index(14)] - #[pallet::weight((T::WeightInfo::delegate_rootnet_control(), DispatchClass::Normal, Pays::No))] - pub fn remove_blacklist( - origin: OriginFor, - netuid: u16, - module: T::AccountId, - ) -> DispatchResult { - Self::do_remove_blacklist(origin, netuid, module) - } } } - impl Pallet { /// Returns the total amount staked by the given key to other keys. #[inline] diff --git a/pallets/subspace/src/subnet.rs b/pallets/subspace/src/subnet.rs index d5b99155c..b85b3937b 100644 --- a/pallets/subspace/src/subnet.rs +++ b/pallets/subspace/src/subnet.rs @@ -564,42 +564,4 @@ impl Pallet { Some(SubnetConsensus::Root) ) } - - pub fn do_add_blacklist( - origin: T::RuntimeOrigin, - netuid: u16, - module: T::AccountId, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - - let params = Self::subnet_params(netuid); - ensure!(params.founder == key, Error::::NotFounder); - - let mut blacklist = ValidatorBlacklist::::get(netuid); - ensure!(!blacklist.contains(&module), Error::::AlreadyBlacklisted); - - blacklist.insert(module); - ValidatorBlacklist::::set(netuid, blacklist); - - Ok(()) - } - - pub fn do_remove_blacklist( - origin: T::RuntimeOrigin, - netuid: u16, - module: T::AccountId, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - - let params = Self::subnet_params(netuid); - ensure!(params.founder == key, Error::::NotFounder); - - let mut blacklist = ValidatorBlacklist::::get(netuid); - ensure!(blacklist.contains(&module), Error::::NotBlacklisted); - - blacklist.remove(&module); - ValidatorBlacklist::::set(netuid, blacklist); - - Ok(()) - } } diff --git a/tests/src/subnet_emission.rs b/tests/src/subnet_emission.rs index 0890a9538..0971d5e1c 100644 --- a/tests/src/subnet_emission.rs +++ b/tests/src/subnet_emission.rs @@ -1260,67 +1260,3 @@ fn yuma_change_permits() { ); }); } - -#[test] -fn yuma_change_permits_blacklisted() { - new_test_ext().execute_with(|| { - zero_min_burn(); - - let netuid = 6; - let first_uid = register_module(netuid, 0, 1, false).unwrap(); - let second_uid = register_module(netuid, 1, to_nano(51000), false).unwrap(); - let third_uid = register_module(netuid, 2, to_nano(52000), false).unwrap(); - - dbg!(ValidatorBlacklist::::get(netuid)); - pallet_subspace::Pallet::::add_blacklist( - get_origin(first_uid.into()), - netuid, - third_uid.into(), - ) - .unwrap(); - - dbg!(ValidatorBlacklist::::get(netuid)); - - MaxAllowedValidators::::set(netuid, Some(2)); - - set_weights(netuid, 2, vec![first_uid, second_uid], vec![50, 60]); - - assert_ok!(YumaEpoch::::new(netuid, ONE).run()); - - assert_eq!( - ValidatorPermits::::get(netuid)[first_uid as usize], - false - ); - assert_eq!( - ValidatorPermits::::get(netuid)[second_uid as usize], - false - ); - assert_eq!( - ValidatorPermits::::get(netuid)[third_uid as usize], - false - ); - - let fourth_uid = register_module(netuid, 3, to_nano(54000), false).unwrap(); - set_weights(netuid, 1, vec![third_uid, fourth_uid], vec![50, 60]); - set_weights(netuid, 3, vec![first_uid, second_uid], vec![50, 60]); - - assert_ok!(YumaEpoch::::new(netuid, ONE).run()); - - assert_eq!( - ValidatorPermits::::get(netuid)[first_uid as usize], - false - ); - assert_eq!( - ValidatorPermits::::get(netuid)[second_uid as usize], - true - ); - assert_eq!( - ValidatorPermits::::get(netuid)[third_uid as usize], - false - ); - assert_eq!( - ValidatorPermits::::get(netuid)[fourth_uid as usize], - true - ); - }); -}