Skip to content

reentrancy mutex gas optimization #1155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions contracts/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

}