Skip to content

Commit b76d155

Browse files
authored
Merge pull request #1322 from opentensor/const-fixes
const test fixes
2 parents c6e5336 + 9396ec0 commit b76d155

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ fn close(value: u64, target: u64, eps: u64) {
1919
)
2020
}
2121

22+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_hotkey_take --exact --show-output --nocapture
23+
#[test]
24+
fn test_hotkey_take() {
25+
new_test_ext(1).execute_with(|| {
26+
let hotkey = U256::from(1);
27+
Delegates::<Test>::insert(hotkey, u16::MAX / 2);
28+
log::info!(
29+
"expected: {:?}",
30+
SubtensorModule::get_hotkey_take_float(&hotkey)
31+
);
32+
log::info!(
33+
"expected: {:?}",
34+
SubtensorModule::get_hotkey_take_float(&hotkey)
35+
);
36+
});
37+
}
38+
2239
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_dynamic_function_various_values --exact --show-output --nocapture
2340
#[test]
2441
fn test_dynamic_function_various_values() {
@@ -476,6 +493,7 @@ fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() {
476493
let stake_before: u64 = 1_000_000_000;
477494
// register_ok_neuron(root, hotkey, coldkey, 0);
478495
register_ok_neuron(netuid, hotkey, coldkey, 0);
496+
Delegates::<Test>::insert(hotkey, 0);
479497
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
480498
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
481499
&hotkey,
@@ -496,7 +514,7 @@ fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() {
496514
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
497515
let root_after =
498516
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, root);
499-
close(stake_before + pending_alpha, stake_after, 10); // Registered gets all alpha emission.
517+
close(stake_before + pending_alpha / 2, stake_after, 10); // Registered gets all alpha emission.
500518
close(stake_before + pending_tao, root_after, 10); // Registered gets all tao emission
501519
});
502520
}
@@ -549,6 +567,8 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root() {
549567
let stake_before: u64 = 1_000_000_000;
550568
register_ok_neuron(netuid, hotkey1, coldkey, 0);
551569
register_ok_neuron(netuid, hotkey2, coldkey, 0);
570+
Delegates::<Test>::insert(hotkey1, 0);
571+
Delegates::<Test>::insert(hotkey2, 0);
552572
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
553573
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
554574
&hotkey1,
@@ -585,8 +605,8 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root() {
585605
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, netuid);
586606
let root_after2 =
587607
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
588-
close(stake_before + pending_alpha / 2, stake_after1, 10); // Registered gets 1/2 emission
589-
close(stake_before + pending_alpha / 2, stake_after2, 10); // Registered gets 1/2 emission.
608+
close(stake_before + pending_alpha / 4, stake_after1, 10); // Registered gets 1/2 emission
609+
close(stake_before + pending_alpha / 4, stake_after2, 10); // Registered gets 1/2 emission.
590610
close(stake_before + pending_tao / 2, root_after1, 10); // Registered gets 1/2 tao emission
591611
close(stake_before + pending_tao / 2, root_after2, 10); // Registered gets 1/2 tao emission
592612
});
@@ -603,6 +623,8 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am
603623
let hotkey2 = U256::from(2);
604624
let coldkey = U256::from(3);
605625
let stake_before: u64 = 1_000_000_000;
626+
Delegates::<Test>::insert(hotkey1, 0);
627+
Delegates::<Test>::insert(hotkey2, 0);
606628
register_ok_neuron(netuid, hotkey1, coldkey, 0);
607629
register_ok_neuron(netuid, hotkey2, coldkey, 0);
608630
SubtensorModule::set_tao_weight(u64::MAX); // Set TAO weight to 1.0
@@ -642,10 +664,14 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am
642664
let root_after2 =
643665
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
644666
let expected_stake = I96F32::from_num(stake_before)
645-
+ I96F32::from_num(pending_alpha) * I96F32::from_num(3.0 / 5.0);
667+
+ (I96F32::from_num(pending_alpha)
668+
* I96F32::from_num(3.0 / 5.0)
669+
* I96F32::from_num(1.0 / 3.0));
646670
close(expected_stake.to_num::<u64>(), stake_after1, 10); // Registered gets 60% of emission
647671
let expected_stake2 = I96F32::from_num(stake_before)
648-
+ I96F32::from_num(pending_alpha) * I96F32::from_num(2.0 / 5.0);
672+
+ I96F32::from_num(pending_alpha)
673+
* I96F32::from_num(2.0 / 5.0)
674+
* I96F32::from_num(1.0 / 2.0);
649675
close(expected_stake2.to_num::<u64>(), stake_after2, 10); // Registered gets 40% emission
650676
let expected_root1 = I96F32::from_num(2 * stake_before)
651677
+ I96F32::from_num(pending_tao) * I96F32::from_num(2.0 / 3.0);
@@ -668,6 +694,8 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am
668694
let hotkey2 = U256::from(2);
669695
let coldkey = U256::from(3);
670696
let stake_before: u64 = 1_000_000_000;
697+
Delegates::<Test>::insert(hotkey1, 0);
698+
Delegates::<Test>::insert(hotkey2, 0);
671699
register_ok_neuron(netuid, hotkey1, coldkey, 0);
672700
register_ok_neuron(netuid, hotkey2, coldkey, 0);
673701
SubtensorModule::set_tao_weight(u64::MAX / 2); // Set TAO weight to 0.5
@@ -708,11 +736,15 @@ fn test_drain_base_with_subnet_with_two_stakers_registered_and_root_different_am
708736
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey2, &coldkey, root);
709737
// hotkey 1 has (1 + (2 * 0.5))/( 1 + 1*0.5 + 1 + (2 * 0.5)) = 0.5714285714 of the hotkey emission.
710738
let expected_stake = I96F32::from_num(stake_before)
711-
+ I96F32::from_num(pending_alpha) * I96F32::from_num(0.5714285714);
739+
+ I96F32::from_num(pending_alpha)
740+
* I96F32::from_num(0.5714285714)
741+
* I96F32::from_num(1.0 / 2.0);
712742
close(expected_stake.to_num::<u64>(), stake_after1, 10);
713743
// hotkey 2 has (1 + 1*0.5)/( 1 + 1*0.5 + 1 + (2 * 0.5)) = 0.4285714286 of the hotkey emission.
714744
let expected_stake2 = I96F32::from_num(stake_before)
715-
+ I96F32::from_num(pending_alpha) * I96F32::from_num(0.4285714286);
745+
+ I96F32::from_num(pending_alpha)
746+
* I96F32::from_num(0.4285714286)
747+
* I96F32::from_num(2.0 / 3.0);
716748
close(expected_stake2.to_num::<u64>(), stake_after2, 10);
717749
// hotkey 1 has 2 / 3 root tao
718750
let expected_root1 = I96F32::from_num(2 * stake_before)
@@ -970,11 +1002,11 @@ fn test_get_root_children_drain() {
9701002
// Alice and Bob both made half of the dividends.
9711003
assert_eq!(
9721004
SubtensorModule::get_stake_for_hotkey_on_subnet(&alice, alpha),
973-
alice_alpha_stake + pending_alpha / 2
1005+
alice_alpha_stake + pending_alpha / 4
9741006
);
9751007
assert_eq!(
9761008
SubtensorModule::get_stake_for_hotkey_on_subnet(&bob, alpha),
977-
bob_alpha_stake + pending_alpha / 2
1009+
bob_alpha_stake + pending_alpha / 4
9781010
);
9791011

9801012
// Lets drain
@@ -1006,7 +1038,7 @@ fn test_get_root_children_drain() {
10061038
// Bob makes it all.
10071039
assert_eq!(
10081040
AlphaDividendsPerSubnet::<Test>::get(alpha, bob),
1009-
pending_alpha
1041+
(I96F32::from_num(pending_alpha) * I96F32::from_num(1.0 - 0.495412844)).to_num::<u64>()
10101042
);
10111043
assert_eq!(TaoDividendsPerSubnet::<Test>::get(alpha, bob), pending_root);
10121044
});
@@ -1083,12 +1115,12 @@ fn test_get_root_children_drain_half_proportion() {
10831115
// Alice and Bob make the same amount.
10841116
close(
10851117
AlphaDividendsPerSubnet::<Test>::get(alpha, alice),
1086-
pending_alpha / 2,
1118+
pending_alpha / 4,
10871119
10,
10881120
);
10891121
close(
10901122
AlphaDividendsPerSubnet::<Test>::get(alpha, bob),
1091-
pending_alpha / 2,
1123+
pending_alpha / 4,
10921124
10,
10931125
);
10941126
});
@@ -1165,7 +1197,7 @@ fn test_get_root_children_drain_with_take() {
11651197
close(AlphaDividendsPerSubnet::<Test>::get(alpha, alice), 0, 10);
11661198
close(
11671199
AlphaDividendsPerSubnet::<Test>::get(alpha, bob),
1168-
pending_alpha,
1200+
pending_alpha / 2,
11691201
10,
11701202
);
11711203
});
@@ -1241,12 +1273,12 @@ fn test_get_root_children_drain_with_half_take() {
12411273
// Alice and Bob make the same amount.
12421274
close(
12431275
AlphaDividendsPerSubnet::<Test>::get(alpha, alice),
1244-
pending_alpha / 4,
1276+
pending_alpha / 8,
12451277
10000,
12461278
);
12471279
close(
12481280
AlphaDividendsPerSubnet::<Test>::get(alpha, bob),
1249-
3 * (pending_alpha / 4),
1281+
3 * (pending_alpha / 8),
12501282
10000,
12511283
);
12521284
});

0 commit comments

Comments
 (0)