Skip to content

Commit

Permalink
Avoid wrapping session index
Browse files Browse the repository at this point in the history
Bit clearer this way.
  • Loading branch information
zdave-parity committed Sep 14, 2023
1 parent 0fc3852 commit cfb405a
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions substrate/frame/mixnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub mod pallet {

Mixnodes::<T>::insert(
// Registering for the _following_ session
registration.session_index.wrapping_add(1),
registration.session_index + 1,
registration.authority_index,
registration.mixnode,
);
Expand Down Expand Up @@ -404,7 +404,13 @@ impl<T: Config> Pallet<T> {

/// Returns the mixnode set for the previous session.
pub fn prev_mixnodes() -> Result<Vec<Mixnode>, MixnodesErr> {
Self::mixnodes(CurrentSessionIndex::<T>::get().wrapping_sub(1))
let Some(prev_session_index) = CurrentSessionIndex::<T>::get().checked_sub(1) else {
return Err(MixnodesErr::InsufficientRegistrations {
num: 0,
min: T::MinMixnodes::get(),
})
};
Self::mixnodes(prev_session_index)
}

/// Returns the mixnode set for the current session.
Expand Down Expand Up @@ -461,7 +467,7 @@ impl<T: Config> Pallet<T> {
/// `session_index` should be the index of the current session. `authority_index` is the
/// authority index in the _next_ session.
fn already_registered(session_index: SessionIndex, authority_index: AuthorityIndex) -> bool {
Mixnodes::<T>::contains_key(session_index.wrapping_add(1), authority_index)
Mixnodes::<T>::contains_key(session_index + 1, authority_index)
}

/// Try to register a mixnode for the next session.
Expand Down Expand Up @@ -566,11 +572,13 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Pallet<T> {
CurrentSessionStartBlock::<T>::put(frame_system::Pallet::<T>::block_number());

// Discard the previous previous mixnode set, which we don't need any more
check_removed_all(Mixnodes::<T>::clear_prefix(
session_index.wrapping_sub(2),
T::MaxAuthorities::get(),
None,
));
if let Some(prev_prev_session_index) = session_index.checked_sub(2) {
check_removed_all(Mixnodes::<T>::clear_prefix(
prev_prev_session_index,
T::MaxAuthorities::get(),
None,
));
}

if changed {
// Save authority set for the next session. Note that we don't care about the authority
Expand Down

0 comments on commit cfb405a

Please sign in to comment.