Skip to content

Commit 4cf3a68

Browse files
committed
add tests for reg disable -> tao-in == 0
1 parent 7eaacf9 commit 4cf3a68

File tree

1 file changed

+186
-0
lines changed

1 file changed

+186
-0
lines changed

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,189 @@ fn test_total_issuance_after_coinbase() {
389389
);
390390
});
391391
}
392+
393+
// Verifies that the total issuance after the coinbase is not changed when registration is disabled.
394+
// Includes TAO weight.
395+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_registration_disabled_total_issuance_same --exact --show-output --nocapture
396+
#[test]
397+
fn test_registration_disabled_total_issuance_same() {
398+
new_test_ext(1).execute_with(|| {
399+
let netuid: u16 = 1;
400+
add_network(netuid, 1, 0);
401+
// Set TAO weight to 18%
402+
SubtensorModule::set_tao_weight(I96F32::from_num(0.18).saturating_to_num::<u64>());
403+
// Set owner cut to ~11.11%
404+
SubtensorModule::set_subnet_owner_cut(u16::MAX / 9);
405+
let total_coinbase_emission: I96F32 = I96F32::from_num(1_123_456_789);
406+
let epsilon: u64 = 100;
407+
408+
// Define hotkeys and coldkeys
409+
let hotkey_a: U256 = U256::from(1);
410+
let hotkey_b: U256 = U256::from(2);
411+
let hotkey_c: U256 = U256::from(3);
412+
let coldkey_a: U256 = U256::from(100);
413+
let coldkey_b: U256 = U256::from(101);
414+
let coldkey_c: U256 = U256::from(102);
415+
416+
// Register neurons with decreasing stakes
417+
register_ok_neuron(netuid, hotkey_a, coldkey_a, 0);
418+
register_ok_neuron(netuid, hotkey_b, coldkey_b, 0);
419+
register_ok_neuron(netuid, hotkey_c, coldkey_c, 0);
420+
421+
// Add initial stakes
422+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, 1_000);
423+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_b, 1_000);
424+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_c, 1_000);
425+
426+
// Swap to alpha
427+
let total_tao: I96F32 = I96F32::from_num(300_000 + 100_000 + 50_000);
428+
let total_alpha: I96F32 = I96F32::from_num(SubtensorModule::swap_tao_for_alpha(
429+
netuid,
430+
total_tao.saturating_to_num::<u64>(),
431+
));
432+
433+
// Set the stakes directly
434+
// This avoids needing to swap tao to alpha, impacting the initial stake distribution.
435+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
436+
&hotkey_a,
437+
&coldkey_a,
438+
netuid,
439+
(total_alpha * I96F32::from_num(300_000) / total_tao).saturating_to_num::<u64>(),
440+
);
441+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
442+
&hotkey_b,
443+
&coldkey_b,
444+
netuid,
445+
(total_alpha * I96F32::from_num(100_000) / total_tao).saturating_to_num::<u64>(),
446+
);
447+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
448+
&hotkey_c,
449+
&coldkey_c,
450+
netuid,
451+
(total_alpha * I96F32::from_num(50_000) / total_tao).saturating_to_num::<u64>(),
452+
);
453+
454+
// Stake some to root
455+
let stake_to_root: u64 = 10_000_000;
456+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, stake_to_root);
457+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
458+
&hotkey_a,
459+
&coldkey_a,
460+
netuid,
461+
stake_to_root,
462+
);
463+
464+
let alpha_price = SubtensorModule::get_alpha_price(netuid);
465+
log::info!("alpha_price: {:?}", alpha_price);
466+
467+
// Get the total issuance
468+
let mut total_issuance_before = TotalIssuance::<Test>::get();
469+
log::info!("total_issuance_before: {:?}", total_issuance_before);
470+
471+
// Disable registration on the network
472+
SubtensorModule::set_network_registration_allowed(netuid, false);
473+
SubtensorModule::set_network_pow_registration_allowed(netuid, false);
474+
475+
// Run the coinbase
476+
SubtensorModule::run_coinbase(total_coinbase_emission);
477+
478+
// Should be the same
479+
let total_issuance_after = TotalIssuance::<Test>::get();
480+
assert_abs_diff_eq!(
481+
total_issuance_after,
482+
total_issuance_before,
483+
epsilon = epsilon
484+
);
485+
});
486+
}
487+
488+
// Verifies that the TAO-in after the coinbase is not changed when registration is disabled.
489+
// Includes TAO weight.
490+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_registration_disabled_tao_in_same --exact --show-output --nocapture
491+
#[test]
492+
fn test_registration_disabled_tao_in_same() {
493+
new_test_ext(1).execute_with(|| {
494+
let netuid: u16 = 1;
495+
add_network(netuid, 1, 0);
496+
// Set TAO weight to 18%
497+
SubtensorModule::set_tao_weight(I96F32::from_num(0.18).saturating_to_num::<u64>());
498+
// Set owner cut to ~11.11%
499+
SubtensorModule::set_subnet_owner_cut(u16::MAX / 9);
500+
let total_coinbase_emission: I96F32 = I96F32::from_num(1_123_456_789);
501+
let epsilon: u64 = 100;
502+
503+
// Define hotkeys and coldkeys
504+
let hotkey_a: U256 = U256::from(1);
505+
let hotkey_b: U256 = U256::from(2);
506+
let hotkey_c: U256 = U256::from(3);
507+
let coldkey_a: U256 = U256::from(100);
508+
let coldkey_b: U256 = U256::from(101);
509+
let coldkey_c: U256 = U256::from(102);
510+
511+
// Register neurons with decreasing stakes
512+
register_ok_neuron(netuid, hotkey_a, coldkey_a, 0);
513+
register_ok_neuron(netuid, hotkey_b, coldkey_b, 0);
514+
register_ok_neuron(netuid, hotkey_c, coldkey_c, 0);
515+
516+
// Add initial stakes
517+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, 1_000);
518+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_b, 1_000);
519+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_c, 1_000);
520+
521+
// Swap to alpha
522+
let total_tao: I96F32 = I96F32::from_num(300_000 + 100_000 + 50_000);
523+
let total_alpha: I96F32 = I96F32::from_num(SubtensorModule::swap_tao_for_alpha(
524+
netuid,
525+
total_tao.saturating_to_num::<u64>(),
526+
));
527+
528+
// Set the stakes directly
529+
// This avoids needing to swap tao to alpha, impacting the initial stake distribution.
530+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
531+
&hotkey_a,
532+
&coldkey_a,
533+
netuid,
534+
(total_alpha * I96F32::from_num(300_000) / total_tao).saturating_to_num::<u64>(),
535+
);
536+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
537+
&hotkey_b,
538+
&coldkey_b,
539+
netuid,
540+
(total_alpha * I96F32::from_num(100_000) / total_tao).saturating_to_num::<u64>(),
541+
);
542+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
543+
&hotkey_c,
544+
&coldkey_c,
545+
netuid,
546+
(total_alpha * I96F32::from_num(50_000) / total_tao).saturating_to_num::<u64>(),
547+
);
548+
549+
// Stake some to root
550+
let stake_to_root: u64 = 10_000_000;
551+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_a, stake_to_root);
552+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
553+
&hotkey_a,
554+
&coldkey_a,
555+
netuid,
556+
stake_to_root,
557+
);
558+
559+
let alpha_price = SubtensorModule::get_alpha_price(netuid);
560+
log::info!("alpha_price: {:?}", alpha_price);
561+
562+
// Get the total issuance
563+
let mut tao_in_before = SubnetTAO::<Test>::get(netuid);
564+
log::info!("tao_in_before: {:?}", tao_in_before);
565+
566+
// Disable registration on the network
567+
SubtensorModule::set_network_registration_allowed(netuid, false);
568+
SubtensorModule::set_network_pow_registration_allowed(netuid, false);
569+
570+
// Run the coinbase
571+
SubtensorModule::run_coinbase(total_coinbase_emission);
572+
573+
// Should be the same
574+
let tao_in_after = SubnetTAO::<Test>::get(netuid);
575+
assert_abs_diff_eq!(tao_in_after, tao_in_before, epsilon = epsilon);
576+
});
577+
}

0 commit comments

Comments
 (0)