Why DeFi reimagines finance in Blockchain / Solidity - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
When we look at DeFi (Decentralized Finance), we want to understand how the work it does grows as more users or transactions happen.
We ask: How does the time to process transactions change when more people use DeFi?
Analyze the time complexity of the following simplified DeFi smart contract function.
function distributeRewards(address[] memory users, uint256 totalReward) public {
uint256 rewardPerUser = totalReward / users.length;
for (uint i = 0; i < users.length; i++) {
balances[users[i]] += rewardPerUser;
}
}
This function divides a total reward equally among a list of users by looping through each user and updating their balance.
- Primary operation: The for-loop that goes through each user to update their balance.
- How many times: Once for every user in the list.
As the number of users grows, the function does more work because it updates each user's balance one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 balance updates |
| 100 | 100 balance updates |
| 1000 | 1000 balance updates |
Pattern observation: The work grows directly with the number of users. Double the users, double the work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of users.
[X] Wrong: "The function runs in the same time no matter how many users there are."
[OK] Correct: Because the function updates each user's balance one by one, more users mean more updates and more time.
Understanding how DeFi functions scale with users helps you explain real blockchain challenges clearly and confidently.
"What if the function used a batch update method that updated all balances at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand DeFi's core feature
DeFi uses technology to remove banks and middlemen from financial processes.Step 2: Compare options to this feature
Only By removing banks and middlemen matches this key idea; others contradict it.Final Answer:
By removing banks and middlemen -> Option AQuick Check:
DeFi removes middlemen = C [OK]
- Thinking DeFi needs physical banks
- Assuming DeFi limits users by location
- Confusing DeFi with cash-only systems
Solution
Step 1: Define smart contract in DeFi
Smart contracts are computer programs that run automatically on blockchain.Step 2: Match options to this definition
Only self-executing code on blockchain matches this definition.Final Answer:
A self-executing code on blockchain -> Option CQuick Check:
Smart contract = code on blockchain [OK]
- Thinking smart contracts are physical papers
- Confusing manual approval with automation
- Assuming banks sign smart contracts
contract SimpleBank {
mapping(address => uint) balances;
function deposit() public payable {
balances[msg.sender] += msg.value;
}
function getBalance() public view returns (uint) {
return balances[msg.sender];
}
}What will
getBalance() return after a user sends 2 ether to deposit()?Solution
Step 1: Understand deposit function behavior
The deposit function adds the sent ether (msg.value) to the sender's balance in wei (smallest ether unit).Step 2: Understand getBalance return value
getBalance returns the balance in wei, so sending 2 ether means balance is 2 * 10^18 wei.Final Answer:
2 ether in wei units -> Option AQuick Check:
Balance returned in wei units = D [OK]
- Assuming balance returns ether directly
- Thinking deposit is not payable
- Confusing zero balance with deposit amount
contract SimpleBank {
mapping(address => uint) balances;
function withdraw(uint amount) public {
require(balances[msg.sender] >= amount);
payable(msg.sender).transfer(amount);
balances[msg.sender] -= amount;
}
}What is the main issue with this code?
Solution
Step 1: Analyze withdrawal order
The code sends ether before updating the balance, which can allow reentrancy attacks.Step 2: Identify security best practice
Best practice is to update balance before sending ether to prevent reentrancy.Final Answer:
It subtracts balance after transfer, risking reentrancy attack -> Option BQuick Check:
Transfer before update risks reentrancy = B [OK]
- Ignoring reentrancy risks
- Thinking transfer vs send is main issue
- Missing importance of deposit function here
Solution
Step 1: Identify DeFi principles
DeFi automates finance tasks with smart contracts and removes middlemen.Step 2: Match options to these principles
Only the option using smart contracts to automate without intermediaries matches.Final Answer:
Use smart contracts to automate staking and rewards without intermediaries -> Option DQuick Check:
Automation + no middlemen = A [OK]
- Thinking manual bank visits fit DeFi
- Assuming paper contracts are needed
- Limiting access contradicts DeFi openness
