In this track: Smart contract security foundations
advanced · 4 MIN READ
Checks, effects and interactions
Make the relevant state consistent before handing control away.
LET’S PICTURE IT
A clerk marks a ticket redeemed before asking another desk to deliver the prize.
SEE IT FOR YOURSELF
A valid counter call, step by stepStep 1 / 4
This example begins with count zero and a request to call increment(). No real contract is deployed.
This example begins with count zero and a request to call increment(). No real contract is deployed.Execution follows the code’s input and permission rules. A website button is not the access-control mechanism.The function reads zero, calculates zero plus one, and writes one to persistent storage.The accepted state contains count one. Later reads can inspect it. Events are emitted only if the code implements them.
Animated local teaching model. No wallet, RPC or live blockchain data. Some labels and hashes are abbreviated for readability.
Check the authorization and inputs
Establish who may act and whether the action’s conditions hold. A frontend disabling a button does not replace checks in the contract.
Apply coherent state effects
Update the accounting needed to preserve the operation’s invariant. Think about the whole related state rather than moving one assignment mechanically.
Treat interaction as a handoff
An external call can execute untrusted code and call back into reachable functions. The state visible at that boundary must satisfy the intended assumptions.
Handle failure consistently
If the interaction fails, choose a response that keeps accounting correct. A propagated revert may restore earlier effects, while a caught failure requires deliberate handling.
Reason about the scenario
check entitlement
record redemption consistently
perform external interaction
handle its outcome
Pseudocode or a simplified scenario for local reasoning. This is not a production contract or a completed security review.
QUICK CHECK · NO PRESSURE
When should relevant accounting normally be coherent?
A callback can observe or act on state before the first call finishes.