0
0
Blockchain / Solidityprogramming~5 mins

Receiving Ether in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Receiving Ether
O(n)
Understanding Time Complexity

When a smart contract receives Ether, it runs code to handle the incoming payment.

We want to understand how the time it takes grows as more Ether transfers happen.

Scenario Under Consideration

Analyze the time complexity of the following Solidity code snippet.


contract ReceiveEther {
    event Received(address sender, uint amount);

    receive() external payable {
        emit Received(msg.sender, msg.value);
    }
}
    

This contract receives Ether and emits an event each time it happens.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: The receive function runs once per Ether transfer.
  • How many times: It runs exactly once for each incoming payment.
How Execution Grows With Input

Each Ether transfer triggers the receive function once, doing a fixed amount of work.

Input Size (n)Approx. Operations
1010 calls to receive()
100100 calls to receive()
10001000 calls to receive()

Pattern observation: The total work grows directly with the number of transfers.

Final Time Complexity

Time Complexity: O(n)

This means the total time grows linearly with the number of Ether transfers received.

Common Mistake

[X] Wrong: "The receive function runs once and handles all Ether transfers at once."

[OK] Correct: Each transfer triggers the receive function separately, so the work adds up with each payment.

Interview Connect

Understanding how contract functions run per transaction helps you explain how blockchain handles many users efficiently.

Self-Check

"What if the receive function also updated a growing list of senders? How would the time complexity change?"