CBSA Smart Contract Design 5 — Questions and Answers
Question 1: What is 'gas optimization' technique of 'packing struct variables' in Solidity?
- Ordering struct fields so that smaller types share a 32-byte storage slot, reducing the number of SSTORE operations (Correct answer)
- Compressing struct data using on-chain zlib compression before storage
- Splitting large structs into multiple smaller contracts to reduce deployment cost
- Using assembly to manually write struct data to avoid Solidity overhead
Correct answer: Ordering struct fields so that smaller types share a 32-byte storage slot, reducing the number of SSTORE operations
EVM storage is organized in 32-byte slots, so placing uint128 + uint128 in sequence allows both to share one slot, halving SSTORE costs compared to non-packed order.
Question 2: Which oracle design pattern reduces manipulation risk by aggregating price data from multiple independent sources?
- Centralized oracle with API key rotation
- Decentralized oracle network with median aggregation (Correct answer)
- On-chain price computation from AMM reserves only
- Block-hash-based price derivation
Correct answer: Decentralized oracle network with median aggregation
Aggregating data from multiple independent oracles and taking the median makes it economically prohibitive for an attacker to manipulate the final price by corrupting a minority of sources.
Question 3: What is 'flash loan' in DeFi and what smart contract design consideration does it introduce?
- A loan repaid in a future block; contracts must lock state across blocks
- An uncollateralized loan repaid within one transaction; contracts must be resilient to large temporary capital imbalances (Correct answer)
- A loan from the contract deployer used to fund initial liquidity
- A government-backed loan for blockchain startups requiring no smart contract changes
Correct answer: An uncollateralized loan repaid within one transaction; contracts must be resilient to large temporary capital imbalances
Flash loans allow borrowing large sums within one atomic transaction, so contracts relying on token balances or prices for security must account for artificially inflated intra-transaction states.
Question 4: In Hyperledger Fabric chaincode design, what is the equivalent of Ethereum's 'revert' for rolling back a transaction?
- Returning an error from the Invoke function so the peer discards world state changes (Correct answer)
- Calling the 'rollback' built-in function in Go chaincode
- Emitting a 'TransactionFailed' event that triggers automatic state reversal
- Sending a negative response code via the stub.SetEvent API
Correct answer: Returning an error from the Invoke function so the peer discards world state changes
In Fabric, returning an error (shim.Error) from the Invoke function causes the transaction to be invalidated and all world state changes from that execution to be discarded.
Question 5: What is the purpose of the 'time-lock' pattern in smart contract governance systems?
- To permanently lock contract funds after a specified date
- To enforce a mandatory delay between proposal approval and execution, giving users time to exit if they disagree (Correct answer)
- To prevent contracts from being called more than once per block
- To synchronize contract state changes with external real-world events
Correct answer: To enforce a mandatory delay between proposal approval and execution, giving users time to exit if they disagree
A timelock introduces a waiting period (e.g., 48 hours) after a governance vote passes, allowing users who disagree with the change to withdraw their assets before it takes effect.
Question 6: Which approach best handles a situation where a Solidity contract must iterate over an unknown number of user accounts to distribute rewards?
- Perform all iterations in a single on-chain transaction using a for loop
- Use a merkle tree so users can claim their own rewards with a proof, eliminating the need to iterate on-chain (Correct answer)
- Store all accounts in calldata and pass them as a parameter to avoid gas limits
- Use block.coinbase to randomly select reward recipients each block
Correct answer: Use a merkle tree so users can claim their own rewards with a proof, eliminating the need to iterate on-chain
A merkle drop pattern shifts the iteration cost to individual users at claim time, making the distribution scale to any number of recipients without hitting the block gas limit.
Question 7: What does 'immutable' keyword in Solidity 0.6.5+ offer compared to 'constant' for smart contract optimization?
- immutable variables can be set once in the constructor and are then inlined like constants, while constant must be set at compile time (Correct answer)
- immutable reduces bytecode size more aggressively than constant for all data types
- immutable variables are stored in a separate memory region that costs no gas to read
- immutable allows state mutation up to 256 times before becoming read-only
Correct answer: immutable variables can be set once in the constructor and are then inlined like constants, while constant must be set at compile time
Unlike 'constant' which requires values known at compile time, 'immutable' allows constructor-time assignment and then inlines the value into bytecode, avoiding expensive SLOAD operations.
What is 'gas optimization' technique of 'packing struct variables' in Solidity?