0
0
Blockchain / Solidityprogramming~5 mins

Oracle integration (Chainlink) in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Oracle integration (Chainlink)
O(n)
Understanding Time Complexity

When using Chainlink oracles, we want to know how the time to get data changes as we ask for more information.

We ask: How does the number of oracle requests affect the time our smart contract takes?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


contract PriceConsumer {
  address[] public oracles;

  function requestPrices() public {
    for (uint i = 0; i < oracles.length; i++) {
      Chainlink.Request memory req = buildChainlinkRequest(...);
      sendChainlinkRequestTo(oracles[i], req, fee);
    }
  }
}
    

This code sends a price request to each oracle in a list one by one.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Loop over the list of oracles to send requests.
  • How many times: Once for each oracle in the array.
How Execution Grows With Input

As the number of oracles grows, the contract sends more requests, so the time grows directly with the number of oracles.

Input Size (n)Approx. Operations
1010 requests sent
100100 requests sent
10001000 requests sent

Pattern observation: The time increases steadily as more oracles are added.

Final Time Complexity

Time Complexity: O(n)

This means the time to send requests grows in direct proportion to the number of oracles.

Common Mistake

[X] Wrong: "Sending requests to multiple oracles happens instantly all at once."

[OK] Correct: Each request is sent one by one in a loop, so more oracles mean more time spent sending requests.

Interview Connect

Understanding how your contract's time grows with oracle calls shows you can write efficient blockchain code that scales well.

Self-Check

"What if we batch multiple oracle requests into one call? How would the time complexity change?"