Skip to content

Commit f0ee331

Browse files
author
unconst
committed
cargo fmt
1 parent de64b11 commit f0ee331

File tree

3 files changed

+265
-165
lines changed

3 files changed

+265
-165
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,12 @@ impl<T: Config> Pallet<T> {
361361
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
362362
if let Ok(owner_hotkey) = SubnetOwnerHotkey::<T>::try_get(netuid) {
363363
// Increase stake for owner hotkey and coldkey.
364-
log::debug!("owner_hotkey: {:?} owner_coldkey: {:?}, owner_cut: {:?}", owner_hotkey, owner_coldkey, owner_cut);
364+
log::debug!(
365+
"owner_hotkey: {:?} owner_coldkey: {:?}, owner_cut: {:?}",
366+
owner_hotkey,
367+
owner_coldkey,
368+
owner_cut
369+
);
365370
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
366371
&owner_hotkey,
367372
&owner_coldkey,

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ impl<T: Config> Pallet<T> {
131131
log::debug!("alpha_stake: {:?}", alpha_stake);
132132

133133
// Step 2: Get the global tao stake for the hotkey
134-
let tao_stake =
135-
I64F64::saturating_from_num(Self::get_tao_inherited_for_hotkey_on_subnet(hotkey, netuid));
134+
let tao_stake = I64F64::saturating_from_num(Self::get_tao_inherited_for_hotkey_on_subnet(
135+
hotkey, netuid,
136+
));
136137
log::debug!("tao_stake: {:?}", tao_stake);
137138

138139
// Step 3: Combine alpha and tao stakes
@@ -223,99 +224,97 @@ impl<T: Config> Pallet<T> {
223224
/// # Note
224225
/// This function uses saturating arithmetic to prevent overflows.
225226
pub fn get_tao_inherited_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
227+
let initial_tao: I96F32 = I96F32::saturating_from_num(
228+
Self::get_stake_for_hotkey_on_subnet(hotkey, Self::get_root_netuid()),
229+
);
230+
231+
// Initialize variables to track alpha allocated to children and inherited from parents.
232+
let mut tao_to_children: I96F32 = I96F32::saturating_from_num(0);
233+
let mut tao_from_parents: I96F32 = I96F32::saturating_from_num(0);
234+
235+
// Step 2: Retrieve the lists of parents and children for the hotkey on the subnet.
236+
let parents: Vec<(u64, T::AccountId)> = Self::get_parents(hotkey, netuid);
237+
let children: Vec<(u64, T::AccountId)> = Self::get_children(hotkey, netuid);
238+
log::trace!(
239+
"Parents for hotkey {:?} on subnet {}: {:?}",
240+
hotkey,
241+
netuid,
242+
parents
243+
);
244+
log::trace!(
245+
"Children for hotkey {:?} on subnet {}: {:?}",
246+
hotkey,
247+
netuid,
248+
children
249+
);
250+
251+
// Step 3: Calculate the total tao allocated to children.
252+
for (proportion, _) in children {
253+
// Convert the proportion to a normalized value between 0 and 1.
254+
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
255+
.safe_div(I96F32::saturating_from_num(u64::MAX));
256+
log::trace!(
257+
"Normalized proportion for child: {:?}",
258+
normalized_proportion
259+
);
226260

227-
let initial_tao: I96F32 =
228-
I96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(hotkey, Self::get_root_netuid()));
229-
230-
// Initialize variables to track alpha allocated to children and inherited from parents.
231-
let mut tao_to_children: I96F32 = I96F32::saturating_from_num(0);
232-
let mut tao_from_parents: I96F32 = I96F32::saturating_from_num(0);
233-
234-
// Step 2: Retrieve the lists of parents and children for the hotkey on the subnet.
235-
let parents: Vec<(u64, T::AccountId)> = Self::get_parents(hotkey, netuid);
236-
let children: Vec<(u64, T::AccountId)> = Self::get_children(hotkey, netuid);
237-
log::trace!(
238-
"Parents for hotkey {:?} on subnet {}: {:?}",
239-
hotkey,
240-
netuid,
241-
parents
242-
);
243-
log::trace!(
244-
"Children for hotkey {:?} on subnet {}: {:?}",
245-
hotkey,
246-
netuid,
247-
children
248-
);
249-
250-
// Step 3: Calculate the total tao allocated to children.
251-
for (proportion, _) in children {
252-
// Convert the proportion to a normalized value between 0 and 1.
253-
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
254-
.safe_div(I96F32::saturating_from_num(u64::MAX));
255-
log::trace!(
256-
"Normalized proportion for child: {:?}",
257-
normalized_proportion
258-
);
259-
260-
// Calculate the amount of tao to be allocated to this child.
261-
let tao_proportion_to_child: I96F32 =
262-
I96F32::saturating_from_num(initial_tao).saturating_mul(normalized_proportion);
263-
log::trace!("Tao proportion to child: {:?}", tao_proportion_to_child);
264-
265-
// Add this child's allocation to the total tao allocated to children.
266-
tao_to_children = tao_to_children.saturating_add(tao_proportion_to_child);
267-
}
268-
log::trace!("Total tao allocated to children: {:?}", tao_to_children);
269-
270-
// Step 4: Calculate the total tao inherited from parents.
271-
for (proportion, parent) in parents {
272-
// Retrieve the parent's total stake on this subnet.
273-
let parent_tao: I96F32 =
274-
I96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(&parent, Self::get_root_netuid()));
275-
log::trace!(
276-
"Parent tao for parent {:?} on subnet {}: {:?}",
277-
parent,
278-
netuid,
279-
parent_tao
280-
);
281-
282-
// Convert the proportion to a normalized value between 0 and 1.
283-
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
284-
.safe_div(I96F32::saturating_from_num(u64::MAX));
285-
log::trace!(
286-
"Normalized proportion from parent: {:?}",
287-
normalized_proportion
288-
);
289-
290-
// Calculate the amount of tao to be inherited from this parent.
291-
let tao_proportion_from_parent: I96F32 =
292-
I96F32::saturating_from_num(parent_tao).saturating_mul(normalized_proportion);
293-
log::trace!(
294-
"Tao proportion from parent: {:?}",
261+
// Calculate the amount of tao to be allocated to this child.
262+
let tao_proportion_to_child: I96F32 =
263+
I96F32::saturating_from_num(initial_tao).saturating_mul(normalized_proportion);
264+
log::trace!("Tao proportion to child: {:?}", tao_proportion_to_child);
265+
266+
// Add this child's allocation to the total tao allocated to children.
267+
tao_to_children = tao_to_children.saturating_add(tao_proportion_to_child);
268+
}
269+
log::trace!("Total tao allocated to children: {:?}", tao_to_children);
270+
271+
// Step 4: Calculate the total tao inherited from parents.
272+
for (proportion, parent) in parents {
273+
// Retrieve the parent's total stake on this subnet.
274+
let parent_tao: I96F32 = I96F32::saturating_from_num(
275+
Self::get_stake_for_hotkey_on_subnet(&parent, Self::get_root_netuid()),
276+
);
277+
log::trace!(
278+
"Parent tao for parent {:?} on subnet {}: {:?}",
279+
parent,
280+
netuid,
281+
parent_tao
282+
);
283+
284+
// Convert the proportion to a normalized value between 0 and 1.
285+
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
286+
.safe_div(I96F32::saturating_from_num(u64::MAX));
287+
log::trace!(
288+
"Normalized proportion from parent: {:?}",
289+
normalized_proportion
290+
);
291+
292+
// Calculate the amount of tao to be inherited from this parent.
293+
let tao_proportion_from_parent: I96F32 =
294+
I96F32::saturating_from_num(parent_tao).saturating_mul(normalized_proportion);
295+
log::trace!(
296+
"Tao proportion from parent: {:?}",
295297
tao_proportion_from_parent
296-
);
297-
298-
// Add this parent's contribution to the total tao inherited from parents.
299-
tao_from_parents = tao_from_parents.saturating_add(tao_proportion_from_parent);
300-
}
301-
log::trace!(
302-
"Total tao inherited from parents: {:?}",
303-
tao_from_parents
304-
);
305-
306-
// Step 5: Calculate the final inherited tao for the hotkey.
307-
let finalized_tao: I96F32 = initial_tao
308-
.saturating_sub(tao_to_children) // Subtract tao allocated to children
309-
.saturating_add(tao_from_parents); // Add tao inherited from parents
310-
log::trace!(
311-
"Finalized tao for hotkey {:?} on subnet {}: {:?}",
312-
hotkey,
313-
netuid,
314-
finalized_tao
315-
);
316-
317-
// Step 6: Return the final inherited tao value.
318-
finalized_tao.saturating_to_num::<u64>()
298+
);
299+
300+
// Add this parent's contribution to the total tao inherited from parents.
301+
tao_from_parents = tao_from_parents.saturating_add(tao_proportion_from_parent);
302+
}
303+
log::trace!("Total tao inherited from parents: {:?}", tao_from_parents);
304+
305+
// Step 5: Calculate the final inherited tao for the hotkey.
306+
let finalized_tao: I96F32 = initial_tao
307+
.saturating_sub(tao_to_children) // Subtract tao allocated to children
308+
.saturating_add(tao_from_parents); // Add tao inherited from parents
309+
log::trace!(
310+
"Finalized tao for hotkey {:?} on subnet {}: {:?}",
311+
hotkey,
312+
netuid,
313+
finalized_tao
314+
);
315+
316+
// Step 6: Return the final inherited tao value.
317+
finalized_tao.saturating_to_num::<u64>()
319318
}
320319

321320
pub fn get_inherited_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {

0 commit comments

Comments
 (0)