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

Added message string for require() #1704

Merged

Conversation

balajipachai
Copy link
Contributor

Enhancement #
Added message string for require statements.

Many a time it happens, a transaction reverts with the message
Error: VM Exception while processing transaction: revert, for reasons such as trying to access a variable that is not in existence, trying to call a contract's function where the contract's address has been address(0) and not yet initialized etc.

The ways to find the reason for revert may include:

  • Adding events in the smart contract which emit the variable's value, catching them using web3js, finally logging them on the console to check the variable's value, which is time consuming and inefficient.

After the addition of message string for require statements, one can easily find the point of revocation and work on fixing the same.

The below approach has been used for adding message string

  • If there is only 1 require statement in a function then the message string is:
    "from OpenZeppelin's:FileName.sol:functionName()."

  • If there are multiple require statement in a function then the message string is:
    "from OpenZeppelin's:FileName.sol:functionName. Cause of the revert."

@nventuro
Copy link
Contributor

nventuro commented Apr 2, 2019

Hey there @balajipachai! We have an outstanding issue open for this (#888), which we plan on tackling during these couple of days. I'm currently doing some testing regarding gas costs, user experience, etc., and trying to figure out what the best approach is for the messages themselves.

I think we can safely skip the 'from OpenZeppelin' bit, and just include the contract name: I'd expect that to be enough to pinpoint the offending contract. Your suggestion to only include the function name is intriguing, since it'd make it easy to look into the code and find the root cause of the revert. That said, I think I favor having a more descriptive message (e.g. null transfer recipient, closing date in the past, etc.), so that, at least in some cases, we can skip the going back to the library source code. We'd need to take care not to repeat messages inside a contract, but I think that's not a tall order. @frangio tagging you so you can share your thoughts regarding this.

@balajipachai
Copy link
Contributor Author

@nventuro
Even though adding just the functionName() might be intriguing, however, since it is the case for functions with only 1 require statement, I thought of the aforesaid approach, and, yes, we can include descriptive text highlighting the cause of the error, this too can be accommodated.
Should I remove the from OpenZeppeling's from the message string?

@nventuro
Copy link
Contributor

nventuro commented Apr 2, 2019

To prevent work duplication, I'd rather we settle on a format first, so that everyone can then later contribute by adding these reason strings and associated tests (which are quite a lot). Please, come join the discussion on our forum and share your thoughts!

@nventuro
Copy link
Contributor

nventuro commented Apr 5, 2019

@balajipachai I opened #1709 with some guidelines regarding message format, among other things, including some examples. Could you review it and update your PR accordingly? Thanks!

@balajipachai
Copy link
Contributor Author

@balajipachai I opened #1709 with some guidelines regarding message format, among other things, including some examples. Could you review it and update your PR accordingly? Thanks!

@nventuro Sure, let me have a look at those guidelines and I'll update my PR accordingly.

@balajipachai
Copy link
Contributor Author

@balajipachai I opened #1709 with some guidelines regarding message format, among other things, including some examples. Could you review it and update your PR accordingly? Thanks!

@nventuro Sure, let me have a look at those guidelines and I'll update my PR accordingly.

@nventuro Updated the PR as per #1709, please have a look and let me know in case of any changes.

@balajipachai
Copy link
Contributor Author

@nventuro Awaiting your reply, once you give a heads up, I can start with the changes in the test cases too for shouldFail.reverting.withMessage.
Thanks!!!

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.

Hello @balajipachai, thank you for your contribution! I left a couple comments to improve some of the messages :) I also noticed some of them are empty, did you not know what to put in those?

I may do a couple myself to serve as a guideline, since I fear the forum post may not be enough.

contracts/access/roles/WhitelistedRole.sol Outdated Show resolved Hide resolved
contracts/crowdsale/Crowdsale.sol Outdated Show resolved Hide resolved
contracts/crowdsale/distribution/FinalizableCrowdsale.sol Outdated Show resolved Hide resolved
contracts/crowdsale/distribution/FinalizableCrowdsale.sol Outdated Show resolved Hide resolved
contracts/crowdsale/validation/TimedCrowdsale.sol Outdated Show resolved Hide resolved
@balajipachai
Copy link
Contributor Author

Hello @balajipachai, thank you for your contribution! I left a couple comments to improve some of the messages :) I also noticed some of them are empty, did you not know what to put in those?

I may do a couple myself to serve as a guideline, since I fear the forum post may not be enough.

I didn't notice what was left empty, my bad, however, as per your guideline, I have started to make changes and will update the PR shortly.
Thanks!!!! for your feedback, appreciated :)

@balajipachai
Copy link
Contributor Author

@nventuro The PR has been updated as per below guidelines:

  • The require messages now describe what went wrong as opposed to the requirement.

  • Symbols have been removed and instead, words are used.

Note
If there is require(to != address(0)) in _burn() as well as in _transferFrom() in ERC20.sol
Below convention is used:
require(to != address(0), 'ERC20: to is address(0) in _burn()')
require(to != address(0), 'ERC20: to is address(0) in _transferFrom()')
This will help us to verify, exactly, which to != address(0) caused the revert.
The same thing has been applied to all such similar cases.

@nventuro
Copy link
Contributor

Hello @balajipachai, thank you very much for your improvements! I did some changes to the revert strings, generally either making them more terse, or changing implementation details for the actual reason for failure.

Could you add these strings to the test suite, to assert the actual failure reason? Thanks!

@balajipachai
Copy link
Contributor Author

Hello @balajipachai, thank you very much for your improvements! I did some changes to the revert strings, generally either making them more terse, or changing implementation details for the actual reason for failure.

Could you add these strings to the test suite, to assert the actual failure reason? Thanks!

Sure @nventuro , I'll add those changes and update !!!

@balajipachai
Copy link
Contributor Author

@nventuro I have started working on the shouldFail.reverting.withMessage, however, I have noticed that :
In shouldFail.js: shouldFail.reverting.withMessage = withMessage;
The above statement in turn calls the shouldFailWithMessage(promise, message)
if (1 in matches && semver.satisfies(matches[1], '>=2.2.0'))
and the above if condition fails since
const matches = /TestRPC\/v([0-9.]+)\/ethereum-js/.exec(nodeInfo); matches == null

In order to be sure about shouldFail.reverting.withMessage is working as expected I did a way around for testing purpose only and modified the below line:
shouldFail.reverting.withMessage = withMessage;
to
shouldFail.reverting.withMessage = shouldFailWithMessage;
And the results were as expected. The test cases are passing, however, this is not the right way.
Guide me on getting things done via
shouldFail.reverting.withMessage = withMessage;
Thanks !!!

@nventuro
Copy link
Contributor

@balajipachai That's weird, you shouldn't be having any issues with that if statement. It is there to check if the ganache version that is being used supports revert reasons (introduced in ganache-core after v2.2.0), and our package.json specifies ganache-cli v6.4.1, which bundles ganache-core v2.5.3.

If you have an older version installed, you could delete node_modules and rerun npm install. Try also running $ npx ganache-cli --version and see what the versions are for your setup. I tested locally and got the helper working just fine.

@nventuro
Copy link
Contributor

@balajipachai I've now reviewed and updated all reason strings.

@balajipachai
Copy link
Contributor Author

@nventuro Thanks!!! Test cases updation is in progress 👍

@nventuro nventuro requested a review from frangio April 17, 2019 19:34
@nventuro
Copy link
Contributor

@frangio could you take a look at the reason strings?

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.

Great work @balajipachai, this must have taken quite a while to get done! I left a couple comments, but most are very minor, we should be able to merge this very soon!

test/behaviors/access/roles/PublicRole.behavior.js Outdated Show resolved Hide resolved
test/crowdsale/IncreasingPriceCrowdsale.test.js Outdated Show resolved Hide resolved
test/crowdsale/IncreasingPriceCrowdsale.test.js Outdated Show resolved Hide resolved
test/crowdsale/MintedCrowdsale.test.js Outdated Show resolved Hide resolved
test/crowdsale/TimedCrowdsale.test.js Outdated Show resolved Hide resolved
test/token/ERC20/TokenTimelock.test.js Show resolved Hide resolved
test/token/ERC721/ERC721.behavior.js Show resolved Hide resolved
test/token/ERC721/ERC721.behavior.js Show resolved Hide resolved
test/token/ERC721/ERC721.behavior.js Outdated Show resolved Hide resolved
Copy link
Contributor

@frangio frangio left a comment

Choose a reason for hiding this comment

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

Awesome work everyone!

contracts/token/ERC20/SafeERC20.sol Outdated Show resolved Hide resolved
contracts/token/ERC721/ERC721Metadata.sol Outdated Show resolved Hide resolved
contracts/token/ERC721/ERC721.sol Outdated Show resolved Hide resolved
contracts/token/ERC721/ERC721.sol Outdated Show resolved Hide resolved
@balajipachai
Copy link
Contributor Author

@nventuro & @frangio I have gone through both of your comments, Thank You both for your feedback.
Please bare me some time, to accommodate the required changes.

@balajipachai
Copy link
Contributor Author

Great work @balajipachai, this must have taken quite a while to get done! I left a couple comments, but most are very minor, we should be able to merge this very soon!

Thank You @nventuro

@nventuro
Copy link
Contributor

@balajipachai sorry we forgot to mention this, but we did some of the changes ourselves in the interest of getting this merged for the release, and to relieve some of the load from you, who has already done so much :)

Thanks a lot everyone!

@nventuro nventuro merged commit 3682c65 into OpenZeppelin:master Apr 24, 2019
@balajipachai
Copy link
Contributor Author

@nventuro It's been more than couple of days, yet, I don't see my name listed amongst the Contributors.
😅😅😅😅

@nventuro
Copy link
Contributor

Where are you looking for that? I do see you on the list:

image

@balajipachai
Copy link
Contributor Author

I am looking at this link:
https://github.com/OpenZeppelin/openzeppelin-solidity/graphs/contributors

Could you share the link where my name is listed?

@nventuro
Copy link
Contributor

That very same link, on number #56 when sorting by commits (PRs) made.

@balajipachai
Copy link
Contributor Author

That very same link, on number #56 when sorting by commits (PRs) made.

Thank You 👍

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.

4 participants