Skip to content

Commit e84c470

Browse files
authored
Merge pull request #996 from opentensor/mechanism-enum
Use an enum to represent mechanism ID
2 parents 485c8f2 + 2d67568 commit e84c470

File tree

14 files changed

+88
-50
lines changed

14 files changed

+88
-50
lines changed

pallets/admin-utils/tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use frame_support::{
55
};
66
use frame_system::Config;
77
use pallet_admin_utils::Error;
8-
use pallet_subtensor::{migrations, Error as SubtensorError, Event};
8+
use pallet_subtensor::{migrations, subnets, Error as SubtensorError, Event};
99
use sp_core::U256;
1010

1111
mod mock;
@@ -1203,7 +1203,7 @@ fn test_set_alpha_values_dispatch_info_ok() {
12031203
fn test_sudo_get_set_alpha() {
12041204
new_test_ext().execute_with(|| {
12051205
let netuid: u16 = 1;
1206-
let mechid: u16 = 1;
1206+
let mechid: subnets::Mechanism = subnets::Mechanism::Dynamic;
12071207
let alpha_low: u16 = 12_u16;
12081208
let alpha_high: u16 = u16::MAX - 10;
12091209

pallets/subtensor/src/benchmarks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#![allow(clippy::arithmetic_side_effects, clippy::unwrap_used)]
33
#![cfg(feature = "runtime-benchmarks")]
44

5-
use crate::Pallet as Subtensor;
65
use crate::*;
6+
use crate::{subnets::Mechanism, Pallet as Subtensor};
77
use frame_benchmarking::{account, benchmarks, whitelisted_caller};
88
use frame_support::assert_ok;
99
use frame_system::RawOrigin;
@@ -300,7 +300,7 @@ benchmarks! {
300300
let amount: u64 = 1;
301301
let amount_to_be_staked = 100_000_000_000_000u64;
302302
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
303-
}: register_network(RawOrigin::Signed(coldkey), hotkey, 1)
303+
}: register_network(RawOrigin::Signed(coldkey), hotkey, Mechanism::Dynamic)
304304

305305
// swap_hotkey {
306306
// let seed: u32 = 1;

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22
use crate::epoch::math::safe_modulo;
33
use alloc::collections::BTreeMap;
4+
use subnets::Mechanism;
45
use substrate_fixed::types::I96F32;
56

67
impl<T: Config> Pallet<T> {
@@ -28,7 +29,7 @@ impl<T: Config> Pallet<T> {
2829

2930
// --- 4. Sum all the SubnetTAO associated with the same mechanism
3031
let mut total_active_tao: I96F32 = I96F32::from_num(0);
31-
let mut mechanism_tao: BTreeMap<u16, I96F32> = BTreeMap::new();
32+
let mut mechanism_tao: BTreeMap<Mechanism, I96F32> = BTreeMap::new();
3233
for netuid in subnets.iter() {
3334
if *netuid == 0 {
3435
continue;
@@ -63,7 +64,7 @@ impl<T: Config> Pallet<T> {
6364
continue;
6465
}
6566
// 1. Get subnet mechanism ID
66-
let mechid: u16 = SubnetMechanism::<T>::get(*netuid);
67+
let mechid: Mechanism = SubnetMechanism::<T>::get(*netuid);
6768
// 2. Get subnet TAO (T_s)
6869
let subnet_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(*netuid));
6970
// 3. Get the denominator as the sum of all TAO associated with a specific mechanism (T_m)
@@ -94,7 +95,7 @@ impl<T: Config> Pallet<T> {
9495
// 11. Increase total issuance: I_new = I_old + E_s
9596
TotalIssuance::<T>::mutate(|total| *total = total.saturating_add(subnet_emission));
9697
// 12. Switch on dynamic or Stable.
97-
if mechid == 1 {
98+
if mechid.is_dynamic() {
9899
// 12a Dynamic: Add the SubnetAlpha directly into the pool immediately: A_s_new = A_s_old + E_m
99100
SubnetAlphaIn::<T>::mutate(*netuid, |total| {
100101
*total = total.saturating_add(block_emission.to_num::<u64>())

pallets/subtensor/src/lib.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ extern crate alloc;
6161
#[frame_support::pallet]
6262
pub mod pallet {
6363
use crate::migrations;
64+
use crate::subnets::Mechanism;
6465
use frame_support::{
6566
dispatch::GetDispatchInfo,
6667
pallet_prelude::{DispatchResult, StorageMap, ValueQuery, *},
@@ -225,11 +226,6 @@ pub mod pallet {
225226
0
226227
}
227228
#[pallet::type_value]
228-
/// Default value for zero.
229-
pub fn DefaultZeroU16<T: Config>() -> u16 {
230-
0
231-
}
232-
#[pallet::type_value]
233229
/// Default value for false.
234230
pub fn DefaultFalse<T: Config>() -> bool {
235231
false
@@ -1012,8 +1008,7 @@ pub mod pallet {
10121008
/// ==== Subnet Parameters =====
10131009
/// ============================
10141010
#[pallet::storage] // --- MAP ( netuid ) --> subnet mechanism
1015-
pub type SubnetMechanism<T: Config> =
1016-
StorageMap<_, Identity, u16, u16, ValueQuery, DefaultZeroU16<T>>;
1011+
pub type SubnetMechanism<T: Config> = StorageMap<_, Identity, u16, Mechanism, ValueQuery>;
10171012
#[pallet::storage]
10181013
/// --- MAP ( netuid ) --> subnetwork_n (Number of UIDs in the network).
10191014
pub type SubnetworkN<T: Config> = StorageMap<_, Identity, u16, u16, ValueQuery, DefaultN<T>>;

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ mod dispatches {
10651065
pub fn register_network(
10661066
origin: OriginFor<T>,
10671067
hotkey: T::AccountId,
1068-
mechid: u16,
1068+
mechid: Mechanism,
10691069
) -> DispatchResult {
10701070
Self::do_register_network(origin, &hotkey, mechid, None)
10711071
}
@@ -1339,7 +1339,7 @@ mod dispatches {
13391339
pub fn register_network_with_identity(
13401340
origin: OriginFor<T>,
13411341
hotkey: T::AccountId,
1342-
mechid: u16,
1342+
mechid: Mechanism,
13431343
identity: Option<SubnetIdentityOf>,
13441344
) -> DispatchResult {
13451345
Self::do_register_network(origin, &hotkey, mechid, identity)

pallets/subtensor/src/macros/genesis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod genesis {
4747
for net in 1..2 {
4848
let netuid: u16 = net as u16;
4949
let hotkey = DefaultAccount::<T>::get();
50-
SubnetMechanism::<T>::insert(netuid, 1); // Make dynamic.
50+
SubnetMechanism::<T>::insert(netuid, Mechanism::Dynamic); // Make dynamic.
5151
Owner::<T>::insert(hotkey.clone(), hotkey.clone());
5252
SubnetAlphaIn::<T>::insert(netuid, 1);
5353
SubnetTAO::<T>::insert(netuid, 10_000_000_000);

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use alloc::string::String;
33
use frame_support::IterableStorageMap;
44
use frame_support::{traits::Get, weights::Weight};
55
use log;
6+
use subnets::Mechanism;
67

78
pub fn migrate_rao<T: Config>() -> Weight {
89
let migration_name = b"migrate_rao".to_vec();
@@ -87,7 +88,7 @@ pub fn migrate_rao<T: Config>() -> Weight {
8788
*total = total.saturating_add(lock);
8889
}); // Increase the stake.
8990
TotalStake::<T>::put(TotalStake::<T>::get().saturating_add(lock)); // Increase the total stake.
90-
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
91+
SubnetMechanism::<T>::insert(netuid, Mechanism::Dynamic); // Convert to dynamic immediately with initialization.
9192
SubnetLocked::<T>::insert(netuid, lock);
9293
// Update all tempos to default
9394
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());

pallets/subtensor/src/staking/stake_ops.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::*;
2+
use subnets::Mechanism;
23
use substrate_fixed::types::I96F32;
34

45
impl<T: Config> Pallet<T> {
@@ -116,14 +117,14 @@ impl<T: Config> Pallet<T> {
116117
tao_staked: u64,
117118
) -> u64 {
118119
// Step 1: Get the mechanism type for the subnet (0 for Stable, 1 for Dynamic)
119-
let mechanism_id: u16 = SubnetMechanism::<T>::get(netuid);
120+
let mechanism_id: Mechanism = SubnetMechanism::<T>::get(netuid);
120121

121122
let alpha_staked: I96F32;
122123
let new_subnet_alpha: I96F32;
123124
// Step 2: Convert tao_staked to a fixed-point number for precise calculations
124125
let tao_staked_float: I96F32 = I96F32::from_num(tao_staked);
125126

126-
if mechanism_id == 1 {
127+
if mechanism_id.is_dynamic() {
127128
// Step 3: Dynamic mechanism calculations
128129
// Step 3a: Get current TAO and Alpha in the subnet
129130
let subnet_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(netuid));
@@ -286,7 +287,7 @@ impl<T: Config> Pallet<T> {
286287
alpha_unstaked: u64,
287288
) -> u64 {
288289
// Step 1: Get the mechanism type for this subnet
289-
let mechid: u16 = SubnetMechanism::<T>::get(netuid);
290+
let mechid: Mechanism = SubnetMechanism::<T>::get(netuid);
290291

291292
// Step 2: Initialize variables for TAO unstaked and new subnet alpha
292293
let tao_unstaked: I96F32;
@@ -306,7 +307,7 @@ impl<T: Config> Pallet<T> {
306307
return 0;
307308
}
308309

309-
if mechid == 1 {
310+
if mechid.is_dynamic() {
310311
// Step 4: Dynamic mechanism
311312
// Step 4a: Get current TAO in the subnet
312313
let subnet_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(netuid));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
use super::*;
2+
use codec::{Decode, Encode, MaxEncodedLen};
23
pub mod lock;
34
pub mod registration;
45
pub mod serving;
56
pub mod subnet;
67
pub mod tempo;
78
pub mod uids;
89
pub mod weights;
10+
11+
#[derive(
12+
Clone,
13+
Copy,
14+
Debug,
15+
Decode,
16+
Default,
17+
Encode,
18+
PartialEq,
19+
Eq,
20+
PartialOrd,
21+
Ord,
22+
MaxEncodedLen,
23+
scale_info::TypeInfo,
24+
)]
25+
pub enum Mechanism {
26+
#[default]
27+
Stable = 0,
28+
Dynamic = 1,
29+
}
30+
31+
impl Mechanism {
32+
pub fn is_stable(&self) -> bool {
33+
matches!(self, Self::Stable)
34+
}
35+
36+
pub fn is_dynamic(&self) -> bool {
37+
matches!(self, Self::Dynamic)
38+
}
39+
}

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22
use frame_support::IterableStorageMap;
33
use sp_core::Get;
4+
use subnets::Mechanism;
45

56
impl<T: Config> Pallet<T> {
67
/// Fetches the total count of subnets.
@@ -81,7 +82,7 @@ impl<T: Config> Pallet<T> {
8182
/// # Returns:
8283
/// * 'u16': The subnet mechanism
8384
///
84-
pub fn get_subnet_mechanism(netuid: u16) -> u16 {
85+
pub fn get_subnet_mechanism(netuid: u16) -> Mechanism {
8586
SubnetMechanism::<T>::get(netuid)
8687
}
8788

@@ -122,7 +123,7 @@ impl<T: Config> Pallet<T> {
122123
pub fn do_register_network(
123124
origin: T::RuntimeOrigin,
124125
hotkey: &T::AccountId,
125-
mechid: u16,
126+
mechid: Mechanism,
126127
identity: Option<SubnetIdentityOf>,
127128
) -> DispatchResult {
128129
// --- 1. Ensure the caller is a signed user.
@@ -135,7 +136,7 @@ impl<T: Config> Pallet<T> {
135136
);
136137

137138
// --- 3. Ensure the mechanism is Dynamic.
138-
ensure!(mechid == 1, Error::<T>::MechanismDoesNotExist);
139+
ensure!(mechid.is_dynamic(), Error::<T>::MechanismDoesNotExist);
139140

140141
// --- 4. Rate limit for network registrations.
141142
let current_block = Self::get_current_block_as_u64();

0 commit comments

Comments
 (0)