foundry-rs / foundry

Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
https://getfoundry.sh
Apache License 2.0
8.18k stars 1.7k forks source link

Ability to read private immutables #4172

Open iFrostizz opened 1 year ago

iFrostizz commented 1 year ago

Component

Forge

Describe the feature you would like

It is currently impossible to read private immutable variables in Foundry because of how they work. Immutables are, similarly to constants, inlined in the bytecode directly and PUSHed where they are needed. The issue is that immutables are, as opposed to constants, not known before the contract construction. This makes them, even more tricky to read if private. Because that would imply reading the code and parsing what's of interest, making it not scalable as the code change, or optimizations.

Example:

contract HardToRead {
    uint256 private immutable val;

    constructor(uint256 _val) {
        val = _val;
    }
}

My proposition would be to add a cheatcode to read immutables, and constants directly from the code called codeLoad (very open to propositions). It would work by using the solc output, probably the bytecode to source code mapping in order to generate bindings for the immutables and constants that are defined in the contract and expose them through the cheatcode.

Additional context

No response

iFrostizz commented 1 year ago

I'm happy to take this one

spaceh3ad commented 10 months ago

Did you take it? :D

iFrostizz commented 10 months ago

Nope @spaceh3ad !

Ethnical commented 1 month ago

Do we currently have a way to do that from cast? Otherwise what choice do we have for private or internal immutable to get them onchain?

spaceh3ad commented 1 month ago

@Ethnical you can use cast storage or load storage slot with vm.load()

Ethnical commented 1 month ago

@spaceh3ad How this is possible since this is not inside the storage as this is inside the bytecode directly?

tmigone commented 1 month ago

+1 to this feature!