Skip to content

Commit d298baf

Browse files
committed
Add stake weights tests
1 parent f634c5c commit d298baf

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

pallets/subtensor/tests/staking.rs

+116
Original file line numberDiff line numberDiff line change
@@ -2494,3 +2494,119 @@ fn test_anneal_global_weight() {
24942494
);
24952495
});
24962496
}
2497+
2498+
// https://github.com/opentensor/subtensor/issues/925
2499+
// cargo test -p pallet-subtensor --test staking -- test_stake_weight_should_not_be_affected_by_zero_stakes --exact --nocapture
2500+
#[test]
2501+
fn test_stake_weight_should_not_be_affected_by_zero_stakes() {
2502+
new_test_ext(1).execute_with(|| {
2503+
let registrar = AccountId::from(42); // SN Owner
2504+
let netuid: u16 = 1;
2505+
Balances::force_set_balance(RuntimeOrigin::root(), registrar, 10_000_000_000_000).unwrap();
2506+
2507+
// Add root network
2508+
add_network(0, 0, 0);
2509+
2510+
// our test subnet with id 1
2511+
SubtensorModule::register_network_with_identity(
2512+
RuntimeOrigin::signed(registrar),
2513+
registrar,
2514+
1,
2515+
None,
2516+
)
2517+
.unwrap();
2518+
2519+
let neuron_1 = AccountId::from(123);
2520+
SubtensorModule::add_balance_to_coldkey_account(&neuron_1, 10_000_000_000_000);
2521+
2522+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_1), netuid, neuron_1)
2523+
.unwrap();
2524+
SubtensorModule::add_stake(
2525+
RuntimeOrigin::signed(neuron_1),
2526+
neuron_1,
2527+
netuid,
2528+
50_000_000,
2529+
)
2530+
.unwrap();
2531+
2532+
let stake_weights_1 = SubtensorModule::get_stake_weights_for_network(netuid);
2533+
2534+
// Check neuron 1's alpha
2535+
assert!(
2536+
stake_weights_1.0[1] > 0,
2537+
"The neuron 1 should get at least some stake"
2538+
);
2539+
2540+
let neuron_2 = AccountId::from(321);
2541+
SubtensorModule::add_balance_to_coldkey_account(&neuron_2, 10_000_000_000_000);
2542+
2543+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_2), netuid, neuron_2)
2544+
.unwrap();
2545+
SubtensorModule::add_stake(
2546+
RuntimeOrigin::signed(neuron_2),
2547+
neuron_2,
2548+
netuid,
2549+
50_000_000,
2550+
)
2551+
.unwrap();
2552+
2553+
let stake_weights_2 = SubtensorModule::get_stake_weights_for_network(netuid);
2554+
2555+
// Check neuron 2's alpha
2556+
assert!(
2557+
stake_weights_2.0[2] > 0,
2558+
"The neuron 2 should get at least some stake"
2559+
);
2560+
});
2561+
}
2562+
2563+
// cargo test -p pallet-subtensor --test staking -- test_stake_weight_no_subnet_stake --exact --nocapture
2564+
#[test]
2565+
fn test_stake_weight_no_subnet_stake() {
2566+
new_test_ext(1).execute_with(|| {
2567+
let registrar = AccountId::from(42); // SN Owner
2568+
let netuid: u16 = 1;
2569+
Balances::force_set_balance(RuntimeOrigin::root(), registrar, 10_000_000_000_000).unwrap();
2570+
2571+
// Add root network
2572+
add_network(0, 0, 0);
2573+
2574+
// our test subnet with id 1
2575+
SubtensorModule::register_network_with_identity(
2576+
RuntimeOrigin::signed(registrar),
2577+
registrar,
2578+
1,
2579+
None,
2580+
)
2581+
.unwrap();
2582+
2583+
let neuron_1 = AccountId::from(123);
2584+
SubtensorModule::add_balance_to_coldkey_account(&neuron_1, 10_000_000_000_000);
2585+
2586+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_1), netuid, neuron_1)
2587+
.unwrap();
2588+
2589+
let stake_weights_1 = SubtensorModule::get_stake_weights_for_network(netuid);
2590+
2591+
// Check neuron 1's alpha
2592+
assert!(
2593+
stake_weights_1.0[1] == 0,
2594+
"The neuron 1's stake should be 0"
2595+
);
2596+
2597+
let neuron_2 = AccountId::from(321);
2598+
SubtensorModule::add_balance_to_coldkey_account(&neuron_2, 10_000_000_000_000);
2599+
2600+
SubtensorModule::burned_register(RuntimeOrigin::signed(neuron_2), netuid, neuron_2)
2601+
.unwrap();
2602+
2603+
let stake_weights_2 = SubtensorModule::get_stake_weights_for_network(netuid);
2604+
2605+
// Check neuron 2's alpha
2606+
assert!(
2607+
stake_weights_2.0[2] == 0,
2608+
"The neuron 2's stake should be 0"
2609+
);
2610+
});
2611+
}
2612+

0 commit comments

Comments
 (0)