Skip to main content

Non Reentrancy Module

Prevent reentrancy in smart contract functions

Key Features
  • Internal functions enter and exit for reentrancy protection.
  • Emits a Reentrancy error 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.
Module Usage

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

PropertyTypeDescription
NON_REENTRANT_SLOTbytes32Reentrancy 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

function enter() ;

exit

This locks the entry into a function

function exit() ;

Errors

Best Practices

Best Practice
  • 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

Shared Storage

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.

Was this helpful?
Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.