Skip to content

Commit 07da454

Browse files
authored
Merge pull request #1333 from opentensor/chore/remove-stake-map
Chore/remove stake map
2 parents 63ae469 + ce31e50 commit 07da454

18 files changed

+603
-830
lines changed

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<T: Config> Pallet<T> {
390390
.iter()
391391
.map(|updated| updated.saturating_add(activity_cutoff) < current_block)
392392
.collect();
393-
log::trace!("Inactive: {:?}", inactive.clone());
393+
log::debug!("Inactive: {:?}", inactive.clone());
394394

395395
// Logical negation of inactive.
396396
let active: Vec<bool> = inactive.iter().map(|&b| !b).collect();
@@ -406,14 +406,14 @@ impl<T: Config> Pallet<T> {
406406
let hotkeys: Vec<(u16, T::AccountId)> =
407407
<Keys<T> as IterableStorageDoubleMap<u16, u16, T::AccountId>>::iter_prefix(netuid)
408408
.collect();
409-
log::trace!("hotkeys: {:?}", &hotkeys);
409+
log::debug!("hotkeys: {:?}", &hotkeys);
410410

411411
// Access network stake as normalized vector.
412412
let (mut total_stake, _alpha_stake, _tao_stake): (Vec<I64F64>, Vec<I64F64>, Vec<I64F64>) =
413413
Self::get_stake_weights_for_network(netuid);
414414
inplace_normalize_64(&mut total_stake);
415415
let stake: Vec<I32F32> = vec_fixed64_to_fixed32(total_stake);
416-
log::trace!("Normalised Stake: {:?}", &stake);
416+
log::debug!("Normalised Stake: {:?}", &stake);
417417

418418
// =======================
419419
// == Validator permits ==
@@ -448,7 +448,7 @@ impl<T: Config> Pallet<T> {
448448

449449
// Normalize active stake.
450450
inplace_normalize(&mut active_stake);
451-
log::trace!("Active Stake:\n{:?}\n", &active_stake);
451+
log::debug!("Active Stake:\n{:?}\n", &active_stake);
452452

453453
// =============
454454
// == Weights ==

pallets/subtensor/src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -998,18 +998,6 @@ pub mod pallet {
998998
#[pallet::storage] // --- MAP ( cold ) --> Vec<hot> | Returns the vector of hotkeys controlled by this coldkey.
999999
pub type OwnedHotkeys<T: Config> =
10001000
StorageMap<_, Blake2_128Concat, T::AccountId, Vec<T::AccountId>, ValueQuery>;
1001-
#[pallet::storage]
1002-
/// (DEPRECATED) DMAP ( hot, cold ) --> stake | Returns the stake under a coldkey prefixed by hotkey.
1003-
pub type Stake<T: Config> = StorageDoubleMap<
1004-
_,
1005-
Blake2_128Concat,
1006-
T::AccountId,
1007-
Identity,
1008-
T::AccountId,
1009-
u64,
1010-
ValueQuery,
1011-
DefaultZeroU64<T>,
1012-
>;
10131001

10141002
#[pallet::storage] // --- DMAP ( cold ) --> () | Maps coldkey to if a coldkey swap is scheduled.
10151003
pub type ColdkeySwapScheduled<T: Config> =

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ mod hooks {
6363
// Populate OwnedHotkeys map for coldkey swap. Doesn't update storage vesion.
6464
// Storage version v6 -> v7
6565
.saturating_add(migrations::migrate_populate_owned_hotkeys::migrate_populate_owned::<T>())
66-
// Populate StakingHotkeys map for coldkey swap. Doesn't update storage vesion.
67-
// Storage version v7 -> v8
68-
.saturating_add(migrations::migrate_populate_staking_hotkeys::migrate_populate_staking_hotkeys::<T>())
69-
// Fix total coldkey stake.
70-
// Storage version v8 -> v9
71-
.saturating_add(migrations::migrate_fix_total_coldkey_stake::migrate_fix_total_coldkey_stake::<T>())
7266
// Migrate Delegate Ids on chain
7367
.saturating_add(migrations::migrate_chain_identity::migrate_set_hotkey_identities::<T>())
7468
// Migrate Commit-Reval 2.0
@@ -83,7 +77,9 @@ mod hooks {
8377
// Set the min burn across all subnets to a new minimum
8478
.saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::<T>())
8579
// Set the min difficulty across all subnets to a new minimum
86-
.saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::<T>());
80+
.saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::<T>())
81+
// Remove Stake map entries
82+
.saturating_add(migrations::migrate_remove_stake_map::migrate_remove_stake_map::<T>());
8783
weight
8884
}
8985

pallets/subtensor/src/migrations/migrate_fix_total_coldkey_stake.rs

Lines changed: 0 additions & 91 deletions
This file was deleted.

pallets/subtensor/src/migrations/migrate_populate_staking_hotkeys.rs

Lines changed: 0 additions & 83 deletions
This file was deleted.

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use frame_support::IterableStorageMap;
44
use frame_support::{traits::Get, weights::Weight};
55
use sp_runtime::format;
66
use substrate_fixed::types::I96F32;
7-
use substrate_fixed::types::U64F64;
87

98
use super::*;
109

@@ -36,29 +35,30 @@ pub fn migrate_rao<T: Config>() -> Weight {
3635
DynamicBlock::<T>::set(Pallet::<T>::get_current_block_as_u64());
3736

3837
// Migrate all TAO to root.
39-
Stake::<T>::iter().for_each(|(hotkey, coldkey, stake)| {
40-
// Increase SubnetTAO on root.
41-
SubnetTAO::<T>::mutate(0, |total| {
42-
*total = total.saturating_add(stake);
43-
});
44-
// Increase SubnetAlphaOut on root.
45-
SubnetAlphaOut::<T>::mutate(0, |total| {
46-
*total = total.saturating_add(stake);
47-
});
48-
// Set all the stake on root 0 subnet.
49-
Alpha::<T>::mutate((hotkey.clone(), coldkey.clone(), 0), |total| {
50-
*total = total.saturating_add(U64F64::saturating_from_num(stake))
51-
});
52-
TotalHotkeyShares::<T>::mutate(hotkey.clone(), 0, |total| {
53-
*total = total.saturating_add(U64F64::saturating_from_num(stake))
54-
});
55-
// Set the total stake on the hotkey
56-
TotalHotkeyAlpha::<T>::mutate(hotkey.clone(), 0, |total| {
57-
*total = total.saturating_add(stake)
58-
});
59-
// 6 reads and 6 writes.
60-
weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 6));
61-
});
38+
// This migration has already run, leaving this only for reference for now, since this is a recent migration
39+
// Stake::<T>::iter().for_each(|(hotkey, coldkey, stake)| {
40+
// // Increase SubnetTAO on root.
41+
// SubnetTAO::<T>::mutate(0, |total| {
42+
// *total = total.saturating_add(stake);
43+
// });
44+
// // Increase SubnetAlphaOut on root.
45+
// SubnetAlphaOut::<T>::mutate(0, |total| {
46+
// *total = total.saturating_add(stake);
47+
// });
48+
// // Set all the stake on root 0 subnet.
49+
// Alpha::<T>::mutate((hotkey.clone(), coldkey.clone(), 0), |total| {
50+
// *total = total.saturating_add(U64F64::saturating_from_num(stake))
51+
// });
52+
// TotalHotkeyShares::<T>::mutate(hotkey.clone(), 0, |total| {
53+
// *total = total.saturating_add(U64F64::saturating_from_num(stake))
54+
// });
55+
// // Set the total stake on the hotkey
56+
// TotalHotkeyAlpha::<T>::mutate(hotkey.clone(), 0, |total| {
57+
// *total = total.saturating_add(stake)
58+
// });
59+
// // 6 reads and 6 writes.
60+
// weight = weight.saturating_add(T::DbWeight::get().reads_writes(6, 6));
61+
// });
6262

6363
// Convert subnets and give them lock.
6464
// Set global weight to 18% from the start
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use super::*;
2+
use crate::HasMigrationRun;
3+
use frame_support::{traits::Get, weights::Weight};
4+
use scale_info::prelude::string::String;
5+
use sp_io::{KillStorageResult, hashing::twox_128, storage::clear_prefix};
6+
7+
pub fn migrate_remove_stake_map<T: Config>() -> Weight {
8+
let migration_name = b"migrate_remove_stake_map".to_vec();
9+
let mut weight = T::DbWeight::get().reads(1);
10+
11+
if HasMigrationRun::<T>::get(&migration_name) {
12+
log::info!(
13+
"Migration '{:?}' has already run. Skipping.",
14+
migration_name
15+
);
16+
return weight;
17+
}
18+
19+
log::info!(
20+
"Running migration '{}'",
21+
String::from_utf8_lossy(&migration_name)
22+
);
23+
24+
// ------------------------------
25+
// Step 1: Remove Stake entries
26+
// ------------------------------
27+
28+
let mut stake_prefix = Vec::new();
29+
stake_prefix.extend_from_slice(&twox_128("SubtensorModule".as_bytes()));
30+
stake_prefix.extend_from_slice(&twox_128("Stake".as_bytes()));
31+
32+
let removal_results = clear_prefix(&stake_prefix, Some(u32::MAX));
33+
34+
let removed_entries_count = match removal_results {
35+
KillStorageResult::AllRemoved(removed) => removed as u64,
36+
KillStorageResult::SomeRemaining(removed) => {
37+
log::info!("Failed To Remove Some Items During {:?}", migration_name);
38+
removed as u64
39+
}
40+
};
41+
42+
weight = weight.saturating_add(T::DbWeight::get().writes(removed_entries_count));
43+
44+
log::info!(
45+
"Removed {:?} entries from Stake map.",
46+
removed_entries_count
47+
);
48+
49+
// ------------------------------
50+
// Step 2: Mark Migration as Completed
51+
// ------------------------------
52+
53+
HasMigrationRun::<T>::insert(&migration_name, true);
54+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
55+
56+
log::info!(
57+
"Migration '{:?}' completed successfully.",
58+
String::from_utf8_lossy(&migration_name)
59+
);
60+
61+
weight
62+
}

pallets/subtensor/src/migrations/migrate_total_issuance.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,13 @@ pub fn migrate_total_issuance<T: Config>(test: bool) -> Weight {
4343
// Execute migration if the current storage version is 5 or if in test mode
4444
if Pallet::<T>::on_chain_storage_version() == StorageVersion::new(5) || test {
4545
// Calculate the sum of all stake values
46-
let stake_sum: u64 =
47-
Stake::<T>::iter().fold(0, |acc, (_, _, stake)| acc.saturating_add(stake));
48-
// Add weight for reading all stake entries
49-
weight = weight.saturating_add(T::DbWeight::get().reads(Stake::<T>::iter().count() as u64));
46+
let stake_sum: u64 = Owner::<T>::iter()
47+
.map(|(hotkey, _coldkey)| Pallet::<T>::get_total_stake_for_hotkey(&hotkey))
48+
.fold(0, |acc, stake| acc.saturating_add(stake));
49+
// Add weight for reading all Owner and TotalHotkeyStake entries
50+
weight = weight.saturating_add(
51+
T::DbWeight::get().reads((Owner::<T>::iter().count() as u64).saturating_mul(2)),
52+
);
5053

5154
// Calculate the sum of all locked subnet values
5255
let locked_sum: u64 =

0 commit comments

Comments
 (0)