question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

require not working in view/pure functions

See original GitHub issue

Many functions in openzepplin contracts are using require statement in view function but it doesn’t work as expected.

📝 Details require in view function does not actually perform any role for now. It is being used in many openzepplin contract. For example: https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L62 https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L74 https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol#L106

It doesn’t stop execution even if require condition is false and return the output. In case address is return data type, it returns zero address and in case uint256 is return type, it returns 3963877391197344453575983046348115674221700746820753546331534351508065746944 whose hex value is 8C379A000000000000000000000000000000000000000000000000000000000 which is the function selector for function Error(string).

Also this works fine with remix-vm and truffle test vm but shows above behaviour with each Ethereum testnet.

I think we need to add a note to inform the users about it in function developer comments or do what is discussed.

Related references: https://github.com/ethereum/web3.js/issues/1903#issuecomment-467191865 https://github.com/ethereum/go-ethereum/issues/19027#issuecomment-513819354 https://github.com/ethereum/solidity/issues/3971#issuecomment-410839984 https://github.com/ethereum/solidity/issues/4840

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
frangiocommented, Sep 9, 2019

Thanks for bringing this up @Aniket-Engg! It’s an interesting subtlety. Unfortunately since this affects basically everything, I’m not sure where we would place the notice in our documentation. I guess we can consider that this has been tackled by @abcoathup’s thorough forum post and close it.

2reactions
abcoathupcommented, Aug 21, 2019

Hi @Aniket-Engg

Thanks for raising this. I have created a topic in the forum and made it a wiki post (so that anyone can edit it): https://forum.openzeppelin.com/t/require-in-view-pure-functions-dont-revert-on-public-networks/1211

Read more comments on GitHub >

github_iconTop Results From Across the Web

Strange results with require() in public view/pure-type functions
So, I just tested the original problem with assert instead of revert, and as expected the result is exactly the same: the client...
Read more >
`require` in view/pure functions don't revert on public networks
There is known issue that require in view/pure functions don't revert on public networks. See the following open issues:.
Read more >
require() function in JavaScript not working [duplicate]
My doubt is the require() function is running fine on js file but not in HTML file. It also not working when I...
Read more >
Does it cost gas if a non view/pure function does not alter the ...
You can implement an empty function with no pure or view on it. Executing it in a transaction will still cost gas, and...
Read more >
the modifier "view/pure" does not apply when calling internal ...
the modifier "view/pure" does not apply when calling internal functions. Yes this could lead to some additional gas.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found