Skip to content

Commit

Permalink
Refactor/reentrancy guard gas optimization #1056 (#1057)
Browse files Browse the repository at this point in the history
* optimizing ReentrancyGuard gas usage

* style fixed
  • Loading branch information
Eenae authored and nventuro committed Jul 28, 2018
1 parent 90b98a7 commit 1200969
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions contracts/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ pragma solidity ^0.4.24;

/**
* @title Helps contracts guard against reentrancy attacks.
* @author Remco Bloemen <remco@2π.com>
* @author Remco Bloemen <remco@2π.com>, Eenae <alexey@mixbytes.io>
* @dev If you mark a function `nonReentrant`, you should also
* mark it `external`.
*/
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.
*/
bool private reentrancyLock = false;
uint private reentrancyLock = REENTRANCY_GUARD_FREE;

/**
* @dev Prevents a contract from calling itself, directly or indirectly.
Expand All @@ -23,10 +30,10 @@ contract ReentrancyGuard {
* wrapper marked as `nonReentrant`.
*/
modifier nonReentrant() {
require(!reentrancyLock);
reentrancyLock = true;
require(reentrancyLock == REENTRANCY_GUARD_FREE);
reentrancyLock = REENTRANCY_GUARD_LOCKED;
_;
reentrancyLock = false;
reentrancyLock = REENTRANCY_GUARD_FREE;
}

}

0 comments on commit 1200969

Please sign in to comment.