Skip to content

Commit

Permalink
Add ReentrancyGuard status getter (OpenZeppelin#3714)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
  • Loading branch information
3 people authored and JulissaDantes committed Nov 3, 2022
1 parent 0f91fc6 commit f1eef97
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
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

0 comments on commit f1eef97

Please sign in to comment.