Withdrawal patterns in Blockchain / Solidity - Time & Space Complexity
When working with withdrawal patterns in blockchain, it's important to see how the time to process withdrawals changes as more users request funds.
We want to know how the number of withdrawal requests affects the work the system must do.
Analyze the time complexity of the following code snippet.
function withdrawAll(address[] memory users) public {
for (uint i = 0; i < users.length; i++) {
uint amount = balances[users[i]];
if (amount > 0) {
balances[users[i]] = 0;
payable(users[i]).transfer(amount);
}
}
}
This code processes withdrawals for a list of users by sending them their balance one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the users array to process each withdrawal.
- How many times: Once for each user in the list (users.length times).
As the number of users increases, the number of withdrawal operations grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 withdrawal checks and transfers |
| 100 | 100 withdrawal checks and transfers |
| 1000 | 1000 withdrawal checks and transfers |
Pattern observation: The work grows directly with the number of users; doubling users doubles the work.
Time Complexity: O(n)
This means the time to complete all withdrawals grows in a straight line with the number of users.
[X] Wrong: "The withdrawal time stays the same no matter how many users there are."
[OK] Correct: Each user requires a separate withdrawal step, so more users mean more work and more time.
Understanding how withdrawal operations scale helps you explain how blockchain contracts handle many users efficiently and safely.
"What if we batch withdrawals so multiple users are paid in one transaction? How would the time complexity change?"
Practice
withdrawal pattern in blockchain smart contracts?Solution
Step 1: Understand withdrawal pattern purpose
The withdrawal pattern lets users request their funds instead of sending automatically.Step 2: Identify safety benefits
This prevents security issues like reentrancy by updating balances before sending.Final Answer:
To let users safely withdraw their funds by calling a function -> Option AQuick Check:
Withdrawal pattern = user-initiated safe withdrawal [OK]
- Thinking funds are sent automatically
- Confusing withdrawal with deposit
- Ignoring security benefits
Solution
Step 1: Check balance update before transfer
function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); } sets balance to zero before sending funds, preventing reentrancy.Step 2: Verify other options
function withdraw() public { payable(msg.sender).transfer(balances[msg.sender]); balances[msg.sender] = 0; } sends funds before zeroing balance (unsafe). Options C and D do not handle balances properly.Final Answer:
function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); } -> Option CQuick Check:
Update balance before transfer = function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); } [OK]
- Sending funds before updating balance
- Not updating balance at all
- Sending fixed amounts ignoring user balance
withdraw() twice in a row on this contract snippet?mapping(address => uint) balances;
function withdraw() public {
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
payable(msg.sender).transfer(amount);
}Solution
Step 1: Analyze first withdraw call
First call reads balance, sends funds, then sets balance to zero.Step 2: Analyze second withdraw call
Second call reads balance which is now zero, so sends zero funds.Final Answer:
The user receives funds only the first time, second call sends zero -> Option DQuick Check:
Balance zero after first withdraw = second call sends zero [OK]
- Assuming funds sent twice
- Expecting error on second call
- Thinking balance resets automatically
function withdraw() public {
payable(msg.sender).transfer(balances[msg.sender]);
balances[msg.sender] = 0;
}Solution
Step 1: Check order of operations
The function sends funds before setting balance to zero.Step 2: Understand security risk
This allows reentrancy attacks where attacker calls withdraw repeatedly before balance is zeroed.Final Answer:
Balance is updated after transfer, risking reentrancy attack -> Option BQuick Check:
Update balance before transfer to prevent reentrancy [OK]
- Updating balance after transfer
- Ignoring reentrancy risks
- Not using payable correctly
Solution
Step 1: Check balance update and transfer order
event Withdraw(address indexed user, uint amount); function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); emit Withdraw(msg.sender, amount); } updates balance before transfer, preventing reentrancy.Step 2: Verify event emits correct amount
event Withdraw(address indexed user, uint amount); function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); emit Withdraw(msg.sender, amount); } emits event after transfer with correct amount saved before zeroing balance.Step 3: Check other options
The other snippets emit event with wrong amount or update balance after transfer, causing errors or security risks.Final Answer:
event Withdraw(address indexed user, uint amount); function withdraw() public { uint amount = balances[msg.sender]; balances[msg.sender] = 0; payable(msg.sender).transfer(amount); emit Withdraw(msg.sender, amount); } -> Option AQuick Check:
Update balance, transfer, then emit event with saved amount [OK]
- Emitting event before saving amount
- Updating balance after transfer
- Emitting event with zero amount
Ready to prove it?
Pass the test and this pattern moves unknown
