Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[Fix] CountedMap::set now takes Counter into account #13214

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
55 changes: 54 additions & 1 deletion frame/support/src/storage/types/counted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ where

/// Store or remove the value to be associated with `key` so that `get` returns the `query`.
pub fn set<KeyArg: EncodeLike<Key>>(key: KeyArg, q: QueryKind::Query) {
<Self as MapWrapper>::Map::set(key, q)
match QueryKind::from_query_to_optional_value(q) {
Copy link
Member

@ggwpez ggwpez Jan 23, 2023

Choose a reason for hiding this comment

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

We could add a migration which scans a CountedStorageMap and corrects the counter.
Would probably need to run something manual against the current chains to check if we do that.
PS: I dont see it being used in Substrate, but for downstream projects it could be relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could add a migration which scans a CountedStorageMap and corrects the counter.

@ggwpez should we just create a ticket to add a migration, gets this merged and have a follow-up?

Would probably need to run something manual against the current chains to check if we do that.

Not sure what this means, could you clarify?

Copy link
Member

@ggwpez ggwpez Jan 23, 2023

Choose a reason for hiding this comment

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

should we just create a ticket to add a migration, gets this merged and have a follow-up?

Yes sure.

Would probably need to run something manual against the current chains to check if we do that.

Not sure what this means, could you clarify?

AFAIK we dont have any reflection to iterate through all CountedStorageMaps in the runtime. It is also not exposed in the runtime metadata… so not sure how we would programmatically loop through all maps.
PS: Will ask in chat.

Some(v) => Self::insert(key, v),
None => Self::remove(key),
}
}

/// Swap the values of two keys.
Expand Down Expand Up @@ -745,6 +748,22 @@ mod test {

// Test initialize_counter.
assert_eq!(A::initialize_counter(), 2);

// Set non-existing.
A::set(30, 100);

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), 100);
assert_eq!(A::try_get(30), Ok(100));
assert_eq!(A::count(), 3);

// Set existing.
A::set(30, 101);

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), 101);
assert_eq!(A::try_get(30), Ok(101));
assert_eq!(A::count(), 3);
})
}

Expand Down Expand Up @@ -976,6 +995,40 @@ mod test {

// Test initialize_counter.
assert_eq!(B::initialize_counter(), 2);

// Set non-existing.
B::set(30, Some(100));

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), Some(100));
assert_eq!(B::try_get(30), Ok(100));
assert_eq!(B::count(), 3);

// Set existing.
B::set(30, Some(101));

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), Some(101));
assert_eq!(B::try_get(30), Ok(101));
assert_eq!(B::count(), 3);

// Unset existing.
B::set(30, None);

assert_eq!(B::contains_key(30), false);
assert_eq!(B::get(30), None);
assert_eq!(B::try_get(30), Err(()));

assert_eq!(B::count(), 2);

// Unset non-existing.
B::set(31, None);

assert_eq!(B::contains_key(31), false);
assert_eq!(B::get(31), None);
assert_eq!(B::try_get(31), Err(()));

assert_eq!(B::count(), 2);
})
}

Expand Down
42 changes: 42 additions & 0 deletions frame/support/src/storage/types/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,48 @@ mod test {
assert_eq!(AValueQueryWithAnOnEmpty::take(2), 97);
assert_eq!(A::contains_key(2), false);

// Set non-existing.
B::set(30, 100);

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), 100);
assert_eq!(B::try_get(30), Ok(100));

// Set existing.
B::set(30, 101);

assert_eq!(B::contains_key(30), true);
assert_eq!(B::get(30), 101);
assert_eq!(B::try_get(30), Ok(101));

// Set non-existing.
A::set(30, Some(100));

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), Some(100));
assert_eq!(A::try_get(30), Ok(100));

// Set existing.
A::set(30, Some(101));

assert_eq!(A::contains_key(30), true);
assert_eq!(A::get(30), Some(101));
assert_eq!(A::try_get(30), Ok(101));

// Unset existing.
A::set(30, None);

assert_eq!(A::contains_key(30), false);
assert_eq!(A::get(30), None);
assert_eq!(A::try_get(30), Err(()));

// Unset non-existing.
A::set(31, None);

assert_eq!(A::contains_key(31), false);
assert_eq!(A::get(31), None);
assert_eq!(A::try_get(31), Err(()));

B::insert(2, 10);
assert_eq!(A::migrate_key::<Blake2_256, _>(2), Some(10));
assert_eq!(A::contains_key(2), true);
Expand Down