Revert to old ReentrancyGuard implementation

This commit is contained in:
Amir Bandeali 2019-08-25 17:06:52 -07:00
parent 020e7609c3
commit 793e338dd3
2 changed files with 17 additions and 20 deletions

View File

@ -24,22 +24,26 @@ import "./LibRichErrors.sol";
contract ReentrancyGuard { contract ReentrancyGuard {
// Mutex counter. // Locked state of mutex.
// Starts at 1 and increases whenever a nonReentrant function is called. bool private locked = false;
uint256 private reentrancyGuardCounter = 1;
/// @dev Functions with this modifer cannot be reentered. /// @dev Functions with this modifer cannot be reentered. The mutex will be locked
/// before function execution and unlocked after.
modifier nonReentrant() { modifier nonReentrant() {
// Increment and remember the current counter value. // Ensure mutex is unlocked.
uint256 localCounter = ++reentrancyGuardCounter; if (locked) {
// Call the function.
_;
// If the counter value is different from what we remember, the function
// was called more than once and an illegal reentrancy occured.
if (localCounter != reentrancyGuardCounter) {
LibRichErrors.rrevert( LibRichErrors.rrevert(
LibReentrancyGuardRichErrors.IllegalReentrancyError() LibReentrancyGuardRichErrors.IllegalReentrancyError()
); );
} }
// Lock mutex before function call
locked = true;
// Perform function call
_;
// Unlock mutex after function call
locked = false;
} }
} }

View File

@ -24,8 +24,6 @@ import "../src/ReentrancyGuard.sol";
contract TestReentrancyGuard is contract TestReentrancyGuard is
ReentrancyGuard ReentrancyGuard
{ {
uint256 internal counter = 2;
/// @dev Exposes the nonReentrant modifier publicly. /// @dev Exposes the nonReentrant modifier publicly.
/// @param shouldBeAttacked True if the contract should be attacked. /// @param shouldBeAttacked True if the contract should be attacked.
/// @return True if successful. /// @return True if successful.
@ -47,19 +45,14 @@ contract TestReentrancyGuard is
external external
returns (bool) returns (bool)
{ {
if (counter > 0) { return this.guarded(true);
counter--;
this.guarded(true);
} else {
counter = 2;
return true;
}
} }
/// @dev This is a function that will not reenter the current contract. /// @dev This is a function that will not reenter the current contract.
/// @return True if successful. /// @return True if successful.
function nonExploitive() function nonExploitive()
external external
pure
returns (bool) returns (bool)
{ {
return true; return true;