Skip to content
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
6 changes: 4 additions & 2 deletions .github/workflows/check-node-compat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ jobs:
strategy:
matrix:
version:
- { name: old, ref: devnet-ready }
- { name: new, ref: ${{ github.head_ref }} }
- name: old
ref: devnet-ready
- name: new
ref: ${{ github.head_ref }}

steps:
- name: Install dependencies
Expand Down
6 changes: 5 additions & 1 deletion pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,11 @@ impl<T: Config> Pallet<T> {
LastRateLimitedBlock::<T>::get(rate_limit_key)
}
pub fn set_rate_limited_last_block(rate_limit_key: &RateLimitKey<T::AccountId>, block: u64) {
LastRateLimitedBlock::<T>::insert(rate_limit_key, block);
if block == 0 {
LastRateLimitedBlock::<T>::remove(rate_limit_key);
} else {
LastRateLimitedBlock::<T>::insert(rate_limit_key, block);
}
}
pub fn remove_rate_limited_last_block(rate_limit_key: &RateLimitKey<T::AccountId>) {
LastRateLimitedBlock::<T>::remove(rate_limit_key);
Expand Down
13 changes: 9 additions & 4 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,18 @@ impl<T: Config> Pallet<T> {
log::debug!("hotkey: {hotkey:?} alpha_divs: {alpha_divs:?}");
Self::increase_stake_for_hotkey_on_subnet(&hotkey, netuid, tou64!(alpha_divs).into());
// Record dividends for this hotkey.
AlphaDividendsPerSubnet::<T>::mutate(netuid, &hotkey, |divs| {
*divs = divs.saturating_add(tou64!(alpha_divs).into());
});
let alpha_divs_u64: u64 = tou64!(alpha_divs);
if alpha_divs_u64 != 0 {
AlphaDividendsPerSubnet::<T>::mutate(netuid, &hotkey, |divs| {
*divs = divs.saturating_add(alpha_divs_u64.into());
});
}
// Record total hotkey alpha based on which this value of AlphaDividendsPerSubnet
// was calculated
let total_hotkey_alpha = TotalHotkeyAlpha::<T>::get(&hotkey, netuid);
TotalHotkeyAlphaLastEpoch::<T>::insert(hotkey, netuid, total_hotkey_alpha);
if !total_hotkey_alpha.is_zero() {
TotalHotkeyAlphaLastEpoch::<T>::insert(hotkey, netuid, total_hotkey_alpha);
}
}

// Distribute root alpha divs.
Expand Down
4 changes: 3 additions & 1 deletion pallets/subtensor/src/macros/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ mod hooks {
// Fix staking hot keys
.saturating_add(migrations::migrate_fix_staking_hot_keys::migrate_fix_staking_hot_keys::<T>())
// Migrate coldkey swap scheduled to announcements
.saturating_add(migrations::migrate_coldkey_swap_scheduled_to_announcements::migrate_coldkey_swap_scheduled_to_announcements::<T>());
.saturating_add(migrations::migrate_coldkey_swap_scheduled_to_announcements::migrate_coldkey_swap_scheduled_to_announcements::<T>())
// Remove zero-valued entries from Alpha and related storage maps
.saturating_add(migrations::migrate_remove_zero_alpha::migrate_remove_zero_alpha::<T>());
weight
}

Expand Down
98 changes: 98 additions & 0 deletions pallets/subtensor/src/migrations/migrate_remove_zero_alpha.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use super::*;
use frame_support::{traits::Get, weights::Weight};
use log;
use scale_info::prelude::string::String;

pub fn migrate_remove_zero_alpha<T: Config>() -> Weight {
let migration_name = b"migrate_remove_zero_alpha".to_vec();
let mut weight = T::DbWeight::get().reads(1);

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

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

// ------------------------------
// Step 1: Remove zero entries in Alpha (StorageNMap, value type: U64F64)
// ------------------------------
let mut removed_alpha = 0u64;

for ((hotkey, coldkey, netuid), value) in Alpha::<T>::iter() {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
if value == 0 {
Alpha::<T>::remove((&hotkey, &coldkey, netuid));
removed_alpha = removed_alpha.saturating_add(1);
}
}
weight = weight.saturating_add(T::DbWeight::get().writes(removed_alpha));
log::info!("Removed {removed_alpha} zero entries from Alpha.");

// ------------------------------
// Step 2: Remove zero entries in TotalHotkeyShares (value type: U64F64)
// ------------------------------
let mut removed_shares = 0u64;

for (hotkey, netuid, value) in TotalHotkeyShares::<T>::iter() {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
if value == 0 {
TotalHotkeyShares::<T>::remove(&hotkey, netuid);
removed_shares = removed_shares.saturating_add(1);
}
}
weight = weight.saturating_add(T::DbWeight::get().writes(removed_shares));
log::info!("Removed {removed_shares} zero entries from TotalHotkeyShares.");

// ------------------------------
// Step 3: Remove zero entries in TotalHotkeyAlphaLastEpoch (value type: AlphaBalance)
// ------------------------------
let mut removed_last_epoch = 0u64;

for (hotkey, netuid, value) in TotalHotkeyAlphaLastEpoch::<T>::iter() {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
if value.is_zero() {
TotalHotkeyAlphaLastEpoch::<T>::remove(&hotkey, netuid);
removed_last_epoch = removed_last_epoch.saturating_add(1);
}
}
weight = weight.saturating_add(T::DbWeight::get().writes(removed_last_epoch));
log::info!("Removed {removed_last_epoch} zero entries from TotalHotkeyAlphaLastEpoch.");

// ------------------------------
// Step 4: Remove zero entries in AlphaDividendsPerSubnet (value type: AlphaBalance)
// ------------------------------
let mut removed_dividends = 0u64;

for (netuid, hotkey, value) in AlphaDividendsPerSubnet::<T>::iter() {
weight = weight.saturating_add(T::DbWeight::get().reads(1));
if value.is_zero() {
AlphaDividendsPerSubnet::<T>::remove(netuid, &hotkey);
removed_dividends = removed_dividends.saturating_add(1);
}
}
weight = weight.saturating_add(T::DbWeight::get().writes(removed_dividends));
log::info!("Removed {removed_dividends} zero entries from AlphaDividendsPerSubnet.");

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

log::info!(
"Migration '{}' completed. Removed: Alpha={removed_alpha}, TotalHotkeyShares={removed_shares}, TotalHotkeyAlphaLastEpoch={removed_last_epoch}, AlphaDividendsPerSubnet={removed_dividends}",
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 @@ -41,6 +41,7 @@ pub mod migrate_remove_tao_dividends;
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;
pub mod migrate_remove_unknown_neuron_axon_cert_prom;
pub mod migrate_remove_unused_maps_and_values;
pub mod migrate_remove_zero_alpha;
pub mod migrate_remove_zero_total_hotkey_alpha;
pub mod migrate_reset_bonds_moving_average;
pub mod migrate_reset_max_burn;
Expand Down
14 changes: 10 additions & 4 deletions pallets/subtensor/src/staking/claim_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,10 @@ impl<T: Config> Pallet<T> {
.saturating_to_num(),
);

// Set the new root claimed value.
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
// Set the new root claimed value, removing if zero to avoid storage bloat.
if new_root_claimed != 0 {
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
}
}
}

Expand All @@ -290,8 +292,12 @@ impl<T: Config> Pallet<T> {
.saturating_to_num(),
);

// Set the new root_claimed value.
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
// Set the new root_claimed value, removing if zero to avoid storage bloat.
if new_root_claimed != 0 {
RootClaimed::<T>::insert((netuid, hotkey, coldkey), new_root_claimed);
} else {
RootClaimed::<T>::remove((netuid, hotkey, coldkey));
}
}
}

Expand Down
8 changes: 2 additions & 6 deletions pallets/subtensor/src/staking/set_children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,11 @@ impl<T: Config> Pallet<T> {
for (parent, _) in relations.parents().iter() {
let mut ck = ChildKeys::<T>::get(parent.clone(), netuid);
PCRelations::<T>::remove_edge(&mut ck, old_hotkey);
ChildKeys::<T>::insert(parent.clone(), netuid, ck);
Self::set_childkeys(parent.clone(), netuid, ck);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
}
// 2c) Clear direct maps of old_hotkey
ChildKeys::<T>::insert(
old_hotkey.clone(),
netuid,
Vec::<(u64, T::AccountId)>::new(),
);
ChildKeys::<T>::remove(old_hotkey.clone(), netuid);
Self::set_parentkeys(
old_hotkey.clone(),
netuid,
Expand Down
8 changes: 6 additions & 2 deletions pallets/subtensor/src/swap/swap_coldkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ impl<T: Config> Pallet<T> {
}

StakingHotkeys::<T>::remove(old_coldkey);
StakingHotkeys::<T>::insert(new_coldkey, new_staking_hotkeys);
if !new_staking_hotkeys.is_empty() {
StakingHotkeys::<T>::insert(new_coldkey, new_staking_hotkeys);
}
}

/// Transfer the ownership of the hotkeys owned by the old coldkey to the new coldkey.
Expand All @@ -178,6 +180,8 @@ impl<T: Config> Pallet<T> {
}
}
OwnedHotkeys::<T>::remove(old_coldkey);
OwnedHotkeys::<T>::insert(new_coldkey, new_owned_hotkeys);
if !new_owned_hotkeys.is_empty() {
OwnedHotkeys::<T>::insert(new_coldkey, new_owned_hotkeys);
}
}
}
81 changes: 50 additions & 31 deletions pallets/subtensor/src/swap/swap_hotkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,13 @@ impl<T: Config> Pallet<T> {
// 1. Swap total hotkey alpha for all subnets it exists on.
// TotalHotkeyAlpha( hotkey, netuid ) -> alpha -- the total alpha that the hotkey has on a specific subnet.
let alpha = TotalHotkeyAlpha::<T>::take(old_hotkey, netuid);

TotalHotkeyAlpha::<T>::mutate(new_hotkey, netuid, |value| {
*value = value.saturating_add(alpha)
});
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if !alpha.is_zero() {
TotalHotkeyAlpha::<T>::mutate(new_hotkey, netuid, |value| {
*value = value.saturating_add(alpha)
});
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
}

// 2. Swap total hotkey shares on all subnets it exists on.
// TotalHotkeyShares( hotkey, netuid ) -> share pool denominator for this hotkey on this subnet.
Expand All @@ -387,8 +389,11 @@ impl<T: Config> Pallet<T> {

let total_old_plus_new_shares =
total_new_shares.add(&total_old_shares).unwrap_or_default();
TotalHotkeySharesV2::<T>::insert(new_hotkey, netuid, total_old_plus_new_shares);
weight.saturating_accrue(T::DbWeight::get().writes(3));
weight.saturating_accrue(T::DbWeight::get().writes(2));
if !total_old_plus_new_shares.is_zero() {
TotalHotkeySharesV2::<T>::insert(new_hotkey, netuid, total_old_plus_new_shares);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
}

// 3. Swap all subnet specific info.
Expand All @@ -397,8 +402,11 @@ impl<T: Config> Pallet<T> {
// IsNetworkMember( hotkey, netuid ) -> bool -- is the hotkey a subnet member.
let is_network_member: bool = IsNetworkMember::<T>::get(old_hotkey, netuid);
IsNetworkMember::<T>::remove(old_hotkey, netuid);
IsNetworkMember::<T>::insert(new_hotkey, netuid, is_network_member);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if is_network_member {
IsNetworkMember::<T>::insert(new_hotkey, netuid, is_network_member);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// 3.2 Swap Uids + Keys.
// Keys( netuid, hotkey ) -> uid -- the uid the hotkey has in the network if it is a member.
Expand Down Expand Up @@ -523,23 +531,28 @@ impl<T: Config> Pallet<T> {
// 8.1 Swap TotalHotkeyAlphaLastEpoch
let old_alpha = TotalHotkeyAlphaLastEpoch::<T>::take(old_hotkey, netuid);
let new_total_hotkey_alpha = TotalHotkeyAlphaLastEpoch::<T>::get(new_hotkey, netuid);
TotalHotkeyAlphaLastEpoch::<T>::insert(
new_hotkey,
netuid,
old_alpha.saturating_add(new_total_hotkey_alpha),
);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
let combined_alpha_last_epoch = old_alpha.saturating_add(new_total_hotkey_alpha);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1));
if !combined_alpha_last_epoch.is_zero() {
TotalHotkeyAlphaLastEpoch::<T>::insert(
new_hotkey,
netuid,
combined_alpha_last_epoch,
);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// 8.2 Swap AlphaDividendsPerSubnet
let old_hotkey_alpha_dividends = AlphaDividendsPerSubnet::<T>::get(netuid, old_hotkey);
let new_hotkey_alpha_dividends = AlphaDividendsPerSubnet::<T>::get(netuid, new_hotkey);
AlphaDividendsPerSubnet::<T>::remove(netuid, old_hotkey);
AlphaDividendsPerSubnet::<T>::insert(
netuid,
new_hotkey,
old_hotkey_alpha_dividends.saturating_add(new_hotkey_alpha_dividends),
);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 2));
let combined_dividends =
old_hotkey_alpha_dividends.saturating_add(new_hotkey_alpha_dividends);
weight.saturating_accrue(T::DbWeight::get().reads_writes(2, 1));
if !combined_dividends.is_zero() {
AlphaDividendsPerSubnet::<T>::insert(netuid, new_hotkey, combined_dividends);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// 8.3 Swap TaoDividendsPerSubnet
// Tao dividends were removed
Expand Down Expand Up @@ -574,12 +587,16 @@ impl<T: Config> Pallet<T> {
let new_alpha = Alpha::<T>::take((new_hotkey, &coldkey, netuid));
Alpha::<T>::remove((old_hotkey, &coldkey, netuid));

// Insert into AlphaV2 because Alpha is deprecated
AlphaV2::<T>::insert(
(new_hotkey, &coldkey, netuid),
SafeFloat::from(alpha.saturating_add(new_alpha)),
);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
// Only insert combined alpha if non-zero to avoid storage bloat.
let combined_alpha = alpha.saturating_add(new_alpha);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if combined_alpha != 0 {
AlphaV2::<T>::insert(
(new_hotkey, &coldkey, netuid),
SafeFloat::from(combined_alpha),
);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// Swap StakingHotkeys.
// StakingHotkeys( coldkey ) --> Vec<hotkey> -- the hotkeys that the coldkey stakes.
Expand All @@ -602,11 +619,13 @@ impl<T: Config> Pallet<T> {

let new_alpha_v2 = AlphaV2::<T>::take((new_hotkey, &coldkey, netuid));
AlphaV2::<T>::remove((old_hotkey, &coldkey, netuid));
AlphaV2::<T>::insert(
(new_hotkey, &coldkey, netuid),
alpha.add(&new_alpha_v2).unwrap_or_default(),
);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 2));
// Only insert combined alpha if non-zero to avoid storage bloat.
let combined_alpha_v2 = alpha.add(&new_alpha_v2).unwrap_or_default();
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
if !combined_alpha_v2.is_zero() {
AlphaV2::<T>::insert((new_hotkey, &coldkey, netuid), combined_alpha_v2);
weight.saturating_accrue(T::DbWeight::get().writes(1));
}

// Swap StakingHotkeys.
// StakingHotkeys( coldkey ) --> Vec<hotkey> -- the hotkeys that the coldkey stakes.
Expand Down
Loading
Loading