-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Remove Builtin Default Over/Underflow Reverts #10695
Comments
I would be interested to know whether the compiler can optimize the boundary checks for that for-loop case, but I suspect there are numerous other deducible cases that will have redundant checks now. Another case I suspect could not be optimized but could be deduced would be a finite geometric sum. |
If that's the case you coded it wrong. That's what If you don't want to use the built-in checks at all, you can always simply
and add your old SafeMath code |
@wjmelements The Yul optimizer is expected to catch the simple and general loop cases once SolYul is live.
Such cases are edge cases (as in not as widely used as the mentioned loops) and can obviously be handled with |
Built-in underflow and overflow checks were just introduced and from the feedback we got they were very well received by the community, so I don't think it's reasonable to just get rid of it right now. |
@mudgen That said, if the issue is mostly about the loop post part, could you please change the issue title? |
@wjmelements Can you give an example of what you are talking about? |
This comment has been minimized.
This comment has been minimized.
It's not mostly about the loop post part. |
In the issue I mentioned the possibility of locking funds in a contract forever because of reverting on under/overflow. Here's an actual, real example of that: The GHST Staking Diamond has millions USD worth of tokens staked in it which earn "frens" points. The stake withdraw functions recalculate frens points. Reverting on over/underflow of calculation of frens locks people's staked money forever. Code is here: https://github.com/aavegotchi/ghst-staking/blob/master/contracts/facets/StakingFacet.sol |
Then I would suggest that the issue should be closed. The feature has been discussed at length for years and has just been added, so I don't think it makes any sense to simply remove it entirely as you suggest. |
@leonardoalt Okay, where is the best public place to give my feedback on Solidity language features? Maybe I posted in the wrong place. |
Sure, I know other contracts that also deal with a lot of money that also rely on underflow and overflow. Those were not written with Solidity 0.8 and built-in revert in mind. If they were, they would use |
This issue reads more like a feature request and not feedback. What exactly is your goal with this issue? |
The contract does not rely on overflows or underflows, that would be a bug. In this case reverting is the wrong action to handle overflow/underflow, resulting in locked funds. |
What I said is still valid. It was written in Solidity 0.7. If it was written in 0.8, it wouldn't be locked because you would have used |
Yes, correct. |
To give feedback and to influence change to hopefully move in the direction of removing or limiting default reverts on overflow/underflow. |
You mean rolling back to old versions and effective apply |
Can there be a |
I hope not 😄 Seriously now, does anyone have any actual data on the gas differences? Or is it still all hypothetical? |
I think this entire problem will diminish significantly in the future, as soon as our latest optimizer work is done. In general, the change in default behaviour, i.e. making checked arithmetic the default and moving unchecked arithmetic to a situation like What is definitely unfortunate, is that it may take a while until this kind of optimization will be available to production code (it will only happen after we switch to Sol->Yul code generation by default). Still, my main question at this point would be: would the ability of the optimizer to remove most or all redundant checks introduced by checked-by-default-arithmetic make a significant difference in your view on the matter? |
@ekpyron Thanks for this good analysis. Much more often then not it is a good thing that a transaction reverts if there is an overflow or underflow, so in this way it makes sense for it to be the default. I'm warming up to the idea and need to get some experience.
This is definitely the ideal and improves my view on the matter for sure. I think more experience with default revert on over/underflow will shift my view on the matter one way or the other and make me more certain. |
Solidity 0.8.0 added built-in, by default, reverts when there is an overflow or underflow in an operation on integers.
I prefer the explicitness of SafeMath, or my own require statements. Over/underflow reverts by default are not a solve-all for the problem. In some cases revert on over/underflow is the wrong action, for example when it locks funds in a contract forever.
I don't want to use
unchecked
. I want to use SafeMath (or equivalent or better library) or my own require statements. This helps ensure that I look and think about each possible over/underflow case and not shotgun use the default functionality and assume it is the right thing to do in every case.In addition, @wjmelements pointed out it is nice to be able to emit a user-friendly message via require specifying what exactly was under/overflowing.
gas
The majority of Solidity for loops increment a uint256 variable that starts at 0. These increment operations never need to be checked for over/underflow because the variable will never reach the max number of uint256 (will run out of gas long before that happens). The default over/underflow check wastes gas in every iteration of virtually every for loop I would ever write. It can be done unchecked in this awkward way:
Or possibly an inlined function can be used:
But I would prefer to write my unchecked for loops (which are all of them) in the usual way like this:
Things possibly could be done to improve the syntax of unchecked for loops. That's great. But I would just prefer to do without default over/underflow reverts, so I handle each possible over/underflow individually, explicitly and correctly.
The text was updated successfully, but these errors were encountered: