Advanced
Protocol Security & Auditing — Key concepts
Learn how pros audit.
Developer Advanced
4/8 — Protocol Security & Auditing — Key concepts
Checks-Effects-InteractionsUpdate state before external calls to block reentrancy drains.
function withdraw(uint256 amount) external nonReentrant {
uint256 bal = deposits[msg.sender];
require(amount <= bal, "exceeds");
deposits[msg.sender] = bal - amount; // effects
token.transfer(msg.sender, amount); // interaction
}
Invariant
vaultAssets = token.balanceOf(vault) = totalDeposits
Add fuzz/invariant tests to enforce accounting.
AuthEvery state-changing path should have clear owner/role checks and events.
Bundle CEI + nonReentrant + auth and caps as your first line; invariants and events make it observable and testable.
Key points
- CEI + nonReentrant guard:External calls.
- Invariants:Assets tracked match on-chain balances.
- Auth:Owner/roles explicit; setters emit events.
- Caps/limits:Protect from unbounded growth and griefing.
- Pause/kill-switch:Controlled halt with tests and events.