Prompt
How do I set up a local blockchain simulator for testing protocol upgrades before deployment?
Latest observation
To set up a local blockchain simulator for testing protocol upgrades before deployment, the usual goal is to create a deterministic, forkable local chain that you can run against your protocol code and upgrade logic.
Here’s a practical approach.
1) Decide what you need to simulate
Different upgrades need different levels of realism:
- Smart contract upgrades: local EVM chain is usually enough
- Consensus / fork / protocol parameter changes: you may need a client-level dev network or simulator
- Cross-chain or L2 upgrades: use the chain’s official local devnet tools if available
2) Choose a simulator/tool
Common options:
For EVM chains
- Anvil (Foundry) — fast local Ethereum node
- Hardhat Network — easy scripting and testing
- Ganache — older, still used in some setups
- geth dev mode — closer to a real Ethereum client
For protocol/client-level testing
- Local devnet / private testnet
- Dockerized nodes of the real client software
- Forked mainnet/testnet for realistic state
3) Set up a local chain
Option A: Hardhat
npm install --save-dev hardhat
npx hardhat
npx hardhat node
You can then deploy contracts and run tests against http://127.0.0.1:8545.
Option B: Foundry / Anvil
curl -L https://foundry.paradigm.xyz | bash
foundryup
anvil
Then connect your scripts/tests to the local RPC endpoint.
4) Fork a live network for realistic state
If your upgrade depends on real deployed contracts or current chain state, use a fork:
Hardhat example
// hardhat.config.js
module.exports = {
networks: {
hardhat: {
forking: {
url: process.env.MAINNET_RPC_URL,
blockNumber: 19000000
}
}
}
};
Anvil example
anvil --fork-url $MAINNET_RPC_URL --fork-block-number 19000000
This gives you:
- real balances
- real contract storage
- real historical state at a specific block
5) Model the upgrade scenario
Create scripts/tests that reproduce:
- pre-upgrade state
- upgrade activation at a specific block/time
- post-upgrade behavior
- edge cases and failure modes
Examples:
- deploy old version
- seed balances/state
- advance blocks/time
- switch config or activate new logic
- verify invariants and outputs
6) Add deterministic block/time control
For protocol upgrade testing, you often need to control time and block height:
- mine blocks manually
- increase time
- set timestamp
- impersonate accounts if needed
Examples:
- Hardhat:
evm_mine,evm_setNextBlockTimestamp - Anvil: similar JSON-RPC cheatcodes
- Foundry tests:
warp,roll,prank
7) Run integration tests
Use your normal test framework:
- unit tests for upgrade logic
- integration tests for full transaction flows
- invariant/property tests for safety
Recommended checks:
- state migration correctness
- governance/authorization
- backward compatibility
- gas usage changes
- no storage layout collisions
- rollback/fallback behavior
8) Mirror production as closely as possible
To reduce surprises:
- match chain ID
- use same compiler version
- match contract bytecode optimization settings
- use the same client version/config if testing protocol-level changes
- test with realistic validator/node counts if consensus-related
9) Automate repeatable scenarios
Use scripts or CI to:
- spin up local chain
- deploy contracts
- initialize state
- run upgrade tests
- generate reports
Docker Compose is useful if you need multiple nodes:
- one sequencer / proposer
- one or more full nodes
- RPC endpoint
- monitoring/logging
10) Validate with a public testnet before mainnet
A local simulator is great for fast iteration, but for final confidence:
- test on a testnet
- run a shadow fork if possible
- compare behavior against expected post-upgrade outcomes
Minimal example workflow
- Start a forked local chain:
anvil --fork-url $RPC_URL --fork-block-number 19000000 - Deploy upgradeable contracts or load protocol state
- Run migration/upgrade script
- Execute test transactions
- Assert final state and invariants
If you tell me which stack you’re using — e.g. Ethereum/Hardhat, Foundry, Cosmos SDK, Substrate, Solana, or another chain — I can give you a setup tailored to that protocol and include exact commands/config files.