Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dedup nomination targets #6307

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,21 +1296,26 @@ pub mod pallet {
Error::<T>::TooManyTargets
);

let mut targets = targets
.into_iter()
.map(|t| T::Lookup::lookup(t).map_err(DispatchError::from))
Copy link
Contributor Author

@AurevoirXavier AurevoirXavier Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use AccountId here? I'm not sure why we are using the Lookup type here.

.collect::<Result<Vec<_>, _>>()?;

targets.sort();
targets.dedup();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes only consecutive duplicates, do we enforce it is sorted somewhere?


let old = Nominators::<T>::get(stash).map_or_else(Vec::new, |x| x.targets.into_inner());

let targets: BoundedVec<_, _> = targets
.into_iter()
.map(|t| T::Lookup::lookup(t).map_err(DispatchError::from))
.map(|n| {
n.and_then(|n| {
if old.contains(&n) || !Validators::<T>::get(&n).blocked {
Ok(n)
} else {
Err(Error::<T>::BadTarget.into())
}
})
if old.contains(&n) || !Validators::<T>::get(&n).blocked {
Ok(n)
} else {
Err(Error::<T>::BadTarget.into())
}
})
.collect::<Result<Vec<_>, _>>()?
.collect::<Result<Vec<_>, DispatchError>>()?
.try_into()
.map_err(|_| Error::<T>::TooManyNominators)?;

Expand Down
25 changes: 25 additions & 0 deletions substrate/frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,31 @@ fn nominators_also_get_slashed_pro_rata() {
});
}

#[test]
fn nominate_same_account_should_be_filtered() {
ExtBuilder::default().build_and_execute(|| {
let duplicated_targets = vec![11, 21, 31, 11, 21, 31, 11, 11, 21, 21, 31, 31];

assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
assert_ok!(Staking::nominate(RuntimeOrigin::signed(101), duplicated_targets.clone()));
assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21, 31]);

// Make sure it can correct the wrong automatically.
Nominators::<Test>::insert(
101,
Nominations {
targets: BoundedVec::truncate_from(duplicated_targets.clone()),
submitted_in: Default::default(),
suppressed: Default::default(),
},
);
assert_eq!(Staking::nominators(101).unwrap().targets, duplicated_targets);

assert_ok!(Staking::nominate(RuntimeOrigin::signed(101), duplicated_targets));
assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21, 31]);
});
}

#[test]
fn double_staking_should_fail() {
// should test (in the same order):
Expand Down
Loading