0
0
Blockchain / Solidityprogramming~5 mins

Transaction confirmation handling in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Transaction confirmation handling
O(n)
Understanding Time Complexity

When a blockchain transaction is sent, the system waits for confirmations to ensure it is securely recorded.

We want to understand how the time to handle these confirmations grows as the number of confirmations increases.

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


function waitForConfirmations(txHash, requiredConfirmations) {
  let confirmations = 0;
  while (confirmations < requiredConfirmations) {
    confirmations = getConfirmations(txHash); // fetch current confirmations
    sleep(1000); // wait 1 second before checking again
  }
  return true;
}
    

This code waits until a transaction reaches the required number of confirmations by repeatedly checking the current count.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The while loop that repeatedly calls getConfirmations.
  • How many times: It runs until the required number of confirmations is reached.
How Execution Grows With Input

Each confirmation requires one loop cycle, so the total checks grow directly with the number of confirmations needed.

Input Size (requiredConfirmations)Approx. Operations (loop cycles)
1010 checks
100100 checks
10001000 checks

Pattern observation: The number of operations grows in a straight line as confirmations increase.

Final Time Complexity

Time Complexity: O(n)

This means the time to confirm grows directly in proportion to the number of confirmations required.

Common Mistake

[X] Wrong: "The confirmation check happens only once, so time is constant regardless of confirmations."

[OK] Correct: The code checks repeatedly until all confirmations arrive, so more confirmations mean more checks and more time.

Interview Connect

Understanding how waiting for confirmations scales helps you reason about blockchain transaction reliability and responsiveness in real projects.

Self-Check

"What if the code checked confirmations in batches instead of one by one? How would the time complexity change?"