0
0
Blockchain / Solidityprogramming~5 mins

Accounts (EOA vs contract accounts) in Blockchain / Solidity - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Accounts (EOA vs contract accounts)
O(n)
Understanding Time Complexity

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.

Scenario Under Consideration

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.

Identify Repeating Operations

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 data array.
How Execution Grows With Input

The time to process a transaction depends on the number of instructions sent to a contract.

Input Size (n)Approx. Operations
1010 contract instructions executed
100100 contract instructions executed
10001000 contract instructions executed

Pattern observation: The execution time grows directly with the number of instructions.

Final Time Complexity

Time Complexity: O(n)

This means the time to process contract transactions grows linearly with the number of instructions sent.

Common Mistake

[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.

Interview Connect

Understanding how contract execution time grows helps you explain blockchain performance and gas costs clearly, a useful skill in many blockchain roles.

Self-Check

"What if the contract calls another contract inside its instructions? How would the time complexity change?"