Skip to content

Commit dbd2aa6

Browse files
committed
Merge branch 'main' of https://github.com/opentensor/subtensor into improved_subnet_owener_power
2 parents a4b2554 + 2408622 commit dbd2aa6

File tree

11 files changed

+307
-32
lines changed

11 files changed

+307
-32
lines changed

docs/running-subtensor-locally.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,4 @@ sudo ./scripts/run/subtensor.sh -e docker --network testnet --node-type archive
7474
```
7575

7676
## Running on cloud
77-
7877
We have not tested these installation scripts on any cloud service. In addition, if you are using Runpod cloud service, then note that this service is already [containerized](https://docs.runpod.io/pods/overview). Hence, the only option available to you is to compile from the source, as described in the above [Compiling your own binary](#compiling-your-own-binary) section. Note that these scripts have not been tested on Runpod.

pallets/admin-utils/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,16 @@ pub mod pallet {
701701
T::Subtensor::set_rao_recycled(netuid, rao_recycled);
702702
Ok(())
703703
}
704+
705+
#[pallet::call_index(42)]
706+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
707+
pub fn sudo_set_weights_min_stake( origin: OriginFor<T>, min_stake: u64 ) -> DispatchResult
708+
{
709+
ensure_root(origin)?;
710+
T::Subtensor::set_weights_min_stake(min_stake);
711+
Ok(())
712+
}
713+
704714
}
705715
}
706716

@@ -785,4 +795,5 @@ pub trait SubtensorInterface<AccountId, Balance, RuntimeOrigin>
785795
fn set_adjustment_interval(netuid: u16, adjustment_interval: u16);
786796
fn set_weights_set_rate_limit(netuid: u16, weights_set_rate_limit: u64);
787797
fn init_new_network(netuid: u16, tempo: u16);
798+
fn set_weights_min_stake(min_stake: u64);
788799
}

pallets/admin-utils/tests/mock.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,11 @@ impl pallet_admin_utils::SubtensorInterface<AccountId, Balance, RuntimeOrigin> f
478478
{
479479
SubtensorModule::init_new_network(netuid, tempo);
480480
}
481+
482+
fn set_weights_min_stake( min_stake: u64 )
483+
{
484+
SubtensorModule::set_weights_min_stake( min_stake );
485+
}
481486
}
482487

483488
impl pallet_admin_utils::Config for Test {

pallets/admin-utils/tests/tests.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,34 @@ fn test_sudo_set_max_allowed_validators() {
684684
});
685685
}
686686

687+
688+
#[test]
689+
fn test_sudo_set_weights_min_stake() {
690+
new_test_ext().execute_with(|| {
691+
let to_be_set: u64 = 10;
692+
let init_value: u64 = SubtensorModule::get_weights_min_stake();
693+
assert_eq!(
694+
AdminUtils::sudo_set_weights_min_stake(
695+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
696+
to_be_set
697+
),
698+
Err(DispatchError::BadOrigin.into())
699+
);
700+
assert_eq!(
701+
SubtensorModule::get_weights_min_stake(),
702+
init_value
703+
);
704+
assert_ok!(AdminUtils::sudo_set_weights_min_stake(
705+
<<Test as Config>::RuntimeOrigin>::root(),
706+
to_be_set
707+
));
708+
assert_eq!(
709+
SubtensorModule::get_weights_min_stake(),
710+
to_be_set
711+
);
712+
});
713+
}
714+
687715
#[test]
688716
fn test_sudo_set_bonds_moving_average() {
689717
new_test_ext().execute_with(|| {

pallets/subtensor/src/lib.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use frame_support::{
1616

1717
use codec::{Decode, Encode};
1818
use frame_support::sp_runtime::transaction_validity::ValidTransaction;
19+
use frame_support::sp_runtime::transaction_validity::InvalidTransaction;
1920
use scale_info::TypeInfo;
2021
use sp_runtime::{
2122
traits::{DispatchInfoOf, Dispatchable, PostDispatchInfoOf, SignedExtension},
@@ -641,7 +642,13 @@ pub mod pallet {
641642
pub fn DefaultAdjustmentAlpha<T: Config>() -> u64 {
642643
T::InitialAdjustmentAlpha::get()
643644
}
645+
#[pallet::type_value]
646+
pub fn DefaultWeightsMinStake<T: Config>() -> u64 {
647+
0
648+
}
644649

650+
#[pallet::storage] // ITEM( weights_min_stake )
651+
pub type WeightsMinStake<T> = StorageValue<_, u64, ValueQuery, DefaultWeightsMinStake<T>>;
645652
#[pallet::storage] // --- MAP ( netuid ) --> Rho
646653
pub type Rho<T> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultRho<T>>;
647654
#[pallet::storage] // --- MAP ( netuid ) --> Kappa
@@ -854,6 +861,7 @@ pub mod pallet {
854861
PowRegistrationAllowed(u16, bool), // --- Event created when POW registration is allowed/disallowed for a subnet.
855862
TempoSet(u16, u16), // --- Event created when setting tempo on a network
856863
RAORecycledForRegistrationSet(u16, u64), // Event created when setting the RAO recycled for registration.
864+
WeightsMinStake(u64), // --- Event created when min stake is set for validators to set weights.
857865
SenateRequiredStakePercentSet(u64), // Event created when setting the minimum required stake amount for senate registration.
858866
AdjustmentAlphaSet(u16, u64), // Event created when setting the adjustment alpha on a subnet.
859867
Faucet(T::AccountId, u64), // Event created when the facuet it called on the test net.
@@ -878,6 +886,7 @@ pub mod pallet {
878886
NotRegistered, // ---- Thrown when the caller requests setting or removing data from a neuron which does not exist in the active set.
879887
NonAssociatedColdKey, // ---- Thrown when a stake, unstake or subscribe request is made by a coldkey which is not associated with the hotkey account.
880888
NotEnoughStaketoWithdraw, // ---- Thrown when the caller requests removing more stake than there exists in the staking account. See: fn remove_stake.
889+
NotEnoughStakeToSetWeights, // ---- Thrown when the caller requests to set weights but has less than WeightsMinStake
881890
NotEnoughBalanceToStake, // ---- Thrown when the caller requests adding more stake than there exists in the cold key account. See: fn add_stake
882891
BalanceWithdrawalError, // ---- Thrown when the caller tries to add stake, but for some reason the requested amount could not be withdrawn from the coldkey account.
883892
NoValidatorPermit, // ---- Thrown when the caller attempts to set non-self weights without being a permitted validator.
@@ -1656,13 +1665,43 @@ pub mod pallet {
16561665
pub fn get_priority_set_weights(hotkey: &T::AccountId, netuid: u16) -> u64 {
16571666
if Uids::<T>::contains_key(netuid, &hotkey) {
16581667
let uid = Self::get_uid_for_net_and_hotkey(netuid, &hotkey.clone()).unwrap();
1668+
let stake = Self::get_total_stake_for_hotkey(&hotkey);
16591669
let current_block_number: u64 = Self::get_current_block_as_u64();
16601670
let default_priority: u64 =
16611671
current_block_number - Self::get_last_update_for_uid(netuid, uid as u16);
16621672
return default_priority + u32::max_value() as u64;
16631673
}
16641674
return 0;
16651675
}
1676+
1677+
// --- Is the caller allowed to set weights
1678+
pub fn check_weights_min_stake(hotkey: &T::AccountId) -> bool {
1679+
// Blacklist weights transactions for low stake peers.
1680+
if Self::get_total_stake_for_hotkey(&hotkey) >= Self::get_weights_min_stake() {
1681+
return true;
1682+
} else {
1683+
return false;
1684+
}
1685+
}
1686+
1687+
pub fn checked_allowed_register( netuid: u16 ) -> bool {
1688+
if netuid == Self::get_root_netuid() {
1689+
return false;
1690+
}
1691+
if !Self::if_subnet_exist(netuid) {
1692+
return false;
1693+
}
1694+
if !Self::get_network_registration_allowed(netuid) {
1695+
return false;
1696+
}
1697+
if Self::get_registrations_this_block(netuid) >= Self::get_max_registrations_per_block(netuid) {
1698+
return false;
1699+
}
1700+
if Self::get_registrations_this_interval(netuid) >= Self::get_target_registrations_per_interval(netuid) * 3 {
1701+
return false;
1702+
}
1703+
true
1704+
}
16661705
}
16671706
}
16681707

@@ -1705,11 +1744,13 @@ where
17051744
}
17061745

17071746
pub fn get_priority_set_weights(who: &T::AccountId, netuid: u16) -> u64 {
1708-
// Return the non vanilla priority for a set weights call.
1709-
17101747
return Pallet::<T>::get_priority_set_weights(who, netuid);
17111748
}
17121749

1750+
pub fn check_weights_min_stake( who: &T::AccountId ) -> bool {
1751+
Pallet::<T>::check_weights_min_stake(who)
1752+
}
1753+
17131754
pub fn u64_to_balance(
17141755
input: u64,
17151756
) -> Option<
@@ -1750,12 +1791,16 @@ where
17501791
) -> TransactionValidity {
17511792
match call.is_sub_type() {
17521793
Some(Call::set_weights { netuid, .. }) => {
1753-
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
1754-
Ok(ValidTransaction {
1755-
priority: priority,
1756-
longevity: 1,
1757-
..Default::default()
1758-
})
1794+
if Self::check_weights_min_stake( who ) {
1795+
let priority: u64 = Self::get_priority_set_weights(who, *netuid);
1796+
Ok(ValidTransaction {
1797+
priority: priority,
1798+
longevity: 1,
1799+
..Default::default()
1800+
})
1801+
} else {
1802+
return Err(InvalidTransaction::Call.into());
1803+
}
17591804
}
17601805
Some(Call::add_stake { .. }) => Ok(ValidTransaction {
17611806
priority: Self::get_priority_vanilla(),
@@ -1925,4 +1970,4 @@ impl<T, H, P> CollectiveInterface<T, H, P> for () {
19251970
fn add_vote(_: &T, _: H, _: P, _: bool) -> Result<bool, DispatchError> {
19261971
Ok(true)
19271972
}
1928-
}
1973+
}

pallets/subtensor/src/root.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl<T: Config> Pallet<T> {
178178
//
179179
// # Returns:
180180
// A 2D vector ('Vec<Vec<I32F32>>') where each entry [i][j] represents the weight of subnetwork
181-
// 'j' with according to the preferences of key. 'j' within the root network.
181+
// 'j' with according to the preferences of key. Validator 'i' within the root network.
182182
//
183183
pub fn get_root_weights() -> Vec<Vec<I64F64>> {
184184
// --- 0. The number of validators on the root network.
@@ -625,6 +625,7 @@ impl<T: Config> Pallet<T> {
625625

626626
Self::remove_network(netuid_to_prune);
627627
log::debug!("remove_network: {:?}", netuid_to_prune,);
628+
Self::deposit_event(Event::NetworkRemoved(netuid_to_prune));
628629
netuid_to_prune
629630
}
630631
};
@@ -821,9 +822,26 @@ impl<T: Config> Pallet<T> {
821822
let _ = Uids::<T>::clear_prefix(netuid, u32::max_value(), None);
822823
let _ = Keys::<T>::clear_prefix(netuid, u32::max_value(), None);
823824
let _ = Bonds::<T>::clear_prefix(netuid, u32::max_value(), None);
824-
let _ = Weights::<T>::clear_prefix(netuid, u32::max_value(), None);
825+
826+
// --- 9. Iterate over stored weights and fill the matrix.
827+
for (uid_i, weights_i) in
828+
<Weights<T> as IterableStorageDoubleMap<u16, u16, Vec<(u16, u16)>>>::iter_prefix(
829+
Self::get_root_netuid(),
830+
)
831+
{
832+
// Create a new vector to hold modified weights.
833+
let mut modified_weights = weights_i.clone();
834+
// Iterate over each weight entry to potentially update it.
835+
for (subnet_id, weight) in modified_weights.iter_mut() {
836+
if subnet_id == &netuid {
837+
// If the condition matches, modify the weight
838+
*weight = 0; // Set weight to 0 for the matching subnet_id.
839+
}
840+
}
841+
Weights::<T>::insert(Self::get_root_netuid(), uid_i, modified_weights);
842+
}
825843

826-
// --- 9. Remove various network-related parameters.
844+
// --- 10. Remove various network-related parameters.
827845
Rank::<T>::remove(netuid);
828846
Trust::<T>::remove(netuid);
829847
Active::<T>::remove(netuid);
@@ -836,7 +854,7 @@ impl<T: Config> Pallet<T> {
836854
ValidatorPermit::<T>::remove(netuid);
837855
ValidatorTrust::<T>::remove(netuid);
838856

839-
// --- 10. Erase network parameters.
857+
// --- 11. Erase network parameters.
840858
Tempo::<T>::remove(netuid);
841859
Kappa::<T>::remove(netuid);
842860
Difficulty::<T>::remove(netuid);
@@ -850,7 +868,7 @@ impl<T: Config> Pallet<T> {
850868
POWRegistrationsThisInterval::<T>::remove(netuid);
851869
BurnRegistrationsThisInterval::<T>::remove(netuid);
852870

853-
// --- 11. Add the balance back to the owner.
871+
// --- 12. Add the balance back to the owner.
854872
Self::add_balance_to_coldkey_account(&owner_coldkey, reserved_amount_as_bal.unwrap());
855873
Self::set_subnet_locked_balance(netuid, 0);
856874
SubnetOwner::<T>::remove(netuid);
@@ -940,7 +958,7 @@ impl<T: Config> Pallet<T> {
940958
}
941959
});
942960

943-
log::info!("{:?}", netuids);
961+
log::info!("Netuids Order: {:?}", netuids);
944962

945963
match netuids.last() {
946964
Some(netuid) => *netuid,

pallets/subtensor/src/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ impl<T: Config> Pallet<T> {
133133
ValidatorPermit::<T>::insert(netuid, updated_validator_permit);
134134
}
135135
}
136+
pub fn set_weights_min_stake( min_stake: u64 ) {
137+
WeightsMinStake::<T>::put( min_stake );
138+
Self::deposit_event(Event::WeightsMinStake(min_stake));
139+
}
136140

137141
pub fn get_rank_for_uid(netuid: u16, uid: u16) -> u16 {
138142
let vec = Rank::<T>::get(netuid);
@@ -222,6 +226,9 @@ impl<T: Config> Pallet<T> {
222226
return false;
223227
}
224228
}
229+
pub fn get_weights_min_stake( ) -> u64 {
230+
WeightsMinStake::<T>::get()
231+
}
225232

226233
// ============================
227234
// ==== Subnetwork Getters ====

0 commit comments

Comments
 (0)