Fork testing (mainnet fork) in Blockchain / Solidity - Time & Space 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?
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 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.
As the number of transactions increases, the time to replay them grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 transaction sends |
| 100 | 100 transaction sends |
| 1000 | 1000 transaction sends |
Pattern observation: Doubling the number of transactions roughly doubles the time needed.
Time Complexity: O(n)
This means the testing time grows directly in proportion to the number of transactions replayed.
[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.
Understanding how replaying transactions scales helps you reason about testing and simulation speed in blockchain development.
What if we batch multiple transactions together instead of sending them one by one? How would the time complexity change?