Skip to content

Commit c7d8010

Browse files
author
unconst
committed
add more tests for root pending etc
1 parent bce7cc6 commit c7d8010

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ impl<T: Config> Pallet<T> {
138138
for netuid_i in subnets.iter() {
139139
// Get alpha out.
140140
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0));
141+
log::debug!("alpha_out_i: {:?}", alpha_out_i);
141142
// Calculate the owner cut.
142143
let owner_cut_i: I96F32 = alpha_out_i.saturating_mul(cut_percent);
144+
log::debug!("owner_cut_i: {:?}", owner_cut_i);
143145
// Save owner cut.
144146
*owner_cuts.entry(*netuid_i).or_insert(asfloat!(0)) = owner_cut_i;
145147
// Save new alpha_out.
@@ -155,24 +157,33 @@ impl<T: Config> Pallet<T> {
155157
for netuid_i in subnets.iter() {
156158
// Get remaining alpha out.
157159
let alpha_out_i: I96F32 = *alpha_out.get(netuid_i).unwrap_or(&asfloat!(0.0));
160+
log::debug!("alpha_out_i: {:?}", alpha_out_i);
158161
// Get total TAO on root.
159162
let root_tao: I96F32 = asfloat!(SubnetTAO::<T>::get(0));
163+
log::debug!("root_tao: {:?}", root_tao);
160164
// Get total ALPHA on subnet.
161165
let alpha_issuance: I96F32 = asfloat!(Self::get_alpha_issuance(*netuid_i));
166+
log::debug!("alpha_issuance: {:?}", alpha_issuance);
162167
// Get tao_weight
163168
let tao_weight: I96F32 = root_tao.saturating_mul(Self::get_tao_weight());
169+
log::debug!("tao_weight: {:?}", tao_weight);
164170
// Get root proportional dividends.
165171
let root_proportion: I96F32 = tao_weight
166172
.checked_div(tao_weight.saturating_add(alpha_issuance))
167173
.unwrap_or(asfloat!(0.0));
174+
log::debug!("root_proportion: {:?}", root_proportion);
168175
// Get root proportion of alpha_out dividends.
169176
let root_alpha: I96F32 = root_proportion
170177
.saturating_mul(alpha_out_i) // Total alpha emission per block remaining.
171178
.saturating_mul(asfloat!(0.5)); // 50% to validators.
172179
// Remove root alpha from alpha_out.
180+
log::debug!("root_alpha: {:?}", root_alpha);
181+
// Get pending alpha as original alpha_out - root_alpha.
173182
let pending_alpha: I96F32 = alpha_out_i.saturating_sub(root_alpha);
183+
log::debug!("pending_alpha: {:?}", pending_alpha);
174184
// Sell root emission through the pool.
175185
let root_tao: u64 = Self::swap_alpha_for_tao(*netuid_i, tou64!(root_alpha));
186+
log::debug!("root_tao: {:?}", root_tao);
176187
// Accumulate alpha emission in pending.
177188
PendingAlphaSwapped::<T>::mutate(*netuid_i, |total| {
178189
*total = total.saturating_add(tou64!(root_alpha));

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,41 @@ fn test_coinbase_alpha_issuance_with_cap_trigger_and_block_emission() {
360360
// No emission.
361361
});
362362
}
363+
364+
365+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_owner_cut_base --exact --show-output --nocapture
366+
#[test]
367+
fn test_owner_cut_base() {
368+
new_test_ext(1).execute_with(|| {
369+
let netuid: u16 = 1;
370+
add_network(netuid, 1, 0);
371+
SubtensorModule::set_tempo(netuid, 10000);// Large number (dont drain)
372+
SubtensorModule::set_subnet_owner_cut( 0 );
373+
SubtensorModule::run_coinbase(I96F32::from_num(0));
374+
assert_eq!( PendingOwnerCut::<Test>::get(netuid), 0 ); // No cut
375+
SubtensorModule::set_subnet_owner_cut( u16::MAX );
376+
SubtensorModule::run_coinbase(I96F32::from_num(0));
377+
assert_eq!( PendingOwnerCut::<Test>::get(netuid), 1_000_000_000 ); // Full cut.
378+
});
379+
}
380+
381+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_pending_swapped --exact --show-output --nocapture
382+
#[test]
383+
fn test_pending_swapped() {
384+
new_test_ext(1).execute_with(|| {
385+
let netuid: u16 = 1;
386+
let emission: u64 = 1_000_000;
387+
add_network(netuid, 1, 0);
388+
SubtensorModule::run_coinbase(I96F32::from_num(0));
389+
assert_eq!( PendingAlphaSwapped::<Test>::get(netuid), 0 ); // Zero tao weight and no root.
390+
SubnetTAO::<Test>::insert( 0, 1_000_000_000); // Add root weight.
391+
SubtensorModule::run_coinbase(I96F32::from_num(0));
392+
assert_eq!( PendingAlphaSwapped::<Test>::get(netuid), 0 ); // Zero tao weight with 1 root.
393+
SubtensorModule::set_tempo(netuid, 10000);// Large number (dont drain)
394+
SubtensorModule::set_tao_weight( u64::MAX ); // Set TAO weight to 1.0
395+
SubtensorModule::run_coinbase(I96F32::from_num(0));
396+
assert_eq!( PendingAlphaSwapped::<Test>::get(netuid), 125000000 ); // 1 TAO / ( 1 + 3 ) = 0.25 * 1 / 2 = 125000000
397+
assert_eq!( PendingEmission::<Test>::get(netuid), 1_000_000_000 - 125000000 ); // 1 - swapped.
398+
assert_eq!( PendingRootDivs::<Test>::get(netuid), 125000000 ); // swapped * (price = 1)
399+
});
400+
}

0 commit comments

Comments
 (0)