Skip to content

Commit 0784e5a

Browse files
authored
Merge pull request #1301 from opentensor/fix/set-diff-only-root
make set diff only root
2 parents ecf7f52 + e8d211a commit 0784e5a

File tree

6 files changed

+70
-3
lines changed

6 files changed

+70
-3
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ pub mod pallet {
653653
netuid: u16,
654654
difficulty: u64,
655655
) -> DispatchResult {
656-
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;
656+
ensure_root(origin)?;
657657
ensure!(
658658
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
659659
Error::<T>::SubnetDoesNotExist

pallets/admin-utils/src/tests/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,18 @@ fn test_sudo_set_difficulty() {
643643
to_be_set
644644
));
645645
assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set);
646+
647+
// Test that SN owner can't set difficulty
648+
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, U256::from(1));
649+
assert_eq!(
650+
AdminUtils::sudo_set_difficulty(
651+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
652+
netuid,
653+
init_value
654+
),
655+
Err(DispatchError::BadOrigin)
656+
);
657+
assert_eq!(SubtensorModule::get_difficulty_as_u64(netuid), to_be_set); // no change
646658
});
647659
}
648660

pallets/subtensor/src/macros/hooks.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ mod hooks {
8181
// Upgrade identities to V2
8282
.saturating_add(migrations::migrate_identities_v2::migrate_identities_to_v2::<T>())
8383
// Set the min burn across all subnets to a new minimum
84-
.saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::<T>());
84+
.saturating_add(migrations::migrate_set_min_burn::migrate_set_min_burn::<T>())
85+
// Set the min difficulty across all subnets to a new minimum
86+
.saturating_add(migrations::migrate_set_min_difficulty::migrate_set_min_difficulty::<T>());
8587
weight
8688
}
8789

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use alloc::string::String;
2+
3+
use frame_support::IterableStorageMap;
4+
use frame_support::{traits::Get, weights::Weight};
5+
6+
use super::*;
7+
8+
pub fn migrate_set_min_difficulty<T: Config>() -> Weight {
9+
let migration_name = b"migrate_set_min_difficulty".to_vec();
10+
11+
// Initialize the weight with one read operation.
12+
let mut weight = T::DbWeight::get().reads(1);
13+
14+
// Check if the migration has already run
15+
if HasMigrationRun::<T>::get(&migration_name) {
16+
log::info!(
17+
"Migration '{:?}' has already run. Skipping.",
18+
migration_name
19+
);
20+
return weight;
21+
}
22+
log::info!(
23+
"Running migration '{}'",
24+
String::from_utf8_lossy(&migration_name)
25+
);
26+
27+
let netuids: Vec<u16> = <NetworksAdded<T> as IterableStorageMap<u16, bool>>::iter()
28+
.map(|(netuid, _)| netuid)
29+
.collect();
30+
weight = weight.saturating_add(T::DbWeight::get().reads(netuids.len() as u64));
31+
32+
for netuid in netuids.iter().clone() {
33+
if *netuid == 0 {
34+
continue;
35+
}
36+
// Set min difficulty to 10 million for all subnets
37+
Pallet::<T>::set_min_difficulty(*netuid, 10_000_000);
38+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
39+
}
40+
41+
// Mark the migration as completed
42+
HasMigrationRun::<T>::insert(&migration_name, true);
43+
weight = weight.saturating_add(T::DbWeight::get().writes(1));
44+
45+
log::info!(
46+
"Migration '{:?}' completed.",
47+
String::from_utf8_lossy(&migration_name)
48+
);
49+
50+
// Return the migration weight.
51+
weight
52+
}

pallets/subtensor/src/migrations/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod migrate_populate_owned_hotkeys;
1212
pub mod migrate_populate_staking_hotkeys;
1313
pub mod migrate_rao;
1414
pub mod migrate_set_min_burn;
15+
pub mod migrate_set_min_difficulty;
1516
pub mod migrate_stake_threshold;
1617
pub mod migrate_subnet_volume;
1718
pub mod migrate_to_v1_separate_emission;

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
229229
// `spec_version`, and `authoring_version` are the same between Wasm and native.
230230
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
231231
// the compatible custom types.
232-
spec_version: 236,
232+
spec_version: 237,
233233
impl_version: 1,
234234
apis: RUNTIME_API_VERSIONS,
235235
transaction_version: 1,

0 commit comments

Comments
 (0)