@@ -1286,9 +1286,21 @@ impl<T: Config> Pallet<T> {
12861286 . iter ( )
12871287 . any ( |& c| c != I32F32 :: saturating_from_num ( 0 ) )
12881288 {
1289- // Liquid Alpha is enabled, compute the liquid alphas matrix.
1290- let alphas: Vec < Vec < I32F32 > > =
1291- Self :: compute_liquid_alpha_values ( netuid, weights, bonds, consensus) ;
1289+ // Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
1290+ let consensus_for_liquid_alpha =
1291+ Self :: compute_consensus_for_liquid_alpha ( netuid, consensus) ;
1292+ log:: trace!(
1293+ "consensus_for_liquid_alpha: {:?}" ,
1294+ & consensus_for_liquid_alpha
1295+ ) ;
1296+
1297+ // Compute the liquid alphas matrix.
1298+ let alphas: Vec < Vec < I32F32 > > = Self :: compute_liquid_alpha_values (
1299+ netuid,
1300+ weights,
1301+ bonds,
1302+ & consensus_for_liquid_alpha,
1303+ ) ;
12921304 log:: trace!( "alphas: {:?}" , & alphas) ;
12931305
12941306 // Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
@@ -1328,9 +1340,21 @@ impl<T: Config> Pallet<T> {
13281340 . iter ( )
13291341 . any ( |& c| c != I32F32 :: saturating_from_num ( 0 ) )
13301342 {
1331- // Liquid Alpha is enabled, compute the liquid alphas matrix.
1332- let alphas: Vec < Vec < I32F32 > > =
1333- Self :: compute_liquid_alpha_values_sparse ( netuid, weights, bonds, consensus) ;
1343+ // Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
1344+ let consensus_for_liquid_alpha =
1345+ Self :: compute_consensus_for_liquid_alpha ( netuid, consensus) ;
1346+ log:: trace!(
1347+ "consensus_for_liquid_alpha: {:?}" ,
1348+ & consensus_for_liquid_alpha
1349+ ) ;
1350+
1351+ // Compute the liquid alphas matrix.
1352+ let alphas: Vec < Vec < I32F32 > > = Self :: compute_liquid_alpha_values_sparse (
1353+ netuid,
1354+ weights,
1355+ bonds,
1356+ & consensus_for_liquid_alpha,
1357+ ) ;
13341358 log:: trace!( "alphas: {:?}" , & alphas) ;
13351359
13361360 // Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
@@ -1344,6 +1368,55 @@ impl<T: Config> Pallet<T> {
13441368 }
13451369 }
13461370
1371+ /// Compute the consensus to use for liquid alpha calculation based on the configured mode
1372+ ///
1373+ /// # Args:
1374+ /// * `netuid` - The network ID.
1375+ /// * `current_consensus` - The current in-memory consensus values.
1376+ ///
1377+ /// # Returns:
1378+ /// A vector of consensus values to use for liquid alpha calculation
1379+ pub fn compute_consensus_for_liquid_alpha (
1380+ netuid : NetUid ,
1381+ current_consensus : & [ I32F32 ] ,
1382+ ) -> Vec < I32F32 > {
1383+ let mode = Self :: get_liquid_alpha_consensus_mode ( netuid) ;
1384+
1385+ match mode {
1386+ ConsensusMode :: Current => {
1387+ // Use the in-memory consensus (current behavior)
1388+ current_consensus. to_vec ( )
1389+ }
1390+ ConsensusMode :: Previous => {
1391+ // Use consensus from storage
1392+ let previous_consensus_u16 = Consensus :: < T > :: get ( netuid) ;
1393+ previous_consensus_u16
1394+ . iter ( )
1395+ . map ( |& c| {
1396+ I32F32 :: saturating_from_num ( c)
1397+ . safe_div ( I32F32 :: saturating_from_num ( u16:: MAX ) )
1398+ } )
1399+ . collect ( )
1400+ }
1401+ ConsensusMode :: Auto => {
1402+ // Auto mode: Previous if bond_penalty == 1, otherwise Current
1403+ let bonds_penalty = Self :: get_float_bonds_penalty ( netuid) ;
1404+ if bonds_penalty == I32F32 :: from_num ( 1 ) {
1405+ let previous_consensus_u16 = Consensus :: < T > :: get ( netuid) ;
1406+ previous_consensus_u16
1407+ . iter ( )
1408+ . map ( |& c| {
1409+ I32F32 :: saturating_from_num ( c)
1410+ . safe_div ( I32F32 :: saturating_from_num ( u16:: MAX ) )
1411+ } )
1412+ . collect ( )
1413+ } else {
1414+ current_consensus. to_vec ( )
1415+ }
1416+ }
1417+ }
1418+ }
1419+
13471420 /// Compute liquid alphas matrix
13481421 /// There is a separate alpha param for each validator-miner binding
13491422 ///
@@ -1551,6 +1624,19 @@ impl<T: Config> Pallet<T> {
15511624 Ok ( ( ) )
15521625 }
15531626
1627+ pub fn do_set_liquid_alpha_consensus_mode (
1628+ origin : OriginFor < T > ,
1629+ netuid : NetUid ,
1630+ mode : ConsensusMode ,
1631+ ) -> Result < ( ) , DispatchError > {
1632+ Self :: ensure_subnet_owner_or_root ( origin, netuid) ?;
1633+
1634+ Self :: set_liquid_alpha_consensus_mode ( netuid, mode. clone ( ) ) ;
1635+
1636+ log:: debug!( "LiquidAlphaConsensusModeSet( netuid: {netuid:?}, mode: {mode:?} )" , ) ;
1637+ Ok ( ( ) )
1638+ }
1639+
15541640 pub fn do_reset_bonds (
15551641 netuid_index : NetUidStorageIndex ,
15561642 account_id : & T :: AccountId ,
0 commit comments