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

why use asserts in SafeMath to take all the gas? #565

Closed
rstormsf opened this issue Nov 17, 2017 · 5 comments
Closed

why use asserts in SafeMath to take all the gas? #565

rstormsf opened this issue Nov 17, 2017 · 5 comments

Comments

@rstormsf
Copy link
Contributor

rstormsf commented Nov 17, 2017

@shrugs
Copy link
Contributor

shrugs commented Nov 17, 2017

assert is more of an invariant label; during runtime, execution should never reach the point where an assert fails. At the moment, this is more of a "recommendation", but in the future it will ideally be fully verifiable.

Assuming that overflows aren't a desirable feature, any time that your int overflows, you can assume that something has gone very wrong (possibly via some sort of malformed/malicious input), so bailing hard and consuming all gas is pretty reasonable.

So I suppose the advice would be to write your contract such that those asserts are never reached, which means using require to screen the input to your calculations. Basically, normal uses of your contract should never hit assert and won't be bitten by the gas consumption.


that said, I totally see why it would be nice to use require, just from a "good citizen" perspective; it might be worth a separate SafeMath library that uses require, but changing the currently used one is probably not reasonable or doable.

@frangio
Copy link
Contributor

frangio commented Nov 17, 2017

The rationale was exactly what @shrugs said, and I'd like to highlight the spot on recommendation:

Write your contract such that those asserts are never reached

Here's a good article explaining all of the differences between assert and require: https://media.consensys.net/when-to-use-revert-assert-and-require-in-solidity-61fb2c0e5a57.

@frangio frangio closed this as completed Nov 17, 2017
@rstormsf
Copy link
Contributor Author

rstormsf commented Nov 17, 2017

Completely disagree here, because safeMath is used widely in some constant functions which are used in some state changing functions which can result in losing all gas not because someone is trying to game the system because of the unpredictability of the state in a contract.

Yes, I think it's better to be a good citizen then a bully to take all up the gas. IMHO, no reason to reward miners for user's errors.
@frangio @shrugs

@frangio
Copy link
Contributor

frangio commented Nov 17, 2017

I think I see your point @rstormsf. I appreciate the discussion.

Can you give some examples of those constant functions? And how it can result in losing all gas.

@rstormsf
Copy link
Contributor Author

My apologies, I was thinking about something else.
You are 100% right about it. I'm sorry.
@frangio

thangphuocnguyen added a commit to thangphuocnguyen/poc-blockchain that referenced this issue Apr 2, 2019
+ Prefix all parameters with underscore
+ Add gas optimization for safeMul
+ Consider to change `assert` to `require` since require make more gas effiency, but still keeping this by follow the rationale from here (OpenZeppelin/openzeppelin-contracts#565)
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

No branches or pull requests

3 participants