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

Unsigned conversion #2111 #2123

Merged
merged 14 commits into from
Mar 27, 2020
Merged

Unsigned conversion #2111 #2123

merged 14 commits into from
Mar 27, 2020

Conversation

pepelu
Copy link
Contributor

@pepelu pepelu commented Mar 13, 2020

Fixes #2111

Add signed to unsigned conversion to SafeCast and its corresponding tests.

Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

Thanks a lot for your contribution @pepelu! I left some minor comments on the code.

About the tests, I noticed you copied the approach from the tests for the other functions. I think we could go with something much simpler: have a single describe block for toUint256 and then test some simple cases, like:

  • 0
  • 1
  • -1
  • MAX_UINT256
  • MIN_INT256

What do you think?

contracts/utils/SafeCast.sol Outdated Show resolved Hide resolved
contracts/utils/SafeCast.sol Outdated Show resolved Hide resolved
Apply suggestions from code review.

Co-Authored-By: Nicolás Venturo <nicolas.venturo@gmail.com>
@pepelu
Copy link
Contributor Author

pepelu commented Mar 16, 2020

I completely agree with your updates. I'll look now into the tests.

@pepelu
Copy link
Contributor Author

pepelu commented Mar 16, 2020

I can also add the inverse (unsigned to signed). I wanted to do just one first to see if everything was ok.

Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

The new tests look great, thanks a lot @pepelu!

I'd say it makes sense to also have the inverse operation, yet. I'd suggest similar values for the tests:

  • 0
  • 1
  • MAX_INT256
  • MAX_INT256 + 1
  • MAX_UINT256

test/utils/SafeCast.test.js Outdated Show resolved Hide resolved
test/utils/SafeCast.test.js Outdated Show resolved Hide resolved
pepelu and others added 2 commits March 24, 2020 11:16
- Update error in conversion to be more clear
- Update constants in test to be powers of 2 instead of shifts
Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

Looks great @pepelu, thanks a lot! I added a changelog entry and left two small comments, once those are addressed we'll be ready to merge this!

test/utils/SafeCast.test.js Outdated Show resolved Hide resolved
test/utils/SafeCast.test.js Show resolved Hide resolved
- Add minus in INT256_MIN for clarity

Co-Authored-By: Nicolás Venturo <nicolas.venturo@gmail.com>
Copy link
Contributor

@nventuro nventuro left a comment

Choose a reason for hiding this comment

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

Thanks a lot for the hard work @pepelu!

@nventuro nventuro merged commit 4476a2d into OpenZeppelin:master Mar 27, 2020
@pepelu
Copy link
Contributor Author

pepelu commented Mar 27, 2020

Thanks for your guidance in my first PR @nventuro!

@pepelu pepelu deleted the unsigned-conversion-#2111 branch March 27, 2020 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add signed to unsigned conversion to SafeCast
2 participants