Skip to content

Commit b54387f

Browse files
committed
Rename ReentrancyGuard into TransientLock and ReentrancyGuradBase into ReentrancyGuard
1 parent bb7fbcd commit b54387f

File tree

3 files changed

+77
-49
lines changed

3 files changed

+77
-49
lines changed

contracts/libraries/ReentrancyGuard.sol

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,53 @@
22

33
pragma solidity ^0.8.0;
44

5-
import { TransientLib, tuint256 } from "./Transient.sol";
6-
7-
struct ReentrancyGuard {
8-
tuint256 _raw;
9-
}
10-
11-
library ReentrancyGuardLib {
12-
using TransientLib for tuint256;
5+
import { TransientLock, TransientLockLib } from "./TransientLock.sol";
6+
7+
/// @dev Base contract with reentrancy guard functionality using transient storage locks.
8+
///
9+
/// Use private _lock defined in this contract:
10+
/// ```solidity
11+
/// function swap(...) external nonReentrant {
12+
/// function doMagic(...) external onlyNonReentrantCall {
13+
/// ```
14+
///
15+
/// Or use your own locks for more granular control:
16+
/// ```solidity
17+
/// TransientLock private _myLock;
18+
/// function swap(...) external nonReentrantLock(_myLock) {
19+
/// function doMagic(...) external onlyNonReentrantCallLock(_myLock) {
20+
/// ```
21+
///
22+
abstract contract ReentrancyGuard {
23+
using TransientLockLib for TransientLock;
24+
25+
error MissingNonReentrantModifier();
26+
27+
TransientLock private _lock;
28+
29+
modifier nonReentrant {
30+
_lock.lock();
31+
_;
32+
_lock.unlock();
33+
}
1334

14-
error ReentrantCallDetected();
15-
error EnterLeaveDisbalance();
35+
modifier onlyNonReentrantCall {
36+
if (!_inNonReentrantCall()) revert MissingNonReentrantModifier();
37+
_;
38+
}
1639

17-
function enter(ReentrancyGuard storage self) internal {
18-
if (self._raw.inc() != 1) revert ReentrantCallDetected();
40+
modifier nonReentrantLock(TransientLock storage lock) {
41+
lock.lock();
42+
_;
43+
lock.unlock();
1944
}
2045

21-
function enterNoIncrement(ReentrancyGuard storage self) internal view {
22-
if (self._raw.tload() != 0) revert ReentrantCallDetected();
46+
modifier onlyNonReentrantCallLock(TransientLock storage lock) {
47+
if (!lock.isLocked()) revert MissingNonReentrantModifier();
48+
_;
2349
}
2450

25-
function leave(ReentrancyGuard storage self) internal {
26-
self._raw.dec(EnterLeaveDisbalance.selector);
51+
function _inNonReentrantCall() internal view returns (bool) {
52+
return _lock.isLocked();
2753
}
2854
}

contracts/libraries/ReentrancyGuardBase.sol

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.0;
4+
5+
import { TransientLib, tuint256 } from "./Transient.sol";
6+
7+
struct TransientLock {
8+
tuint256 _raw;
9+
}
10+
11+
library TransientLockLib {
12+
using TransientLib for tuint256;
13+
14+
uint256 constant private _UNLOCKED = 0;
15+
uint256 constant private _LOCKED = 1;
16+
17+
error UnexpectedLock();
18+
error UnexpectedUnlock();
19+
20+
function lock(TransientLock storage self) internal {
21+
if (self._raw.inc() != _LOCKED) revert UnexpectedLock();
22+
}
23+
24+
function isLocked(TransientLock storage self) internal view returns (bool) {
25+
return self._raw.tload() == _LOCKED;
26+
}
27+
28+
function unlock(TransientLock storage self) internal {
29+
self._raw.dec(UnexpectedUnlock.selector);
30+
}
31+
32+
function unlock(TransientLock storage self, bytes4 exception) internal {
33+
self._raw.dec(exception);
34+
}
35+
}

0 commit comments

Comments
 (0)