Skip to content

Commit

Permalink
fix: add a code substitue for the out of bounds error
Browse files Browse the repository at this point in the history
  • Loading branch information
saiintbrisson committed May 31, 2024
1 parent a94c512 commit c6ce7a2
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 20 deletions.
6 changes: 3 additions & 3 deletions pallets/subspace/src/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,19 @@ pub fn vec_mask_sparse_matrix(
first_vector: &[u64],
second_vector: &[u64],
mask_fn: impl Fn(u64, u64) -> bool,
) -> Vec<Vec<(u16, I32F32)>> {
) -> Option<Vec<Vec<(u16, I32F32)>>> {
let n: usize = sparse_matrix.len();
let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];

for (i, sparse_row) in sparse_matrix.iter().enumerate() {
for (j, value) in sparse_row.iter() {
if !mask_fn(first_vector[i], second_vector[*j as usize]) {
if !mask_fn(*first_vector.get(i)?, *second_vector.get(*j as usize)?) {
result[i].push((*j, *value));
}
}
}

result
Some(result)
}

pub fn inplace_mask_vector(mask: &[bool], vector: &mut [I32F32]) {
Expand Down
2 changes: 1 addition & 1 deletion pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
});

if res.is_err() {
return;
continue;
}
}

Expand Down
23 changes: 13 additions & 10 deletions pallets/subspace/src/step/yuma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl<T: Config> YumaCalc<T> {
})
.unzip();

let mut weights = self.compute_weights();
let mut weights =
self.compute_weights().ok_or(YumaError::Other("weights storage is broken"))?;
log::trace!("final weights: {weights:?}");

let stake = self.compute_stake()?;
Expand Down Expand Up @@ -119,7 +120,9 @@ impl<T: Config> YumaCalc<T> {
let BondsAndDividends {
ema_bonds,
dividends,
} = self.compute_bonds_and_dividends(&weights, &active_stake, &incentives);
} = self
.compute_bonds_and_dividends(&weights, &active_stake, &incentives)
.ok_or(YumaError::Other("bonds storage is broken"))?;

let Emissions {
pruning_scores,
Expand Down Expand Up @@ -288,7 +291,7 @@ impl<T: Config> YumaCalc<T> {
Ok(emissions)
}

fn compute_weights(&self) -> WeightsVal {
fn compute_weights(&self) -> Option<WeightsVal> {
// Access network weights row unnormalized.
let mut weights = Pallet::<T>::get_weights_sparse(self.netuid);
log::trace!(" original weights: {weights:?}");
Expand All @@ -309,14 +312,14 @@ impl<T: Config> YumaCalc<T> {
&self.last_update,
&self.block_at_registration,
|updated, registered| updated <= registered,
);
)?;
log::trace!(" no deregistered modules weights: {weights:?}");

// Normalize remaining weights.
inplace_row_normalize_sparse(&mut weights);
log::trace!(" normalized weights: {weights:?}");

WeightsVal::unchecked_from_inner(weights)
Some(WeightsVal::unchecked_from_inner(weights))
}

fn compute_stake(&self) -> Result<StakeVal, &'static str> {
Expand Down Expand Up @@ -424,7 +427,7 @@ impl<T: Config> YumaCalc<T> {
weights: &WeightsVal,
active_stake: &ActiveStake,
incentives: &IncentivesVal,
) -> BondsAndDividends {
) -> Option<BondsAndDividends> {
// Access network bonds.
let mut bonds = Pallet::<T>::get_bonds_sparse(self.netuid);
log::trace!(" original bonds: {bonds:?}");
Expand All @@ -439,7 +442,7 @@ impl<T: Config> YumaCalc<T> {
&self.last_update,
&self.block_at_registration,
|updated, registered| updated <= registered,
);
)?;
log::trace!(" no deregistered modules bonds: {bonds:?}");

// Normalize remaining bonds: sum_i b_ij = 1.
Expand Down Expand Up @@ -478,10 +481,10 @@ impl<T: Config> YumaCalc<T> {
inplace_col_max_upscale_sparse(&mut ema_bonds, self.module_count);
log::trace!(" upscaled ema bonds: {ema_bonds:?}");

BondsAndDividends {
Some(BondsAndDividends {
ema_bonds,
dividends: DividendsVal::unchecked_from_inner(dividends),
}
})
}

fn compute_emissions<'a>(
Expand Down Expand Up @@ -679,7 +682,7 @@ impl<T: Config> Pallet<T> {
fn get_bonds_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnet_n(netuid) as usize;
let mut bonds: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, bonds_i) in Bonds::<T>::iter_prefix(netuid) {
for (uid_i, bonds_i) in Bonds::<T>::iter_prefix(netuid).filter(|(uid, _)| *uid < n as u16) {
for (uid_j, bonds_ij) in bonds_i {
bonds[uid_i as usize].push((uid_j, I32F32::from_num(bonds_ij)));
}
Expand Down
3 changes: 1 addition & 2 deletions pallets/subspace/tests/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use sp_core::U256;
use log::info;
use pallet_subspace::{
voting::ApplicationStatus, CuratorApplications, Emission, Error, MaxAllowedModules,
MaxAllowedUids, MinStake, RemovedSubnets, Stake, SubnetNames,
TotalSubnets, N,
MaxAllowedUids, MinStake, RemovedSubnets, Stake, SubnetNames, TotalSubnets, N,
};
use sp_runtime::{DispatchResult, Percent};

Expand Down
4 changes: 2 additions & 2 deletions pallets/subspace/tests/step_linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use frame_support::assert_ok;
use log::info;
use mock::*;
use pallet_subspace::{
DaoTreasuryDistribution, GlobalDaoTreasury, MaxAllowedWeights,
MinAllowedWeights, MinBurn, SubnetStakeThreshold, Tempo,
DaoTreasuryDistribution, GlobalDaoTreasury, MaxAllowedWeights, MinAllowedWeights, MinBurn,
SubnetStakeThreshold, Tempo,
};
use sp_core::U256;
use sp_runtime::Percent;
Expand Down
3 changes: 1 addition & 2 deletions pallets/subspace/tests/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use frame_support::{assert_err, assert_ok};
use log::info;
use mock::*;
use pallet_subspace::{
Dividends, Error, FounderShare, MaximumSetWeightCallsPerEpoch,
SubnetNames, Tempo, N,
Dividends, Error, FounderShare, MaximumSetWeightCallsPerEpoch, SubnetNames, Tempo, N,
};
use sp_core::U256;
use sp_runtime::Percent;
Expand Down

0 comments on commit c6ce7a2

Please # to comment.