Skip to content

bug(forge test): storage inside the test contract is not persisted between forks #10296

@hexonaut

Description

@hexonaut

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge Version: 1.0.0-stable Commit SHA: e144b82 Build Timestamp: 2025-02-13T20:02:34.979686000Z (1739476954) Build Profile: maxperf

What version of Foundryup are you on?

foundryup: 1.0.1

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

There is a very strange bug where storage inside the test contract is not persisted between forks. I have created a minimal version of the bug here: https://github.com/hexonaut/fork-storage-issue

The readme explains things. I'm not sure why exactly it is happening, but it should be clear in the example what is wrong.

I'll paste the code here too just for easy reference:

pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {StdChains} from "forge-std/StdChains.sol";

contract CounterTest is Test {
    struct Domain {
        StdChains.Chain chain;
        uint256 forkId;
    }

    struct Bridge {
        Domain source;
        Domain destination;
        uint256 someVal;
    }

    struct SomeStruct {
        Domain domain;
        Bridge[] bridges;
    }

    mapping(uint256 => SomeStruct) internal data;

    function setUp() public {
        StdChains.Chain memory chain1 = getChain("mainnet");
        StdChains.Chain memory chain2 = getChain("base");
        Domain memory domain1 = Domain(chain1, vm.createFork(chain1.rpcUrl, 22253716));
        Domain memory domain2 = Domain(chain2, vm.createFork(chain2.rpcUrl, 28839981));
        data[1].domain = domain1;
        data[2].domain = domain2;

        vm.selectFork(domain1.forkId);

        data[2].bridges.push(Bridge(domain1, domain2, 123));
        vm.selectFork(data[2].domain.forkId);
        vm.selectFork(data[1].domain.forkId);
        data[2].bridges.push(Bridge(domain1, domain2, 456));

        assertEq(data[2].bridges.length, 2);
    }

    function test_storage() public {
        // UNCOMMENT THIS TO FIX THE ASSERT BELOW
        // Why does this work?
        //assertEq(data[2].bridges.length, 2);

        vm.selectFork(data[2].domain.forkId);

        assertEq(data[2].bridges.length, 2);
    }
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions