Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reentrancy protection with optional parameter #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/TransientContext.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ library TransientContext {
/// Equal to bytes32(uint256(keccak256("transient.calldepth")) - 1).
bytes32 internal constant CALL_DEPTH_SLOT = 0x7a74fd168763fd280eaec3bcd2fd62d0e795027adc8183a693c497a7c2b10b5c;

/// @notice Reentrancy modifier that gives inheritance flexibility automatically.
/// bytes32 key is used to create separate locks, which do not interfere with each other to ensure atomic access.
modifier nonreentrant(bytes32 key) {
assembly {
if tload(key) { revert(0, 0) }
tstore(key, 1)
}
_;
assembly {
tstore(key, 0)
}
}

/// @notice Gets the call depth.
/// @return callDepth_ Current call depth.
function callDepth() internal view returns (uint256 callDepth_) {
Expand Down