Skip to content
mv-reyesPublic

About

Zero-credential mainnet-fork testing for Solana programs: dump mainnet state to JSON, boot solana-test-validator offline with real accounts and local builds.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

svm-fork

Zero-credential mainnet-fork testing for Solana programs.

svm-fork replays real mainnet state into solana-test-validator so your test suite runs against the real deployment: the real program-state accounts, the real mints, the real configuration, with your locally built binaries loaded at their on-chain program ids. No API key, no RPC access at validator startup, no --clone flag. The state comes from local JSON files, so the fork boots offline and every run is deterministic.

I built this while integration-testing against perp DEX programs, where the bugs that matter only show up against real deployment state: config values governance actually set, account layouts the deployed program actually wrote, caps that are zero on mainnet but nonzero in a fresh local setup. Cloning over RPC at validator startup works until the RPC rate-limits your CI or the state drifts between runs; dumping once to JSON fixes both.

The zero-credential guarantee

  • dump.mjs reads accounts over the public mainnet RPC (https://api.mainnet-beta.solana.com by default, overridable). No API key.
  • start-validator.sh passes no --url and no --clone to solana-test-validator. Every account the validator has at genesis comes from a local JSON file, so startup works offline.
  • Your test suite talks only to the local validator.
  • No registry dependencies anywhere: the two Node scripts use only node:* builtins and a small base58 implementation in lib/.

Prerequisites

  • Node.js 18 or newer.
  • Solana CLI with solana-test-validator (any recent stable; match your repo's toolchain pin if it has one).
  • Your program builds (anchor build or cargo build-sbf) producing target/deploy/*.so.

The flow

node bin/dump.mjs            # 1. dump mainnet accounts to ./fork-preload
<patch/fabricate if needed>  # 2. optional: fork-local account edits
bin/start-validator.sh       # 3. boot the fork validator, offline
<run your test suite>        # 4. against http://127.0.0.1:8899

1. Fill the manifest and dump

Copy examples/fork.manifest.example.json to fork.manifest.json and list every account your suite touches:

{
  "accounts": {
    "program_state": "Fg6Pa...",
    "usdc_mint": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
    "oracle_buffer": ""
  },
  "fatal": ["program_state"]
}

Labels are free-form and show up in log lines. Empty strings are skipped with a warning (placeholders you have not filled yet). If an account listed under fatal does not exist on chain, the dump aborts: that is the account the suite cannot run without, and a silent skip would surface forty minutes later as a confusing test failure.

node bin/dump.mjs

Before any network call, the dumper preflights every manifest address (valid base58, decodes to 32 bytes), so a value mistyped out of an explorer fails immediately instead of after a failed fetch. It writes one JSON file per account to fork-preload/, in the --account schema the validator expects. Responses are cached under .fork-cache/, so a re-run after a partial failure does not re-fetch. It also writes preload-manifest.json: the sha256 of every file, so later steps can tell intact dumps from truncated or corrupt ones.

Flags: --manifest, --out, --cache, --rpc (or SVM_FORK_RPC / SOLANA_RPC_URL). Point --rpc at any mainnet endpoint; no credentials required either way.

2. Optional: fork-local edits

Two edits come up in almost every real fork suite. Both are legitimate fork-testing practice: they change nothing about the replayed state except what cannot otherwise exist on a fork.

Fabricate balances. On a fork the mint authorities are mainnet keys you do not hold, so test balances can only enter the ledger by being injected at genesis: write an extra SPL token account JSON into fork-preload/ (your test wallet's ATA, funded with whatever amount the suite needs). Files not listed in preload-manifest.json are reported as extras and loaded like any other account.

Re-arm restart guards. Programs that compare a cached last_restarted_slot against the LastRestartSlot sysvar will reject every mutating instruction on a fresh fork, because a new validator always reports 0 there and the on-chain refresh is usually admin-gated. Patch the dumped state account in place: zero the cached slot in the genesis image. This changes no economics; it re-arms the guard against the fork's own restart slot. Without the patch you will see the program's stale-state error on every write.

Substitute mock verifiers. If the program under test calls an oracle verifier (Chainlink, Pyth receiver, wormhole), build a mock that returns the payload unchecked and load it at BOTH its own id and the real verifier's mainnet id (list it twice in the programs config). Replayed accounts that name the real verifier then resolve to the mock, and the fork needs no signed oracle data.

3. Boot the fork validator

Copy examples/fork.programs.example.json to fork.programs.json:

{
  "programs": [
    { "id": "Fg6Pa...", "so": "target/deploy/my_program.so", "upgradeable": true },
    { "id": "HDwcJ...", "so": "external-programs/wormhole.so", "upgradeable": false }
  ]
}

upgradeable: true loads via --upgradeable-program with your configured wallet as authority (programs under test); false loads via --bpf-program (fixed externals). Then:

bin/start-validator.sh

The script checks the binaries and every .so, runs verify-preload.mjs against the preload dir (every file's sha256 against the dumper's manifest; warn-only, because step-2 patches are expected to mismatch), then execs solana-test-validator -r with one --account per JSON. No --url, no --clone: startup needs no network.

4. Run your suite

Point your test harness at http://127.0.0.1:8899. From here it is an ordinary local validator, except the state is real. Things worth asserting because they can only be true on a faithful fork:

  • the replayed state account decodes and its config fields match what governance actually set (a zero cap on mainnet rejects what a fresh local setup would accept);
  • reads through PDAs derived from replayed state resolve to replayed accounts;
  • your patched restart guard re-arms (the cached slot reads 0);
  • fabricated balances move exactly as the program's real accounting says.

What this rig deliberately does not cover

Paths that consume fresh oracle prices are out of scope unless you do the mock-verifier substitution above: the fork only has the prices that were current when the state was dumped, and it has no credentials to refresh them. Treat that as a suite boundary, not an accident: no test should send an instruction that reads a live oracle price unless a mock is in place.

LiteSVM users: the preload JSON is the plain solana account --output json envelope (pubkey, lamports, base64 data, owner). Converting it for a LiteSVM preload is a mechanical transform; the dump and integrity-check steps are unchanged.

Troubleshooting

  • dump.mjs hits HTTP 429/403/502 on the public RPC: it retries with exponential backoff, up to 8 attempts. If the endpoint keeps refusing, pass --rpc <url>. Cached responses live in .fork-cache/; delete it to force a full re-fetch.
  • The dump reports a fatal account missing: the address is wrong or the deployment derives its state PDA differently than you assumed. Re-derive from the program's seeds and confirm on an explorer that the account's owner is the program.
  • The validator fails to boot: read the startup log; the most common cause is a stale or corrupt ledger. The validator runs with -r (reset on start); delete .fork-ledger/ manually if the ledger itself is corrupt.
  • The suite fails with the program's stale-state error on every write: the restart-guard patch from step 2 was not applied. Re-apply and restart.

About

Zero-credential mainnet-fork testing for Solana programs: dump mainnet state to JSON, boot solana-test-validator offline with real accounts and local builds.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages