foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Using SSTORE2 in tests can exceed contract size limit

Open frolic opened this issue 2 years ago • 2 comments

Component

Forge

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

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (359dd77 2022-09-15T00:04:51.833948Z)

What command(s) is the bug in?

forge test

Operating System

macOS (Apple Silicon)

Describe the bug

Depends on SSTORE2

Running the below test code allows for contracts to be created that exceed 24576 bytes (contract size limit)

pragma solidity ^0.8.9;

import "sstore2/SSTORE2.sol";
import "sstore2/utils/Bytecode.sol";

contract TestSSTORE2 {
    function testWrite() public {
        bytes memory data = bytes(vm.readFile("path/to/large/file"));
        console.log("data length", data.length);

        address memory pointer = SSTORE2.write(data);
        console.log("contract size", Bytecode.codeSize(pointer));
    }
}

frolic avatar Sep 16 '22 03:09 frolic

intrinsic gas too high

is thrown if gas exceeds block gas limit

you can manually set --gas-limit for anvil and bump it

mattsse avatar Sep 25 '22 13:09 mattsse

sorry for taking so long.

since you're writing an entire file to storage, the tx gas likely exceeds the max block gas limit, but you can bump this manually.

Closing this, lmk if you're still facing issues after increasing anvil's block gas limit

mattsse avatar Sep 25 '22 13:09 mattsse

I don’t think this is a gas limit issue. I can write well over the contract size limit before hitting anything close to a block size limit. It seems like anvil and forge tests aren’t respecting EIP-170? See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-170.md

frolic avatar Sep 25 '22 14:09 frolic

what's the error you get when sending a create tx for a large contract?

mattsse avatar Sep 25 '22 14:09 mattsse

what's the error you get when sending a create tx for a large contract?

The problem is there isn’t an error when I expected one. I can throw tons of data at it, well over the contract size limit, and get no errors in anvil/tests.

EIP-170 says anything over ~24kb should be an out of gas error. I can try running this against a testnet and confirm if that’s the error I get for >24kb.

frolic avatar Sep 25 '22 15:09 frolic

we're not enforcing size limits rn, but I think we should make this configurable.

still don't understand if your issue is that there's no error or whether there's something else wrong?

mattsse avatar Sep 25 '22 15:09 mattsse

we're not enforcing size limits rn, but I think we should make this configurable.

still don't understand if your issue is that there's no error or whether there's something else wrong?

The issue is that there is no error. Being able to toggle on/off size limits would be great!

I deleted the earlier comments and updated the original issue. Let me know if that’s more clear.

frolic avatar Sep 25 '22 15:09 frolic

thanks,

will add an option for this

mattsse avatar Sep 25 '22 16:09 mattsse

Is it possible to allow forge test to configure in the same way? Ultimately I want to write Solidity tests that will fail if the code size is exceeded.

frolic avatar Sep 30 '22 04:09 frolic

no but we could add if you open a separate issue

mattsse avatar Sep 30 '22 10:09 mattsse

we're not enforcing size limits rn, but I think we should make this configurable. still don't understand if your issue is that there's no error or whether there's something else wrong?

The issue is that there is no error. Being able to toggle on/off size limits would be great!

I deleted the earlier comments and updated the original issue. Let me know if that’s more clear.

I came into a similar situation. While EIP 170 introduced a limit of 24KB on bytecode size of smart contracts in Ethereum, NOT .sol file size. My 56KB .sol file compiles in Remix with optimization runs set to 200 and the compiled contract bytecode size was only 14KB in my case, which was far below the limit. Not sure if this is the same issue you met.

CaixiangFan avatar Nov 25 '23 17:11 CaixiangFan

While testing my deployment was failing with ABC is above the EIP-170 contract size limit (30134 > 24576).

with the added option of --code-size-limit 31000 to the forge script script/deploy.s.sol

It still fails with Execution reverted: EvmError: CreateContractSizeLimit (gas: 9223372036854775857)

TokenTitan avatar Feb 12 '24 04:02 TokenTitan

While testing my deployment was failing with ABC is above the EIP-170 contract size limit (30134 > 24576).

with the added option of --code-size-limit 31000 to the forge script script/deploy.s.sol

It still fails with Execution reverted: EvmError: CreateContractSizeLimit (gas: 9223372036854775857)

I have same problem.

kamikazebr avatar Feb 28 '24 21:02 kamikazebr