Skip to content

Commit 4a4adfd

Browse files
committed
Merge branch 'main' into feat/check-metadata-hash-extension
2 parents c80df3d + 5adbbd9 commit 4a4adfd

File tree

15 files changed

+1158
-291
lines changed

15 files changed

+1158
-291
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/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ mod errors {
126126
CommitRevealEnabled,
127127
/// Attemtping to commit/reveal weights when disabled.
128128
CommitRevealDisabled,
129+
/// Not able to join the senate.
130+
CouldNotJoinSenate,
129131
/// Attempting to set alpha high/low while disabled
130132
LiquidAlphaDisabled,
131133
/// Alpha high is too low: alpha_high > 0.8

pallets/subtensor/src/events.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,13 @@ mod events {
132132
MinDelegateTakeSet(u16),
133133
/// the target stakes per interval is set by sudo/admin transaction
134134
TargetStakesPerIntervalSet(u64),
135+
/// a member of the senate is adjusted
136+
SenateAdjusted {
137+
/// the account ID of the old senate member, if any
138+
old_member: Option<T::AccountId>,
139+
/// the account ID of the new senate member
140+
new_member: T::AccountId,
141+
},
135142
/// A coldkey has been swapped
136143
ColdkeySwapped {
137144
/// the account ID of old coldkey

pallets/subtensor/src/lib.rs

Lines changed: 40 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
}
@@ -2038,6 +2043,15 @@ pub mod pallet {
20382043
Self::do_root_register(origin, hotkey)
20392044
}
20402045

2046+
/// Attempt to adjust the senate membership to include a hotkey
2047+
#[pallet::call_index(63)]
2048+
#[pallet::weight((Weight::from_parts(0, 0)
2049+
.saturating_add(T::DbWeight::get().reads(0))
2050+
.saturating_add(T::DbWeight::get().writes(0)), DispatchClass::Normal, Pays::Yes))]
2051+
pub fn adjust_senate(origin: OriginFor<T>, hotkey: T::AccountId) -> DispatchResult {
2052+
Self::do_adjust_senate(origin, hotkey)
2053+
}
2054+
20412055
/// User register a new subnetwork via burning token
20422056
#[pallet::call_index(7)]
20432057
#[pallet::weight((Weight::from_parts(177_000_000, 0)
@@ -2052,17 +2066,17 @@ pub mod pallet {
20522066
}
20532067

20542068
/// The extrinsic for user to change its hotkey
2055-
#[pallet::call_index(70)]
2056-
#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
2057-
.saturating_add(T::DbWeight::get().reads(272))
2058-
.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
2059-
pub fn swap_hotkey(
2060-
origin: OriginFor<T>,
2061-
hotkey: T::AccountId,
2062-
new_hotkey: T::AccountId,
2063-
) -> DispatchResultWithPostInfo {
2064-
Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
2065-
}
2069+
///#[pallet::call_index(70)]
2070+
///#[pallet::weight((Weight::from_parts(1_940_000_000, 0)
2071+
///.saturating_add(T::DbWeight::get().reads(272))
2072+
///.saturating_add(T::DbWeight::get().writes(527)), DispatchClass::Operational, Pays::No))]
2073+
///pub fn swap_hotkey(
2074+
/// origin: OriginFor<T>,
2075+
/// hotkey: T::AccountId,
2076+
/// new_hotkey: T::AccountId,
2077+
///) -> DispatchResultWithPostInfo {
2078+
/// Self::do_swap_hotkey(origin, &hotkey, &new_hotkey)
2079+
///}
20662080
20672081
/// The extrinsic for user to change the coldkey associated with their account.
20682082
///
@@ -2089,7 +2103,6 @@ pub mod pallet {
20892103
) -> DispatchResultWithPostInfo {
20902104
Self::do_swap_coldkey(origin, &new_coldkey)
20912105
}
2092-
20932106
/// Unstakes all tokens associated with a hotkey and transfers them to a new coldkey.
20942107
///
20952108
/// # Arguments
@@ -2105,6 +2118,7 @@ pub mod pallet {
21052118
/// # Weight
21062119
///
21072120
/// Weight is calculated based on the number of database reads and writes.
2121+
#[cfg(test)]
21082122
#[pallet::call_index(72)]
21092123
#[pallet::weight((Weight::from_parts(21_000_000, 0)
21102124
.saturating_add(T::DbWeight::get().reads(3))
@@ -2244,6 +2258,17 @@ pub mod pallet {
22442258
pub fn dissolve_network(origin: OriginFor<T>, netuid: u16) -> DispatchResult {
22452259
Self::user_remove_network(origin, netuid)
22462260
}
2261+
2262+
/// Sets values for liquid alpha
2263+
#[pallet::call_index(64)]
2264+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
2265+
pub fn sudo_hotfix_swap_coldkey_delegates(
2266+
_origin: OriginFor<T>,
2267+
_old_coldkey: T::AccountId,
2268+
_new_coldkey: T::AccountId,
2269+
) -> DispatchResult {
2270+
Ok(())
2271+
}
22472272
}
22482273

22492274
// ---- Subtensor helper functions.

pallets/subtensor/src/migration.rs

Lines changed: 80 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,85 @@ 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+
// Iterate through all staking hotkeys.
41+
for (coldkey, hotkey_vec) in StakingHotkeys::<T>::iter() {
42+
// Init the zero value.
43+
let mut coldkey_stake_sum: u64 = 0;
44+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
45+
46+
// Calculate the total stake for the current coldkey.
47+
for hotkey in hotkey_vec {
48+
// Cant fail on retrieval.
49+
coldkey_stake_sum =
50+
coldkey_stake_sum.saturating_add(Stake::<T>::get(hotkey, coldkey.clone()));
51+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
52+
}
53+
// Update the `TotalColdkeyStake` storage with the calculated stake sum.
54+
// Cant fail on insert.
55+
TotalColdkeyStake::<T>::insert(coldkey.clone(), coldkey_stake_sum);
56+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
57+
}
58+
weight
59+
}
60+
61+
/// Migrates and fixes the total coldkey stake.
62+
///
63+
/// This function checks if the migration has already run, and if not, it performs the migration
64+
/// to fix the total coldkey stake. It also marks the migration as completed after running.
65+
///
66+
/// # Returns
67+
/// The weight of the migration process.
68+
pub fn migrate_fix_total_coldkey_stake<T: Config>() -> Weight {
69+
let migration_name = b"fix_total_coldkey_stake_v7".to_vec();
70+
71+
// Initialize the weight with one read operation.
72+
let mut weight = T::DbWeight::get().reads(1);
73+
74+
// Check if the migration has already run
75+
if HasMigrationRun::<T>::get(&migration_name) {
76+
log::info!(
77+
"Migration '{:?}' has already run. Skipping.",
78+
migration_name
79+
);
80+
return Weight::zero();
81+
}
82+
83+
log::info!(
84+
"Running migration '{}'",
85+
String::from_utf8_lossy(&migration_name)
86+
);
87+
88+
// Run the migration
89+
weight = weight.saturating_add(do_migrate_fix_total_coldkey_stake::<T>());
90+
91+
// Mark the migration as completed
92+
HasMigrationRun::<T>::insert(&migration_name, true);
93+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
94+
95+
// Set the storage version to 7
96+
StorageVersion::new(7).put::<Pallet<T>>();
97+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
98+
99+
log::info!(
100+
"Migration '{:?}' completed. Storage version set to 7.",
101+
String::from_utf8_lossy(&migration_name)
102+
);
103+
104+
// Return the migration weight.
105+
weight
106+
}
27107
/// Performs migration to update the total issuance based on the sum of stakes and total balances.
28108
/// This migration is applicable only if the current storage version is 5, after which it updates the storage version to 6.
29109
///

0 commit comments

Comments
 (0)