Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
const authored and const committed Jul 15, 2024
1 parent 3c61aa8 commit ff23b6f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
17 changes: 8 additions & 9 deletions pallets/subtensor/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub mod deprecated_loaded_emission_format {
StorageMap<Pallet<T>, Identity, u16, Vec<(AccountIdOf<T>, u64)>, OptionQuery>;
}


/// Migrates and fixes the total coldkey stake.
///
/// This function iterates through all staking hotkeys, calculates the total stake for each coldkey,
Expand All @@ -33,7 +32,7 @@ pub mod deprecated_loaded_emission_format {
///
/// # Returns
/// The weight of the migration process.
pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight{
pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
// Initialize the weight with one read operation.
let mut weight = T::DbWeight::get().reads(1);

Expand All @@ -46,19 +45,19 @@ pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight{
// Calculate the total stake for the current coldkey.
for hotkey in hotkey_vec {
// Cant fail on retrieval.
coldkey_stake_sum = coldkey_stake_sum.saturating_add(Stake::<T>::get(hotkey, coldkey.clone()));
coldkey_stake_sum =
coldkey_stake_sum.saturating_add(Stake::<T>::get(hotkey, coldkey.clone()));
weight = weight.saturating_add(T::DbWeight::get().reads(1));
}
// Update the `TotalColdkeyStake` storage with the calculated stake sum.
// Cant fail on insert.
TotalColdkeyStake::<T>::insert( coldkey.clone(), coldkey_stake_sum );
TotalColdkeyStake::<T>::insert(coldkey.clone(), coldkey_stake_sum);
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}
weight
}
// Public migrate function to be called by Lib.rs on upgrade.
pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {

let current_storage_version: u16 = 7;
let next_storage_version: u16 = 8;

Expand All @@ -67,15 +66,15 @@ pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {

// Grab the current on-chain storage version.
// Cant fail on retrieval.
let onchain_version = Pallet::<T>::on_chain_storage_version();
let onchain_version = Pallet::<T>::on_chain_storage_version();

// Only run this migration on storage version 6.
if onchain_version == current_storage_version {
weight = weight.saturating_add( do_migrate_fix_total_coldkey_stake::<T>() );
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());
// Cant fail on insert.
StorageVersion::new( next_storage_version ).put::<Pallet<T>>();
StorageVersion::new(next_storage_version).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
}

// Return the migration weight.
weight
Expand Down
15 changes: 6 additions & 9 deletions pallets/subtensor/tests/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
mod mock;
use frame_support::assert_ok;
use frame_system::Config;
use pallet_subtensor::*;
use mock::*;
use pallet_subtensor::*;
use sp_core::U256;

#[test]
Expand Down Expand Up @@ -278,12 +278,11 @@ fn test_migration_delete_subnet_21() {
})
}


// SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test migration -- test_migrate_fix_total_coldkey_stake --exact --nocapture
#[test]
fn test_migrate_fix_total_coldkey_stake() {
new_test_ext(1).execute_with(|| {
let coldkey = U256::from(0);
let coldkey = U256::from(0);
TotalColdkeyStake::<Test>::insert(coldkey, 0);
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
Expand All @@ -298,7 +297,7 @@ fn test_migrate_fix_total_coldkey_stake() {
#[test]
fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
new_test_ext(1).execute_with(|| {
let coldkey = U256::from(0);
let coldkey = U256::from(0);
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
Expand All @@ -313,7 +312,7 @@ fn test_migrate_fix_total_coldkey_stake_value_already_in_total() {
#[test]
fn test_migrate_fix_total_coldkey_stake_no_entry() {
new_test_ext(1).execute_with(|| {
let coldkey = U256::from(0);
let coldkey = U256::from(0);
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
Stake::<Test>::insert(U256::from(2), U256::from(0), 10000);
Expand All @@ -327,7 +326,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry() {
#[test]
fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
new_test_ext(1).execute_with(|| {
let coldkey = U256::from(0);
let coldkey = U256::from(0);
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
pallet_subtensor::migration::do_migrate_fix_total_coldkey_stake::<Test>();
Expand All @@ -339,7 +338,7 @@ fn test_migrate_fix_total_coldkey_stake_no_entry_in_hotkeys() {
#[test]
fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() {
new_test_ext(1).execute_with(|| {
let coldkey = U256::from(0);
let coldkey = U256::from(0);
TotalColdkeyStake::<Test>::insert(coldkey, 100000000);
StakingHotkeys::<Test>::insert(coldkey, vec![U256::from(1), U256::from(2), U256::from(3)]);
Stake::<Test>::insert(U256::from(1), U256::from(0), 10000);
Expand All @@ -348,5 +347,3 @@ fn test_migrate_fix_total_coldkey_stake_one_hotkey_stake_missing() {
assert_eq!(TotalColdkeyStake::<Test>::get(coldkey), 20000);
})
}


2 changes: 1 addition & 1 deletion pallets/subtensor/tests/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1886,4 +1886,4 @@ fn test_coldkey_delegations() {
assert_eq!(Stake::<Test>::get(delegate, new_coldkey), 100);
assert_eq!(Stake::<Test>::get(delegate, coldkey), 0);
});
}
}

0 comments on commit ff23b6f

Please sign in to comment.