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

Misuse of BigNumber instances in tests #204

Closed
3 tasks
frangio opened this issue Apr 21, 2017 · 9 comments
Closed
3 tasks

Misuse of BigNumber instances in tests #204

frangio opened this issue Apr 21, 2017 · 9 comments
Assignees
Labels
bug good first issue Low hanging fruit for new contributors to get involved! tests Test suite and helpers.

Comments

@frangio
Copy link
Contributor

frangio commented Apr 21, 2017

Most of the values obtained interacting with web3 are bignums, for example the web3.eth.getBalance return value. In most of the assertions throughout the tests we compare them with JavaScript numbers. This coerces them into numbers and possibly introduces numerical errors. (Because JavaScript's native numbers are floating point, and e.g. 1000000000000000000 is indistinguishable from 1000000000000000001.)

I find it unlikely that this is causing false positives, but it should be fixed nevertheless. The numbers we work with in Ethereum can be pretty big and prone to this kind of error. For example the number shown in the previous paragraph is equivalent to 1 ether.

Apart from that, we should probably establish some convention to avoid this problem in the future, and document it in the contribution guidelines. I think it might be good to wrap assert.equal, etc., to treat bignums specially.

  • Establish how to deal with this. Possibly defining some helper functions.
  • Find all coercions to number (implicit or explicit) and fix them.
  • Add a section in CONTRIBUTING.md.
@maraoz
Copy link
Contributor

maraoz commented Apr 24, 2017

Great idea @frangio, thanks!

frangio added a commit to frangio/openzeppelin-contracts that referenced this issue May 8, 2017
@frangio
Copy link
Contributor Author

frangio commented May 8, 2017

I've fixed some of these assertions but we should check the rest of the tests in case I missed other errors.

@misteraverin has pointed out that the issue is not only numerical error but also flat out wrong comparisons when two BigNumber objects are compared.

@misteraverin
Copy link
Contributor

@frangio Nice fix. But it can be confusing for somebody to see that in one place you compare it as BigNumbers and in another as Strings (equal case).

@frangio
Copy link
Contributor Author

frangio commented May 8, 2017

The thing with that is that assertion error messages are more useful when they show the expected and actual values. Using assert.equal with strings provides that benefit. The same goes for "greater than" tests, actually...

I pushed my quick fix to get this going, but like I said before we should probably write an assert wrapper to take care of it automatically. I had a quick look and didn't find anything that we could just import and use in this regard. I won't have time to implement it myself for a few days.

@frangio frangio changed the title Possibility of numerical error in tests Misuse of BigNumber instances in tests May 9, 2017
frangio added a commit that referenced this issue Jun 30, 2017
includes fix to MultisigWallet test because bigger balances result in
floating point errors (see #204)
@frangio
Copy link
Contributor Author

frangio commented Jul 5, 2017

We're incorporating chai into our tests. For now only crowdsale and TokenTimelock tests are written in this way. The rest will have to be gradually ported.

This issue will be addressed by using chai-bignumber as seen in the Crowdsale tests.

@frangio frangio removed the chore label Aug 7, 2017
ProphetDaniel pushed a commit to classicdelta/Smart-Contracts that referenced this issue Mar 9, 2018
includes fix to MultisigWallet test because bigger balances result in
floating point errors (see OpenZeppelin#204)
@frangio frangio removed the backlog label Jun 15, 2018
@frangio frangio removed the next label Jul 20, 2018
@frangio frangio added the tests Test suite and helpers. label Aug 10, 2018
@nventuro nventuro added the good first issue Low hanging fruit for new contributors to get involved! label Aug 11, 2018
@nventuro nventuro assigned nventuro and unassigned shrugs Aug 11, 2018
@nventuro nventuro added this to the v2.1 milestone Aug 13, 2018
@Aniket-Engg
Copy link
Contributor

I think this has been fixed.

@frangio
Copy link
Contributor Author

frangio commented Dec 26, 2018

@Aniket-Engg Perhaps! If you've checked would you mind sharing what things you searched for?

I found a few more cases of this in ERC721Full.

https://github.com/OpenZeppelin/openzeppelin-solidity/blob/7361ffd26b6354499d0b68d6d2d9da1f8cb21c8b/test/token/ERC721/ERC721Full.test.js#L67

@Aniket-Engg
Copy link
Contributor

@frangio I haven't checked it specifically. I supposed that as per the last comment chai-bignumber has been introduced, so required things would have been implemented. I will check it now.

Aniket-Engg added a commit to Aniket-Engg/zeppelin-solidity that referenced this issue Jan 2, 2019
@Aniket-Engg
Copy link
Contributor

@frangio I have gone through almost each test file to introduce changes. Have a look to changelog and let me know if anything has left.

frangio pushed a commit that referenced this issue Jan 4, 2019
* signing prefix added

* Minor improvement

* Successfully tested

* Minor improvements

* Minor improvements

* Revert "Dangling commas are now required. (#1359)"

This reverts commit a688977.

* updates

* fixes #1404

* approve failing test

* suggested changes done

* ISafeERC20 removed

* conflict fixes

* added examples

* fixes #706

* linting

* fixes #204

* file fixing

* deep bignumber comparison removed

* Update SafeERC20Helper.sol

* Update IERC20.sol

* Update SafeERC20.sol

* Update package-lock.json

* Revert "deep bignumber comparison removed"

This reverts commit 230b272.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Low hanging fruit for new contributors to get involved! tests Test suite and helpers.
Projects
None yet
Development

No branches or pull requests

8 participants