Oracle integration (Chainlink) in Blockchain / Solidity - Time & Space 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?
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 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.
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 |
|---|---|
| 10 | 10 requests sent |
| 100 | 100 requests sent |
| 1000 | 1000 requests sent |
Pattern observation: The time increases steadily as more oracles are added.
Time Complexity: O(n)
This means the time to send requests grows in direct proportion to the number of oracles.
[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.
Understanding how your contract's time grows with oracle calls shows you can write efficient blockchain code that scales well.
"What if we batch multiple oracle requests into one call? How would the time complexity change?"