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

Uint and Time helpers #375

Merged
merged 8 commits into from
Aug 10, 2018
Merged

Uint and Time helpers #375

merged 8 commits into from
Aug 10, 2018

Conversation

bingen
Copy link
Contributor

@bingen bingen commented Jul 30, 2018

Closes #361
TODO:

  • Add tests for new functions

@bingen bingen requested review from izqui and sohkai July 30, 2018 11:25
@CLAassistant
Copy link

CLAassistant commented Jul 30, 2018

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented Jul 30, 2018

Coverage Status

Coverage increased (+0.02%) to 98.667% when pulling b36187a on uint64_helper into 69aafc1 on dev.

@bingen bingen changed the title Uint64 helpers [WIP] Uint64 helpers Jul 30, 2018
* Using a function rather than `block.number` allows us to easily mock the block number in
* tests.
*/
function getBlockNumber64() internal view returns (uint64) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'd be nice if we moved these time helpers into their own contract file, e.g. TimeHelpers.

pragma solidity ^0.4.18;


library Uint64Helpers {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd call this Uint256Helpers, since it's working on uint256s.

it('fails converting from uint256 to uint64 if too big', async () => {
const a = new web3.BigNumber(2).pow(64)
return assertRevert(async () => {
assert.equal((await uint64Mock.convert(a)).toString(), a, "Values should match")
Copy link
Contributor

Choose a reason for hiding this comment

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

Small nitpick: this can just be await uint64Mock.convert(a) rather than an assert() test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha, yes, problems of copy-pasting...

@bingen bingen changed the title [WIP] Uint64 helpers Uint and Time helpers Jul 30, 2018
@sohkai
Copy link
Contributor

sohkai commented Jul 30, 2018

Lacking coverage will be fixed with #372.

Copy link
Contributor

@sohkai sohkai left a comment

Choose a reason for hiding this comment

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

🌮 🎉

pragma solidity ^0.4.18;


contract TimeHelpers {
Copy link
Contributor

Choose a reason for hiding this comment

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

Last thing 😅, I'd move this to contracts/common since it's not imported / copied from somewhere else (it's similar to IsContract, which also lives there).

* tests.
*/
function getBlockNumber64() internal view returns (uint64) {
return uint64(block.number);
Copy link
Contributor

Choose a reason for hiding this comment

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

On these conversions, can we use the new Uint256Helpers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can, and I thought about it, but I considered unnecessary to import another library as I thought if block number or timestamp were ever bigger than 2^64 (~1.8e19, for time almost 6e11 years) this would be a minor problem. ;-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Despite being improbable, I think it's best to use the safe conversion. We use SafeMath when incrementing a lot of these time-based variables in the apps so we should apply the same rigour here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll change it, although I kind of disagree. I don't remember by heart all that occurrences, but I think they mostly involve "external" data (parameters, computed, etc), while here we are just converting "system" values (block.number and now) so I think it's pointless.

* tests.
*/
function getBlockNumber64() internal view returns (uint64) {
return block.number.toUint64();
Copy link
Contributor

Choose a reason for hiding this comment

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

If we do getBlockNumber().toUint64() it is easier to mock, as you only have to mock getBlockNumber() and the result of both getBlockNumber() and getBlockNumber64() will be the same.

* tests.
*/
function getTimestamp64() internal view returns (uint64) {
return now.toUint64();
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above.

@@ -0,0 +1,11 @@
pragma solidity ^0.4.18;
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this file be in the common directory as well?

Address PR 375 comments.
Copy link
Contributor

@izqui izqui left a comment

Choose a reason for hiding this comment

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

LGTM now. Just one extra test to ensure that the returned values are what we expect and ready to be merged

})

it('checks block number', async () => {
assert.equal((await timeHelpersMock.getBlockNumberExt.call()).toString(), (await timeHelpersMock.getBlockNumber64Ext.call()).toString(), "block number should match")
Copy link
Contributor

Choose a reason for hiding this comment

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

We should also test that the returned value is the actual timestamp or block number.

Address PR 375 comments.
@sohkai sohkai merged commit 898310f into dev Aug 10, 2018
@sohkai sohkai deleted the uint64_helper branch August 10, 2018 01:31
@cfl0ws cfl0ws added this to the sprint: 1.1 milestone Aug 28, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants