Skip to content

Clear epoch values for root subnets. #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ mod hooks {
// Reset max burn
.saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::<T>())
// Migrate ColdkeySwapScheduled structure to new format
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>());
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>())
// Clear epoch storage values for root netuid
.saturating_add(migrations::migrate_clear_root_epoch_values::migrate_clear_root_epoch_values::<T>());
Comment on lines 117 to +120
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should wait for #1660 to be merged because the weights are not accounted for atm

weight
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::Pallet as Subtensor;
use crate::{Config, HasMigrationRun};
use alloc::string::String;
use frame_support::{traits::Get, weights::Weight};

// List of cleared maps for root netuid:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need these values for root:

SubnetworkN
Tempo
Keys
MaxAllowedValidators
SubnetOwnerHotkey
BlockAtRegistration

// - ActivityCutoff
// - Bonds
// - Kappa
// - BondsPenalty
// - Yuma3On
// - Rank
// - Trust
// - Active
// - Emission
// - Consensus
// - Incentive
// - Dividends
// - LastUpdate
// - PruningScores
// - ValidatorTrust
// - ValidatorPermit
// - StakeWeight
pub fn migrate_clear_root_epoch_values<T: Config>() -> Weight {
let migration_name = b"migrate_clear_root_epoch_values".to_vec();
let mut weight = T::DbWeight::get().reads(1);

if HasMigrationRun::<T>::get(&migration_name) {
log::info!(
"Migration '{:?}' has already run. Skipping.",
migration_name
);
return weight;
}

log::info!(
"Running migration '{}'",
String::from_utf8_lossy(&migration_name)
);

// Clear root epoch values
let root_netuid = Subtensor::<T>::get_root_netuid();

crate::ActivityCutoff::<T>::remove(root_netuid);
crate::Kappa::<T>::remove(root_netuid);
crate::BondsPenalty::<T>::remove(root_netuid);
crate::Yuma3On::<T>::remove(root_netuid);
crate::Rank::<T>::remove(root_netuid);
crate::Trust::<T>::remove(root_netuid);
crate::Active::<T>::remove(root_netuid);
crate::Emission::<T>::remove(root_netuid);
crate::Consensus::<T>::remove(root_netuid);
crate::Incentive::<T>::remove(root_netuid);
crate::Dividends::<T>::remove(root_netuid);
crate::LastUpdate::<T>::remove(root_netuid);
crate::PruningScores::<T>::remove(root_netuid);
crate::ValidatorTrust::<T>::remove(root_netuid);
crate::ValidatorPermit::<T>::remove(root_netuid);
crate::StakeWeight::<T>::remove(root_netuid);

let total_simple_removals = 16u64;

let mut total_db_operations = total_simple_removals;

let bonds_removal_res = crate::Bonds::<T>::clear_prefix(root_netuid, u32::MAX, None);

total_db_operations = total_db_operations.saturating_add(bonds_removal_res.backend.into());
weight = weight.saturating_add(T::DbWeight::get().writes(total_db_operations));

// Mark Migration as Completed
HasMigrationRun::<T>::insert(&migration_name, true);
weight = weight.saturating_add(T::DbWeight::get().writes(1));

log::info!(
"Migration '{:?}' completed successfully.",
String::from_utf8_lossy(&migration_name)
);

weight
}
1 change: 1 addition & 0 deletions pallets/subtensor/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use sp_io::KillStorageResult;
use sp_io::hashing::twox_128;
use sp_io::storage::clear_prefix;
pub mod migrate_chain_identity;
pub mod migrate_clear_root_epoch_values;
pub mod migrate_coldkey_swap_scheduled;
pub mod migrate_commit_reveal_v2;
pub mod migrate_create_root_network;
Expand Down
101 changes: 100 additions & 1 deletion pallets/subtensor/src/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use alloc::collections::BTreeMap;
use approx::assert_abs_diff_eq;
use codec::{Decode, Encode};
use frame_support::{
StorageHasher, Twox64Concat, assert_ok,
StorageDoubleMap, StorageHasher, Twox64Concat, assert_ok,
storage::unhashed::{get, get_raw, put, put_raw},
traits::{StorageInstance, StoredMap},
weights::Weight,
Expand Down Expand Up @@ -820,3 +820,102 @@ fn test_migrate_remove_commitments_rate_limit() {
assert!(!weight.is_zero(), "Migration weight should be non-zero");
});
}

#[test]
fn test_migrate_clear_root_epoch_values() {
new_test_ext(1).execute_with(|| {
// ------------------------------
// Step 1: Simulate Old Storage Entry
// ------------------------------
const MIGRATION_NAME: &str = "migrate_clear_root_epoch_values";

let root_netuid = Pallet::<Test>::get_root_netuid();

SubnetworkN::<Test>::insert(root_netuid, 0);
Tempo::<Test>::insert(root_netuid, 0);
ActivityCutoff::<Test>::insert(root_netuid, 0);
MaxAllowedValidators::<Test>::insert(root_netuid, 0);
SubnetOwnerHotkey::<Test>::insert(root_netuid, U256::from(1));

Kappa::<Test>::insert(root_netuid, 0);
BondsPenalty::<Test>::insert(root_netuid, 0);
Yuma3On::<Test>::insert(root_netuid, false);
Rank::<Test>::insert(root_netuid, Vec::<u16>::new());
Trust::<Test>::insert(root_netuid, Vec::<u16>::new());

Active::<Test>::insert(root_netuid, Vec::<bool>::new());
Emission::<Test>::insert(root_netuid, Vec::<u64>::new());
Consensus::<Test>::insert(root_netuid, Vec::<u16>::new());
Incentive::<Test>::insert(root_netuid, Vec::<u16>::new());
Dividends::<Test>::insert(root_netuid, Vec::<u16>::new());

LastUpdate::<Test>::insert(root_netuid, Vec::<u64>::new());
PruningScores::<Test>::insert(root_netuid, Vec::<u16>::new());
ValidatorTrust::<Test>::insert(root_netuid, Vec::<u16>::new());
ValidatorPermit::<Test>::insert(root_netuid, Vec::<bool>::new());
StakeWeight::<Test>::insert(root_netuid, Vec::<u16>::new());

Bonds::<Test>::insert(root_netuid, root_netuid, Vec::<(u16, u16)>::new());
Keys::<Test>::insert(root_netuid, root_netuid, U256::from(1));
BlockAtRegistration::<Test>::insert(root_netuid, root_netuid, 0);

assert!(
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
"Migration should not have run yet"
);

assert!(ActivityCutoff::<Test>::contains_key(root_netuid));
assert!(Kappa::<Test>::contains_key(root_netuid));
assert!(BondsPenalty::<Test>::contains_key(root_netuid));
assert!(Yuma3On::<Test>::contains_key(root_netuid));
assert!(Rank::<Test>::contains_key(root_netuid));
assert!(Trust::<Test>::contains_key(root_netuid));
assert!(Active::<Test>::contains_key(root_netuid));
assert!(Emission::<Test>::contains_key(root_netuid));
assert!(Consensus::<Test>::contains_key(root_netuid));
assert!(Incentive::<Test>::contains_key(root_netuid));
assert!(Dividends::<Test>::contains_key(root_netuid));
assert!(LastUpdate::<Test>::contains_key(root_netuid));
assert!(PruningScores::<Test>::contains_key(root_netuid));
assert!(ValidatorTrust::<Test>::contains_key(root_netuid));
assert!(ValidatorPermit::<Test>::contains_key(root_netuid));
assert!(StakeWeight::<Test>::contains_key(root_netuid));
assert!(Bonds::<Test>::contains_prefix(root_netuid));

// ------------------------------
// Step 2: Run the Migration
// ------------------------------
let weight =
crate::migrations::migrate_clear_root_epoch_values::migrate_clear_root_epoch_values::<
Test,
>();

assert!(
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
"Migration should be marked as completed"
);

// ------------------------------
// Step 3: Verify Migration Effects
// ------------------------------
assert!(!ActivityCutoff::<Test>::contains_key(root_netuid));
assert!(!Kappa::<Test>::contains_key(root_netuid));
assert!(!BondsPenalty::<Test>::contains_key(root_netuid));
assert!(!Yuma3On::<Test>::contains_key(root_netuid));
assert!(!Rank::<Test>::contains_key(root_netuid));
assert!(!Trust::<Test>::contains_key(root_netuid));
assert!(!Active::<Test>::contains_key(root_netuid));
assert!(!Emission::<Test>::contains_key(root_netuid));
assert!(!Consensus::<Test>::contains_key(root_netuid));
assert!(!Incentive::<Test>::contains_key(root_netuid));
assert!(!Dividends::<Test>::contains_key(root_netuid));
assert!(!LastUpdate::<Test>::contains_key(root_netuid));
assert!(!PruningScores::<Test>::contains_key(root_netuid));
assert!(!ValidatorTrust::<Test>::contains_key(root_netuid));
assert!(!ValidatorPermit::<Test>::contains_key(root_netuid));
assert!(!StakeWeight::<Test>::contains_key(root_netuid));
assert!(!Bonds::<Test>::contains_prefix(root_netuid));

assert!(!weight.is_zero(), "Migration weight should be non-zero");
});
}
Loading