Skip to content
Lattice

ReentrancyGuardLib

Git Source

Title: ReentrancyGuard Library

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Solady (https://github.com/vectorized/solady/blob/main/src/utils/ReentrancyGuardTransient.sol)

Reentrancy protection for Diamond facets — transient-storage variant.

On Ethereum mainnet the lock lives in transient storage (TSTORE/TLOAD, auto-cleared per transaction); on every other chain it uses a regular SSTORE guard for widespread L2/EVM compatibility (only mainnet is expensive anyway — Solady's default behavior). Diverges from upstream: the virtual _useTransientReentrancyGuardOnlyOnMainnet() hook is dropped (library functions cannot see contract-level overrides — the default is baked in), and the revert error is Lattice's existing ReentrancyGuardReentrantCall().

Call entry() at the start and exit() at the end of a guarded function — or inherit the ReentrancyGuard mixin and use its nonReentrant/nonReadReentrant modifiers.

Functions

entry

Asserts that no reentrant call is in progress, then takes the lock.

Call at the start of a guarded function; must be paired with exit(). Reverts with ReentrancyGuardReentrantCall if the lock is already held.

function entry() internal;

exit

Releases the lock taken by entry().

Call at the end of a guarded function; must be paired with entry(). On non-mainnet chains the slot is reset to a non-zero sentinel (the slot value itself) rather than zero, avoiding a fresh zero-to-nonzero SSTORE on the next entry.

function exit() internal;

check

Asserts that no reentrant call is in progress WITHOUT taking the lock.

Guards view functions against read-only reentrancy (a guarded write path calling back into a view mid-lock). Reverts with ReentrancyGuardReentrantCall if the lock is held.

function check() internal view;

nonReentrantBefore

Asserts that no reentrant call is in progress, then locks.

Alias for entry(), kept for existing callers. Prefer the ReentrancyGuard nonReentrant modifier or entry()/exit() in new code.

function nonReentrantBefore() internal;

nonReentrantAfter

Resets the reentrancy lock.

Alias for exit(), kept for existing callers.

function nonReentrantAfter() internal;

reentrancyGuardEntered

Returns true when a guarded call is currently executing.

Equivalent to OZ v5.1.0 _reentrancyGuardEntered(). Useful for view helpers that must behave differently when called mid-execution inside a guarded function.

function reentrancyGuardEntered() internal view returns (bool entered_);

Returns

NameTypeDescription
entered_bool