0
0
Blockchain / Solidityprogramming~5 mins

Fork testing (mainnet fork) in Blockchain / Solidity - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Fork testing (mainnet fork)
O(n)
Understanding Time Complexity

When testing a mainnet fork, we want to know how the time to run tests changes as the number of transactions or blocks increases.

We ask: How does the testing time grow when we add more data from the mainnet?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


const fork = await network.provider.request({
  method: 'hardhat_reset',
  params: [{
    forking: { jsonRpcUrl: MAINNET_URL, blockNumber: 15000000 }
  }]
});

for (const tx of transactions) {
  await network.provider.send('eth_sendRawTransaction', [tx]);
}
    

This code resets the blockchain state to a mainnet fork at a specific block, then replays a list of transactions on it.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Sending each transaction one by one to the forked network.
  • How many times: Once for every transaction in the list.
How Execution Grows With Input

As the number of transactions increases, the time to replay them grows proportionally.

Input Size (n)Approx. Operations
1010 transaction sends
100100 transaction sends
10001000 transaction sends

Pattern observation: Doubling the number of transactions roughly doubles the time needed.

Final Time Complexity

Time Complexity: O(n)

This means the testing time grows directly in proportion to the number of transactions replayed.

Common Mistake

[X] Wrong: "Replaying transactions on a fork is instant regardless of how many there are."

[OK] Correct: Each transaction must be processed one after another, so more transactions take more time.

Interview Connect

Understanding how replaying transactions scales helps you reason about testing and simulation speed in blockchain development.

Self-Check

What if we batch multiple transactions together instead of sending them one by one? How would the time complexity change?