Overview
Curated: · Written: · Reviewed:
Smart Contract Development (Solidity & EVM)
In one line
A Solidity contract is bytecode that every node re-executes, so it fits shared state that several parties must trust without an operator. Design for who can call it, what each operation costs in gas, and what happens after deployment, since upgrades and reversals are not free.
Key definitions
- Storage: persistent 32-byte slots in global state; survives across transactions.
- Calldata: immutable transaction input, cheapest to read, no copy needed.
- Function selector: first four bytes of the Keccak-256 hash of the canonical signature.
- msg.sender vs tx.origin: the immediate caller vs the EOA that signed the transaction.
- Reentrancy: a callee calls back into the caller before the caller's state update has run.
- Delegatecall: runs callee code against the caller's storage, balance and context.
Formulas and complexity
How it works
- A transaction carries
toand calldata. The dispatcher matches the first four bytes to a function, decodes the arguments and runs the body. - Persistent state lives in storage. Memory and stack are discarded when the call returns.
- Every external call hands control to other code, which can reenter before your function finishes.
- A revert undoes state and events in that call context, but the transaction still pays for the gas consumed.
- Deployment runs init code once. The constructor returns the runtime bytecode, which is what gets stored at the new address.
When to use and when not to
| Situation | Use a contract | Redesign or avoid |
|---|---|---|
| Shared state several parties must trust without an operator | Yes | Keep it off-chain if no one needs to verify it |
| External data needed during execution | Only through an oracle with explicit trust | The EVM cannot fetch HTTP during execution, because every node must reach the same result |
| Payouts to a list that grows | No as a single loop | Pull claims, paginated |
| Logic expected to change | Upgradeable proxy, accepting the storage and governance risk | Immutable contract if the rules are fixed |
| Secrets needed inside logic | No: storage is readable by any node | Keep secrets off-chain, commit only hashes |
Traps interviewers probe
- Effects after the external call. Attackers reenter and pass the stale balance check. Fix: checks, then effects, then interactions. Add a reentrancy guard where shared state spans several functions.
- Push payments in a loop. A growing list exceeds the block gas limit, and an attacker can inflate it. Fix: each recipient claims its own balance (pull payments), with pagination where batching is needed.
tx.originfor authorization. A phishing intermediary keeps the victim as origin. Fix: checkmsg.senderagainst roles or ownership.- Treating
privateas secret. Any node can read the slot witheth_getStorageAt. Fix: keep secrets off-chain and commit to hashes only. - Inserting a variable mid-layout in an upgrade. Later slots shift and live state is misread. Fix: append only, diff storage layouts between versions, and reserve gaps.
Interview tips
Opening: 'Every node re-executes the transaction, so I design for who calls, what each step costs, and what the code can never change after deployment.'
Expect these follow-ups: read-only reentrancy after basic reentrancy; why transfer and send with their 2300 gas stipend are no longer recommended; timelocks after access control; storage gaps and namespaced storage after proxies; why a receipt's status and events matter more than the transaction hash.
30-second outline:
- Mechanism: selector, dispatch, state in storage.
- Risk: external call before effects allows reentry; fix the ordering, then guard if shared state needs it.
- Cost: user-growing loops hit the gas limit; use pull claims.
- Upgrades: append-only layout, role-gated upgrade, timelock for users to exit.
- Verification: check receipt status and expected events before reporting success.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.