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?
Practice
mainnet fork in blockchain development?Solution
Step 1: Understand what a mainnet fork is
A mainnet fork is a copy of the live blockchain used for testing.Step 2: Identify the purpose of testing on a fork
It allows developers to try code changes safely without risking the real network.Final Answer:
To safely test code using real blockchain data without affecting the live network -> Option AQuick Check:
Fork testing = safe real-data testing [OK]
- Thinking a fork changes the live blockchain
- Confusing fork with creating a new coin
- Assuming fork speeds up mining
Solution
Step 1: Recall Hardhat command for forking
The command to start a local node forked from mainnet includesnode --forkwith a mainnet RPC URL.Step 2: Match the correct syntax
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY usesnpx hardhat node --forkwith a mainnet URL, which is correct.Final Answer:
npx hardhat node --fork https://mainnet.infura.io/v3/YOUR_API_KEY -> Option AQuick Check:
Fork command = hardhat node --fork [OK]
- Using compile or deploy commands to fork
- Missing the --fork flag
- Not providing a mainnet RPC URL
const { ethers } = require("hardhat");
async function main() {
const balance = await ethers.provider.getBalance("0x742d35Cc6634C0532925a3b844Bc454e4438f44e");
console.log(ethers.utils.formatEther(balance));
}
main();What will this script output when run on a mainnet fork?
Solution
Step 1: Understand mainnet fork data availability
A mainnet fork copies the live blockchain state, including balances.Step 2: Analyze the script's balance query
The script queries the balance of a known mainnet address, so it returns the real Ether amount.Final Answer:
The Ether balance of the address on the live mainnet -> Option BQuick Check:
Fork has live data = real balance [OK]
- Assuming fork has empty state
- Confusing mainnet fork with testnet
- Thinking address format is invalid
Invalid JSON RPC response. What is the most likely cause?Solution
Step 1: Understand the error context
"Invalid JSON RPC response" usually means the node cannot connect to the RPC endpoint.Step 2: Identify common causes
Not providing a valid mainnet RPC URL or missing API key causes this error.Final Answer:
You forgot to provide a valid mainnet RPC URL with an API key -> Option DQuick Check:
Missing RPC URL/API key = JSON RPC error [OK]
- Blaming Hardhat version without checking URL
- Assuming address errors cause RPC failure
- Ignoring internet connectivity issues
Solution
Step 1: Fork mainnet and deploy old contract
Start by creating a mainnet fork and deploying the current contract version to replicate live state.Step 2: Run upgrade scripts and test new behavior
Apply upgrade scripts on the fork and test the new contract to ensure correctness without risk.Final Answer:
Fork mainnet locally, deploy old contract, run upgrade scripts, test new contract behavior -> Option CQuick Check:
Fork + old contract + upgrade + test = safe upgrade [OK]
- Deploying new contract directly on mainnet
- Ignoring old contract state in tests
- Skipping mainnet fork and testing only on testnet
Ready to prove it?
Pass the test and this pattern moves unknown
