@@ -128,16 +128,17 @@ impl<T: Config> Pallet<T> {
128
128
// Step 1: Get stake of hotkey (neuron)
129
129
let alpha_stake =
130
130
I64F64 :: saturating_from_num ( Self :: get_inherited_for_hotkey_on_subnet ( hotkey, netuid) ) ;
131
- log:: trace !( "alpha_stake: {:?}" , alpha_stake) ;
131
+ log:: debug !( "alpha_stake: {:?}" , alpha_stake) ;
132
132
133
133
// Step 2: Get the global tao stake for the hotkey
134
- let tao_stake =
135
- I64F64 :: saturating_from_num ( Self :: get_inherited_for_hotkey_on_subnet ( hotkey, 0 ) ) ;
136
- log:: trace!( "tao_stake: {:?}" , tao_stake) ;
134
+ let tao_stake = I64F64 :: saturating_from_num ( Self :: get_tao_inherited_for_hotkey_on_subnet (
135
+ hotkey, netuid,
136
+ ) ) ;
137
+ log:: debug!( "tao_stake: {:?}" , tao_stake) ;
137
138
138
139
// Step 3: Combine alpha and tao stakes
139
140
let total_stake = alpha_stake. saturating_add ( tao_stake. saturating_mul ( tao_weight) ) ;
140
- log:: trace !( "total_stake: {:?}" , total_stake) ;
141
+ log:: debug !( "total_stake: {:?}" , total_stake) ;
141
142
142
143
( total_stake, alpha_stake, tao_stake)
143
144
}
@@ -173,8 +174,8 @@ impl<T: Config> Pallet<T> {
173
174
. map ( |uid| {
174
175
if Keys :: < T > :: contains_key ( netuid, uid) {
175
176
let hotkey: T :: AccountId = Keys :: < T > :: get ( netuid, uid) ;
176
- I64F64 :: saturating_from_num ( Self :: get_inherited_for_hotkey_on_subnet (
177
- & hotkey, 0 ,
177
+ I64F64 :: saturating_from_num ( Self :: get_tao_inherited_for_hotkey_on_subnet (
178
+ & hotkey, netuid ,
178
179
) )
179
180
} else {
180
181
I64F64 :: saturating_from_num ( 0 )
@@ -222,6 +223,100 @@ impl<T: Config> Pallet<T> {
222
223
///
223
224
/// # Note
224
225
/// This function uses saturating arithmetic to prevent overflows.
226
+ 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
+ ) ;
260
+
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: {:?}" ,
297
+ tao_proportion_from_parent
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 > ( )
318
+ }
319
+
225
320
pub fn get_inherited_for_hotkey_on_subnet ( hotkey : & T :: AccountId , netuid : u16 ) -> u64 {
226
321
// Step 1: Retrieve the initial total stake (alpha) for the hotkey on the specified subnet.
227
322
let initial_alpha: I96F32 =
0 commit comments