-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Discussion: Should we remove the SafeMath library for Solidity 0.8? #2465
Comments
I think as long as all internal contracts do not use SafeMath, then keeping it around to ease the transition is probably OK. My biggest concerns was keeping it around and losing the gas savings if it was still embedded in the internal contracts. Another option is it could be broken out into a separate project - that way users have to explicitly add the package to remove errors, or the errors will encourage them to migrate fully to 0.8. |
An additional reason to keep it: Have custom revert message for safemath operation Solidity 0.8.0 will revert in case of overflow, but will not let you customize the revert reason. In some cases people may want to pass a reason message such as "ERC20: transfer amount exceeds balance". |
For the "Checked" math, which is default in 0.8.0 is not free. The compiler will add some overflow checks, somehow similar to those implemented by Lets consider this piece of code
For each element in the array, there are two math operations, one is
but then you lose the overflow protection on array, so its not good. This is why you may want to do
In that particular example, the extra cost of the function might make the whole Thus my 0.8.0
|
@Amxx that is actually a pretty interesting example. I think you've convinced me that it should be included. The user gets the most flexibility for optimizing this way. If a function has loops and various things, they could put the whole function contents inside |
One reason against it that it simply would lead to more confusion. On announcement, several people were not sure whether checked arithmetic in solidity can replace SafeMath libraries. Having it available in OpenZeppelin will likely exacerbate this feeling. If a wrapper is made part of it, please make it clear that using the library is not recommended. |
Agree. I think the documentation could provide similar examples to what @Amxx posted, where it shows the cases that it can be useful to still be used. |
Regarding gas, I'm curious whether anyone has actual data on how much more gas is spent? |
|
@Amxx can you share the total cost too (not just the savings)? |
I did not mean transaction total, rather the total cost of the operation (including stack manipulation). |
I'm totally with this sentiment. But the only concerns I had while reviewing the new semantics about the inbuilt checks in 0.8 were that I could no longer specify revert reasons. If I imagine
I understand how frustrating it would be to see 100s or 1000s of errors in a project, I don't think there is a point partially upgrading to Solidity 0.8. But I think dedicating some time for the effort to completely upgrade to 0.8 should be worth it. And one can always stay on older version of Solidity until they block some time to work on upgrade to 0.8 in their project. If |
I fully agree with @Amxx points and comments. Migrating to 0.8 doesn't mean just removing usage of balance = balance.sub(amount, "Insufficient balance"); would turn into require(amount >= balance, "Insufficient balance");
balance -= amount; but since checked arithmetic is no longer needed, require(amount >= balance, "Insufficient balance");
unchecked {
balance -= amount;
} I think the net effect is good: it allows for overall lower gas costs and highlights the actual error conditions. However, forcing this sort of migration on a user is too much of a burden. Keep in mind most codebases are not even on 0.7: if checked arithmetic is what convinced them to pull the trigger, then the migration is already non-trivial no begin with. And if people only upgrade once 0.9 comes with some other long-awaited feature (like the mythical Yul IR), this will only make moving forward harder. |
Taking more of the developer point of view, we should also considered there are years worth of documentation that scare developers away from doing math themselves without using SafeMath. The absence of SafeMath will be disorienting to those developers and might require a bit of an educational push to properly inform users as to why it's now safe to not use safe math. Perhaps it could be moved to a deprecated folder which will be self-explanatory to users. As a side note, I love the explicitness of safeMath. X.sub(y) is just a more pleasurable experience. Maybe we can keep it as syntactic sugar and build a transpiler...... (joking!) |
Twitter poll for some community sentiment: https://twitter.com/OpenZeppelin/status/1348768595350745088?s=20 |
Following this discussion, |
Thanks everyone for participating in the discussion! It was great to get everyone's perspectives and we're looking forward to more discussions like this one in the future. |
There has been some discussion in #2442 on whether to keep or remove
SafeMath
from the Solidity 0.8 release of the library. We don't seem to have consensus yet so I would like to continue the discussion and explore the arguments that are being made.The origin of the discussion is that Solidity 0.8 includes checked arithmetic operations by default, and this renders
SafeMath
unnecessary. What is not under discussion is that internally the library will use the built in checked arithmetic, rather than the current manual overflow checks inSafeMath
. The question is whether we want to keepSafeMath
for other reasons.Backwards compatibility
It may be better to keep
SafeMath
to ease the transition from Contracts 3.x (Solidity 0.6-0.7) to Contracts 4.x (Solidity 0.8). If we remove it, users who update will be flooded with error messages that they will have to fix before they do anything else. They may want to try other interesting things in Solidity 0.8, and/or removeSafeMath
progressively (for example making sure their tests continue to pass during the migration).@axic's counterargument:
It's true that using
SafeMath
function calls will be more expensive than just built in arithmetic. The idea would be that users should removeSafeMath
once they're using Solidity 0.8. But should we force people to do this migration as soon as they jump on Solidity 0.8? If there was a way to nudge them, such as a deprecation warning, I would agree that we should do it, but removingSafeMath
entirely is a lot stronger and causes what seems like unnecessary breakage.Use inside
unchecked
blocks@Amxx mentions that
SafeMath
would be a way to insert checked operations insideunchecked
blocks. This is an interesting idea. If we think this is a genuine use case it would not make sense to deprecateSafeMath
.Several people seem to have reacted against this proposal and it would be great to hear more from them. (@mlntr @cruzdanilo @hrkrshnn @stvndf)
Personally I think we should keep the wrapper for backwards compatibility only, deprecated and planned to be removed in the next major release. This is motivated by an intention to keep breaking changes to a minimum. Even if we do not remove it, we can communicate this deprecation clearly and recommend users to refactor their code to remove
SafeMath
once they migrate to 0.8.The text was updated successfully, but these errors were encountered: