Skip to content

Commit 86b4b19

Browse files
Migrate NetworkLastRegistered storage
1 parent 98c4e86 commit 86b4b19

File tree

8 files changed

+143
-21
lines changed

8 files changed

+143
-21
lines changed

pallets/subtensor/src/coinbase/root.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,10 +645,10 @@ impl<T: Config> Pallet<T> {
645645
NetworkLastLockCost::<T>::get()
646646
}
647647
pub fn get_network_last_lock_block() -> u64 {
648-
NetworkLastRegistered::<T>::get()
648+
Self::get_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered)
649649
}
650650
pub fn set_network_last_lock_block(block: u64) {
651-
NetworkLastRegistered::<T>::set(block);
651+
Self::set_rate_limited_last_block(&RateLimitKey::NetworkLastRegistered, block);
652652
}
653653
pub fn set_lock_reduction_interval(interval: u64) {
654654
NetworkLockReductionInterval::<T>::set(interval);

pallets/subtensor/src/lib.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,11 +556,6 @@ pub mod pallet {
556556
T::InitialNetworkImmunityPeriod::get()
557557
}
558558
#[pallet::type_value]
559-
/// Default value for network last registered.
560-
pub fn DefaultNetworkLastRegistered<T: Config>() -> u64 {
561-
0
562-
}
563-
#[pallet::type_value]
564559
/// Default value for network min allowed UIDs.
565560
pub fn DefaultNetworkMinAllowedUids<T: Config>() -> u16 {
566561
T::InitialNetworkMinAllowedUids::get()
@@ -1194,10 +1189,6 @@ pub mod pallet {
11941189
pub type NetworkImmunityPeriod<T> =
11951190
StorageValue<_, u64, ValueQuery, DefaultNetworkImmunityPeriod<T>>;
11961191
#[pallet::storage]
1197-
/// ITEM( network_last_registered_block )
1198-
pub type NetworkLastRegistered<T> =
1199-
StorageValue<_, u64, ValueQuery, DefaultNetworkLastRegistered<T>>;
1200-
#[pallet::storage]
12011192
/// ITEM( min_network_lock_cost )
12021193
pub type NetworkMinLockCost<T> = StorageValue<_, u64, ValueQuery, DefaultNetworkMinLockCost<T>>;
12031194
#[pallet::storage]
@@ -2713,4 +2704,6 @@ impl<T, H, P> CollectiveInterface<T, H, P> for () {
27132704
pub enum RateLimitKey {
27142705
// The setting sn owner hotkey operation is rate limited per netuid
27152706
SetSNOwnerHotkey(u16),
2707+
// Last registered network limit
2708+
NetworkLastRegistered,
27162709
}

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ mod hooks {
113113
// Reset max burn
114114
.saturating_add(migrations::migrate_reset_max_burn::migrate_reset_max_burn::<T>())
115115
// Migrate ColdkeySwapScheduled structure to new format
116-
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>());
116+
.saturating_add(migrations::migrate_coldkey_swap_scheduled::migrate_coldkey_swap_scheduled::<T>())
117+
// Migrate last block rate limiting storage items
118+
.saturating_add(migrations::migrate_rate_limiting_last_blocks::migrate_obsolete_rate_limiting_last_blocks_storage::<T>());
117119
weight
118120
}
119121

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use crate::Vec;
2+
use crate::{Config, HasMigrationRun, Pallet};
3+
use alloc::string::String;
4+
use codec::Decode;
5+
use frame_support::traits::Get;
6+
use frame_support::weights::Weight;
7+
use sp_io::hashing::twox_128;
8+
use sp_io::storage::{clear, get};
9+
10+
pub fn migrate_obsolete_rate_limiting_last_blocks_storage<T: Config>() -> Weight {
11+
migrate_network_last_registered::<T>()
12+
}
13+
14+
pub fn migrate_network_last_registered<T: Config>() -> Weight {
15+
let migration_name = b"migrate_network_last_registered".to_vec();
16+
let pallet_name = "SubtensorModule";
17+
let storage_name = "NetworkLastRegistered";
18+
19+
migrate_value::<T, _>(migration_name, pallet_name, storage_name, |limit| {
20+
Pallet::<T>::set_network_last_lock_block(limit);
21+
})
22+
}
23+
fn migrate_value<T, SetValueFunction>(
24+
migration_name: Vec<u8>,
25+
pallet_name: &str,
26+
storage_name: &str,
27+
set_value: SetValueFunction,
28+
) -> Weight
29+
where
30+
T: Config,
31+
SetValueFunction: Fn(u64 /*limit in blocks*/),
32+
{
33+
// Initialize the weight with one read operation.
34+
let mut weight = T::DbWeight::get().reads(1);
35+
36+
// Check if the migration has already run
37+
if HasMigrationRun::<T>::get(&migration_name) {
38+
log::info!(
39+
"Migration '{:?}' has already run. Skipping.",
40+
migration_name
41+
);
42+
return weight;
43+
}
44+
log::info!(
45+
"Running migration '{}'",
46+
String::from_utf8_lossy(&migration_name)
47+
);
48+
49+
let pallet_name_hash = twox_128(pallet_name.as_bytes());
50+
let storage_name_hash = twox_128(storage_name.as_bytes());
51+
let full_key = [pallet_name_hash, storage_name_hash].concat();
52+
53+
if let Some(value_bytes) = get(&full_key) {
54+
if let Ok(rate_limit) = Decode::decode(&mut &value_bytes[..]) {
55+
set_value(rate_limit);
56+
}
57+
58+
clear(&full_key);
59+
}
60+
61+
weight = weight.saturating_add(T::DbWeight::get().writes(2));
62+
weight = weight.saturating_add(T::DbWeight::get().reads(1));
63+
64+
// Mark the migration as completed
65+
HasMigrationRun::<T>::insert(&migration_name, true);
66+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
67+
68+
log::info!(
69+
"Migration '{:?}' completed.",
70+
String::from_utf8_lossy(&migration_name)
71+
);
72+
73+
// Return the migration weight.
74+
weight
75+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod migrate_init_total_issuance;
1515
pub mod migrate_orphaned_storage_items;
1616
pub mod migrate_populate_owned_hotkeys;
1717
pub mod migrate_rao;
18+
pub mod migrate_rate_limiting_last_blocks;
1819
pub mod migrate_remove_commitments_rate_limit;
1920
pub mod migrate_remove_stake_map;
2021
pub mod migrate_remove_total_hotkey_coldkey_stakes_this_interval;

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<T: Config> Pallet<T> {
193193
);
194194

195195
// --- 11. Set the creation terms.
196-
NetworkLastRegistered::<T>::set(current_block);
196+
Self::set_network_last_lock_block(current_block);
197197
NetworkRegisteredAt::<T>::insert(netuid_to_register, current_block);
198198

199199
// --- 14. Init the pool by putting the lock as the initial alpha.

pallets/subtensor/src/tests/migration.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,62 @@ fn test_migrate_remove_commitments_rate_limit() {
820820
assert!(!weight.is_zero(), "Migration weight should be non-zero");
821821
});
822822
}
823+
824+
#[test]
825+
fn test_migrate_network_last_registered() {
826+
new_test_ext(1).execute_with(|| {
827+
// ------------------------------
828+
// Step 1: Simulate Old Storage Entry
829+
// ------------------------------
830+
const MIGRATION_NAME: &str = "migrate_network_last_registered";
831+
832+
let pallet_name = "SubtensorModule";
833+
let storage_name = "NetworkLastRegistered";
834+
let pallet_name_hash = twox_128(pallet_name.as_bytes());
835+
let storage_name_hash = twox_128(storage_name.as_bytes());
836+
let prefix = [pallet_name_hash, storage_name_hash].concat();
837+
838+
let mut full_key = prefix.clone();
839+
840+
let original_value: u64 = 123;
841+
put_raw(&full_key, &original_value.encode());
842+
843+
let stored_before = get_raw(&full_key).expect("Expected RateLimit to exist");
844+
assert_eq!(
845+
u64::decode(&mut &stored_before[..]).expect("Failed to decode RateLimit"),
846+
original_value
847+
);
848+
849+
assert!(
850+
!HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
851+
"Migration should not have run yet"
852+
);
853+
854+
// ------------------------------
855+
// Step 2: Run the Migration
856+
// ------------------------------
857+
let weight = crate::migrations::migrate_rate_limiting_last_blocks::
858+
migrate_obsolete_rate_limiting_last_blocks_storage::<Test>();
859+
860+
assert!(
861+
HasMigrationRun::<Test>::get(MIGRATION_NAME.as_bytes().to_vec()),
862+
"Migration should be marked as completed"
863+
);
864+
865+
// ------------------------------
866+
// Step 3: Verify Migration Effects
867+
// ------------------------------
868+
869+
assert_eq!(
870+
SubtensorModule::get_network_last_lock_block(),
871+
original_value
872+
);
873+
assert_eq!(
874+
get_raw(&full_key),
875+
None,
876+
"RateLimit storage should have been cleared"
877+
);
878+
879+
assert!(!weight.is_zero(), "Migration weight should be non-zero");
880+
});
881+
}

pallets/subtensor/src/utils/rate_limiting.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,6 @@ impl<T: Config> Pallet<T> {
117117
}
118118
}
119119

120-
/// Set the block number of the last transaction for a specific key, and transaction type
121-
pub fn set_last_transaction_block(key: &T::AccountId, tx_type: &TransactionType, block: u64) {
122-
match tx_type {
123-
TransactionType::RegisterNetwork => Self::set_network_last_lock_block(block),
124-
_ => Self::set_last_transaction_block_on_subnet(key, 0, tx_type, block),
125-
}
126-
}
127-
128120
/// Set the block number of the last transaction for a specific hotkey, network, and transaction type
129121
pub fn set_last_transaction_block_on_subnet(
130122
key: &T::AccountId,

0 commit comments

Comments
 (0)