How Does the Checks-Effects-Interactions Pattern Prevent Reentrancy Attacks?
The Checks-Effects-Interactions pattern is a security best practice for writing smart contracts. It dictates that the contract should first perform all necessary checks (e.g. verifying permissions), then apply all effects to its internal state (e.g. updating balances), and only then interact with external contracts.
By updating the state before making an external call, the contract prevents a malicious actor from re-entering the function in a vulnerable state. This ensures that even if the external contract calls back, the internal state is already updated, and the conditions for the attack are no longer met.