How Does the Nonreentrant Modifier Implement the CEI Principle?

The nonReentrant modifier implements the CEI principle by using a mutex lock (a state variable). At the start of the function (Checks), it verifies the lock is not set.

It then sets the lock (Effects). The function's logic, including the external call (Interaction), executes.

Crucially, the lock is only released at the end of the function. This ensures that any re-entry attempt will find the lock set and revert, enforcing the order of operations.

What Is a “Private Smart Contract” and Why Is It Needed?
How Does the Compiler Handle Assembly Code regarding Integer Checks?
What Is a Mutex in Traditional Programming, and How Does It Relate to a Reentrancy Guard?
What Is the Checks-Effects-Interactions Pattern?
Give an Example of a State Variable in a Smart Contract Used for Collateralized Lending
What Is the Difference between Time-Based and Milestone-Based Vesting?
How Can a Reentrancy Guard Modifier Offer a Simpler Alternative to This Pattern?
What Is the Primary Difference between a ‘View’ Function and a ‘State-Changing’ Function?

Glossar