Browse Source

reentrancy mutex gas optimization (#1155)

* reentrancy mutex gas optimization

* 1) uint => uint256 2) ++ to += 1
yaronvel 7 years ago
parent
commit
31ac59b224
1 changed files with 5 additions and 14 deletions
  1. 5 14
      contracts/ReentrancyGuard.sol

+ 5 - 14
contracts/ReentrancyGuard.sol

@@ -9,17 +9,8 @@ pragma solidity ^0.4.24;
  */
 contract ReentrancyGuard {
 
-  /// @dev Constant for unlocked guard state - non-zero to prevent extra gas costs.
-  /// See: https://github.com/OpenZeppelin/openzeppelin-solidity/issues/1056
-  uint private constant REENTRANCY_GUARD_FREE = 1;
-
-  /// @dev Constant for locked guard state
-  uint private constant REENTRANCY_GUARD_LOCKED = 2;
-
-  /**
-   * @dev We use a single lock for the whole contract.
-   */
-  uint private reentrancyLock = REENTRANCY_GUARD_FREE;
+  /// @dev counter to allow mutex lock with only one SSTORE operation
+  uint256 private guardCounter = 1;
 
   /**
    * @dev Prevents a contract from calling itself, directly or indirectly.
@@ -30,10 +21,10 @@ contract ReentrancyGuard {
    * wrapper marked as `nonReentrant`.
    */
   modifier nonReentrant() {
-    require(reentrancyLock == REENTRANCY_GUARD_FREE);
-    reentrancyLock = REENTRANCY_GUARD_LOCKED;
+    guardCounter += 1;
+    uint256 localCounter = guardCounter;
     _;
-    reentrancyLock = REENTRANCY_GUARD_FREE;
+    require(localCounter == guardCounter);
   }
 
 }