Skip to content
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

Add ReentrancyGuard status getter #3714

Merged
merged 15 commits into from
Oct 17, 2022
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* `ReentrancyGuard`: Add a `_reentrancyGuardEntered` function to expose the guard status. ([#3714](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3714))

## Unreleased

* `TimelockController`: Added a new `admin` constructor parameter that is assigned the admin role instead of the deployer account. ([#3722](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3722))
Expand Down
8 changes: 8 additions & 0 deletions contracts/mocks/ReentrancyMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,12 @@ contract ReentrancyMock is ReentrancyGuard {
function _count() private {
counter += 1;
}

function guardedCheckEntered() public nonReentrant {
require(_reentrancyGuardEntered());
}

function unguardedCheckNotEntered() public view {
require(!_reentrancyGuardEntered());
}
}
8 changes: 8 additions & 0 deletions contracts/security/ReentrancyGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ abstract contract ReentrancyGuard {
// https://eips.ethereum.org/EIPS/eip-2200)
_status = _NOT_ENTERED;
}

/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
8 changes: 8 additions & 0 deletions test/security/ReentrancyGuard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ contract('ReentrancyGuard', function (accounts) {
this.reentrancyMock.countAndCall(attacker.address), 'ReentrancyAttack: failed call');
});

it('_reentrancyGuardEntered should be true when guarded', async function () {
await this.reentrancyMock.guardedCheckEntered();
});

it('_reentrancyGuardEntered should be false when unguarded', async function () {
await this.reentrancyMock.unguardedCheckNotEntered();
});

// The following are more side-effects than intended behavior:
// I put them here as documentation, and to monitor any changes
// in the side-effects.
Expand Down