reentrancy mutex gas optimization (#1155)

* reentrancy mutex gas optimization

* 1) uint => uint256 2) ++ to += 1
This commit is contained in:
yaronvel
2018-08-06 21:22:08 +03:00
committed by Nicolás Venturo
parent bf34911857
commit 31ac59b224

View File

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