Secure Design Patterns
A small set of patterns prevents the most common vulnerabilities. This lesson explains checks-effects-interactions, pull payments, reentrancy guards, emergency stops, rate limits and the general principles of secure contract design.
Learning outcomes
After completing this lesson, you will be able to:
- Describe the principles of secure contract design.
- Apply checks-effects-interactions ordering to a function.
- Explain the pull payment pattern and reentrancy guards.
- Explain how emergency stops and rate limits contain incidents.
Design principles
Secure contracts share a few principles. The following list describes them.
- Keep it simple: less code and fewer features mean fewer bugs; move logic that does not need trust off chain.
- Least privilege: give each role only the permissions it needs, and as few roles as possible.
- Fail safely: revert on unexpected conditions rather than continuing in an unknown state.
- Plan for failure: decide before deployment how an incident would be contained, such as pausing or upgrading.
- Reuse audited code: prefer well-tested libraries to new implementations.
Secure design principles
Checks-effects-interactions
Checks-effects-interactions (CEI) orders every function that calls other contracts in three phases, as the following sequence shows.
-
Checks
Validate the caller, inputs and state:
require, custom errors and modifiers. -
Effects
Update the contract's own state: balances, flags, counters.
-
Interactions
Only now call other contracts or send ether.
If an interaction re-enters the contract, it sees the updated state, so a second withdrawal finds a zero balance. CEI costs nothing and should be the default structure of every function.
The following diagram shows the three phases in order.

Which phase?
Pull payments and reentrancy guards
The pull payment (withdrawal) pattern records what each account is owed and lets each account withdraw it, instead of the contract pushing ether to many accounts.
mapping(address => uint256) public owed;
function _credit(address to, uint256 amount) internal {
owed[to] += amount; // no external call here
}
function withdraw() external nonReentrant {
uint256 amount = owed[msg.sender];
owed[msg.sender] = 0;
(bool ok, ) = msg.sender.call{value: amount}("");
if (!ok) revert TransferFailed();
}A recipient that rejects ether only blocks its own withdrawal, and each withdrawal is a small, bounded operation. A reentrancy guard (nonReentrant) adds a lock as a second line of defence; OpenZeppelin also provides ReentrancyGuardTransient, which keeps the lock in cheaper transient storage.
Push or pull?
Emergency stops and rate limits
An emergency stop (circuit breaker) lets an authorised account pause sensitive functions during an incident. OpenZeppelin's Pausable provides _pause(), _unpause() and the whenNotPaused and whenPaused modifiers. A common design pauses deposits and trading but leaves withdrawals open, so users can always exit.
Rate limits and caps, such as a maximum withdrawal per day or a maximum total deposit during a launch, limit how much an attacker can take before the team reacts. These controls add a trusted role, which must itself be protected, typically by a multisig.
Designing an emergency stop
Reordering a function
Rewrite this function in checks-effects-interactions order: it calls token.transfer(msg.sender, reward), then checks !claimed[msg.sender], then sets claimed[msg.sender] = true.
First check: if (claimed[msg.sender]) revert AlreadyClaimed();. Then the effect: claimed[msg.sender] = true;. Finally the interaction: token.safeTransfer(msg.sender, reward);. In the original order the check came after the transfer, so a re-entering call, or simply the lack of a check before paying, could claim repeatedly.
What to pause
A lending protocol adds Pausable. The team proposes pausing every function, including repay and withdraw, during incidents. What is the risk?
Pausing repayments could push borrowers into liquidation they cannot avoid, and pausing withdrawals traps users' funds and concentrates trust in the pauser. A better design pauses new deposits and borrowing, keeps repayment and withdrawal available where safe, and protects the pause role with a multisig.
Put the three phases of checks-effects-interactions in order and say what each does.
Show answer
Checks validate the caller, inputs and state; effects update the contract's own state; interactions call other contracts or send ether last.
Exam Tips
Remember for the exam
- Order functions as checks, effects, interactions.
- Pull payments isolate failing recipients and keep work bounded.
- nonReentrant is a second layer of defence, not a replacement for CEI.
- Pausable and rate limits contain incidents but add trusted roles.