Skip to content

Commit d0214db

Browse files
committed
Remove .expect from set_children.rs
1 parent 522d744 commit d0214db

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

pallets/subtensor/src/macros/hooks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ mod hooks {
151151
// Migrate subnet burn cost to 2500
152152
.saturating_add(migrations::migrate_network_lock_cost_2500::migrate_network_lock_cost_2500::<T>())
153153
// Cleanup child/parent keys
154-
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>()),
154+
.saturating_add(migrations::migrate_fix_childkeys::migrate_fix_childkeys::<T>())
155155
// Migrate AutoStakeDestinationColdkeys
156156
.saturating_add(migrations::migrate_auto_stake_destination::migrate_auto_stake_destination::<T>());
157157
weight

pallets/subtensor/src/staking/set_children.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl<T: Config> Pallet<T> {
273273
let new_children_map = relations.children();
274274
let new_children_vec: Vec<(u64, T::AccountId)> = new_children_map
275275
.iter()
276-
.map(|(c, w)| (*w, c.clone()))
276+
.map(|(c, p)| (*p, c.clone()))
277277
.collect();
278278

279279
let prev_children_vec = ChildKeys::<T>::get(&pivot, netuid);
@@ -293,30 +293,36 @@ impl<T: Config> Pallet<T> {
293293
.iter()
294294
.filter(|c| !prev_children_set.contains(*c))
295295
{
296+
let p = match new_children_map.get(added) {
297+
Some(p) => *p,
298+
None => return Err(Error::<T>::ChildParentInconsistency.into()),
299+
};
296300
let mut pk = ParentKeys::<T>::get(added.clone(), netuid);
297-
let w = *new_children_map.get(added).expect("in set; qed");
298-
PCRelations::<T>::upsert_edge(&mut pk, w, &pivot);
299-
Self::set_parentkeys(added.clone(), netuid, pk.clone());
301+
PCRelations::<T>::upsert_edge(&mut pk, p, &pivot);
302+
Self::set_parentkeys(added.clone(), netuid, pk);
300303
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
301304
}
302305

303306
// Updated children = intersection where proportion changed
304307
for common in new_children_set.intersection(&prev_children_set) {
305-
let new_p = *new_children_map.get(common).expect("in set; qed");
308+
let new_p = match new_children_map.get(common) {
309+
Some(p) => *p,
310+
None => return Err(Error::<T>::ChildParentInconsistency.into()),
311+
};
306312
let mut pk = ParentKeys::<T>::get(common.clone(), netuid);
307313
PCRelations::<T>::upsert_edge(&mut pk, new_p, &pivot);
308-
Self::set_parentkeys(common.clone(), netuid, pk.clone());
314+
Self::set_parentkeys(common.clone(), netuid, pk);
309315
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
310316
}
311317

312-
// Removed children = prev / new
318+
// Removed children = prev \ new => remove (pivot) from ParentKeys(child)
313319
for removed in prev_children_set
314320
.iter()
315321
.filter(|c| !new_children_set.contains(*c))
316322
{
317-
let mut pk = ParentKeys::<T>::get(&removed, netuid);
323+
let mut pk = ParentKeys::<T>::get(removed.clone(), netuid);
318324
PCRelations::<T>::remove_edge(&mut pk, &pivot);
319-
Self::set_parentkeys(removed.clone(), netuid, pk.clone());
325+
Self::set_parentkeys(removed.clone(), netuid, pk);
320326
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
321327
}
322328

@@ -326,7 +332,7 @@ impl<T: Config> Pallet<T> {
326332
let new_parents_map = relations.parents();
327333
let new_parents_vec: Vec<(u64, T::AccountId)> = new_parents_map
328334
.iter()
329-
.map(|(p, w)| (*w, p.clone()))
335+
.map(|(p, pr)| (*pr, p.clone()))
330336
.collect();
331337

332338
let prev_parents_vec = ParentKeys::<T>::get(&pivot, netuid);
@@ -338,28 +344,34 @@ impl<T: Config> Pallet<T> {
338344
prev_parents_vec.iter().map(|(_, p)| p.clone()).collect();
339345
let new_parents_set: BTreeSet<T::AccountId> = new_parents_map.keys().cloned().collect();
340346

341-
// Added parents = new / prev => ensure ChildKeys(parent) has (w, pivot)
347+
// Added parents = new / prev => ensure ChildKeys(parent) has (p, pivot)
342348
for added in new_parents_set
343349
.iter()
344350
.filter(|p| !prev_parents_set.contains(*p))
345351
{
352+
let p_val = match new_parents_map.get(added) {
353+
Some(p) => *p,
354+
None => return Err(Error::<T>::ChildParentInconsistency.into()),
355+
};
346356
let mut ck = ChildKeys::<T>::get(added.clone(), netuid);
347-
let w = *new_parents_map.get(added).expect("in set; qed");
348-
PCRelations::<T>::upsert_edge(&mut ck, w, &pivot);
357+
PCRelations::<T>::upsert_edge(&mut ck, p_val, &pivot);
349358
Self::set_childkeys(added.clone(), netuid, ck);
350359
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
351360
}
352361

353-
// Updated parents = intersection where weight changed
362+
// Updated parents = intersection where proportion changed
354363
for common in new_parents_set.intersection(&prev_parents_set) {
355-
let new_w = *new_parents_map.get(common).expect("in set; qed");
364+
let new_p = match new_parents_map.get(common) {
365+
Some(p) => *p,
366+
None => return Err(Error::<T>::ChildParentInconsistency.into()),
367+
};
356368
let mut ck = ChildKeys::<T>::get(common.clone(), netuid);
357-
PCRelations::<T>::upsert_edge(&mut ck, new_w, &pivot);
369+
PCRelations::<T>::upsert_edge(&mut ck, new_p, &pivot);
358370
Self::set_childkeys(common.clone(), netuid, ck);
359371
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
360372
}
361373

362-
// Removed parents = prev / new => remove (pivot) from ChildKeys(parent)
374+
// Removed parents = prev \ new => remove (pivot) from ChildKeys(parent)
363375
for removed in prev_parents_set
364376
.iter()
365377
.filter(|p| !new_parents_set.contains(*p))

0 commit comments

Comments
 (0)