Fork testing
Learn how to test contracts against blockchain state by forking testnet or mainnet
Fork testing runs tests against blockchain state by loading accounts and libraries from TON testnet or TON mainnet. Use it to test contract interactions with deployed contracts without complex local environment setup.
What fork testing is
Fork testing keeps a local view (fork) of remote chain state. When test code reads an account that exists on testnet or mainnet while fork mode is on, the runner:
- Fetches the account's state from the network
- Caches it locally for the test run
- Uses that remote state inside local execution
Fork testing is useful for:
- Testing integrations with existing contracts
- Verifying behavior against real contract code
- Debugging issues with deployed contracts
- Testing edge cases with real on-chain data
It combines real on-chain data with local test execution speed.
Enable fork testing
Basic usage
Pass --fork-net when running tests:
# Fork from testnet
acton test --fork-net testnet
# Fork from mainnet
acton test --fork-net mainnetPin a historical state by passing a block sequence number:
acton test --fork-net mainnet --fork-block-number 54321000Pin the fork block for faster reruns
If you rerun the same fork tests often, pass --fork-block-number instead
of using a live fork. Pinned fork runs persist fetched accounts under
build/cache/<network>/<seqno>/, so the next CLI run can load the same
accounts from disk before calling the remote API. Live fork runs without a
block number only use the in-memory cache for the current acton test
process and will fetch accounts again on the next run.
You can also set defaults in Acton.toml:
[test]
fork-net = "mainnet"
fork-block-number = 54321000Configure API access
Remote state is fetched through the TON Center APIs. Obtain the API key in the Telegram bot to access higher RPS limits.
Set TONCENTER_MAINNET_API_KEY or TONCENTER_TESTNET_API_KEY environment variables in the .env file or export them on the shell:
# Testnet API key
TONCENTER_TESTNET_API_KEY=YOUR_API_KEY acton test --fork-net testnet
# Mainnet API key
TONCENTER_MAINNET_API_KEY=YOUR_API_KEY acton test --fork-net mainnetCustom networks
Use custom:<name> to fork from a network defined under [networks]:
[networks.archive-rpc]
api = { v2 = "https://your-rpc.example/api/v2/jsonRPC" }
[test]
fork-net = "custom:archive-rpc"With this default in Acton.toml, run tests normally and Acton will fork from
archive-rpc:
acton testYou can also select the custom fork network explicitly for a single run:
acton test --fork-net custom:archive-rpc
acton test tests/wallet.test.tolk --fork-net custom:archive-rpcExecution model
When fork testing is enabled and a test touches an account:
- The system first checks if the account was already loaded in the local cache.
- If
--fork-block-numberis set, it checks the persistent fork account cache. - If the account is still not cached and
--fork-netis specified, it queries the configured API endpoint. - Account state, i.e., balance, code, data, status, is downloaded and deserialized.
- The state is cached for subsequent accesses.
- Tests execute using this real state in the local emulator.
Network latency
The first fetch of each distinct account hits the network and adds latency. Subsequent fetches reuse the cache. Plan accordingly when many distinct accounts appear in one run.
Cache scope and refresh behavior
Fork testing uses two layers of account state:
- Local account changes are scoped to the current test.
- The remote snapshot cache is shared by tests in the same
acton testrun. - When a fork block number is pinned, fetched remote snapshots are also saved to
disk under
build/cache/<network>/<seqno>/<workchain>_<address-hash>.json.
The shared remote cache stores only fetched remote snapshots:
- The cache key is the tuple of fork network, optional pinned block number, and account address.
- The second test that touches the same remote account reuses the cached remote snapshot instead of re-fetching it.
- A fresh
acton test --fork-net ...run starts with an empty in-memory cache. - If
--fork-block-numberis set, a fresh run can reuse the persistent account cache before making a network request. - Live fork runs without
--fork-block-numberare not persisted across CLI runs. - Pass
--no-fork-cacheto skip persistent cache reads and writes for the current run. - Pass
--clear-cacheto remove persistent fork account entries together with the rest ofbuild/cache. - Failed lookups are not cached. If a request errors, the next access can try the network again.
Local changes are scoped to the current test. If a test fetches a remote account and then changes it locally, later reads in that same test use the changed local account. Other tests do not inherit that local mutation; they start from a fresh local state and, if they touch the same account, reuse the shared remote snapshot that was fetched earlier in the run.
To refetch remote snapshots from the same network and pinned block, run with
--no-fork-cache once or clear build/cache. Changing --fork-net or
--fork-block-number also uses a different cache key.
Fork testing only loads accounts that are not already present in the current test's local account state. Accounts deployed or changed during a test are not replaced by remote snapshots.
Library lookup precedence
Library lookup follows the same "local first" rule.
When code asks to resolve a library hash, Acton first checks libraries already registered in the current emulated blockchain state, for example via testing.registerLibrary(...). Only if the library is still missing does it fall back to the configured fork network.
That ordering matters when a forked test registers a different library cell than the remote network would supply: the locally registered library overrides its remote counterpart for the rest of the run.
Last updated on