Skip to content

Commit

Permalink
fix: Fix handling duplicate values in UniqueMultimap
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Sep 5, 2024
1 parent ccb1443 commit 9cce2bd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
3 changes: 3 additions & 0 deletions frame/multimap/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ fn unique_multimap_works() {

// overflow capacity per
assert!(matches!(Multimap::try_insert(0, 1002), Err(Error::<Test>::CapacityOverflow)));
assert!(matches!(Multimap::try_insert(0, 1001), Ok(false)));
assert_eq!(Multimap::get(0), BTreeSet::from_iter(vec![1000, 1001]));

// duplicate value
assert!(matches!(Multimap::try_insert(1, 1000), Err(Error::<Test>::DuplicateValue)));

Expand Down
5 changes: 4 additions & 1 deletion frame/multimap/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ impl<T: Config<I>, I: 'static> UniqueMultimap<T::Key, T::Value> for Pallet<T, I>

fn try_insert(key: T::Key, value: T::Value) -> Result<bool, Error<T, I>> {
Map::<T, I>::try_mutate(&key, |values| {
ensure!(!Index::<T, I>::contains_key(&value), Error::<T, I>::DuplicateValue);
ensure!(
Index::<T, I>::get(&value).filter(|k| *k != key).is_none(),
Error::<T, I>::DuplicateValue
);
values
.try_insert(value.clone())
.inspect(|ok| {
Expand Down

0 comments on commit 9cce2bd

Please sign in to comment.