|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.20; |
| 4 | + |
| 5 | +/// @title Mutex |
| 6 | +/// |
| 7 | +/// @author TSxo |
| 8 | +/// |
| 9 | +/// @dev Provides mechanisms to guard against reentrancy attacks. |
| 10 | +/// |
| 11 | +/// # Guards |
| 12 | +/// |
| 13 | +/// Exposes two modifiers: `lock` for write operations and `whenUnlocked` for |
| 14 | +/// reads. |
| 15 | +/// |
| 16 | +/// The `lock` modifier acquires a lock before function execution and releases |
| 17 | +/// it afterward. While locked, no other function using `lock` or `whenUnlocked` |
| 18 | +/// can be called. |
| 19 | +/// |
| 20 | +/// The `whenUnlocked` modifier ensures no lock is held during calls to view |
| 21 | +/// functions, preventing read-level reentrancy. |
| 22 | +/// |
| 23 | +/// Functions guarded by these modifiers cannot directly call each other. To |
| 24 | +/// navigate this issue, extract reusable logic into modifier-free internal or |
| 25 | +/// private functions and compose calls to them from a single guarded entry point. |
| 26 | +/// |
| 27 | +/// For the rationale behind `1` and `2` representing locked states, see: |
| 28 | +/// - https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuard.sol |
| 29 | +/// - https://eips.ethereum.org/EIPS/eip-2200 |
| 30 | +/// |
| 31 | +/// # Upgrade Compatible |
| 32 | +/// |
| 33 | +/// This contract utilizes ERC-7201 namespaced storage, making it compatible with |
| 34 | +/// upgradeable contract architectures. Note, it does not enforce any restrictions |
| 35 | +/// against reinitialization, leaving such protection mechanisms up to the |
| 36 | +/// inheriting contract. |
| 37 | +/// |
| 38 | +/// # Disclaimer |
| 39 | +/// |
| 40 | +/// This contract prioritizes an opinionated balance between optimization and |
| 41 | +/// readability. **It was not designed with user safety in mind** and contains |
| 42 | +/// minimal safety checks. It is experimental software and is provided **as-is**, |
| 43 | +/// without any warranties or guarantees of functionality, security, or fitness |
| 44 | +/// for any particular purpose. |
| 45 | +/// |
| 46 | +/// There are implicit invariants this contract expects to hold. Users and |
| 47 | +/// developers integrating this contract **do so at their own risk** and are |
| 48 | +/// responsible for thoroughly reviewing the code before use. |
| 49 | +/// |
| 50 | +/// The author assumes **no liability** for any loss, damage, or unintended |
| 51 | +/// behavior resulting from the use, deployment, or interaction with this contract. |
| 52 | +/// |
| 53 | +/// # Acknowledgements |
| 54 | +/// |
| 55 | +/// Heavy inspiration is taken from: |
| 56 | +/// - Open Zeppelin; |
| 57 | +/// - Solmate; and |
| 58 | +/// - Solady. |
| 59 | +/// |
| 60 | +/// Thank you. |
| 61 | +abstract contract Mutex { |
| 62 | + // ------------------------------------------------------------------------- |
| 63 | + // Type Declarations |
| 64 | + |
| 65 | + /// @custom:storage-location erc7201:libsol.storage.Mutex |
| 66 | + struct MutexStorage { |
| 67 | + uint256 locked; |
| 68 | + } |
| 69 | + |
| 70 | + // ------------------------------------------------------------------------- |
| 71 | + // State |
| 72 | + |
| 73 | + /// @dev keccak256(abi.encode(uint256(keccak256("libsol.storage.Mutex")) - 1)) & ~bytes32(uint256(0xff)) |
| 74 | + bytes32 private constant STORAGE = 0x4772547a0c85096864cfb8fb79e76bca1fc87ca09848b27c5507e4697fcfd100; |
| 75 | + |
| 76 | + /// @dev Represents the unlocked state. |
| 77 | + uint256 private constant UNLOCKED = 1; |
| 78 | + |
| 79 | + /// @dev Represents the locked state. |
| 80 | + uint256 private constant LOCKED = 2; |
| 81 | + |
| 82 | + // ------------------------------------------------------------------------- |
| 83 | + // Errors |
| 84 | + |
| 85 | + /// @notice Raised when entering a guarded function after a lock has been |
| 86 | + /// acquired. |
| 87 | + error Mutex__Locked(); |
| 88 | + |
| 89 | + // ------------------------------------------------------------------------- |
| 90 | + // Modifiers |
| 91 | + |
| 92 | + /// @dev Guards against reentrancy attacks by acquiring a lock before function |
| 93 | + /// execution and releasing it afterward. |
| 94 | + modifier lock() virtual { |
| 95 | + _acquireLock(); |
| 96 | + _; |
| 97 | + _releaseLock(); |
| 98 | + } |
| 99 | + |
| 100 | + /// @dev Guards against read-only reentrancy attacks by ensuring no write |
| 101 | + /// lock is held. |
| 102 | + /// |
| 103 | + /// Important: Does not acquire a lock. See the `lock()` modifier. |
| 104 | + modifier whenUnlocked() virtual { |
| 105 | + _assertUnlocked(); |
| 106 | + _; |
| 107 | + } |
| 108 | + |
| 109 | + // ------------------------------------------------------------------------- |
| 110 | + // Functions - Init |
| 111 | + |
| 112 | + /// @notice Initializes the contract by setting the locked status to `UNLOCKED`. |
| 113 | + function _initializeMutex() internal virtual { |
| 114 | + assembly ("memory-safe") { |
| 115 | + sstore(STORAGE, UNLOCKED) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // ------------------------------------------------------------------------- |
| 120 | + // Functions - Internal |
| 121 | + |
| 122 | + /// @notice Acquires the lock. Once acquired, no reentrant calls can be made |
| 123 | + /// to functions that use the `lock` or `whenUnlocked` modifiers. |
| 124 | + function _acquireLock() internal virtual { |
| 125 | + _assertUnlocked(); |
| 126 | + |
| 127 | + assembly ("memory-safe") { |
| 128 | + sstore(STORAGE, LOCKED) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /// @notice Releases the lock. |
| 133 | + function _releaseLock() internal virtual { |
| 134 | + assembly ("memory-safe") { |
| 135 | + sstore(STORAGE, UNLOCKED) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + /// @notice Returns whether the lock is currently acquired. |
| 140 | + /// |
| 141 | + /// @return result True if the lock is acquired, false otherwise. |
| 142 | + function _isLocked() internal view virtual returns (bool result) { |
| 143 | + assembly ("memory-safe") { |
| 144 | + result := eq(sload(STORAGE), LOCKED) |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /// @notice Asserts that the lock has not been acquired. |
| 149 | + function _assertUnlocked() internal view virtual { |
| 150 | + assembly ("memory-safe") { |
| 151 | + if eq(sload(STORAGE), LOCKED) { |
| 152 | + mstore(0x00, 0x02c73c37) // `Mutex__Locked()` |
| 153 | + revert(0x1c, 0x04) |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments