Fix ReentrancyGuard for Proxy Pattern#2171
Conversation
frangio
left a comment
There was a problem hiding this comment.
Thanks for this suggestion!
First of all we want to make clear that this library is not intended for use behind proxies, because it has constructors and we make use of storage in ways that don't work with proxies, and also because we make changes that can result in storage incompatibilities and corrupted upgrades. For use with proxies and upgrades, we maintain the separate @openzeppelin/contracts-ethereum-package where we do take all of these precautions.
That said, this PR is interesting because of the improvement in gas costs. I ran some tests and indeed this implementation is significantly cheaper (by ~1800 gas). As you mentioned, the improvement comes from using uint256 instead of bool. Not only because the value isn't masked when read from storage, but also because when booleans are written to storage the entire slot is first read from storage again in order to modify only the relevant bytes. 🤯
|
It's a sad state of affairs that using the right tools provided by the language, |
|
@BrendanChou Let us know if you want to work on this or if we should take over. |
|
@frangio go ahead and take it over. I'm less familiar with the standards you all have for code quality and linting |
|
Hi all! |
|
Not stale, we'll pick this up soon. |
Allows the ReentrancyGuard to be used in contracts that are proxied. Fixes two problems:
In both cases, the "corrupted" value of the
_notEnteredslot (zero in the case of new contracts, non-zero in the case of existing contracts) is now fixed in the first call to anynonReentrantfunction, making it backwards-compatible with any previous version ofReentrancyGuardAnother added benefit is that this should decrease the gas costs of this function as
uint256values use the entire value of the slot and thus do not have to be checked by bytecode that the storage slot is returning a valid boolean value upon eachSLOADorSSTOREI invite anyone to take-on and clean-up this PR as necessary