Accounts (EOA vs contract accounts) in Blockchain / Solidity - Performance Comparison
When working with blockchain accounts, it's important to understand how the time to process transactions changes as more accounts or contracts are involved.
We want to know how the execution time grows when interacting with externally owned accounts (EOAs) versus contract accounts.
Analyze the time complexity of the following simplified blockchain transaction processing code.
function processTransaction(sender, receiver, data) {
if (isContract(receiver)) {
executeContract(receiver, data);
} else {
transferEther(sender, receiver);
}
}
function executeContract(contract, data) {
for (let i = 0; i < data.length; i++) {
contract.runInstruction(data[i]);
}
}
This code checks if the receiver is a contract account or an EOA, then either transfers ether or executes contract instructions.
Look for loops or repeated steps in the code.
- Primary operation: Loop over contract instructions inside
executeContract. - How many times: Once per instruction in the
dataarray.
The time to process a transaction depends on the number of instructions sent to a contract.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 contract instructions executed |
| 100 | 100 contract instructions executed |
| 1000 | 1000 contract instructions executed |
Pattern observation: The execution time grows directly with the number of instructions.
Time Complexity: O(n)
This means the time to process contract transactions grows linearly with the number of instructions sent.
[X] Wrong: "Transactions to EOAs and contracts always take the same time."
[OK] Correct: EOAs just transfer ether, which is quick, but contract accounts run code that can take longer depending on input size.
Understanding how contract execution time grows helps you explain blockchain performance and gas costs clearly, a useful skill in many blockchain roles.
"What if the contract calls another contract inside its instructions? How would the time complexity change?"