Skip to content

Commit

Permalink
add in nominating check
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseAbram committed Aug 29, 2024
1 parent 13af928 commit f185d77
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
49 changes: 35 additions & 14 deletions pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,7 @@ pub mod pallet {
pallet_staking::Pallet::<T>::ledger(StakingAccount::Controller(controller.clone()))
.map_err(|_| Error::<T>::NoThresholdKey)?;

let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(ledger.stash)
.or(Err(Error::<T>::InvalidValidatorId))?;

let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?;
let signers_length = Self::ensure_not_signer_or_next_signer(&ledger.stash)?;

pallet_staking::Pallet::<T>::unbond(origin, value)?;

Expand All @@ -430,10 +427,7 @@ pub mod pallet {
pallet_staking::Pallet::<T>::ledger(StakingAccount::Controller(controller.clone()))
.map_err(|_| Error::<T>::NoThresholdKey)?;

let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(ledger.stash)
.or(Err(Error::<T>::InvalidValidatorId))?;

let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?;
let signers_length = Self::ensure_not_signer_or_next_signer(&ledger.stash)?;

pallet_staking::Pallet::<T>::chill(origin)?;

Expand All @@ -452,10 +446,11 @@ pub mod pallet {
pallet_staking::Pallet::<T>::ledger(StakingAccount::Controller(controller.clone()))
.map_err(|_| Error::<T>::NoThresholdKey)?;

let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(ledger.stash)
.or(Err(Error::<T>::InvalidValidatorId))?;
let validator_id =
<T as pallet_session::Config>::ValidatorId::try_from(ledger.stash.clone())
.or(Err(Error::<T>::InvalidValidatorId))?;

let signers_length = Self::ensure_not_signer_or_next_signer(&validator_id)?;
let signers_length = Self::ensure_not_signer_or_next_signer(&ledger.stash)?;

pallet_staking::Pallet::<T>::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
Expand Down Expand Up @@ -572,16 +567,42 @@ pub mod pallet {

/// Ensures that the current validator is not a signer or a next signer
pub fn ensure_not_signer_or_next_signer(
validator_id: &<T as pallet_session::Config>::ValidatorId,
stash: &T::AccountId,
) -> Result<u32, DispatchError> {
let validator_id = <T as pallet_session::Config>::ValidatorId::try_from(stash.clone())
.or(Err(Error::<T>::InvalidValidatorId))?;
let nominations = pallet_staking::Nominators::<T>::get(stash)
.map_or_else(Vec::new, |x| x.targets.into_inner());

let signers = Self::signers();
ensure!(!signers.contains(validator_id), Error::<T>::NoUnbondingWhenSigner);
ensure!(!signers.contains(&validator_id), Error::<T>::NoUnbondingWhenSigner);
for nominated in &nominations {
let validator_id_nominated =
<T as pallet_session::Config>::ValidatorId::try_from(nominated.clone())
.or(Err(Error::<T>::InvalidValidatorId))?;
ensure!(
!signers.contains(&validator_id_nominated),
Error::<T>::NoUnbondingWhenSigner
);
}

ensure!(!signers.contains(&validator_id), Error::<T>::NoUnbondingWhenSigner);

if let Some(next_signers) = Self::next_signers() {
ensure!(
!next_signers.next_signers.contains(validator_id),
!next_signers.next_signers.contains(&validator_id),
Error::<T>::NoUnbondingWhenNextSigner
);

for nominated in &nominations {
let validator_id_nominated =
<T as pallet_session::Config>::ValidatorId::try_from(nominated.clone())
.or(Err(Error::<T>::InvalidValidatorId))?;
ensure!(
!next_signers.next_signers.contains(&validator_id_nominated),
Error::<T>::NoUnbondingWhenNextSigner
);
}
}

Ok(signers.len() as u32)
Expand Down
2 changes: 1 addition & 1 deletion pallets/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ impl pallet_parameters::Config for Test {
pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap();
let pallet_balances = pallet_balances::GenesisConfig::<Test> {
balances: vec![(1, 100), (2, 100), (3, 100), (4, 100), (7, 100), (8, 100)],
balances: vec![(1, 100), (2, 100), (3, 100), (4, 100), (7, 100), (8, 100), (101, 200)],
};
let pallet_staking_extension = pallet_staking_extension::GenesisConfig::<Test> {
// (ValidatorID, (AccountId, X25519PublicKey, TssServerURL))
Expand Down
18 changes: 18 additions & 0 deletions pallets/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,18 @@ fn it_stops_chill_when_signer_or_next_signer() {
Signers::<Test>::put(vec![7]);
start_active_era(1);

assert_ok!(FrameStaking::bond(
RuntimeOrigin::signed(101),
100u64,
pallet_staking::RewardDestination::Account(1),
));
// test nominating flow
assert_ok!(FrameStaking::nominate(RuntimeOrigin::signed(101), vec![7]));
assert_noop!(
Staking::unbond(RuntimeOrigin::signed(101), 100u64),
Error::<Test>::NoUnbondingWhenSigner
);

assert_ok!(FrameStaking::bond(
RuntimeOrigin::signed(7),
100u64,
Expand All @@ -544,5 +556,11 @@ fn it_stops_chill_when_signer_or_next_signer() {
Staking::chill(RuntimeOrigin::signed(8)),
Error::<Test>::NoUnbondingWhenNextSigner
);

assert_ok!(FrameStaking::nominate(RuntimeOrigin::signed(101), vec![8]));
assert_noop!(
Staking::unbond(RuntimeOrigin::signed(101), 100u64),
Error::<Test>::NoUnbondingWhenNextSigner
);
});
}

0 comments on commit f185d77

Please sign in to comment.