diff --git a/pallets/staking/src/lib.rs b/pallets/staking/src/lib.rs index e16f3d791..beaef29ed 100644 --- a/pallets/staking/src/lib.rs +++ b/pallets/staking/src/lib.rs @@ -414,23 +414,11 @@ pub mod pallet { let validator_id = ::ValidatorId::try_from(ledger.stash) .or(Err(Error::::InvalidValidatorId))?; - let signers = Self::signers(); - ensure!(!signers.contains(&validator_id), Error::::NoUnbondingWhenSigner); - - let next_signers = Self::next_signers(); - if next_signers.is_some() { - ensure!( - !next_signers - .ok_or(Error::::ReshareNotInProgress)? - .next_signers - .contains(&validator_id), - Error::::NoUnbondingWhenNextSigner - ); - } + let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?; pallet_staking::Pallet::::unbond(origin, value)?; - Ok(Some(::WeightInfo::unbond(signers.len() as u32)).into()) + Ok(Some(::WeightInfo::unbond(signers_length)).into()) } /// Wraps's substrate chill but checks to make targeted validator sure not signer or next signer @@ -445,23 +433,11 @@ pub mod pallet { let validator_id = ::ValidatorId::try_from(ledger.stash) .or(Err(Error::::InvalidValidatorId))?; - let signers = Self::signers(); - ensure!(!signers.contains(&validator_id), Error::::NoUnbondingWhenSigner); - - let next_signers = Self::next_signers(); - if next_signers.is_some() { - ensure!( - !next_signers - .ok_or(Error::::ReshareNotInProgress)? - .next_signers - .contains(&validator_id), - Error::::NoUnbondingWhenNextSigner - ); - } + let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?; pallet_staking::Pallet::::chill(origin)?; - Ok(Some(::WeightInfo::chill(signers.len() as u32)).into()) + Ok(Some(::WeightInfo::chill(signers_length)).into()) } /// Wraps's substrate withdraw unbonded but clears extra state if fully unbonded @@ -479,18 +455,7 @@ pub mod pallet { let validator_id = ::ValidatorId::try_from(ledger.stash) .or(Err(Error::::InvalidValidatorId))?; - let signers = Self::signers(); - ensure!(!signers.contains(&validator_id), Error::::NoUnbondingWhenSigner); - let next_signers = Self::next_signers(); - if next_signers.is_some() { - ensure!( - !next_signers - .ok_or(Error::::ReshareNotInProgress)? - .next_signers - .contains(&validator_id), - Error::::NoUnbondingWhenNextSigner - ); - } + let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?; pallet_staking::Pallet::::withdraw_unbonded(origin, num_slashing_spans)?; // TODO: do not allow unbonding of validator if not enough validators https://github.com/entropyxyz/entropy-core/issues/942 @@ -501,7 +466,7 @@ pub mod pallet { IsValidatorSynced::::remove(&validator_id); Self::deposit_event(Event::NodeInfoRemoved(controller)); } - Ok(Some(::WeightInfo::withdraw_unbonded(signers.len() as u32)).into()) + Ok(Some(::WeightInfo::withdraw_unbonded(signers_length)).into()) } /// Wrap's Substrate's `staking_pallet::validate()` extrinsic, but enforces that @@ -605,6 +570,23 @@ pub mod pallet { Ok(ledger.stash) } + /// Ensures that the current validator is not a signer or a next signer + pub fn ensure_not_signer_or_next_signer( + validator_id: &::ValidatorId, + ) -> Result { + let signers = Self::signers(); + ensure!(!signers.contains(validator_id), Error::::NoUnbondingWhenSigner); + + if let Some(next_signers) = Self::next_signers() { + ensure!( + !next_signers.next_signers.contains(validator_id), + Error::::NoUnbondingWhenNextSigner + ); + } + + Ok(signers.len() as u32) + } + pub fn get_randomness() -> ChaCha20Rng { let phrase = b"signer_rotation"; // TODO: Is randomness freshness an issue here