Skip to content

Commit 6722dca

Browse files
committed
add unit test
1 parent 3fd2f16 commit 6722dca

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,9 @@ fn test_drain_alpha_childkey_parentkey() {
10801080
// Childkey take is 10%
10811081
ChildkeyTake::<Test>::insert(child, netuid, u16::MAX / 10);
10821082

1083+
let parent_stake_before = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
1084+
let child_stake_before = SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid);
1085+
10831086
let pending_alpha = AlphaCurrency::from(1_000_000_000);
10841087
SubtensorModule::drain_pending_emission(
10851088
netuid,
@@ -1099,6 +1102,7 @@ fn test_drain_alpha_childkey_parentkey() {
10991102
expected.to_num::<u64>(),
11001103
parent_stake_after
11011104
);
1105+
11021106
close(expected.to_num::<u64>(), parent_stake_after.into(), 10_000);
11031107
let expected = I96F32::from_num(u64::from(pending_alpha)) / I96F32::from_num(10);
11041108
close(expected.to_num::<u64>(), child_stake_after.into(), 10_000);
@@ -2753,3 +2757,72 @@ fn test_coinbase_v3_liquidity_update() {
27532757
assert!(liquidity_before < liquidity_after);
27542758
});
27552759
}
2760+
2761+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_alpha_childkey_parentkey_with_burn --exact --show-output --nocapture
2762+
#[test]
2763+
fn test_drain_alpha_childkey_parentkey_with_burn() {
2764+
new_test_ext(1).execute_with(|| {
2765+
let netuid = NetUid::from(1);
2766+
add_network(netuid, 1, 0);
2767+
let parent = U256::from(1);
2768+
let child = U256::from(2);
2769+
let coldkey = U256::from(3);
2770+
let stake_before = AlphaCurrency::from(1_000_000_000);
2771+
register_ok_neuron(netuid, child, coldkey, 0);
2772+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
2773+
&parent,
2774+
&coldkey,
2775+
netuid,
2776+
stake_before,
2777+
);
2778+
mock_set_children_no_epochs(netuid, &parent, &[(u64::MAX, child)]);
2779+
2780+
// Childkey take is 10%
2781+
ChildkeyTake::<Test>::insert(child, netuid, u16::MAX / 10);
2782+
2783+
let burn_rate = SubtensorModule::get_ck_burn();
2784+
let parent_stake_before = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
2785+
let child_stake_before = SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid);
2786+
2787+
let pending_alpha = AlphaCurrency::from(1_000_000_000);
2788+
SubtensorModule::drain_pending_emission(
2789+
netuid,
2790+
pending_alpha,
2791+
TaoCurrency::ZERO,
2792+
AlphaCurrency::ZERO,
2793+
AlphaCurrency::ZERO,
2794+
);
2795+
let parent_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
2796+
let child_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid);
2797+
2798+
let expected_ck_burn = I96F32::from_num(pending_alpha)
2799+
* I96F32::from_num(9.0 / 10.0)
2800+
* I96F32::from_num(burn_rate);
2801+
2802+
let expected_total = I96F32::from_num(pending_alpha) - expected_ck_burn;
2803+
let parent_ratio = (I96F32::from_num(pending_alpha) * I96F32::from_num(9.0 / 10.0)
2804+
- expected_ck_burn)
2805+
/ expected_total;
2806+
let child_ratio = (I96F32::from_num(pending_alpha) / I96F32::from_num(10)) / expected_total;
2807+
2808+
let expected =
2809+
I96F32::from_num(stake_before) + I96F32::from_num(pending_alpha) * parent_ratio;
2810+
log::info!(
2811+
"expected: {:?}, parent_stake_after: {:?}",
2812+
expected.to_num::<u64>(),
2813+
parent_stake_after
2814+
);
2815+
2816+
close(
2817+
expected.to_num::<u64>(),
2818+
parent_stake_after.into(),
2819+
3_000_000,
2820+
);
2821+
let expected = I96F32::from_num(u64::from(pending_alpha)) * child_ratio;
2822+
close(
2823+
expected.to_num::<u64>(),
2824+
child_stake_after.into(),
2825+
3_000_000,
2826+
);
2827+
});
2828+
}

precompiles/src/solidity/subnet.abi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,5 +1046,8 @@
10461046
"outputs": [],
10471047
"stateMutability": "payable",
10481048
"type": "function"
1049+
},
1050+
{
1051+
"inputs"
10491052
}
10501053
]

0 commit comments

Comments
 (0)