CBSA Smart Contract Design 2 — Questions and Answers
Question 1: Which design pattern separates a smart contract's logic from its data storage to enable upgradability?
- Proxy pattern (Correct answer)
- Factory pattern
- Singleton pattern
- Observer pattern
Correct answer: Proxy pattern
The proxy pattern delegates calls to a logic contract while keeping state in a separate storage contract, allowing logic upgrades without migrating data.
Question 2: What is a primary security risk of using 'tx.origin' for authorization in a Solidity smart contract?
- It exposes the contract to phishing attacks via malicious intermediary contracts (Correct answer)
- It consumes significantly more gas than msg.sender
- It is deprecated and no longer available in Solidity
- It only works on private blockchain networks
Correct answer: It exposes the contract to phishing attacks via malicious intermediary contracts
A malicious contract can trick the original sender into calling it, and tx.origin still returns that user's address, bypassing intended authorization checks.
Question 3: In smart contract design, what does the 'check-effects-interactions' pattern primarily prevent?
- Integer overflow vulnerabilities
- Reentrancy attacks (Correct answer)
- Front-running attacks
- Gas limit exhaustion
Correct answer: Reentrancy attacks
By updating internal state (effects) before calling external contracts (interactions), the pattern prevents reentrant calls from exploiting stale state.
Question 4: Which Ethereum opcode is used internally when a contract sends Ether using the 'transfer' function, and what is its gas stipend?
- CALL with 2300 gas (Correct answer)
- SEND with 21000 gas
- STATICCALL with 2300 gas
- DELEGATECALL with 5000 gas
Correct answer: CALL with 2300 gas
The 'transfer' function uses the CALL opcode but forwards only 2300 gas, which is enough for a log event but prevents most reentrancy.
Question 5: What is the purpose of the 'circuit breaker' (emergency stop) pattern in smart contract design?
- To reduce gas costs during high network congestion
- To pause contract functionality when a vulnerability is detected (Correct answer)
- To automatically upgrade contract logic when a bug is found
- To throttle the number of transactions per block
Correct answer: To pause contract functionality when a vulnerability is detected
A circuit breaker allows an authorized admin to halt critical functions so funds can be protected while a vulnerability is investigated and fixed.
Question 6: When designing a smart contract that accepts ERC-20 token payments, why must you call 'approve' before 'transferFrom'?
- Because transferFrom requires prior owner authorization via the allowance mechanism (Correct answer)
- Because approve permanently locks tokens in the contract
- Because ERC-20 tokens cannot be transferred without minting new tokens first
- Because the EVM requires two-step transactions for all token operations
Correct answer: Because transferFrom requires prior owner authorization via the allowance mechanism
The ERC-20 allowance mechanism requires the token owner to first approve a spender's address for a specific amount before transferFrom can pull those tokens.
Question 7: What is 'storage collision' in the context of upgradeable smart contracts using the proxy pattern?
- When two functions write to the same storage slot, corrupting data
- When a proxy and its logic contract use the same storage slot for different variables (Correct answer)
- When a contract runs out of available storage slots
- When two contracts compete to write to the blockchain simultaneously
Correct answer: When a proxy and its logic contract use the same storage slot for different variables
If a proxy contract's admin variable occupies the same storage slot as a variable in the logic contract, one will overwrite the other causing data corruption.
Which design pattern separates a smart contract's logic from its data storage to enable upgradability?