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

Deserialize boundedbtreeset #781

Merged
merged 4 commits into from
Aug 31, 2023
Merged

Deserialize boundedbtreeset #781

merged 4 commits into from
Aug 31, 2023

Conversation

ozgunozerk
Copy link
Contributor

I followed BoundedVec's Deserialize implementation.

Tests are passing 🥳

@bkchr bkchr requested a review from KiChjang August 30, 2023 19:44

while let Some(value) = seq.next_element()? {
values.insert(value);
if values.len() > max {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is weird. You insert before checking for the max, i.e. you add 1 more value over the limit before you check the limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also found it weird. It was like that in BoundedVec's Deserialize implementation: https://github.com/paritytech/parity-common/blob/master/bounded-collections/src/bounded_vec.rs#L90-L92

I thought that was intentional and didn't put much thought tbh.

I can fix them both if you like?

Copy link
Contributor

@KiChjang KiChjang Aug 31, 2023

Choose a reason for hiding this comment

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

Sure, but there's one more additional issue: checking values.len() > max doesn't sound right to me either, because it too is also waiting for 1 element over the max before the condition triggers. What should really happen is values.len() >= max.

It would be so great if you can write tests for these as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we are already discussing the implementation of Deserialize for BoundedVec as well, I had another thing which confused me:

https://github.com/paritytech/parity-common/blob/master/bounded-collections/src/bounded_vec.rs#L84-L85

So, if we are performing a check in here for the size, do we need another check in here:
https://github.com/paritytech/parity-common/blob/master/bounded-collections/src/bounded_vec.rs#L90-L92
?

I think the second check is not necessary and the error produced by the second check is unreachable. So, I don't know if it's possible to write a test for triggering that error. It will trigger the previous error instead.

I might be missing something, so feel free to correct me

Copy link
Member

Choose a reason for hiding this comment

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

The first check is using size_hint() and that could also be None in which case the size is 0 as you see in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a new commit:

  • insertion is now performed after the bound check
  • new tests for bound check:
    • i've copied the deserializer_test and made the size equal to the bound
    • for the tests, size_hint() does indeed return None, so I'm positive I'm testing the inner check
    • if you prefer not to have an additional test, the same test can be done by changing the bound to the size of the deserialized string in the deserializer_test

@bkchr bkchr merged commit a8a85a4 into paritytech:master Aug 31, 2023
4 of 6 checks passed
@ozgunozerk ozgunozerk deleted the deserialize_boundedbtreeset branch August 31, 2023 18:30
@ordian ordian added the changelog Needs to be added to the changelog label Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog Needs to be added to the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants