Skip to content

Commit 42d020c

Browse files
authored
Merge pull request #240 from opentensor/subnet_dereg_automatic_weighing
Automatically set 0 to deregistered subnets
2 parents bde8ee9 + dd3299f commit 42d020c

File tree

5 files changed

+127
-10
lines changed

5 files changed

+127
-10
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/subtensor/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ pub mod pallet {
861861
PowRegistrationAllowed(u16, bool), // --- Event created when POW registration is allowed/disallowed for a subnet.
862862
TempoSet(u16, u16), // --- Event created when setting tempo on a network
863863
RAORecycledForRegistrationSet(u16, u64), // Event created when setting the RAO recycled for registration.
864-
WeightsMinStake(u64),
864+
WeightsMinStake(u64), // --- Event created when min stake is set for validators to set weights.
865865
SenateRequiredStakePercentSet(u64), // Event created when setting the minimum required stake amount for senate registration.
866866
AdjustmentAlphaSet(u16, u64), // Event created when setting the adjustment alpha on a subnet.
867867
Faucet(T::AccountId, u64), // Event created when the facuet it called on the test net.
@@ -1666,7 +1666,6 @@ pub mod pallet {
16661666
if Uids::<T>::contains_key(netuid, &hotkey) {
16671667
let uid = Self::get_uid_for_net_and_hotkey(netuid, &hotkey.clone()).unwrap();
16681668
let stake = Self::get_total_stake_for_hotkey(&hotkey);
1669-
if stake <= 20_000_000_000_000_000 { return 0; } // Blacklist weights transactions for low stake peers.
16701669
let current_block_number: u64 = Self::get_current_block_as_u64();
16711670
let default_priority: u64 =
16721671
current_block_number - Self::get_last_update_for_uid(netuid, uid as u16);

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));
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/tests/root.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,4 +573,105 @@ fn test_network_prune_results() {
573573
SubtensorModule::set_emission_values(&vec![1u16, 2u16, 3u16], vec![4u64, 5u64, 5u64]);
574574
assert_eq!(SubtensorModule::get_subnet_to_prune(), 1u16);
575575
});
576+
}
577+
578+
579+
#[test]
580+
fn test_weights_after_network_pruning() {
581+
new_test_ext().execute_with(|| {
582+
migration::migrate_create_root_network::<Test>();
583+
584+
assert_eq!(SubtensorModule::get_total_issuance(), 0);
585+
586+
// Set up N subnets, with max N + 1 allowed UIDs
587+
let n: usize = 2;
588+
let root_netuid: u16 = 0;
589+
SubtensorModule::set_network_immunity_period(3);
590+
SubtensorModule::set_max_registrations_per_block(root_netuid, n as u16);
591+
SubtensorModule::set_max_subnets(n as u16);
592+
SubtensorModule::set_weights_set_rate_limit(root_netuid, 0 as u64);
593+
594+
// No validators yet.
595+
assert_eq!(SubtensorModule::get_subnetwork_n(root_netuid), 0);
596+
597+
for i in 0..n {
598+
599+
// Register a validator
600+
let hot: U256 = U256::from(i);
601+
let cold: U256 = U256::from(i);
602+
603+
SubtensorModule::add_balance_to_coldkey_account(&cold, 1_000_000_000_000);
604+
605+
// Register a network
606+
assert_ok!(SubtensorModule::register_network(
607+
<<Test as Config>::RuntimeOrigin>::signed(cold)
608+
));
609+
610+
log::debug!("Adding network with netuid: {}", (i as u16) + 1);
611+
assert!(SubtensorModule::if_subnet_exist((i as u16) + 1));
612+
step_block(3);
613+
}
614+
615+
// Register a validator in subnet 0
616+
let hot: U256 = U256::from((n as u64) - 1);
617+
let cold: U256 = U256::from((n as u64) - 1);
618+
619+
assert_ok!(SubtensorModule::root_register(
620+
<<Test as Config>::RuntimeOrigin>::signed(cold),
621+
hot
622+
));
623+
assert_ok!(SubtensorModule::add_stake(
624+
<<Test as Config>::RuntimeOrigin>::signed(cold),
625+
hot,
626+
1_000
627+
));
628+
629+
// Let's give these subnets some weights
630+
let uids: Vec<u16> = (0..(n as u16)+1).collect();
631+
let values: Vec<u16> = vec![4u16, 2u16, 6u16];
632+
log::info!("uids set: {:?}", uids);
633+
log::info!("values set: {:?}", values);
634+
log::info!("In netuid: {:?}", root_netuid);
635+
assert_ok!(SubtensorModule::set_weights(
636+
<<Test as Config>::RuntimeOrigin>::signed(hot),
637+
root_netuid,
638+
uids,
639+
values,
640+
0
641+
));
642+
643+
log::info!("Root network weights before extra network registration: {:?}", SubtensorModule::get_root_weights());
644+
log::info!("Max subnets: {:?}", SubtensorModule::get_max_subnets());
645+
let i = (n as u16) + 1;
646+
let hot: U256 = U256::from(i);
647+
let cold: U256 = U256::from(i);
648+
649+
SubtensorModule::add_balance_to_coldkey_account(&cold, 1_000_000_000_000_000_000);
650+
let subnet_to_prune = SubtensorModule::get_subnet_to_prune();
651+
652+
// Subnet 1 should be pruned here.
653+
assert_eq!(subnet_to_prune, 1);
654+
log::info!("Removing subnet: {:?}", subnet_to_prune);
655+
656+
// Check that the weights have been set appropriately.
657+
let latest_weights = SubtensorModule::get_root_weights();
658+
log::info!("Weights before register network: {:?}", latest_weights);
659+
// We expect subnet 1 to be deregistered as it is oldest and has lowest emissions
660+
assert_eq!(latest_weights[0][1], 21845);
661+
662+
assert_ok!(SubtensorModule::register_network(
663+
<<Test as Config>::RuntimeOrigin>::signed(cold)
664+
));
665+
666+
// Subnet should not exist, as it would replace a previous subnet.
667+
assert!(!SubtensorModule::if_subnet_exist((i as u16) + 1));
668+
669+
log::info!("Root network weights: {:?}", SubtensorModule::get_root_weights());
670+
671+
let latest_weights = SubtensorModule::get_root_weights();
672+
log::info!("Weights after register network: {:?}", SubtensorModule::get_root_weights());
673+
674+
// Subnet 0 should be kicked, and thus its weight should be 0
675+
assert_eq!(latest_weights[0][1], 0);
676+
});
576677
}

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
121121
// `spec_version`, and `authoring_version` are the same between Wasm and native.
122122
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
123123
// the compatible custom types.
124-
spec_version: 141,
124+
spec_version: 142,
125125
impl_version: 1,
126126
apis: RUNTIME_API_VERSIONS,
127127
transaction_version: 1,

0 commit comments

Comments
 (0)