Non Reentrancy Module
Prevent reentrancy in smart contract functions
- Internal functions
enterandexitfor reentrancy protection. - Emits a
Reentrancyerror if reentrancy is detected. - No external dependencies, designed for direct integration into facets.
- Compatible with ERC-2535 diamonds by managing the guard variable within diamond storage.
This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.
Overview
This module provides internal functions to prevent reentrancy attacks by managing entry and exit points. Facets can import and use these functions to ensure critical operations are not called recursively within the same execution context. This enhances the security and reliability of your diamond.
Storage
State Variables
| Property | Type | Description |
|---|---|---|
NON_REENTRANT_SLOT | bytes32 | Reentrancy guard storage slot (Value: keccak256("compose.nonreentrant")) |
Functions
enter
How to use as a library in user facets How to use as a modifier in user facets This unlocks the entry into a function
exit
This locks the entry into a function
Errors
Best Practices
- Call
NonReentrancyMod.enter()at the beginning of state-changing functions to prevent reentrancy. - Call
NonReentrancyMod.exit()at the end of state-changing functions to unlock reentrancy. - Ensure the reentrancy guard variable is properly initialized and managed across facet upgrades.
Integration Notes
This module does not directly interact with diamond storage. It relies on a simple storage variable (e.g., uint256) within the calling facet to track the reentrancy state. The enter function checks if the guard is already set, reverting with Reentrancy if it is. Upon successful execution, enter sets the guard, and exit unsets it. This pattern ensures that state changes are atomic and not subject to recursive calls within the same transaction.