Skip to content

Commit c7a3238

Browse files
author
unconst
committed
fix subnet creation logic
1 parent 913231b commit c7a3238

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use sp_runtime::format;
66
use substrate_fixed::types::U64F64;
77

88
use super::*;
9-
use crate::subnets::subnet::POOL_INITIAL_TAO;
109

1110
pub fn migrate_rao<T: Config>() -> Weight {
1211
let migration_name = b"migrate_rao".to_vec();
@@ -76,18 +75,26 @@ pub fn migrate_rao<T: Config>() -> Weight {
7675

7776
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
7877
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
79-
let pool_initial_tao = POOL_INITIAL_TAO.min(lock.max(1));
78+
let pool_initial_tao = Pallet::<T>::get_network_min_lock();
79+
if lock < pool_initial_tao {
80+
let difference: u64 = pool_initial_tao.saturating_sub( lock );
81+
TotalIssuance::<T>::mutate(|total| {
82+
*total = total.saturating_add(difference);
83+
});
84+
}
8085

8186
let remaining_lock = lock.saturating_sub(pool_initial_tao);
8287
// Refund the owner for the remaining lock.
8388
Pallet::<T>::add_balance_to_coldkey_account(&owner, remaining_lock);
84-
SubnetTAO::<T>::insert(netuid, pool_initial_tao); // Set TAO to the lock.
85-
89+
SubnetLocked::<T>::insert( netuid, 0 ); // Clear lock amount.
90+
SubnetTAO::<T>::insert( netuid, pool_initial_tao);
91+
TotalStake::<T>::mutate(|total| {
92+
*total = total.saturating_add( pool_initial_tao);
93+
}); // Increase total stake.
8694
SubnetAlphaIn::<T>::insert(
8795
netuid,
88-
pool_initial_tao.saturating_mul(netuids.len() as u64),
89-
); // Set AlphaIn to the initial alpha distribution.
90-
96+
pool_initial_tao
97+
); // Set initial alpha to pool initial tao.
9198
SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
9299
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
93100
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use super::*;
22
use frame_support::IterableStorageMap;
33
use sp_core::Get;
44

5-
pub(crate) const POOL_INITIAL_TAO: u64 = 100_000_000_000;
6-
75
impl<T: Config> Pallet<T> {
86
/// Retrieves the unique identifier (UID) for the root network.
97
///
@@ -237,16 +235,10 @@ impl<T: Config> Pallet<T> {
237235

238236
// Put initial TAO from lock into subnet TAO and produce numerically equal amount of Alpha
239237
// The initial TAO is the locked amount, with a minimum of 1 RAO and a cap of 100 TAO.
240-
let pool_initial_tao = POOL_INITIAL_TAO.min(actual_tao_lock_amount.max(1));
241-
242-
let actual_tao_lock_amount_less_pool_tao =
243-
actual_tao_lock_amount.saturating_sub(pool_initial_tao);
238+
let pool_initial_tao = Self::get_network_min_lock();
239+
let actual_tao_lock_amount_less_pool_tao = actual_tao_lock_amount.saturating_sub(pool_initial_tao);
244240
SubnetTAO::<T>::insert(netuid_to_register, pool_initial_tao);
245-
SubnetAlphaIn::<T>::insert(
246-
netuid_to_register,
247-
pool_initial_tao.saturating_mul(Self::get_all_subnet_netuids().len() as u64),
248-
); // Set AlphaIn to the initial alpha distribution.
249-
241+
SubnetAlphaIn::<T>::insert( netuid_to_register, pool_initial_tao);
250242
SubnetOwner::<T>::insert(netuid_to_register, coldkey.clone());
251243
SubnetOwnerHotkey::<T>::insert(netuid_to_register, hotkey.clone());
252244
TotalStakeAtDynamic::<T>::insert(netuid_to_register, TotalStake::<T>::get());

pallets/subtensor/src/tests/migration.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ fn test_migrate_commit_reveal_2() {
551551
});
552552
}
553553

554-
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --workspace --test migration -- test_migrate_rao --exact --nocapture
554+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::migration::test_migrate_rao --exact --show-output --nocapture
555555
#[test]
556556
fn test_migrate_rao() {
557557
new_test_ext(1).execute_with(|| {
@@ -565,6 +565,7 @@ fn test_migrate_rao() {
565565
let coldkey3 = U256::from(5);
566566
let stake_amount: u64 = 1_000_000_000;
567567
let lock_amount: u64 = 500;
568+
NetworkMinLockCost::<Test>::set(500);
568569

569570
// Add networks root and alpha
570571
add_network(netuid_0, 1, 0);
@@ -600,7 +601,7 @@ fn test_migrate_rao() {
600601
assert_eq!(SubnetTAO::<Test>::get(netuid_0), 4 * stake_amount); // Root has everything
601602
assert_eq!(SubnetTAO::<Test>::get(netuid_1), lock_amount); // Initial Rao amount.
602603
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_0), 1); // No Alpha in pool on root.
603-
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_1), 2 * lock_amount); // Initial Rao amount == num_subnets * lock_amount
604+
assert_eq!(SubnetAlphaIn::<Test>::get(netuid_1), lock_amount); // Initial Rao amount == num_subnets * lock_amount
604605
assert_eq!(SubnetAlphaOut::<Test>::get(netuid_0), 4 * stake_amount); // All stake is outstanding.
605606
assert_eq!(SubnetAlphaOut::<Test>::get(netuid_1), 0); // No stake outstanding.
606607

pallets/subtensor/src/tests/networks.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ fn test_schedule_dissolve_network_execution_with_coldkey_swap() {
282282
})
283283
}
284284

285+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::networks::test_register_subnet_low_lock_cost --exact --show-output --nocapture
285286
#[test]
286287
fn test_register_subnet_low_lock_cost() {
287288
new_test_ext(1).execute_with(|| {
@@ -300,32 +301,34 @@ fn test_register_subnet_low_lock_cost() {
300301
// Ensure that both Subnet TAO and Subnet Alpha In equal to (actual) lock_cost
301302
assert_eq!(
302303
SubnetTAO::<Test>::get(netuid),
303-
lock_cost - ExistentialDeposit::get(),
304+
lock_cost,
304305
);
305306
assert_eq!(
306307
SubnetAlphaIn::<Test>::get(netuid),
307-
lock_cost - ExistentialDeposit::get(),
308+
lock_cost,
308309
);
309310
})
310311
}
311312

313+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::networks::test_register_subnet_high_lock_cost --exact --show-output --nocapture
312314
#[test]
313315
fn test_register_subnet_high_lock_cost() {
314316
new_test_ext(1).execute_with(|| {
315-
NetworkMinLockCost::<Test>::set(1_000_000_000_000);
316-
NetworkLastLockCost::<Test>::set(1_000_000_000_000);
317+
let lock_cost: u64 = 1_000_000_000_000;
318+
NetworkMinLockCost::<Test>::set(lock_cost);
319+
NetworkLastLockCost::<Test>::set(lock_cost);
317320

318321
// Make sure lock cost is higher than 100 TAO
319322
let lock_cost = SubtensorModule::get_network_lock_cost();
320-
assert!(lock_cost > 100_000_000_000);
323+
assert!(lock_cost >= 1_000_000_000_000);
321324

322325
let subnet_owner_coldkey = U256::from(1);
323326
let subnet_owner_hotkey = U256::from(2);
324327
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
325328
assert!(SubtensorModule::if_subnet_exist(netuid));
326329

327330
// Ensure that both Subnet TAO and Subnet Alpha In equal to 100 TAO
328-
assert_eq!(SubnetTAO::<Test>::get(netuid), 100_000_000_000,);
329-
assert_eq!(SubnetAlphaIn::<Test>::get(netuid), 100_000_000_000,);
331+
assert_eq!(SubnetTAO::<Test>::get(netuid), lock_cost);
332+
assert_eq!(SubnetAlphaIn::<Test>::get(netuid), lock_cost);
330333
})
331334
}

pallets/subtensor/src/utils/try_state.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use frame_support::traits::fungible::Inspect;
22

33
use super::*;
4-
use crate::subnets::subnet::POOL_INITIAL_TAO;
54

65
impl<T: Config> Pallet<T> {
76
/// Checks [`TotalIssuance`] equals the sum of currency issuance, total stake, and total subnet
@@ -51,7 +50,7 @@ impl<T: Config> Pallet<T> {
5150
// root network doesn't have initial pool TAO
5251
acc
5352
} else {
54-
acc.saturating_sub(POOL_INITIAL_TAO)
53+
acc.saturating_sub(Self::get_network_min_lock())
5554
}
5655
});
5756

0 commit comments

Comments
 (0)