2. Which of the following is the correct syntax to start a batch operation in a blockchain smart contract (pseudocode)?
easy
A. batch { /* tasks */ }
B. start batch { /* tasks */ }
C. beginBatch() /* tasks */ endBatch()
D. batch.start() { /* tasks */ }
Solution
Step 1: Recognize common batch syntax
Batch operations often use a block or function named batch enclosing tasks.
Step 2: Compare options
batch { /* tasks */ } uses batch { /* tasks */ } which is a common and clean way to group tasks.
Final Answer:
batch { /* tasks */ } -> Option A
Quick Check:
Batch block syntax = batch { } [OK]
Hint: Batch usually wraps tasks inside curly braces [OK]
Common Mistakes:
Using incorrect keywords like start or beginBatch
Missing curly braces for grouping tasks
Confusing batch syntax with function calls
3. Given the following pseudocode for a batch operation:
batch {
transfer(from: A, to: B, amount: 10)
transfer(from: B, to: C, amount: 5)
transfer(from: C, to: A, amount: 3)
}
What happens if the second transfer fails due to insufficient funds?
medium
A. All transfers are rolled back; none are applied.
B. Only the second transfer fails; the others succeed.
C. The batch skips the failed transfer and continues.
D. The batch completes but logs an error for the second transfer.
Solution
Step 1: Understand atomicity of batch operations
Batch operations run all tasks together or none at all to keep data consistent.
Step 2: Apply failure effect
If one task fails (second transfer), the entire batch is rolled back, so no transfers happen.
Final Answer:
All transfers are rolled back; none are applied. -> Option A
Quick Check:
Batch atomicity = all or nothing [OK]
Hint: If one fails, batch rolls back all tasks [OK]
Common Mistakes:
Thinking partial batch tasks succeed
Assuming batch skips failed tasks
Believing batch logs errors but applies others
4. Consider this batch operation pseudocode:
batch {
mintTokens(user: X, amount: 100)
burnTokens(user: X, amount: 50)
transferTokens(from: X, to: Y, amount: 60)
}
The batch fails with an error. What is the most likely cause?
medium
A. Minting tokens always fails in batch operations.
B. Trying to transfer more tokens than user X has after burning.
C. Burning tokens cannot be done inside a batch.
D. Batch operations do not support token transfers.
Solution
Step 1: Calculate user X's token balance after mint and burn
User X mints 100 tokens, then burns 50, so balance is 50 tokens.
Step 2: Check transfer amount validity
Transfer tries to send 60 tokens, which is more than 50 available, causing failure.
Final Answer:
Trying to transfer more tokens than user X has after burning. -> Option B
Quick Check:
Transfer > balance causes batch failure [OK]
Hint: Check token balances after each batch step [OK]
Common Mistakes:
Assuming minting always fails
Believing burning is not allowed in batch
Thinking batch disallows transfers
5. You want to update multiple user balances atomically in a blockchain. Which approach best uses batch operations to ensure either all updates succeed or none do?
function updateBalances(updates) {
batch {
for (update in updates) {
setBalance(user: update.user, amount: update.amount)
}
}
}
What is a key consideration to avoid silent failures in this batch?
hard
A. Use multiple batches for each user update.
B. Run each update outside batch to isolate errors.
C. Ignore errors inside batch to continue all updates.
D. Validate each update's amount before batch to prevent invalid data.
Solution
Step 1: Understand batch atomicity and error handling
Batch runs all updates together; if one fails, all rollback. Silent failures can happen if invalid data is inside batch.
Step 2: Importance of pre-validation
Validating each update before batch ensures no invalid data causes failure, avoiding silent rollback.
Final Answer:
Validate each update's amount before batch to prevent invalid data. -> Option D
Quick Check:
Pre-validate data to avoid batch rollback [OK]
Hint: Check data before batch to prevent rollback [OK]
Common Mistakes:
Running updates outside batch loses atomicity
Ignoring errors causes silent rollback
Splitting updates into many batches loses efficiency