diff --git a/pallets/staking/src/lib.rs b/pallets/staking/src/lib.rs index 4674f6106..cd615ae95 100644 --- a/pallets/staking/src/lib.rs +++ b/pallets/staking/src/lib.rs @@ -569,40 +569,38 @@ pub mod pallet { pub fn ensure_not_signer_or_next_signer( stash: &T::AccountId, ) -> Result { - let validator_id = ::ValidatorId::try_from(stash.clone()) - .or(Err(Error::::InvalidValidatorId))?; let nominations = pallet_staking::Nominators::::get(stash) .map_or_else(Vec::new, |x| x.targets.into_inner()); let signers = Self::signers(); - ensure!(!signers.contains(&validator_id), Error::::NoUnbondingWhenSigner); - for nominated in &nominations { - let validator_id_nominated = - ::ValidatorId::try_from(nominated.clone()) - .or(Err(Error::::InvalidValidatorId))?; - ensure!( - !signers.contains(&validator_id_nominated), - Error::::NoUnbondingWhenSigner - ); - } - ensure!(!signers.contains(&validator_id), Error::::NoUnbondingWhenSigner); + // Check if the validator_id or any nominated validator is in signers + let in_signers = |id: &T::AccountId| { + let validator_id = ::ValidatorId::try_from(id.clone()); + match validator_id { + Ok(v_id) => signers.contains(&v_id), + Err(_) => false, + } + }; + + ensure!(!in_signers(stash), Error::::NoUnbondingWhenSigner); + ensure!(!nominations.iter().any(|n| in_signers(n)), Error::::NoUnbondingWhenSigner); if let Some(next_signers) = Self::next_signers() { + let next_signers_contains = |id: &T::AccountId| { + let validator_id = + ::ValidatorId::try_from(id.clone()); + match validator_id { + Ok(v_id) => next_signers.next_signers.contains(&v_id), + Err(_) => false, + } + }; + + ensure!(!next_signers_contains(stash), Error::::NoUnbondingWhenNextSigner); ensure!( - !next_signers.next_signers.contains(&validator_id), + !nominations.iter().any(|n| next_signers_contains(n)), Error::::NoUnbondingWhenNextSigner ); - - for nominated in &nominations { - let validator_id_nominated = - ::ValidatorId::try_from(nominated.clone()) - .or(Err(Error::::InvalidValidatorId))?; - ensure!( - !next_signers.next_signers.contains(&validator_id_nominated), - Error::::NoUnbondingWhenNextSigner - ); - } } Ok(signers.len() as u32)