Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
hexonaut opened this issue Apr 12, 2025 · 1 comment · Fixed by #10301
Closed
2 tasks done

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

hexonaut opened this issue Apr 12, 2025 · 1 comment · Fixed by #10301
Assignees
Labels
C-forge Command: forge Cmd-forge-test Command: forge test T-bug Type: bug

Comments

@hexonaut
Copy link
Contributor

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);
    }
}
@hexonaut hexonaut added T-bug Type: bug T-needs-triage Type: this issue needs to be labelled labels Apr 12, 2025
@github-project-automation github-project-automation bot moved this to Todo in Foundry Apr 12, 2025
@grandizzy grandizzy added C-forge Command: forge and removed T-needs-triage Type: this issue needs to be labelled labels Apr 14, 2025
@zerosnacks zerosnacks changed the title Odd Storage/Fork Bug bug(forge test): storage inside the test contract is not persisted between forks Apr 14, 2025
@zerosnacks zerosnacks added Cmd-forge-test Command: forge test T-to-investigate Type: to investigate labels Apr 14, 2025
@grandizzy grandizzy self-assigned this Apr 14, 2025
@grandizzy grandizzy removed the T-to-investigate Type: to investigate label Apr 14, 2025
@grandizzy grandizzy moved this from Todo to Ready For Review in Foundry Apr 15, 2025
@github-project-automation github-project-automation bot moved this from Ready For Review to Done in Foundry Apr 15, 2025
@grandizzy grandizzy moved this from Done to Completed in Foundry Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-test Command: forge test T-bug Type: bug
Projects
Status: Completed
Development

Successfully merging a pull request may close this issue.

3 participants