@@ -286,7 +286,7 @@ impl<T: Config> Pallet<T> {
286286 let owner_uid: Option < u16 > = Self :: get_owner_uid ( netuid) ;
287287
288288 // Access network weights row unnormalized.
289- let mut weights: Vec < Vec < I32F32 > > = Self :: get_weights ( netuid ) ;
289+ let mut weights: Vec < Vec < I32F32 > > = Self :: get_weights ( netuid_index ) ;
290290 log:: trace!( "W: {:?}" , & weights) ;
291291
292292 // Mask weights that are not from permitted validators.
@@ -363,7 +363,7 @@ impl<T: Config> Pallet<T> {
363363 log:: trace!( "B: {:?}" , & bonds) ;
364364
365365 // Compute the Exponential Moving Average (EMA) of bonds.
366- ema_bonds = Self :: compute_bonds ( netuid_index , & weights_for_bonds, & bonds, & consensus) ;
366+ ema_bonds = Self :: compute_bonds ( netuid , & weights_for_bonds, & bonds, & consensus) ;
367367 log:: trace!( "emaB: {:?}" , & ema_bonds) ;
368368
369369 // Normalize EMA bonds.
@@ -397,7 +397,7 @@ impl<T: Config> Pallet<T> {
397397 log:: trace!( "ΔB: {:?}" , & bonds_delta) ;
398398
399399 // Compute the Exponential Moving Average (EMA) of bonds.
400- ema_bonds = Self :: compute_ema_bonds_normal ( & bonds_delta, & bonds, netuid_index ) ;
400+ ema_bonds = Self :: compute_ema_bonds_normal ( & bonds_delta, & bonds, netuid ) ;
401401 inplace_col_normalize ( & mut ema_bonds) ; // sum_i b_ij = 1
402402 log:: trace!( "emaB: {:?}" , & ema_bonds) ;
403403
@@ -706,7 +706,7 @@ impl<T: Config> Pallet<T> {
706706 let owner_uid: Option < u16 > = Self :: get_owner_uid ( netuid) ;
707707
708708 // Access network weights row unnormalized.
709- let mut weights: Vec < Vec < ( u16 , I32F32 ) > > = Self :: get_weights_sparse ( netuid ) ;
709+ let mut weights: Vec < Vec < ( u16 , I32F32 ) > > = Self :: get_weights_sparse ( netuid_index ) ;
710710 log:: trace!( "Weights: {:?}" , & weights) ;
711711
712712 // Mask weights that are not from permitted validators.
@@ -1608,47 +1608,42 @@ impl<T: Config> Pallet<T> {
16081608 }
16091609
16101610 /// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
1611- pub fn get_weights_sparse ( netuid : NetUid ) -> Vec < Vec < ( u16 , I32F32 ) > > {
1611+ pub fn get_weights_sparse ( netuid_index : NetUidStorageIndex ) -> Vec < Vec < ( u16 , I32F32 ) > > {
1612+ let ( netuid, _) = Self :: get_netuid_and_subid ( netuid_index) . unwrap_or_default ( ) ;
16121613 let n = Self :: get_subnetwork_n ( netuid) as usize ;
16131614 let mut weights: Vec < Vec < ( u16 , I32F32 ) > > = vec ! [ vec![ ] ; n] ;
1614- for ( uid_i, weights_i) in <Weights < T > as IterableStorageDoubleMap <
1615- NetUidStorageIndex ,
1616- u16 ,
1617- Vec < ( u16 , u16 ) > ,
1618- > >:: iter_prefix ( NetUidStorageIndex :: from ( netuid) )
1615+ for ( uid_i, weights_i) in Weights :: < T > :: iter_prefix ( netuid_index)
16191616 . filter ( |( uid_i, _) | * uid_i < n as u16 )
16201617 {
16211618 for ( uid_j, weight_ij) in weights_i. iter ( ) . filter ( |( uid_j, _) | * uid_j < n as u16 ) {
1622- weights
1623- . get_mut ( uid_i as usize )
1624- . expect ( "uid_i is filtered to be less than n; qed" )
1625- . push ( ( * uid_j, I32F32 :: saturating_from_num ( * weight_ij) ) ) ;
1619+ if let Some ( row) = weights. get_mut ( uid_i as usize ) {
1620+ row. push ( ( * uid_j, I32F32 :: saturating_from_num ( * weight_ij) ) ) ;
1621+ } else {
1622+ log:: error!( "uid_i {:?} is filtered to be less than n" , uid_i) ;
1623+ }
16261624 }
16271625 }
16281626 weights
16291627 }
16301628
16311629 /// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16.
1632- pub fn get_weights ( netuid : NetUid ) -> Vec < Vec < I32F32 > > {
1630+ pub fn get_weights ( netuid_index : NetUidStorageIndex ) -> Vec < Vec < I32F32 > > {
1631+ let ( netuid, _) = Self :: get_netuid_and_subid ( netuid_index) . unwrap_or_default ( ) ;
16331632 let n = Self :: get_subnetwork_n ( netuid) as usize ;
16341633 let mut weights: Vec < Vec < I32F32 > > = vec ! [ vec![ I32F32 :: saturating_from_num( 0.0 ) ; n] ; n] ;
1635- for ( uid_i, weights_vec) in <Weights < T > as IterableStorageDoubleMap <
1636- NetUidStorageIndex ,
1637- u16 ,
1638- Vec < ( u16 , u16 ) > ,
1639- > >:: iter_prefix ( NetUidStorageIndex :: from ( netuid) )
1634+ for ( uid_i, weights_vec) in Weights :: < T > :: iter_prefix ( netuid_index)
16401635 . filter ( |( uid_i, _) | * uid_i < n as u16 )
16411636 {
16421637 for ( uid_j, weight_ij) in weights_vec
16431638 . into_iter ( )
16441639 . filter ( |( uid_j, _) | * uid_j < n as u16 )
16451640 {
1646- * weights
1641+ if let Some ( cell ) = weights
16471642 . get_mut ( uid_i as usize )
1648- . expect ( "uid_i is filtered to be less than n; qed" )
1649- . get_mut ( uid_j as usize )
1650- . expect ( "uid_j is filtered to be less than n; qed" ) =
1651- I32F32 :: saturating_from_num ( weight_ij ) ;
1643+ . and_then ( |row| row . get_mut ( uid_j as usize ) )
1644+ {
1645+ * cell = I32F32 :: saturating_from_num ( weight_ij ) ;
1646+ }
16521647 }
16531648 }
16541649 weights
@@ -1730,8 +1725,10 @@ impl<T: Config> Pallet<T> {
17301725 pub fn compute_ema_bonds_normal_sparse (
17311726 bonds_delta : & [ Vec < ( u16 , I32F32 ) > ] ,
17321727 bonds : & [ Vec < ( u16 , I32F32 ) > ] ,
1733- netuid : NetUidStorageIndex ,
1728+ netuid_index : NetUidStorageIndex ,
17341729 ) -> Vec < Vec < ( u16 , I32F32 ) > > {
1730+ let ( netuid, _) = Self :: get_netuid_and_subid ( netuid_index) . unwrap_or_default ( ) ;
1731+
17351732 // Retrieve the bonds moving average for the given network ID and scale it down.
17361733 let bonds_moving_average: I64F64 =
17371734 I64F64 :: saturating_from_num ( Self :: get_bonds_moving_average ( netuid) )
@@ -1764,7 +1761,7 @@ impl<T: Config> Pallet<T> {
17641761 pub fn compute_ema_bonds_normal (
17651762 bonds_delta : & [ Vec < I32F32 > ] ,
17661763 bonds : & [ Vec < I32F32 > ] ,
1767- netuid : NetUidStorageIndex ,
1764+ netuid : NetUid ,
17681765 ) -> Vec < Vec < I32F32 > > {
17691766 // Retrieve the bonds moving average for the given network ID and scale it down.
17701767 let bonds_moving_average: I64F64 =
@@ -1798,13 +1795,11 @@ impl<T: Config> Pallet<T> {
17981795 /// # Returns:
17991796 /// A vector of EMA bonds.
18001797 pub fn compute_bonds (
1801- netuid_index : NetUidStorageIndex ,
1798+ netuid : NetUid ,
18021799 weights : & [ Vec < I32F32 > ] , // weights_for_bonds
18031800 bonds : & [ Vec < I32F32 > ] ,
18041801 consensus : & [ I32F32 ] ,
18051802 ) -> Vec < Vec < I32F32 > > {
1806- let ( netuid, _) = Self :: get_netuid_and_subid ( netuid_index) . unwrap_or_default ( ) ;
1807-
18081803 // Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
18091804 if LiquidAlphaOn :: < T > :: get ( netuid)
18101805 && !consensus. is_empty ( )
@@ -1821,7 +1816,7 @@ impl<T: Config> Pallet<T> {
18211816 mat_ema_alpha ( weights, bonds, & alphas)
18221817 } else {
18231818 // Liquid Alpha is disabled, compute the liquid alpha value.
1824- let alpha: I32F32 = Self :: compute_disabled_liquid_alpha ( netuid_index ) ;
1819+ let alpha: I32F32 = Self :: compute_disabled_liquid_alpha ( netuid ) ;
18251820
18261821 // Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
18271822 mat_ema ( weights, bonds, alpha)
@@ -1863,7 +1858,7 @@ impl<T: Config> Pallet<T> {
18631858 mat_ema_alpha_sparse ( weights, bonds, & alphas)
18641859 } else {
18651860 // Liquid Alpha is disabled, compute the liquid alpha value.
1866- let alpha: I32F32 = Self :: compute_disabled_liquid_alpha ( netuid_index ) ;
1861+ let alpha: I32F32 = Self :: compute_disabled_liquid_alpha ( netuid ) ;
18671862
18681863 // Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
18691864 mat_ema_sparse ( weights, bonds, alpha)
@@ -2018,7 +2013,7 @@ impl<T: Config> Pallet<T> {
20182013 clamp_value ( alpha, alpha_low, alpha_high)
20192014 }
20202015
2021- pub fn compute_disabled_liquid_alpha ( netuid : NetUidStorageIndex ) -> I32F32 {
2016+ pub fn compute_disabled_liquid_alpha ( netuid : NetUid ) -> I32F32 {
20222017 // Retrieve the bonds moving average for the given network ID and scale it down.
20232018 let bonds_moving_average: I64F64 = I64F64 :: from_num ( Self :: get_bonds_moving_average ( netuid) )
20242019 . saturating_div ( I64F64 :: from_num ( 1_000_000 ) ) ;
0 commit comments