Common vulnerability patterns in Blockchain / Solidity - Time & Space Complexity
When we look at common vulnerability patterns in blockchain code, we want to understand how the cost of running the code changes as inputs grow.
We ask: How does the program's work increase when it faces more data or more users?
Analyze the time complexity of the following code snippet.
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}("");
require(success);
}
This code allows a user to withdraw funds from their balance, sending Ether to their address.
Look for loops or repeated actions that affect time.
- Primary operation: Single balance check and transfer call per withdrawal.
- How many times: Runs once per withdrawal request, no loops inside.
The work done is mostly fixed per withdrawal, no matter how many users or transactions happen.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations (one per withdrawal) |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The cost grows linearly with the number of withdrawal calls.
Time Complexity: O(n)
This means the total work grows directly with the number of withdrawal requests made.
[X] Wrong: "The withdrawal function runs in constant time no matter how many users withdraw."
[OK] Correct: Each withdrawal call costs time, so more calls mean more total work, even if each call alone is simple.
Understanding how vulnerabilities affect time helps you explain risks clearly and shows you can think about code efficiency and safety together.
"What if the withdraw function included a loop over all users to update balances? How would the time complexity change?"
Practice
Solution
Step 1: Understand reentrancy attack
Reentrancy happens when a contract calls another contract and the called contract calls back before the first call finishes.Step 2: Identify the vulnerability type
This allows attackers to exploit the contract by repeatedly calling a function, draining funds.Final Answer:
Reentrancy attack -> Option DQuick Check:
Repeated calls before completion = Reentrancy attack [OK]
- Confusing integer overflow with repeated calls
- Thinking timestamp dependency allows repeated calls
- Mixing phishing attack with smart contract vulnerabilities
Solution
Step 1: Recall Solidity 0.8+ overflow behavior
From Solidity 0.8, arithmetic operations revert on overflow automatically.Step 2: Identify correct prevention method
So, just usinguint256 x = a + b;is safe without extra checks or libraries.Final Answer:
uint256 x = a + b; // no extra check needed -> Option AQuick Check:
Solidity 0.8+ auto checks overflow = uint256 x = a + b; // no extra check needed [OK]
- Using SafeMath in Solidity 0.8+ unnecessarily
- Adding manual require checks that are redundant
- Confusing subtraction with addition overflow
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
balances[msg.sender] -= amount;
}
What is the main vulnerability in this code?Solution
Step 1: Analyze call order in function
The external call tomsg.sender.callhappens before updatingbalances[msg.sender].Step 2: Identify vulnerability type
This allows reentrancy: attacker can call withdraw again before balance is reduced.Final Answer:
Reentrancy attack due to external call before state update -> Option BQuick Check:
External call before balance update = Reentrancy risk [OK]
- Thinking integer overflow is the issue here
- Ignoring call return value is not the main problem
- Confusing timestamp dependency with this pattern
mapping(address => uint) public balances;
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
balances[msg.sender] -= amount;
(bool success, ) = msg.sender.call{value: amount}('');
require(success);
balances[msg.sender] = 0;
}Solution
Step 1: Check balance update order
Balance is reduced before sending funds (good for reentrancy prevention), but then reset to zero after sending.Step 2: Identify bug impact
For partial withdrawals (e.g., balance=20, amount=10), after -=10 and send 10, setting to 0 discards the remaining 10 without sending it, stealing user funds.Final Answer:
Balance is set to zero after sending funds, causing loss of remaining balance for partial withdrawals -> Option AQuick Check:
Post-call balance=0 after partial -= = remaining balance theft [OK]
- Ignoring that balance reset happens after external call
- Thinking missing payable modifier causes this bug
- Assuming call success check is missing
Solution
Step 1: Prevent reentrancy by state update order
Update all internal state variables before making any external calls to avoid reentrancy.Step 2: Prevent integer overflow using Solidity 0.8+
Use Solidity 0.8+ built-in overflow checks instead of external libraries.Final Answer:
Update state variables before external calls and use Solidity 0.8+ arithmetic -> Option CQuick Check:
State update first + Solidity 0.8+ overflow checks = Safe transfer [OK]
- Calling external contracts before state update
- Relying on timestamp checks for safety
- Ignoring overflow checks in older Solidity versions
Ready to prove it?
Pass the test and this pattern moves unknown
