Commit 31ac59b2 by yaronvel Committed by Nicolás Venturo

reentrancy mutex gas optimization (#1155)

* reentrancy mutex gas optimization

* 1) uint => uint256 2) ++ to += 1
parent bf349118
...@@ -9,17 +9,8 @@ pragma solidity ^0.4.24; ...@@ -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 { ...@@ -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);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment