Skip to content

Commit 12ce999

Browse files
4meta5joelamouche
andauthored
stake: revoke nomination => leave nominators if no nominations left (paritytech#294)
* done * bump spec version * make if else into ret early on edge case * update comments * update test (paritytech#296) * update test * update runtime Co-authored-by: Antoine Estienne <[email protected]>
1 parent 5f7e770 commit 12ce999

File tree

5 files changed

+29
-15
lines changed

5 files changed

+29
-15
lines changed

pallets/parachain-staking/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,9 +1079,18 @@ pub mod pallet {
10791079
collator: T::AccountId,
10801080
) -> DispatchResultWithPostInfo {
10811081
let mut nominator = <NominatorState<T>>::get(&acc).ok_or(Error::<T>::NominatorDNE)?;
1082+
let old_total = nominator.total;
10821083
let remaining = nominator
10831084
.rm_nomination(collator.clone())
10841085
.ok_or(Error::<T>::NominationDNE)?;
1086+
// edge case; if no nominations remaining, leave set of nominators
1087+
if nominator.nominations.0.len().is_zero() {
1088+
// leave the set of nominators because no nominations left
1089+
Self::nominator_leaves_collator(acc.clone(), collator)?;
1090+
<NominatorState<T>>::remove(&acc);
1091+
Self::deposit_event(Event::NominatorLeft(acc, old_total));
1092+
return Ok(().into());
1093+
}
10851094
ensure!(
10861095
remaining >= T::MinNominatorStk::get(),
10871096
Error::<T>::NomBondBelowMin

pallets/parachain-staking/src/tests.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -735,21 +735,25 @@ fn revoke_nomination_or_leave_nominators() {
735735
Stake::revoke_nomination(Origin::signed(6), 2),
736736
Error::<Test>::NominationDNE
737737
);
738-
// must leave set of nominators if total bonds below MinNominatorStk
739-
assert_noop!(
740-
Stake::revoke_nomination(Origin::signed(6), 1),
741-
Error::<Test>::NomBondBelowMin
742-
);
743738
assert_noop!(
744739
Stake::leave_nominators(Origin::signed(1)),
745740
Error::<Test>::NominatorDNE
746741
);
747-
assert_ok!(Stake::leave_nominators(Origin::signed(6)));
742+
assert_ok!(Stake::nominate(Origin::signed(6), 2, 3));
743+
assert_ok!(Stake::nominate(Origin::signed(6), 3, 3));
744+
assert_ok!(Stake::revoke_nomination(Origin::signed(6), 1));
745+
// cannot revoke nomination because would leave remaining total below MinNominatorStk
746+
assert_noop!(
747+
Stake::revoke_nomination(Origin::signed(6), 2),
748+
Error::<Test>::NomBondBelowMin
749+
);
748750
assert_noop!(
749-
Stake::revoke_nomination(Origin::signed(8), 2),
751+
Stake::revoke_nomination(Origin::signed(6), 3),
750752
Error::<Test>::NomBondBelowMin
751753
);
752-
assert_ok!(Stake::nominate(Origin::signed(8), 1, 10));
754+
// can revoke both remaining by calling leave nominators
755+
assert_ok!(Stake::leave_nominators(Origin::signed(6)));
756+
// this leads to 8 leaving set of nominators
753757
assert_ok!(Stake::revoke_nomination(Origin::signed(8), 2));
754758
});
755759
}

runtime/src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,15 +385,15 @@ parameter_types! {
385385
pub const MinBlocksPerRound: u32 = 20;
386386
/// Default BlocksPerRound is every hour (600 * 6 second block times)
387387
pub const DefaultBlocksPerRound: u32 = 600;
388-
/// Reward payments and validator exit requests are delayed by 2 hours (2 * 600 * block_time)
388+
/// Reward payments and collator exit requests are delayed by 2 hours (2 * 600 * block_time)
389389
pub const BondDuration: u32 = 2;
390-
/// Maximum 8 valid block authors at any given time
390+
/// Minimum 8 collators selected per round, default at genesis and minimum forever after
391391
pub const MinSelectedCandidates: u32 = 8;
392-
/// Maximum 10 nominators per validator
392+
/// Maximum 10 nominators per collator
393393
pub const MaxNominatorsPerCollator: u32 = 10;
394-
/// The maximum percent a validator can take off the top of its rewards is 50%
394+
/// The maximum percent a collator can take off the top of its rewards is 50%
395395
pub const MaxFee: Perbill = Perbill::from_percent(50);
396-
/// Minimum stake required to be reserved to be a validator is 1_000
396+
/// Minimum stake required to be reserved to be a collator is 1_000
397397
pub const MinCollatorStk: u128 = 1_000 * GLMR;
398398
/// Minimum stake required to be reserved to be a nominator is 5
399399
pub const MinNominatorStk: u128 = 5 * GLMR;

tools/staking-test-spec.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tools/test-staking.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ async function test() {
185185

186186
// Revoke Nomination
187187
const unsub3 = await polkadotApi.tx.parachainStaking
188-
.leaveNominators()
188+
.revokeNomination(GERALD) //TODO: when converting to test add .leaveNominators()
189+
// that should produce the same behavior
189190
.signAndSend(alith, ({ events = [], status }) => {
190191
console.log(`Current status is ${status.type}`);
191192

0 commit comments

Comments
 (0)