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

Ability to mine/wait for blocks in scripts #1902

Closed
naps62 opened this issue Jun 10, 2022 · 2 comments · Fixed by #2481
Closed

Ability to mine/wait for blocks in scripts #1902

naps62 opened this issue Jun 10, 2022 · 2 comments · Fixed by #2481
Labels
C-forge Command: forge Cmd-forge-script Command: forge script D-hard Difficulty: hard T-feature Type: feature

Comments

@naps62
Copy link
Contributor

naps62 commented Jun 10, 2022

Component

Forge

Describe the feature you would like

This is a follow-up on #1848 , which got closed even thought the bug (rust panic) still persists

In scripts, we can use --slow to force transactions to happen across different blocks. However, if those transactions rely on data such as blockhash(block.number - 1), forge will actually fail to simulate them, because no actual blocks and blockhashes are being created during that time. e.g., the following fails in a script, but works in a unit test:

        for (uint256 i = 0; i < 10; ++i) {
           vm.broadcast();
           submitTransaction();
            vm.roll(block.number + 1); // need to roll because `submitTransaction` didn't actually create a new block
            console.logBytes32(blockhash(block.number - 1));
        }

The specific error I get is :

Message:  Failed to get block hash for 10826800
block 10826800 not found

It would be very useful to be able to request our forked instance (anvil?) to simulate a new block being mined, which is the behaviour necessary here, and what suposedly --slow should enforce

Additional context

My real-world scenario here is actually trying to solve Ethernaut #03 on-chain using a forge script.
The script must:

  • deploy the ethernaut level instance
  • solve it, which requires submitting 10 different transactions across 10 different blocks

The current blocker for this is that each call relies on blockhash(block.number - 1), which doesn't seem to exist while simulating the script

@naps62 naps62 added the T-feature Type: feature label Jun 10, 2022
@onbjerg onbjerg added this to Foundry Jun 10, 2022
@onbjerg onbjerg moved this to Todo in Foundry Jun 10, 2022
@onbjerg onbjerg added C-forge Command: forge D-hard Difficulty: hard Cmd-forge-script Command: forge script labels Jun 10, 2022
@onbjerg
Copy link
Collaborator

onbjerg commented Jun 10, 2022

Marking this as hard because I think it will be - feel free to correct @joshieDo

@foufrix
Copy link

foufrix commented Feb 3, 2023

Hi, @naps62 did you manage to make it work with a while loop? I'm having exactly the same issue as you and launched it 10 times by hand to solve that, I'm pretty sure there could be a cleaner way to do that.

EDIT: actually working for testing locally, but not possible when using a script on testnet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-script Command: forge script D-hard Difficulty: hard T-feature Type: feature
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants