-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
|
||
/// @notice Reentrancy guard mixin. | ||
/// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/ReentrancyGuard.sol) | ||
abstract contract ReentrancyGuard { | ||
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ | ||
/* STORAGE */ | ||
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ | ||
|
||
/// @dev Equivalent to: `uint72(bytes9(keccak256("_REENTRANCY_GUARD_SLOT")))`. | ||
/// Large enough to avoid collisions with lower slots, | ||
/// but not too large to prevent bytecode bloat. | ||
uint256 private constant _REENTRANCY_GUARD_SLOT = 0x929eee149b4bd21268; | ||
|
||
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ | ||
/* REENTRANCY GUARD */ | ||
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ | ||
|
||
modifier nonReentrant() virtual { | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
if eq(sload(_REENTRANCY_GUARD_SLOT), 2) { revert(codesize(), 0x00) } | ||
sstore(_REENTRANCY_GUARD_SLOT, 2) | ||
} | ||
_; | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
sstore(_REENTRANCY_GUARD_SLOT, 1) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.21; | ||
|
||
import "./utils/SoladyTest.sol"; | ||
import {ReentrancyGuard} from "../src/utils/ReentrancyGuard.sol"; | ||
|
||
contract ReentrancyGuardTest is SoladyTest, ReentrancyGuard { | ||
uint256 enterTimes; | ||
|
||
function testUnprotectedCall() public { | ||
this.unprotectedCall(0); | ||
assertEq(enterTimes, (1 + 0)); | ||
this.unprotectedCall(2); | ||
assertEq(enterTimes, (1 + 0) + (1 + 2)); | ||
} | ||
|
||
function testProtectedCall() public { | ||
this.protectedCall(0); | ||
assertEq(enterTimes, 1); | ||
} | ||
|
||
function testProtectedCallRevertsOnReentrancy() public { | ||
vm.expectRevert(); | ||
this.protectedCall(1); | ||
} | ||
|
||
function unprotectedCall(uint256 recurse) public { | ||
unchecked { | ||
enterTimes++; | ||
if (recurse == 0) return; | ||
this.unprotectedCall(recurse - 1); | ||
} | ||
} | ||
|
||
function protectedCall(uint256 recurse) public nonReentrant { | ||
unchecked { | ||
enterTimes++; | ||
if (recurse == 0) return; | ||
this.protectedCall(recurse - 1); | ||
} | ||
} | ||
} |