Skip to content
generated from refcell/femplate

A demo for test smart contract's dynamic data's storage slot's, seuch as array, map, using Foundry

License

Notifications You must be signed in to change notification settings

0xevm/sloads_demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sloads Demo • tests license solidity

A demo for test smart contract's dynamic data's storage slot's, such as array, map, using Foundry.

Usage

Install Modified Forge

$ git clone https://github.com/0xevm/foundry.git

$ cd foundry
  
$ git checkout beijing-eth

$ cargo build --release -p foundry-cli --bin forge

# !! update the PATH to your foundry build release path
$ export PATH="/Users/flyq/workspace/github/0xevm/foundry/target/release:$PATH"

# confirm the forge is the modified forge
$ which forge
/Users/flyq/workspace/github/0xevm/foundry/target/release/forge

Building & Testing

$ forge build

$ forge test -vvv

Get the storage slot

$ forge inspect ./src/RecordMapping.sol:RecordMapping storage --pretty
| Name       | Type                                         | Slot | Offset | Bytes | Contract                            |
| ---------- | -------------------------------------------- | ---- | ------ | ----- | ----------------------------------- |
| length     | int256                                       | 0    | 0      | 32    | src/RecordMapping.sol:RecordMapping |
| data       | mapping(address => int256)                   | 1    | 0      | 32    | src/RecordMapping.sol:RecordMapping |
| nestedData | mapping(int256 => mapping(int256 => int256)) | 2    | 0      | 32    | src/RecordMapping.sol:RecordMapping |

Debug to see the stack&memory&storage

$ forge test --debug testRecordMapping

New cheatcode

    /// Let foundry-evm start recording Storage slot related data.
    function startMappingRecording() external;

    /// Get the length of a map in a smart contract.
    /// @param `target`, the address of smart contract.
    /// @param `slot`, the slot of a map or a array.
    function getMappingLength(address target, bytes32 slot) external returns (uint);

    /// Get the storage slot index of one item of a map
    function getMappingSlotAt(address target, bytes32 slot, uint256 idx) external returns (bytes32);

    /// Get the map item's key in the slot.
    function getMappingKeyOf(address target, bytes32 slot) external returns (uint);
    
    /// Get the slot below which map in a contract
    function getMappingParentOf(address target, bytes32 slot) external returns (bytes32);

Todo

update the interface name, as they also can used in array.

Notable Mentions

Disclaimer

These smart contracts are being provided as is. No guarantee, representation or warranty is being made, express or implied, as to the safety or correctness of the user interface or the smart contracts. They have not been audited and as such there can be no assurance they will work as intended, and users may experience delays, failures, errors, omissions, loss of transmitted information or loss of funds. The creators are not liable for any of the foregoing. Users should proceed with caution and use at their own risk.

See LICENSE for more details.

About

A demo for test smart contract's dynamic data's storage slot's, seuch as array, map, using Foundry

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published