Skip to content

Commit aa5e890

Browse files
camfairchildSamuel Daredistributedstatemachineconstsam0x17
authored
Merge main into devnet ready 7 18 2024 (#650)
* draft: hotkey swap for senate * feat: remove schedule coldkey swap * chore: fix tests * fixes for network resumption * chore: bump spec * fix: add back arbitration check * fix: hotkey * fix: coldkey arb swap hotkey * swap delegate stake also * add test for this * swap over stakinghotkeys map * check map first * add staking hotkeys test * no take * keep old stake * add check to test * fix some tests * fix delegate test * update staking hotekys maps * init * comment out * add admin swap * fix swap * .. * hotkey staking maps fix * remove staking hotkeys * remove commented code * cargo fmt * cargo fix --workspace * bump spec_version to 165 * swap hotkey benchmark removed * add migration and tests for total coldkey hotfix * adds new test for missing hotkey value * bump migration values * fmt * feat: try runtime passing * chore: remove commented code * chore: make logs human readable * chore: remove comments * Update pallets/subtensor/src/lib.rs Co-authored-by: Sam Johnson <[email protected]> * add test first * clear map before rest of migration * track weight for clear/removal * add comment * rename test * add test for emission appends staking hotkeys map * Update runtime/src/lib.rs --------- Co-authored-by: Samuel Dare <[email protected]> Co-authored-by: distributedstatemachine <[email protected]> Co-authored-by: const <[email protected]> Co-authored-by: Sam Johnson <[email protected]> Co-authored-by: Unconst <[email protected]>
1 parent 98a1ed2 commit aa5e890

File tree

12 files changed

+819
-261
lines changed

12 files changed

+819
-261
lines changed

pallets/admin-utils/tests/mock.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ parameter_types! {
110110
pub const InitialSubnetLimit: u16 = 10; // Max 10 subnets.
111111
pub const InitialNetworkRateLimit: u64 = 0;
112112
pub const InitialTargetStakesPerInterval: u16 = 1;
113-
pub const InitialHotkeySwapCost: u64 = 1_000_000_000;
113+
pub const InitialKeySwapCost: u64 = 1_000_000_000;
114114
pub const InitialAlphaHigh: u16 = 58982; // Represents 0.9 as per the production default
115115
pub const InitialAlphaLow: u16 = 45875; // Represents 0.7 as per the production default
116116
pub const InitialLiquidAlphaOn: bool = false; // Default value for LiquidAlphaOn
@@ -166,7 +166,7 @@ impl pallet_subtensor::Config for Test {
166166
type InitialSubnetLimit = InitialSubnetLimit;
167167
type InitialNetworkRateLimit = InitialNetworkRateLimit;
168168
type InitialTargetStakesPerInterval = InitialTargetStakesPerInterval;
169-
type HotkeySwapCost = InitialHotkeySwapCost;
169+
type KeySwapCost = InitialKeySwapCost;
170170
type AlphaHigh = InitialAlphaHigh;
171171
type AlphaLow = InitialAlphaLow;
172172
type LiquidAlphaOn = InitialLiquidAlphaOn;

pallets/subtensor/src/benchmarks.rs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -314,33 +314,33 @@ benchmarks! {
314314
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
315315
}: dissolve_network(RawOrigin::Signed(coldkey), 1)
316316

317-
swap_hotkey {
318-
let seed: u32 = 1;
319-
let coldkey: T::AccountId = account("Alice", 0, seed);
320-
let old_hotkey: T::AccountId = account("Bob", 0, seed);
321-
let new_hotkey: T::AccountId = account("Charlie", 0, seed);
322-
323-
let netuid = 1u16;
324-
Subtensor::<T>::init_new_network(netuid, 100);
325-
Subtensor::<T>::set_min_burn(netuid, 1);
326-
Subtensor::<T>::set_max_burn(netuid, 1);
327-
Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
328-
Subtensor::<T>::set_max_registrations_per_block(netuid, 256);
329-
330-
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
331-
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
332-
assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));
333-
334-
let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
335-
for i in 0..max_uids - 1 {
336-
let coldkey: T::AccountId = account("Axon", 0, i);
337-
let hotkey: T::AccountId = account("Hotkey", 0, i);
338-
339-
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
340-
assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
341-
assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
342-
}
343-
}: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
317+
// swap_hotkey {
318+
// let seed: u32 = 1;
319+
// let coldkey: T::AccountId = account("Alice", 0, seed);
320+
// let old_hotkey: T::AccountId = account("Bob", 0, seed);
321+
// let new_hotkey: T::AccountId = account("Charlie", 0, seed);
322+
323+
// let netuid = 1u16;
324+
// Subtensor::<T>::init_new_network(netuid, 100);
325+
// Subtensor::<T>::set_min_burn(netuid, 1);
326+
// Subtensor::<T>::set_max_burn(netuid, 1);
327+
// Subtensor::<T>::set_target_registrations_per_interval(netuid, 256);
328+
// Subtensor::<T>::set_max_registrations_per_block(netuid, 256);
329+
330+
// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
331+
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, old_hotkey.clone()));
332+
// assert_ok!(Subtensor::<T>::become_delegate(RawOrigin::Signed(coldkey.clone()).into(), old_hotkey.clone()));
333+
334+
// let max_uids = Subtensor::<T>::get_max_allowed_uids(netuid) as u32;
335+
// for i in 0..max_uids - 1 {
336+
// let coldkey: T::AccountId = account("Axon", 0, i);
337+
// let hotkey: T::AccountId = account("Hotkey", 0, i);
338+
339+
// Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), 10_000_000_000u64);
340+
// assert_ok!(Subtensor::<T>::burned_register(RawOrigin::Signed(coldkey.clone()).into(), netuid, hotkey));
341+
// assert_ok!(Subtensor::<T>::add_stake(RawOrigin::Signed(coldkey).into(), old_hotkey.clone(), 1_000_000_000));
342+
// }
343+
// }: _(RawOrigin::Signed(coldkey), old_hotkey, new_hotkey)
344344

345345
commit_weights {
346346
let tempo: u16 = 1;
@@ -429,30 +429,4 @@ reveal_weights {
429429

430430
}: reveal_weights(RawOrigin::Signed(hotkey.clone()), netuid, uids, weight_values, salt, version_key)
431431

432-
schedule_coldkey_swap {
433-
let seed: u32 = 1;
434-
let old_coldkey: T::AccountId = account("OldColdkey", 0, seed);
435-
let new_coldkey: T::AccountId = account("NewColdkey", 0, seed + 1);
436-
let hotkey: T::AccountId = account("Hotkey", 0, seed);
437-
438-
let netuid = 1u16;
439-
let tempo = 1u16;
440-
let block_number: u64 = Subtensor::<T>::get_current_block_as_u64();
441-
let nonce = 0;
442-
443-
// Initialize the network
444-
Subtensor::<T>::init_new_network(netuid, tempo);
445-
Subtensor::<T>::set_network_registration_allowed(netuid, true);
446-
447-
// Add balance to the old coldkey account
448-
let amount_to_be_staked: u64 = 1000000u32.into();
449-
Subtensor::<T>::add_balance_to_coldkey_account(&old_coldkey.clone(), amount_to_be_staked+1000000000);
450-
// Burned register the hotkey with the old coldkey
451-
assert_ok!(Subtensor::<T>::burned_register(
452-
RawOrigin::Signed(old_coldkey.clone()).into(),
453-
netuid,
454-
hotkey.clone()
455-
));
456-
457-
}: schedule_coldkey_swap(RawOrigin::Signed(old_coldkey.clone()), new_coldkey.clone(), vec![], block_number, nonce)
458432
}

pallets/subtensor/src/lib.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub mod pallet {
8484

8585
/// Tracks version for migrations. Should be monotonic with respect to the
8686
/// order of migrations. (i.e. always increasing)
87-
const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
87+
const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);
8888

8989
/// Minimum balance required to perform a coldkey swap
9090
pub const MIN_BALANCE_TO_PERFORM_COLDKEY_SWAP: u64 = 100_000_000; // 0.1 TAO in RAO
@@ -247,7 +247,7 @@ pub mod pallet {
247247
type InitialTargetStakesPerInterval: Get<u64>;
248248
/// Cost of swapping a hotkey.
249249
#[pallet::constant]
250-
type HotkeySwapCost: Get<u64>;
250+
type KeySwapCost: Get<u64>;
251251
/// The upper bound for the alpha parameter. Used for Liquid Alpha.
252252
#[pallet::constant]
253253
type AlphaHigh: Get<u16>;
@@ -1140,6 +1140,9 @@ pub mod pallet {
11401140
DefaultBonds<T>,
11411141
>;
11421142

1143+
#[pallet::storage] // --- Storage for migration run status
1144+
pub type HasMigrationRun<T: Config> = StorageMap<_, Identity, Vec<u8>, bool, ValueQuery>;
1145+
11431146
/// ==================
11441147
/// ==== Genesis =====
11451148
/// ==================
@@ -1419,7 +1422,9 @@ pub mod pallet {
14191422
// Populate OwnedHotkeys map for coldkey swap. Doesn't update storage vesion.
14201423
.saturating_add(migration::migrate_populate_owned::<T>())
14211424
// Populate StakingHotkeys map for coldkey swap. Doesn't update storage vesion.
1422-
.saturating_add(migration::migrate_populate_staking_hotkeys::<T>());
1425+
.saturating_add(migration::migrate_populate_staking_hotkeys::<T>())
1426+
// Fix total coldkey stake.
1427+
.saturating_add(migration::migrate_fix_total_coldkey_stake::<T>());
14231428

14241429
weight
14251430
}
@@ -2062,17 +2067,17 @@ pub mod pallet {
20622067
}
20632068

20642069
/// The extrinsic for user to change its hotkey
2065-
#[pallet::call_index(70)]
2066-
#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
2067-
.saturating_add(T::DbWeight::get().reads(272))
2068-
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
2069-
pub fn swap_hotkey(
2070-
origin: OriginFor<T>,
2071-
hotkey: T::AccountId,
2072-
new_hotkey: T::AccountId,
2073-
) -> DispatchResultWithPostInfo {
2074-
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
2075-
}
2070+
///#[pallet::call_index(70)]
2071+
///#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
2072+
///.saturating_add(T::DbWeight::get().reads(272))
2073+
///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
2074+
///pub fn swap_hotkey(
2075+
/// origin: OriginFor<T>,
2076+
/// hotkey: T::AccountId,
2077+
/// new_hotkey: T::AccountId,
2078+
///) -> DispatchResultWithPostInfo {
2079+
/// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
2080+
///}
20762081
20772082
/// The extrinsic for user to change the coldkey associated with their account.
20782083
///
@@ -2099,7 +2104,6 @@ pub mod pallet {
20992104
) -> DispatchResultWithPostInfo {
21002105
Self::do_swap_coldkey(origin, &new_coldkey)
21012106
}
2102-
21032107
/// Unstakes all tokens associated with a hotkey and transfers them to a new coldkey.
21042108
///
21052109
/// # Arguments
@@ -2115,6 +2119,7 @@ pub mod pallet {
21152119
/// # Weight
21162120
///
21172121
/// Weight is calculated based on the number of database reads and writes.
2122+
#[cfg(test)]
21182123
#[pallet::call_index(72)]
21192124
#[pallet::weight((Weight::from_parts(21_000_000, 0)
21202125
.saturating_add(T::DbWeight::get().reads(3))
@@ -2254,6 +2259,17 @@ pub mod pallet {
22542259
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
22552260
Self::user_remove_network(origin, netuid)
22562261
}
2262+
2263+
/// Sets values for liquid alpha
2264+
#[pallet::call_index(64)]
2265+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
2266+
pub fn sudo_hotfix_swap_coldkey_delegates(
2267+
_origin: OriginFor<T>,
2268+
_old_coldkey: T::AccountId,
2269+
_new_coldkey: T::AccountId,
2270+
) -> DispatchResult {
2271+
Ok(())
2272+
}
22572273
}
22582274

22592275
// ---- Subtensor helper functions.

pallets/subtensor/src/migration.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use alloc::string::String;
23
use frame_support::traits::DefensiveResult;
34
use frame_support::{
45
pallet_prelude::{Identity, OptionQuery},
@@ -24,6 +25,92 @@ pub mod deprecated_loaded_emission_format {
2425
StorageMap<Pallet<T>, Identity, u16, Vec<(AccountIdOf<T>, u64)>, OptionQuery>;
2526
}
2627

28+
/// Migrates and fixes the total coldkey stake.
29+
///
30+
/// This function iterates through all staking hotkeys, calculates the total stake for each coldkey,
31+
/// and updates the `TotalColdkeyStake` storage accordingly. The migration is only performed if the
32+
/// on-chain storage version is 6.
33+
///
34+
/// # Returns
35+
/// The weight of the migration process.
36+
pub fn do_migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
37+
// Initialize the weight with one read operation.
38+
let mut weight = T::DbWeight::get().reads(1);
39+
40+
// Clear everything from the map first, no limit (u32::MAX)
41+
let removal_results = TotalColdkeyStake::<T>::clear(u32::MAX, None);
42+
// 1 read/write per removal
43+
let entries_removed: u64 = removal_results.backend.into();
44+
weight =
45+
weight.saturating_add(T::DbWeight::get().reads_writes(entries_removed, entries_removed));
46+
47+
// Iterate through all staking hotkeys.
48+
for (coldkey, hotkey_vec) in StakingHotkeys::<T>::iter() {
49+
// Init the zero value.
50+
let mut coldkey_stake_sum: u64 = 0;
51+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
52+
53+
// Calculate the total stake for the current coldkey.
54+
for hotkey in hotkey_vec {
55+
// Cant fail on retrieval.
56+
coldkey_stake_sum =
57+
coldkey_stake_sum.saturating_add(Stake::<T>::get(hotkey, coldkey.clone()));
58+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
59+
}
60+
// Update the `TotalColdkeyStake` storage with the calculated stake sum.
61+
// Cant fail on insert.
62+
TotalColdkeyStake::<T>::insert(coldkey.clone(), coldkey_stake_sum);
63+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
64+
}
65+
weight
66+
}
67+
68+
/// Migrates and fixes the total coldkey stake.
69+
///
70+
/// This function checks if the migration has already run, and if not, it performs the migration
71+
/// to fix the total coldkey stake. It also marks the migration as completed after running.
72+
///
73+
/// # Returns
74+
/// The weight of the migration process.
75+
pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
76+
let migration_name = b"fix_total_coldkey_stake_v7".to_vec();
77+
78+
// Initialize the weight with one read operation.
79+
let mut weight = T::DbWeight::get().reads(1);
80+
81+
// Check if the migration has already run
82+
if HasMigrationRun::<T>::get(&migration_name) {
83+
log::info!(
84+
"Migration '{:?}' has already run. Skipping.",
85+
migration_name
86+
);
87+
return Weight::zero();
88+
}
89+
90+
log::info!(
91+
"Running migration '{}'",
92+
String::from_utf8_lossy(&migration_name)
93+
);
94+
95+
// Run the migration
96+
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());
97+
98+
// Mark the migration as completed
99+
HasMigrationRun::<T>::insert(&migration_name, true);
100+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
101+
102+
// Set the storage version to 7
103+
StorageVersion::new(7).put::<Pallet<T>>();
104+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
105+
106+
log::info!(
107+
"Migration '{:?}' completed. Storage version set to 7.",
108+
String::from_utf8_lossy(&migration_name)
109+
);
110+
111+
// Return the migration weight.
112+
weight
113+
}
27114
/// Performs migration to update the total issuance based on the sum of stakes and total balances.
28115
/// This migration is applicable only if the current storage version is 5, after which it updates the storage version to 6.
29116
///

0 commit comments

Comments
 (0)