Skip to content

Commit 660d9c7

Browse files
authored
Merge pull request #1449 from opentensor/debloat-TotalHotkeyAlpha
Debloat total hotkey alpha
2 parents 8206aa3 + 554a7e8 commit 660d9c7

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

pallets/subtensor/src/macros/hooks.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ mod hooks {
8383
// Remove unused maps entries
8484
.saturating_add(migrations::migrate_remove_unused_maps_and_values::migrate_remove_unused_maps_and_values::<T>())
8585
// Set last emission block number for all existed subnets before start call feature applied
86-
.saturating_add(migrations::migrate_set_first_emission_block_number::migrate_set_first_emission_block_number::<T>());
86+
.saturating_add(migrations::migrate_set_first_emission_block_number::migrate_set_first_emission_block_number::<T>())
87+
// Remove all zero value entries in TotalHotkeyAlpha
88+
.saturating_add(migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::<T>());
8789
weight
8890
}
8991

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use super::*;
2+
use frame_support::{traits::Get, weights::Weight};
3+
use log;
4+
use scale_info::prelude::string::String;
5+
6+
pub fn migrate_remove_zero_total_hotkey_alpha<T: Config>() -> Weight {
7+
let migration_name = b"migrate_remove_zero_total_hotkey_alpha".to_vec();
8+
let mut weight = T::DbWeight::get().reads(1);
9+
10+
// ------------------------------
11+
// Step 0: Check if already run
12+
// ------------------------------
13+
if HasMigrationRun::<T>::get(&migration_name) {
14+
log::info!(
15+
"Migration '{:?}' has already run. Skipping.",
16+
migration_name
17+
);
18+
return weight;
19+
}
20+
21+
log::info!(
22+
"Running migration '{}'",
23+
String::from_utf8_lossy(&migration_name)
24+
);
25+
26+
// ------------------------------
27+
// Step 1: Remove any zero entries in TotalHotkeyAlpha
28+
// ------------------------------
29+
30+
let mut removed_entries_count = 0u64;
31+
32+
// For each (hotkey, netuid, alpha) entry, remove if alpha == 0
33+
for (hotkey, netuid, alpha) in TotalHotkeyAlpha::<T>::iter() {
34+
if alpha == 0 {
35+
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
36+
removed_entries_count = removed_entries_count.saturating_add(1);
37+
}
38+
}
39+
40+
weight = weight.saturating_add(T::DbWeight::get().reads(removed_entries_count));
41+
weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count));
42+
43+
log::info!(
44+
"Removed {} zero entries from TotalHotkeyAlpha.",
45+
removed_entries_count
46+
);
47+
48+
// ------------------------------
49+
// Step 2: Mark Migration as Completed
50+
// ------------------------------
51+
HasMigrationRun::<T>::insert(&migration_name, true);
52+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
53+
54+
log::info!(
55+
"Migration '{:?}' completed successfully.",
56+
String::from_utf8_lossy(&migration_name)
57+
);
58+
59+
weight
60+
}

pallets/subtensor/src/migrations/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub mod migrate_populate_owned_hotkeys;
1111
pub mod migrate_rao;
1212
pub mod migrate_remove_stake_map;
1313
pub mod migrate_remove_unused_maps_and_values;
14+
pub mod migrate_remove_zero_total_hotkey_alpha;
1415
pub mod migrate_set_first_emission_block_number;
1516
pub mod migrate_set_min_burn;
1617
pub mod migrate_set_min_difficulty;

pallets/subtensor/src/staking/recycle_alpha.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ impl<T: Config> Pallet<T> {
4242
Error::<T>::InsufficientLiquidity
4343
);
4444

45-
TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| *v = v.saturating_sub(amount));
45+
if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
46+
*v = v.saturating_sub(amount);
47+
*v
48+
}) == 0
49+
{
50+
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
51+
}
52+
4653
SubnetAlphaOut::<T>::mutate(netuid, |total| {
4754
*total = total.saturating_sub(amount);
4855
});
@@ -92,7 +99,13 @@ impl<T: Config> Pallet<T> {
9299
Error::<T>::InsufficientLiquidity
93100
);
94101

95-
TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| *v = v.saturating_sub(amount));
102+
if TotalHotkeyAlpha::<T>::mutate(&hotkey, netuid, |v| {
103+
*v = v.saturating_sub(amount);
104+
*v
105+
}) == 0
106+
{
107+
TotalHotkeyAlpha::<T>::remove(&hotkey, netuid);
108+
}
96109

97110
// Deposit event
98111
Self::deposit_event(Event::AlphaBurned(coldkey, hotkey, amount, netuid));

pallets/subtensor/src/tests/migration.rs

+42
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,45 @@ fn test_migrate_set_first_emission_block_number() {
438438
}
439439
});
440440
}
441+
442+
#[test]
443+
fn test_migrate_remove_zero_total_hotkey_alpha() {
444+
new_test_ext(1).execute_with(|| {
445+
const MIGRATION_NAME: &str = "migrate_remove_zero_total_hotkey_alpha";
446+
let netuid = 1u16;
447+
448+
let hotkey_zero = U256::from(100u64);
449+
let hotkey_nonzero = U256::from(101u64);
450+
451+
// Insert one zero-alpha entry and one non-zero entry
452+
TotalHotkeyAlpha::<Test>::insert(hotkey_zero, netuid, 0u64);
453+
TotalHotkeyAlpha::<Test>::insert(hotkey_nonzero, netuid, 123u64);
454+
455+
assert_eq!(TotalHotkeyAlpha::<Test>::get(hotkey_zero, netuid), 0u64);
456+
assert_eq!(TotalHotkeyAlpha::<Test>::get(hotkey_nonzero, netuid), 123u64);
457+
458+
assert!(
459+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
460+
"Migration should not have run yet."
461+
);
462+
463+
let weight = crate::migrations::migrate_remove_zero_total_hotkey_alpha::migrate_remove_zero_total_hotkey_alpha::<Test>();
464+
465+
assert!(
466+
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
467+
"Migration should be marked as run."
468+
);
469+
470+
assert!(
471+
!TotalHotkeyAlpha::<Test>::contains_key(hotkey_zero, netuid),
472+
"Zero-alpha entry should have been removed."
473+
);
474+
475+
assert_eq!(TotalHotkeyAlpha::<Test>::get(hotkey_nonzero, netuid), 123u64);
476+
477+
assert!(
478+
!weight.is_zero(),
479+
"Migration weight should be non-zero."
480+
);
481+
});
482+
}

0 commit comments

Comments
 (0)