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.31k stars 1.75k forks source link

Forge is failing to deploy valid contract #7460

Closed PatrickAlphaC closed 7 months ago

PatrickAlphaC commented 7 months ago

Component

Forge

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

What version of Foundry are you on?

forge 0.2.0 (1a4960d 2024-03-20T00:28:07.727577000Z)

What command(s) is the bug in?

forge script

Operating System

macOS (Intel)

Describe the bug

Deploying certain bytecodes fail for foundry, or cause foundry to get confused.

The following

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

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

contract CounterScript is Script {
    function setUp() public {}

    function run() public {
        vm.startBroadcast();
        bytes memory bytecode = "0x60a88060093d393df3602035335f540114156100a457";
        address addr;
        assembly {
            addr := create(callvalue(), add(bytecode, 0x20), mload(bytecode))
        }
        require(addr != address(0), "Failed to deploy contract");
    }
}

This should deploy the 0x60a88060093d393df3602035335f540114156100a457 bytecode on chain, however, running:

forge script script/Counter.s.sol

We get the following output:

  [9079256848778899890] CounterScript::run()
    ├─ [0] VM::startBroadcast()
    │   └─ ← ()
    ├─ [2625] → new <unknown>@0x34A1D3fff3958843C43aD80F30b94c510645C316
    │   └─ ← 0 bytes of code
    └─ ← revert: Failed to deploy contract

Gas used: 9079256848778920954
Error: 
script failed: revert: Failed to deploy contract

I am able to deploy this contract to sepolia, no problem:

cast send --rpc-url $SEPOLIA_RPC_URL --private-key ew-plaintext-key --create 0x60a88060093d393df3602035335f540114156100a457

And I have done so here:

https://sepolia.etherscan.io/tx/0x98469582507a99a166bdb1ca58f3073fa4f5eb26bc5e3552571821b8e106b371

I ran into this as foundry was having a hard time running a test in --debug mode, where it kept crashing and not being able to read the opcodes on certain contracts. I'm not sure what that's about.

klkvr commented 7 months ago

@mattsse The CREATE failure reason coming from revm is InvalidOperandOOG, will look into when it might happen, but wondering if you might have a guess?

klkvr commented 7 months ago

this line: https://github.com/bluealloy/revm/blob/1d5faf0c847a6bc8c09ee5a5a6fb013504309f91/crates/interpreter/src/instructions/system.rs#L85

klkvr commented 7 months ago

@PatrickAlphaC "0x60a88060093d393df3602035335f540114156100a457" is interpreted as a string

to save hex bytes to bytecode, change it to bytecode = hex"60a88060093d393df3602035335f540114156100a457";

on my machine this change fixes the issue

PatrickAlphaC commented 7 months ago

That solved it!

PatrickAlphaC commented 7 months ago

Closed... but I'm wondering if I should have?

derekbar90 commented 5 months ago

@PatrickAlphaC Seeing this the past few days when running some calls against Telos, thanks for reporting.

prajwalghate commented 5 months ago

@PatrickAlphaC Seeing this the past few days when running some calls against Telos, thanks for reporting.

Same here while using foundry with telos. Happening with both test and script. You found any solution?

klkvr commented 5 months ago

@derekbar90 @prajwalghate could you please provide a repro we could use?

prajwalghate commented 5 months ago

hex

Hey @klkvr I created a repo solely to check if the problem persist. Getting the InvalidOperandOOG error.

Here is the repo telos-foundry-repo Running this command forge script script/PendingRewards.s.sol:PendingRewards --rpc-url https://mainnet.telos.net/evm -vvvv

prajwalghate commented 5 months ago

Hey, @klkvr any update on this, I tried from my side still couldn't figure out what is wrong

klkvr commented 5 months ago

tbh those network RPCs were behaving very weird when I was trying to test your repro :/ so not sure what is wrong exactly

maybe related https://x.com/pcaversaccio/status/1799053406792974822?s=46&t=4P6kq9daicxFwv9BbZasAA

prajwalghate commented 5 months ago

tbh those network RPCs were behaving very weird when I was trying to test your repro :/ so not sure what is wrong exactly

maybe related https://x.com/pcaversaccio/status/1799053406792974822?s=46&t=4P6kq9daicxFwv9BbZasAA

Yes, you were right it is related to rpc. This old rpc https://rpc3.telos.net/evm solved the issue. Thanks.

yiweichi commented 3 weeks ago

I met same issue, change it to: bytecode = hex"60a88060093d393df3602035335f540114156100a457"; as a hex string, fixes the issue.