Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion src/libraries/LibNonReentrancy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ library LibNonReentrancy {
bytes32 position = NON_REENTRANT_SLOT;
assembly ("memory-safe") {
if tload(position) {
mstore(0x00, 0x43a0d067)
// Store the selector for "Reentrancy()" (0xab143c06) at the beginning of memory.
// We shift left by 224 bits (256 - 32) to left-align the 4-byte selector in the 32-byte slot.
mstore(0x00, shl(224, 0xab143c06))
revert(0x00, 0x04)
}
tstore(position, 1)
Expand Down
38 changes: 38 additions & 0 deletions test/libraries/LibNonReentrancy.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.30;

import {Test} from "forge-std/Test.sol";
import {LibNonReentrancy} from "src/libraries/LibNonReentrancy.sol";
import {NonReentrantHarness} from "test/libraries/harnesses/LibNonReentrancyHarness.sol";

contract LibNonReentrancyTest is Test {
NonReentrantHarness internal harness;

function setUp() public {
harness = new NonReentrantHarness();
}

function test_GuardedIncrement_IncrementsCounter() public {
harness.guardedIncrement();
assertEq(harness.counter(), 1);
}

function test_GuardedIncrement_AllowsSequentialCalls() public {
harness.guardedIncrement();
harness.guardedIncrement();
assertEq(harness.counter(), 2);
}

function test_RevertWhen_ReenteringFunction() public {
vm.expectRevert(LibNonReentrancy.Reentrancy.selector);
harness.guardedIncrementAndReenter();
}

function test_GuardResetsAfterRevert() public {
vm.expectRevert(NonReentrantHarness.ForcedFailure.selector);
harness.guardedIncrementAndForceRevert();

harness.guardedIncrement();
assertEq(harness.counter(), 1);
}
}
31 changes: 31 additions & 0 deletions test/libraries/harnesses/LibNonReentrancyHarness.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.30;

import {LibNonReentrancy} from "src/libraries/LibNonReentrancy.sol";

contract NonReentrantHarness {
error ForcedFailure();

uint256 public counter;

function guardedIncrement() public {
LibNonReentrancy.enter();
counter++;
LibNonReentrancy.exit();
}

function guardedIncrementAndReenter() external {
LibNonReentrancy.enter();
counter++;

this.guardedIncrement();

LibNonReentrancy.exit();
}

function guardedIncrementAndForceRevert() external {
LibNonReentrancy.enter();
counter++;
revert ForcedFailure();
}
}
1 change: 1 addition & 0 deletions test/token/ERC6909/ERC6909/ERC6909Facet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ contract ERC6909FacetTest is Test {
}

function testFuzz_Mint(address caller, address to, uint256 id, uint256 amount) external {
vm.assume(to != address(0));
vm.expectEmit();
emit ERC6909Facet.Transfer(caller, address(0), to, id, amount);

Expand Down
Loading