Skip to content

Commit 102e6cc

Browse files
author
unconst
committed
alpha divs
1 parent 6872057 commit 102e6cc

File tree

2 files changed

+192
-7
lines changed

2 files changed

+192
-7
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl<T: Config> Pallet<T> {
272272
// Run the epoch.
273273
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
274274
Self::epoch(netuid, pending_alpha.saturating_add(pending_swapped));
275+
log::debug!("hotkey_emission: {:?}", hotkey_emission);
275276

276277
// Accumulate emission of dividends and incentive per hotkey.
277278
let mut incentives: BTreeMap<T::AccountId, u64> = BTreeMap::new();
@@ -293,8 +294,10 @@ impl<T: Config> Pallet<T> {
293294
.or_insert(asfloat!(parent_div));
294295
}
295296
}
297+
log::debug!("incentives: {:?}", incentives);
298+
log::debug!("dividends: {:?}", dividends);
296299

297-
// Accumulate root divs and alpha_divs. For each hotkye we compute their
300+
// Accumulate root divs and alpha_divs. For each hotkey we compute their
298301
// local and root dividend proportion based on their alpha_stake/root_stake
299302
let mut total_root_divs: I96F32 = asfloat!(0);
300303
let mut root_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
@@ -311,19 +314,15 @@ impl<T: Config> Pallet<T> {
311314
let root_alpha: I96F32 = root_stake.saturating_mul(Self::get_tao_weight());
312315
// Get total from root and local
313316
let total_alpha: I96F32 = alpha_stake.saturating_add(root_alpha);
314-
// Compute alpha prop.
315-
let alpha_prop: I96F32 = alpha_stake.checked_div(total_alpha).unwrap_or(zero);
316317
// Copmute root prop.
317318
let root_prop: I96F32 = root_alpha.checked_div(total_alpha).unwrap_or(zero);
318-
// Compute alpha dividends
319-
let alpha_divs: I96F32 = dividend.saturating_mul(alpha_prop);
320319
// Compute root dividends
321320
let root_divs: I96F32 = dividend.saturating_mul(root_prop);
322321
// Record the root dividends.
323322
alpha_dividends
324323
.entry(hotkey.clone())
325-
.and_modify(|e| *e = e.saturating_add(alpha_divs))
326-
.or_insert(alpha_divs);
324+
.and_modify(|e| *e = e.saturating_add(dividend))
325+
.or_insert(dividend);
327326
// Record the alpha_dividends.
328327
root_dividends
329328
.entry(hotkey.clone())
@@ -332,6 +331,8 @@ impl<T: Config> Pallet<T> {
332331
// Accumulate total root divs.
333332
total_root_divs = total_root_divs.saturating_add(root_divs);
334333
}
334+
log::debug!("alpha_dividends: {:?}", alpha_dividends);
335+
log::debug!("root_dividends: {:?}", root_dividends);
335336

336337
// Compute root divs as TAO. Here we take
337338
let mut tao_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
@@ -346,6 +347,7 @@ impl<T: Config> Pallet<T> {
346347
.and_modify(|e| *e = root_tao)
347348
.or_insert(root_tao);
348349
}
350+
log::debug!("tao_dividends: {:?}", tao_dividends);
349351

350352
// Distribute the owner cut.
351353
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
@@ -374,6 +376,8 @@ impl<T: Config> Pallet<T> {
374376
// Distribute alpha divs.
375377
let _ = AlphaDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
376378
for (hotkey, mut alpha_divs) in alpha_dividends {
379+
log::debug!("hotkey: {:?} alpha_divs: {:?}", hotkey, alpha_divs);
380+
377381
// Get take prop
378382
let alpha_take: I96F32 =
379383
Self::get_hotkey_take_float(&hotkey).saturating_mul(alpha_divs);
@@ -397,6 +401,7 @@ impl<T: Config> Pallet<T> {
397401
// Distribute root tao divs.
398402
let _ = TaoDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
399403
for (hotkey, mut root_tao) in tao_dividends {
404+
log::debug!("hotkey: {:?} root_tao: {:?}", hotkey, root_tao);
400405
// Get take prop
401406
let tao_take: I96F32 = Self::get_hotkey_take_float(&hotkey).saturating_mul(root_tao);
402407
// Remove take prop from root_tao

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,184 @@ fn test_pending_swapped() {
397397
assert_eq!( PendingEmission::<Test>::get(netuid), 1_000_000_000 - 125000000 ); // 1 - swapped.
398398
assert_eq!( PendingRootDivs::<Test>::get(netuid), 125000000 ); // swapped * (price = 1)
399399
});
400+
}
401+
402+
403+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base --exact --show-output --nocapture
404+
#[test]
405+
fn test_drain_base() {
406+
new_test_ext(1).execute_with(|| {
407+
SubtensorModule::drain_pending_emission(
408+
0,
409+
0,
410+
0,
411+
0,
412+
0,
413+
)
414+
});
415+
}
416+
417+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet --exact --show-output --nocapture
418+
#[test]
419+
fn test_drain_base_with_subnet() {
420+
new_test_ext(1).execute_with(|| {
421+
let netuid: u16 = 1;
422+
add_network(netuid, 1, 0);
423+
SubtensorModule::drain_pending_emission(
424+
netuid,
425+
0,
426+
0,
427+
0,
428+
0,
429+
)
430+
});
431+
}
432+
433+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_not_registered --exact --show-output --nocapture
434+
#[test]
435+
fn test_drain_base_with_subnet_with_single_staker_not_registered() {
436+
new_test_ext(1).execute_with(|| {
437+
let netuid: u16 = 1;
438+
add_network(netuid, 1, 0);
439+
let hotkey = U256::from(1);
440+
let coldkey = U256::from(2);
441+
let stake_before: u64 = 1_000_000_000;
442+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
443+
&hotkey,
444+
&coldkey,
445+
netuid,
446+
stake_before,
447+
);
448+
let pending_alpha: u64 = 1_000_000_000;
449+
SubtensorModule::drain_pending_emission(
450+
netuid,
451+
pending_alpha,
452+
0,
453+
0,
454+
0,
455+
);
456+
let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
457+
&hotkey, &coldkey, netuid,
458+
);
459+
assert_eq!( stake_before, stake_after ); // Not registered.
460+
});
461+
}
462+
463+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_registered --exact --show-output --nocapture
464+
#[test]
465+
fn test_drain_base_with_subnet_with_single_staker_registered() {
466+
new_test_ext(1).execute_with(|| {
467+
let netuid: u16 = 1;
468+
add_network(netuid, 1, 0);
469+
let hotkey = U256::from(1);
470+
let coldkey = U256::from(2);
471+
let stake_before: u64 = 1_000_000_000;
472+
register_ok_neuron(netuid, hotkey, coldkey, 0);
473+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
474+
&hotkey,
475+
&coldkey,
476+
netuid,
477+
stake_before,
478+
);
479+
let pending_alpha: u64 = 1_000_000_000;
480+
SubtensorModule::drain_pending_emission(
481+
netuid,
482+
pending_alpha,
483+
0,
484+
0,
485+
0,
486+
);
487+
let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
488+
&hotkey, &coldkey, netuid,
489+
);
490+
close( stake_before + pending_alpha, stake_after, 10 ); // Registered gets all emission.
491+
});
492+
}
493+
494+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_single_staker_registered_root_weight --exact --show-output --nocapture
495+
#[test]
496+
fn test_drain_base_with_subnet_with_single_staker_registered_root_weight() {
497+
new_test_ext(1).execute_with(|| {
498+
let root: u16 = 0;
499+
let netuid: u16 = 1;
500+
add_network(netuid, 1, 0);
501+
let hotkey = U256::from(1);
502+
let coldkey = U256::from(2);
503+
let stake_before: u64 = 1_000_000_000;
504+
// register_ok_neuron(root, hotkey, coldkey, 0);
505+
register_ok_neuron(netuid, hotkey, coldkey, 0);
506+
SubtensorModule::set_tao_weight( u64::MAX ); // Set TAO weight to 1.0
507+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
508+
&hotkey,
509+
&coldkey,
510+
root,
511+
stake_before,
512+
);
513+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
514+
&hotkey,
515+
&coldkey,
516+
netuid,
517+
stake_before,
518+
);
519+
let pending_tao: u64 = 1_000_000_000;
520+
let pending_alpha: u64 = 1_000_000_000;
521+
SubtensorModule::drain_pending_emission(
522+
netuid,
523+
pending_alpha,
524+
pending_tao,
525+
0,
526+
0,
527+
);
528+
let stake_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
529+
&hotkey, &coldkey, netuid,
530+
);
531+
let root_after = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
532+
&hotkey, &coldkey, root,
533+
);
534+
close( stake_before + pending_alpha, stake_after, 10 ); // Registered gets all alpha emission.
535+
close( stake_before + pending_tao, root_after, 10 ); // Registered gets all tao emission
536+
});
537+
}
538+
539+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_drain_base_with_subnet_with_two_stakers_registered --exact --show-output --nocapture
540+
#[test]
541+
fn test_drain_base_with_subnet_with_two_stakers_registered() {
542+
new_test_ext(1).execute_with(|| {
543+
let netuid: u16 = 1;
544+
add_network(netuid, 1, 0);
545+
let hotkey1 = U256::from(1);
546+
let hotkey2 = U256::from(2);
547+
let coldkey = U256::from(3);
548+
let stake_before: u64 = 1_000_000_000;
549+
register_ok_neuron(netuid, hotkey1, coldkey, 0);
550+
register_ok_neuron(netuid, hotkey2, coldkey, 0);
551+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
552+
&hotkey1,
553+
&coldkey,
554+
netuid,
555+
stake_before,
556+
);
557+
SubtensorModule::increase_stake_for_hotkey_and_coldkey_on_subnet(
558+
&hotkey2,
559+
&coldkey,
560+
netuid,
561+
stake_before,
562+
);
563+
let pending_alpha: u64 = 1_000_000_000;
564+
SubtensorModule::drain_pending_emission(
565+
netuid,
566+
pending_alpha,
567+
0,
568+
0,
569+
0,
570+
);
571+
let stake_after1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
572+
&hotkey1, &coldkey, netuid,
573+
);
574+
let stake_after2 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
575+
&hotkey2, &coldkey, netuid,
576+
);
577+
close( stake_before + pending_alpha/2, stake_after1, 10 ); // Registered gets 1/2 emission
578+
close( stake_before + pending_alpha/2, stake_after2, 10 ); // Registered gets 1/2 emission.
579+
});
400580
}

0 commit comments

Comments
 (0)