Skill 3.2.1

Apply checks-effects-interactions, pull payments and reentrancy guards

Key Concepts

Checks-effects-interactions orders every function in three parts: first validate conditions and inputs (checks), then update the contract's own state (effects), and only then call other contracts or send ether (interactions). If a callback re-enters, the state already reflects the change, so the second attempt fails its checks.

Pull payments (the withdrawal pattern) record what each account is owed and let each account withdraw its own funds in a separate transaction, instead of pushing payments to many recipients. A recipient that reverts affects only itself, and each withdrawal is a small, bounded operation.

A reentrancy guard sets a lock flag on entry and clears it on exit, reverting if the function is entered again while locked. OpenZeppelin provides ReentrancyGuard with the nonReentrant modifier, and a variant that uses cheaper transient storage. The guard is a second line of defence; correct ordering remains the primary one.