foundry-rs / foundry-rs/foundry

feat(cheatcodes): add `vm.callTimeout` and `vm.setGas` for improved gas control

Open
#7,892 2 comments 0 reactions 0 assignees View on GitHub
A-cheatcodes C-forge T-feature
Dominant language
Rust
Stars
10.6k
Forks
2.6k
Avg merge
18h 20m
Merged PRs (30d)
510

Description

### Component

Forge

### Describe the feature you would like

When you use `vm.pauseGasMetering()` and call an address, if the address is a contract address and the contract uses the `gasleft()` function in a loop, the call can get stuck in an infinite loop. The reason is that the `gasleft()` function keeps returning the same gas limit indefinitely. While using `vm.resumeGasMetering()` can prevent this issue, it's not always effective.

For instance, I have a script that calls many contracts, and I don't want to encounter gas limit exceeded or out-of-gas errors. Thus, setting the gas limit again after it's lowered by `vm.resumeGasMetering()` isn't always possible.

A possible solution is to introduce an option like `vm.callTimeout(address, data, timeout)`, which could be highly useful in various contexts such as fuzzing.

Alternatively, adding a function like `vm.setGas(uint256)` to adjust the gas limit inside the contract, allowing it to increase after execution, would be extremely beneficial.

## Proposed Feature:

1. **`vm.callTimeout(address, data, timeout)`**
- Calls an address with a specified data payload and a timeout.
- Terminates the call if the timeout duration is exceeded.

2. **`vm.setGas(uint256)`**
- Sets the gas limit to the specified value.
- Allows resetting the gas limit to a higher value after execution.

These options would offer developers more control over gas usage and prevent infinite loops in contract calls.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.