Skip to content
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

Slow mode and skip simulation for script don't work #5776

Closed
2 tasks done
beeb opened this issue Sep 2, 2023 · 12 comments
Closed
2 tasks done

Slow mode and skip simulation for script don't work #5776

beeb opened this issue Sep 2, 2023 · 12 comments
Assignees
Labels
T-bug Type: bug

Comments

@beeb
Copy link
Contributor

beeb commented Sep 2, 2023

Component

Forge

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

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (2f4a77e 2023-08-31T00:32:46.319290372Z)

What command(s) is the bug in?

forge script

Operating System

Linux

Describe the bug

I'm trying to run multiple transactions in subsequent blocks as part of a single script run.

I came accross the --slow argument which is supposed to do that, and I found a PR that described that it would skip blocks between transactions in simulation mode.

However, when I try to run the script below with --slow, the second call reverts (meaning it's in the same block as the first one).

What's more, when trying to actually run the script with broadcast on testnet, I can't get past the simulation with the --skip-simulation argument and no transaction is broadcast at all (not even the contract deployment).

// script/Bug.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

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

contract Test {
    uint256 public lastBlock;

    function test() external {
        if (lastBlock == block.number) {
            revert();
        }
        lastBlock = block.number;
    }
}

/// Run with `forge script --slow --rpc-url sepolia script/Bug.sol:MyScript`
contract MyScript is Script {
    function run() public {
        uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
        vm.startBroadcast(deployerPrivateKey);
        Test test = new Test();

        for (uint256 i = 0; i < 10; i++) {
            console2.log(block.number);
            test.test();
        }

        console2.log(test.lastBlock());
        vm.stopBroadcast();
    }
}
Traces:
  [282344] → new MyScript@0x5b73C5498c1E3b4dbA84de0F1833c4a029d90519
    └─ ← 1189 bytes of code

  [115630] MyScript::run() 
    ├─ [0] VM::envUint(DEPLOYER_PRIVATE_KEY) [staticcall]
    │   └─ ← <env var value>
    ├─ [0] VM::startBroadcast(<pk>) 
    │   └─ ← ()
    ├─ [32487] → new Test@0x953f8C492E577E471F4C72e10D02a7f1893a011C
    │   └─ ← 162 bytes of code
    ├─ [0] console::log(4209527 [4.209e6]) [staticcall]
    │   └─ ← ()
    ├─ [22271] Test::test() 
    │   └─ ← ()
    ├─ [0] console::log(4209527 [4.209e6]) [staticcall]
    │   └─ ← ()
    ├─ [262] Test::test() 
    │   └─ ← "EvmError: Revert"
    └─ ← "EvmError: Revert"



== Logs ==
  4209527
  4209527
@beeb beeb added the T-bug Type: bug label Sep 2, 2023
@gakonst gakonst added this to Foundry Sep 2, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Sep 2, 2023
@beeb
Copy link
Contributor Author

beeb commented Sep 2, 2023

I just found another reported issue with script CLI arguments, maybe related (if the command ignores some arguments altogether) #5709

@emo-eth
Copy link
Contributor

emo-eth commented Sep 5, 2023

I can also report that --skip-simulation is not working on my end.

@imthatcarlos
Copy link

same, --skip-simulation stopped working a few days ago

@Sabnock01
Copy link
Contributor

@DaniPopes my hunch is that this is related to the recent directory reorg?

@Evalir
Copy link
Member

Evalir commented Sep 11, 2023

Looking at this

@Evalir Evalir self-assigned this Sep 11, 2023
@Evalir
Copy link
Member

Evalir commented Sep 11, 2023

hi all! tried to quickly repro but can't neither repro --skip-simulation nor --slow not working. Is it possible for y'all to post a more minimal repro?

@beeb
Copy link
Contributor Author

beeb commented Sep 11, 2023

Is it possible for y'all to post a more minimal repro?

@Evalir my repro is a small as it gets, one single file that you can run against a sepolia fork (I use an alias in my foundry.toml, see below).

forge script --slow --rpc-url sepolia script/Bug.sol:MyScript

The script above should NOT revert. The error is clearly shown in the logs: the first and second call to test() happen in the same block.

# foundry.toml
[rpc_endpoints]
sepolia = "https://sepolia.gateway.tenderly.co"

Which version did you use? I just reproduced now with forge 0.2.0 (5cfed63 2023-09-11T00:24:34.136213513Z)

@adu-web3
Copy link

Hi, do you have any workaround to enable skip local simulation? simulation is good but not friendly to evm chains with precompiles

@adu-web3
Copy link

@Evalir Hi sir, have you investigated into this problem? I'd like to help if anybody could share some insights on this

@Evalir
Copy link
Member

Evalir commented Dec 22, 2023

So, coming back to this @beeb / @adu-web3:

Slow mode and skip simulation both work as intended. What is happening here, and what happens with chains with custom precompiles, is a script limitation:

  • --skip-simulation avoids using the provided URL to do gas estimation. It does not prevent foundry from simulating the script locally. The latter is not possible to disable, as we must simulate the script to gather all the transactions to broadcast. If the local simulation fails, the whole script fails.
  • Right now it is impossible for forge script to simulate block changes, as we don't have safe evm manipulation cheatcodes for scripts (warp/roll equivalents) and foundry tests/simulations are run as a single transaction. This means that simulating this script locally is impossible. It works as intended if you remove the block.number check and use slow.
  • --slow does simulate mining if it's an onchain simulation. This is not the case for the unskippable local simulation. While this does mean that checking for block changes locally on scripts is impossible, it's still possible to create scripts that depend on transactions being mined, as the --slow flag will simulate this on-chain, and wait for tx receipts before sending the next tx in the sequence.

As a result, I'm going to close this specific issue as it's misleading (it does work), but it's a limitation which we should remove in a future script refactor and I'll document this in a new issue.

@Evalir Evalir closed this as completed Dec 22, 2023
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Dec 22, 2023
@beeb
Copy link
Contributor Author

beeb commented Dec 23, 2023

Hey @Evalir , thanks for the in-depth explanation.

So if I understand correctly, it's not currently possible to run with broadcast a script where each transaction must happen in a separate block due to a revert condition? The internal simulation of the script, which cannot be disabled, would fail and so the transaction discovery could not be done.

Let's hope a future refactor of the evm simulation makes this possible, as this would enable some deployments with safety locks preventing two transactions from being in the same block.

Cheers

@Mouradif
Copy link

Mouradif commented Sep 24, 2024

@Evalir

  • --skip-simulation avoids using the provided URL to do gas estimation. It does not prevent foundry from simulating the script locally. The latter is not possible to disable

In that case:

  • Is it possible to ask forge to simulate using eth_call to the provided RPC?
  • Or to provide forge with custom precompiles?
  • Or to run etch() in a script but just for the simulation part?

I have the same problem as #8163 and I saw that as of April (with PR: #7589) it's possible to inject precompiles to Anvil. Is there a way to do this for a forge script?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

7 participants