Gas and transaction fees in Blockchain / Solidity - Time & Space Complexity
When working with blockchain, gas and transaction fees affect how much work a transaction requires.
We want to understand how the cost grows as transactions do more work.
Analyze the time complexity of the following code snippet.
function executeTransaction(actions) {
let totalGas = 0;
for (let i = 0; i < actions.length; i++) {
totalGas += actions[i].gasCost;
// perform action
}
return totalGas;
}
This code calculates the total gas needed by adding up gas costs for each action in a transaction.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each action in the transaction.
- How many times: Once for every action in the list.
As the number of actions grows, the total gas calculation takes longer because it adds each action's cost.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 additions |
| 100 | 100 additions |
| 1000 | 1000 additions |
Pattern observation: The work grows directly with the number of actions.
Time Complexity: O(n)
This means the time to calculate gas grows in a straight line as more actions are added.
[X] Wrong: "Gas calculation time stays the same no matter how many actions there are."
[OK] Correct: Each action adds work, so more actions mean more time to add up gas costs.
Understanding how gas calculation scales helps you explain blockchain transaction costs clearly and shows you know how work grows with input size.
"What if the actions array was sorted by gas cost before summing? Would the time complexity change?"
Practice
gas represent in a blockchain transaction?Solution
Step 1: Understand the role of gas in blockchain
Gas is a unit that measures how much computational effort is needed to execute operations like transactions or smart contracts.Step 2: Differentiate gas from other transaction elements
Gas is not the amount of cryptocurrency sent, nor the number of transactions or confirmation time.Final Answer:
The amount of computational work required to process the transaction -> Option AQuick Check:
Gas = computational work [OK]
- Confusing gas with transaction value
- Thinking gas is transaction speed
- Mixing gas with block size
Solution
Step 1: Recall the transaction fee formula
Transaction fees are calculated by multiplying the gas used by the gas price.Step 2: Verify the formula correctness
Dividing, adding, or subtracting gas used and gas price does not represent the fee calculation.Final Answer:
Transaction fees = gas used x gas price -> Option DQuick Check:
Fees = gas used x gas price [OK]
- Using division instead of multiplication
- Adding gas used and gas price
- Subtracting gas values
Solution
Step 1: Identify given values
Gas used = 21000 units, Gas price = 50 gwei per unit.Step 2: Calculate total fee
Total fee = gas used x gas price = 21000 x 50 = 1,050,000 gwei.Final Answer:
1,050,000 gwei -> Option BQuick Check:
21000 x 50 = 1,050,000 [OK]
- Multiplying incorrectly
- Confusing gas units with gwei
- Using gas price as total fee
gas_used = 30000 gas_price = 20 transaction_fee = gas_used + gas_price print(transaction_fee)
Solution
Step 1: Review the code logic
The code adds gas_used and gas_price instead of multiplying them.Step 2: Understand correct fee calculation
Transaction fee should be gas_used x gas_price, not addition.Final Answer:
Using addition instead of multiplication for fee calculation -> Option AQuick Check:
Fee = gas_used x gas_price, not + [OK]
- Adding gas values instead of multiplying
- Confusing variable names
- Ignoring correct formula
Solution
Step 1: Calculate original fee
Original fee = 21000 x 40 = 840,000 gwei.Step 2: Calculate new fee
New fee = 21000 x 60 = 1,260,000 gwei.Step 3: Find difference
Difference = 1,260,000 - 840,000 = 420,000 gwei more.Final Answer:
420,000 gwei more -> Option CQuick Check:
Increase = (60-40)x21000 = 420,000 [OK]
- Calculating total fee instead of difference
- Mixing gas price and gas used
- Incorrect multiplication
Ready to prove it?
Pass the test and this pattern moves unknown
