Prompt

How do I set up a local blockchain simulator for testing protocol upgrades before deployment?

Latest observation

Jul 22, 2026 · OpenAI APIWeb search: off

To test protocol upgrades locally before deployment, you generally want a deterministic local blockchain simulator that can:

  • fork or emulate mainnet/state,
  • let you control block production and timestamps,
  • deploy and upgrade contracts,
  • run your tests automatically.

Here’s a practical setup that works well for most EVM-based chains. If you’re on a different stack, tell me which chain and I can tailor it.


1) Pick the right local simulator

Common options

  • Anvil (Foundry): fast, excellent for forking and testing.
  • Hardhat Network: very flexible, widely used in JS/TS projects.
  • Ganache: older, still usable, but less preferred now.
  • Custom devnet: best if you need consensus-layer behavior, validators, or protocol-level changes.

Recommendation

  • For smart contract and app-level protocol upgrade testing: use Anvil or Hardhat.
  • For client/protocol changes or consensus changes: use a private devnet/testnet or a full local multi-node setup.

2) Decide what you need to simulate

Protocol upgrades can mean different things:

A. Contract upgrade

Example: upgrading a proxy contract, changing business logic, adding storage fields.

Use:

  • local EVM,
  • mainnet fork,
  • deployment scripts,
  • unit/integration tests.

B. Chain protocol upgrade

Example: changing block gas rules, hardfork behavior, opcode semantics.

Use:

  • a local node with a chosen hardfork setting,
  • tests that run against multiple fork rules,
  • possibly a custom client build.

C. Validator / consensus upgrade

Example: changing block timing, validator set, consensus parameters.

Use:

  • multi-node devnet,
  • Docker Compose or Kubernetes,
  • chain-specific local testnet tooling.

3) Set up a local EVM simulator with fork support

Example with Anvil

Install Foundry:

curl -L https://foundry.paradigm.xyz | bash
foundryup

Run a local chain:

anvil

Fork a live network state:

anvil --fork-url https://mainnet.infura.io/v3/YOUR_KEY

Fork at a specific block:

anvil --fork-url https://mainnet.infura.io/v3/YOUR_KEY --fork-block-number 19000000

This gives you:

  • real contract state,
  • ability to simulate changes locally,
  • no risk to mainnet.

4) Configure the simulator for the target upgrade

You usually need to set:

  • chain ID
  • hardfork rules
  • block time
  • gas limit
  • base fee behavior
  • timestamp control
  • accounts / balances

Example Hardhat config

module.exports = {
  solidity: "0.8.24",
  networks: {
    hardhat: {
      chainId: 31337,
      forking: {
        url: process.env.MAINNET_RPC,
        blockNumber: 19000000
      },
      mining: {
        auto: false,
        interval: 0
      },
      initialBaseFeePerGas: 0
    }
  }
};

Example Anvil options

anvil \
  --chain-id 31337 \
  --fork-url $MAINNET_RPC \
  --fork-block-number 19000000 \
  --block-time 12

If you need to simulate a hardfork:

  • choose the client setting that matches the upgrade rules,
  • or use a client version that includes the new behavior.

5) Build upgrade tests

You want tests that cover:

State migration

  • existing storage layout remains valid,
  • old contracts/proxies still work,
  • migrations initialize new variables correctly.

Behavior changes

  • new functions behave as expected,
  • old functions still preserve invariants,
  • access control remains correct.

Economic/invariant checks

  • balances remain conserved,
  • no unexpected reentrancy or overflow,
  • no broken accounting after upgrade.

Regression tests

  • replay known real-world transactions against the fork,
  • ensure old workflows still succeed.

6) Use a mainnet fork for realistic testing

Forking is especially useful because you can test against:

  • actual token balances,
  • real liquidity pools,
  • deployed dependencies,
  • live contract interfaces.

Typical workflow:

  1. fork at a known block,
  2. impersonate relevant accounts,
  3. simulate upgrade steps,
  4. run downstream integration tests.

Hardhat impersonation example

await network.provider.request({
  method: "hardhat_impersonateAccount",
  params: ["0xYourWhaleAddress"]
});

7) Automate block progression and timestamps

Protocol upgrades often depend on block height or time.

In tests, advance time:

Hardhat:

await network.provider.send("evm_increaseTime", [3600]);
await network.provider.send("evm_mine");

Anvil:

await provider.send("evm_increaseTime", [3600]);
await provider.send("evm_mine", []);

This lets you test:

  • upgrade activation at a specific block,
  • timelock delays,
  • epoch transitions,
  • time-based logic.

8) If you need a true protocol-level simulator

If your upgrade changes consensus rules, block validation, or node behavior, a local EVM is not enough.

Use a devnet with multiple nodes

Set up:

  • 2–4 validator/full nodes,
  • a bootnode or beacon chain component if needed,
  • local RPC endpoints,
  • a script to produce blocks and submit transactions.

Tools

  • Docker Compose
  • Kubernetes
  • Chain-specific devnet tooling
  • Custom builds of the client software

This is how you test:

  • network propagation,
  • forks,
  • validator liveness,
  • protocol parameter changes.

9) Add CI to prevent regressions

Run your upgrade tests automatically in CI:

  • unit tests,
  • fork tests,
  • migration tests,
  • invariant/property tests.

Useful additions:

  • fuzz testing,
  • invariant testing,
  • snapshot testing,
  • replaying historical transactions.

10) Practical minimal setup

If you want something quick and effective:

  1. Install Foundry.
  2. Run Anvil with a fork of mainnet/testnet.
  3. Write tests for:
    • upgrade deployment,
    • state migration,
    • core invariants.
  4. Automate block/time changes.
  5. Run the same suite in CI.

Example workflow

# 1. Start simulator
anvil --fork-url $MAINNET_RPC --fork-block-number 19000000

# 2. Run deployment script against local chain
forge script script/Upgrade.s.sol --rpc-url http://127.0.0.1:8545 --broadcast

# 3. Run tests
forge test -vvv

Best practices

  • Fork from a fixed block for reproducibility.
  • Use snapshot/revert to isolate tests.
  • Test both pre-upgrade and post-upgrade behavior.
  • Add checks for storage layout compatibility.
  • If possible, replay a few historical transactions.
  • Simulate failure cases: partial migration, reorg-like conditions, stale prices, etc.

If you want, I can give you:

  1. a Foundry/Anvil setup,
  2. a Hardhat setup, or
  3. a Docker Compose multi-node devnet example.

Observation history

See 1 earlier observation

Create a free Obsurfable account to unlock historical responses, model comparisons, and deeper brand intelligence.

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (2 observations for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, and Meta AI.