Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove root divs before alpha payout #1182

Open
wants to merge 3 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 37 additions & 26 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,29 +356,6 @@ impl<T: Config> Pallet<T> {

// Calculate the validator take and root alpha divs using the alpha divs.
for (hotkey, dividend_tuples) in dividends_to_distribute.iter() {
// Get the local alpha and root alpha.
let hotkey_tao: I96F32 = I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(
hotkey,
Self::get_root_netuid(),
));
let hotkey_tao_as_alpha: I96F32 = hotkey_tao.saturating_mul(Self::get_tao_weight());
let hotkey_alpha =
I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(hotkey, netuid));
log::debug!("Hotkey tao for hotkey {:?} on root netuid: {:?}, hotkey tao as alpha: {:?}, hotkey alpha: {:?}", hotkey, hotkey_tao, hotkey_tao_as_alpha, hotkey_alpha);

// Compute alpha and root proportions.
let alpha_prop: I96F32 = hotkey_alpha
.checked_div(hotkey_alpha.saturating_add(hotkey_tao_as_alpha))
.unwrap_or(I96F32::from_num(0.0));
let root_prop: I96F32 = hotkey_tao_as_alpha
.checked_div(hotkey_alpha.saturating_add(hotkey_tao_as_alpha))
.unwrap_or(I96F32::from_num(0.0));
log::debug!(
"Alpha proportion: {:?}, root proportion: {:?}",
alpha_prop,
root_prop
);

// Calculate the dividends to hotkeys based on the local vs root proportion.
for (hotkey_j, divs_j) in dividend_tuples.iter() {
log::debug!(
Expand All @@ -388,6 +365,29 @@ impl<T: Config> Pallet<T> {
*divs_j
);

// Get the local alpha and root alpha.
let hotkey_tao: I96F32 = I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(
hotkey_j,
Self::get_root_netuid(),
));
let hotkey_tao_as_alpha: I96F32 = hotkey_tao.saturating_mul(Self::get_tao_weight());
let hotkey_alpha =
I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(hotkey_j, netuid));
log::debug!("Hotkey tao for hotkey {:?} on root netuid: {:?}, hotkey tao as alpha: {:?}, hotkey alpha: {:?}", hotkey_j, hotkey_tao, hotkey_tao_as_alpha, hotkey_alpha);

// Compute alpha and root proportions.
let alpha_prop: I96F32 = hotkey_alpha
.checked_div(hotkey_alpha.saturating_add(hotkey_tao_as_alpha))
.unwrap_or(I96F32::from_num(0.0));
let root_prop: I96F32 = hotkey_tao_as_alpha
.checked_div(hotkey_alpha.saturating_add(hotkey_tao_as_alpha))
.unwrap_or(I96F32::from_num(0.0));
log::debug!(
"Alpha proportion: {:?}, root proportion: {:?}",
alpha_prop,
root_prop
);

// Remove the hotkey take straight off the top.
let take_prop: I96F32 = I96F32::from_num(Self::get_hotkey_take(hotkey_j))
.checked_div(I96F32::from_num(u16::MAX))
Expand Down Expand Up @@ -472,6 +472,15 @@ impl<T: Config> Pallet<T> {
rem_divs_j
);

let possible_root_divs = *root_alpha_divs.get(hotkey_j).unwrap_or(&0);
let alpha_divs: I96F32 =
rem_divs_j.saturating_sub(I96F32::from_num(possible_root_divs));
log::debug!(
"Removed root divs for hotkey {:?}: {:?}",
hotkey_j,
possible_root_divs
);

// Distribute validator take.
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
hotkey_j,
Expand All @@ -490,18 +499,20 @@ impl<T: Config> Pallet<T> {
Self::increase_stake_for_hotkey_on_subnet(
hotkey_j,
netuid,
rem_divs_j.to_num::<u64>(),
alpha_divs.to_num::<u64>(),
);
log::debug!(
"Distributed alpha dividends for hotkey {:?} on netuid {:?}: {:?}",
hotkey_j,
netuid,
rem_divs_j.to_num::<u64>()
alpha_divs.to_num::<u64>()
);

// Record dividends for this hotkey on this subnet.
AlphaDividendsPerSubnet::<T>::mutate(netuid, hotkey_j.clone(), |divs| {
*divs = divs.saturating_add(*divs_j);
*divs = divs
.saturating_add(alpha_divs.to_num::<u64>())
.saturating_add(validator_take.to_num::<u64>());
});
log::debug!(
"Recorded dividends for hotkey {:?} on netuid {:?}: {:?}",
Expand Down
5 changes: 3 additions & 2 deletions pallets/subtensor/src/staking/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ impl<T: Config> Pallet<T> {
);

// 6. Unstake from the origin subnet, returning TAO (or a 1:1 equivalent).
let fee = DefaultMinStake::<T>::get().saturating_div(2);
let tao_unstaked =
Self::unstake_from_subnet(&hotkey, &coldkey, origin_netuid, alpha_amount);
Self::unstake_from_subnet(&hotkey, &coldkey, origin_netuid, alpha_amount, fee);

// 7. Check that the unstaked amount is above the minimum stake threshold.
ensure!(
Expand All @@ -298,7 +299,7 @@ impl<T: Config> Pallet<T> {
);

// 8. Stake the unstaked amount into the destination subnet, using the same coldkey/hotkey.
Self::stake_into_subnet(&hotkey, &coldkey, destination_netuid, tao_unstaked);
Self::stake_into_subnet(&hotkey, &coldkey, destination_netuid, tao_unstaked, fee);

// 9. Emit an event for logging.
log::info!(
Expand Down
16 changes: 8 additions & 8 deletions pallets/subtensor/src/tests/move_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ fn test_do_swap_success() {
let stake_amount = DefaultMinStake::<Test>::get() * 10;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, stake_amount);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, stake_amount, 0);
let alpha_before = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
&coldkey,
Expand Down Expand Up @@ -1180,7 +1180,7 @@ fn test_do_swap_insufficient_stake() {
let attempted_swap = stake_amount * 2;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid1, stake_amount);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid1, stake_amount, 0);

assert_noop!(
SubtensorModule::do_swap_stake(
Expand Down Expand Up @@ -1209,7 +1209,7 @@ fn test_do_swap_wrong_origin() {
let stake_amount = 100_000;

SubtensorModule::create_account_if_non_existent(&real_coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &real_coldkey, netuid1, stake_amount);
SubtensorModule::stake_into_subnet(&hotkey, &real_coldkey, netuid1, stake_amount, 0);

assert_noop!(
SubtensorModule::do_swap_stake(
Expand Down Expand Up @@ -1237,7 +1237,7 @@ fn test_do_swap_minimum_stake_check() {
let swap_amount = 1;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid, total_stake);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid, total_stake, 0);

assert_err!(
SubtensorModule::do_swap_stake(
Expand All @@ -1264,7 +1264,7 @@ fn test_do_swap_same_subnet() {
let stake_amount = DefaultMinStake::<Test>::get() * 10;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid, stake_amount);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid, stake_amount, 0);

let alpha_before =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&hotkey, &coldkey, netuid);
Expand Down Expand Up @@ -1296,7 +1296,7 @@ fn test_do_swap_partial_stake() {
let total_stake = DefaultMinStake::<Test>::get() * 10;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, total_stake);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, total_stake, 0);

let swap_amount = total_stake / 2;
assert_ok!(SubtensorModule::do_swap_stake(
Expand Down Expand Up @@ -1341,7 +1341,7 @@ fn test_do_swap_storage_updates() {
let stake_amount = DefaultMinStake::<Test>::get() * 10;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, stake_amount);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, origin_netuid, stake_amount, 0);

let alpha = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&hotkey,
Expand Down Expand Up @@ -1390,7 +1390,7 @@ fn test_do_swap_multiple_times() {
let initial_stake = DefaultMinStake::<Test>::get() * 10;

SubtensorModule::create_account_if_non_existent(&coldkey, &hotkey);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid1, initial_stake);
SubtensorModule::stake_into_subnet(&hotkey, &coldkey, netuid1, initial_stake, 0);

for _ in 0..3 {
let alpha1 = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
Expand Down
Loading