Skip to content

Commit 22fa65e

Browse files
committed
extract function and add test
1 parent 03d4bc6 commit 22fa65e

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ impl<T: Config> Pallet<T> {
266266
pending_swapped,
267267
owner_cut
268268
);
269-
// Setup.
270-
let zero: I96F32 = asfloat!(0.0);
271269

272270
// Run the epoch.
273271
let hotkey_emission: Vec<(T::AccountId, u64, u64)> =
@@ -297,6 +295,25 @@ impl<T: Config> Pallet<T> {
297295
log::debug!("incentives: {:?}", incentives);
298296
log::debug!("dividends: {:?}", dividends);
299297

298+
Self::distribute_dividends_and_incentives(
299+
netuid,
300+
pending_tao,
301+
owner_cut,
302+
incentives,
303+
dividends,
304+
);
305+
}
306+
307+
pub fn distribute_dividends_and_incentives(
308+
netuid: u16,
309+
pending_tao: u64,
310+
owner_cut: u64,
311+
incentives: BTreeMap<T::AccountId, u64>,
312+
dividends: BTreeMap<T::AccountId, I96F32>,
313+
) {
314+
// Setup.
315+
let zero: I96F32 = asfloat!(0.0);
316+
300317
// Accumulate root divs and alpha_divs. For each hotkey we compute their
301318
// local and root dividend proportion based on their alpha_stake/root_stake
302319
let mut total_root_divs: I96F32 = asfloat!(0);

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use super::mock::*;
33

44
use crate::*;
5+
use alloc::collections::BTreeMap;
56
use approx::assert_abs_diff_eq;
67
use frame_support::assert_ok;
78
use sp_core::U256;
@@ -1439,3 +1440,51 @@ fn test_get_root_children_drain_with_half_take() {
14391440
// );
14401441
// });
14411442
// }
1443+
1444+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --lib -- tests::coinbase::test_incentive_to_subnet_owner_is_burned --exact --show-output --nocapture
1445+
#[test]
1446+
fn test_incentive_to_subnet_owner_is_burned() {
1447+
new_test_ext(1).execute_with(|| {
1448+
let subnet_owner_ck = U256::from(0);
1449+
let subnet_owner_hk = U256::from(1);
1450+
1451+
let other_ck = U256::from(2);
1452+
let other_hk = U256::from(3);
1453+
1454+
let netuid = add_dynamic_network(&subnet_owner_hk, &subnet_owner_ck);
1455+
1456+
let pending_tao: u64 = 1_000_000_000;
1457+
let owner_cut: u64 = 0;
1458+
let mut incentives: BTreeMap<U256, u64> = BTreeMap::new();
1459+
let mut dividends: BTreeMap<U256, I96F32> = BTreeMap::new();
1460+
1461+
// Give incentive to other_hk
1462+
incentives.insert(other_hk, 10_000_000);
1463+
1464+
// Give incentives to subnet_owner_hk
1465+
incentives.insert(subnet_owner_hk, 10_000_000);
1466+
1467+
// Verify stake before
1468+
let subnet_owner_stake_before =
1469+
SubtensorModule::get_stake_for_hotkey_on_subnet(&subnet_owner_hk, netuid);
1470+
assert_eq!(subnet_owner_stake_before, 0);
1471+
let other_stake_before = SubtensorModule::get_stake_for_hotkey_on_subnet(&other_hk, netuid);
1472+
assert_eq!(other_stake_before, 0);
1473+
1474+
// Distribute dividends and incentives
1475+
SubtensorModule::distribute_dividends_and_incentives(
1476+
netuid,
1477+
pending_tao,
1478+
owner_cut,
1479+
incentives,
1480+
dividends,
1481+
);
1482+
1483+
// Verify stake after
1484+
let subnet_owner_stake_after =
1485+
SubtensorModule::get_stake_for_hotkey_on_subnet(&subnet_owner_hk, netuid);
1486+
assert_eq!(subnet_owner_stake_after, 0);
1487+
let other_stake_after = SubtensorModule::get_stake_for_hotkey_on_subnet(&other_hk, netuid);
1488+
assert!(other_stake_after > 0);
1489+
});
1490+
}

0 commit comments

Comments
 (0)