Why logic controls execution in Blockchain / Solidity - Performance Analysis
In blockchain programming, the logic you write decides how many steps your code takes.
We want to see how this logic affects the time it takes to run as input grows.
Analyze the time complexity of the following code snippet.
function processTransactions(transactions) {
for (let i = 0; i < transactions.length; i++) {
if (transactions[i].amount > 1000) {
validateTransaction(transactions[i]);
}
}
}
function validateTransaction(tx) {
// some validation logic here
}
This code checks each transaction and only validates those over 1000 units.
- Primary operation: Looping through all transactions once.
- How many times: Once for each transaction in the list.
As the number of transactions grows, the code checks each one once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows directly with the number of transactions.
Time Complexity: O(n)
This means the time to run grows in a straight line as the input size grows.
[X] Wrong: "Since only some transactions are validated, the code runs faster than checking all."
[OK] Correct: The loop still checks every transaction, so the time grows with all transactions, not just the validated ones.
Understanding how your logic controls execution helps you write efficient blockchain code and explain it clearly in conversations.
"What if we added a nested loop to validate transactions against a list of rules? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of logic in code
Logic decides which code runs by checking conditions, like a traffic light controlling cars.Step 2: Apply this to blockchain context
In blockchain, logic ensures only allowed actions happen, protecting assets and data.Final Answer:
It controls which parts of the code run based on conditions -> Option CQuick Check:
Logic controls execution = D [OK]
- Confusing logic with data storage
- Thinking logic speeds network
- Assuming logic creates cryptocurrencies
Solution
Step 1: Recall Solidity if statement syntax
Solidity uses C-style syntax: condition in parentheses, code block in braces.Step 2: Check each option
if (balance > 0) { sendFunds(); } matches correct syntax with parentheses and braces; others miss parentheses, braces, or semicolon.Final Answer:
if (balance > 0) { sendFunds(); } -> Option AQuick Check:
Solidity if syntax = C [OK]
- Omitting parentheses around condition
- Missing braces for code block
- Leaving out semicolons
amount = 50?
function checkAmount(uint amount) public pure returns (string memory) {
if (amount > 100) {
return "High";
} else if (amount > 20) {
return "Medium";
} else {
return "Low";
}
}Solution
Step 1: Evaluate the if conditions with amount = 50
50 > 100 is false, so skip first if; 50 > 20 is true, so return "Medium".Step 2: Confirm no errors in code
Code syntax is correct, so no compilation error.Final Answer:
"Medium" -> Option DQuick Check:
50 > 20 = true, output = "Medium" [OK]
- Choosing "High" because 50 is large
- Ignoring else if condition order
- Thinking code has syntax errors
function transfer(uint amount) public {
if amount > balance {
revert("Insufficient balance");
}
balance -= amount;
}Solution
Step 1: Check if statement syntax
In Solidity, conditions must be inside parentheses: if (condition) {...}Step 2: Verify other parts
Using revert is correct for errors; semicolon is present; balance can be state variable.Final Answer:
Missing parentheses around the if condition -> Option AQuick Check:
If condition needs parentheses = A [OK]
- Thinking revert is wrong
- Assuming balance must be local
- Missing that parentheses are required
Solution
Step 1: Understand the required conditions
Both conditions must be true: sender is owner AND amount less than balance.Step 2: Check each option's logic
if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } uses && (AND) correctly; B uses || (OR) which allows wrong cases; C and D use negations incorrectly.Final Answer:
if (msg.sender == owner && amount < balance) { transfer(amount); } else { revert("Not allowed"); } -> Option BQuick Check:
Use AND for both conditions true = A [OK]
- Using OR (||) instead of AND (&&)
- Negating conditions incorrectly
- Allowing transfers when conditions fail
Ready to prove it?
Pass the test and this pattern moves unknown
