Skip to content

Commit 3e79baf

Browse files
authored
feat: reduce default gas limit to ~1B (#8274)
* feat: reduce default gas limit to ~1B * com
1 parent 8b56536 commit 3e79baf

File tree

5 files changed

+49
-81
lines changed

5 files changed

+49
-81
lines changed

crates/config/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ initial_balance = '0xffffffffffffffffffffffff'
124124
block_number = 0
125125
fork_block_number = 0
126126
chain_id = 1
127-
# NOTE due to a toml-rs limitation, this value needs to be a string if the desired gas limit exceeds `i64::MAX` (9223372036854775807)
128-
# `gas_limit = "Max"` is equivalent to `gas_limit = "18446744073709551615"`
129-
gas_limit = 9223372036854775807
127+
# NOTE due to a toml-rs limitation, this value needs to be a string if the desired gas limit exceeds 2**63-1 (9223372036854775807).
128+
# `gas_limit = "max"` is equivalent to `gas_limit = "18446744073709551615"`. This is not recommended
129+
# as it will make infinite loops effectively hang during execution.
130+
gas_limit = 1073741824
130131
gas_price = 0
131132
block_base_fee_per_gas = 0
132133
block_coinbase = '0x0000000000000000000000000000000000000000'

crates/config/src/lib.rs

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use inflector::Inflector;
3838
use regex::Regex;
3939
use revm_primitives::{FixedBytes, SpecId};
4040
use semver::Version;
41-
use serde::{Deserialize, Deserializer, Serialize, Serializer};
41+
use serde::{Deserialize, Serialize, Serializer};
4242
use std::{
4343
borrow::Cow,
4444
collections::HashMap,
@@ -2075,11 +2075,11 @@ impl Default for Config {
20752075
prompt_timeout: 120,
20762076
sender: Self::DEFAULT_SENDER,
20772077
tx_origin: Self::DEFAULT_SENDER,
2078-
initial_balance: U256::from(0xffffffffffffffffffffffffu128),
2078+
initial_balance: U256::from((1u128 << 96) - 1),
20792079
block_number: 1,
20802080
fork_block_number: None,
20812081
chain: None,
2082-
gas_limit: i64::MAX.into(),
2082+
gas_limit: (1u64 << 30).into(), // ~1B
20832083
code_size_limit: None,
20842084
gas_price: None,
20852085
block_base_fee_per_gas: 0,
@@ -2138,29 +2138,14 @@ impl Default for Config {
21382138
///
21392139
/// Due to this limitation this type will be serialized/deserialized as String if it's larger than
21402140
/// `i64`
2141-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2142-
pub struct GasLimit(pub u64);
2141+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
2142+
pub struct GasLimit(#[serde(deserialize_with = "crate::deserialize_u64_or_max")] pub u64);
21432143

21442144
impl From<u64> for GasLimit {
21452145
fn from(gas: u64) -> Self {
21462146
Self(gas)
21472147
}
21482148
}
2149-
impl From<i64> for GasLimit {
2150-
fn from(gas: i64) -> Self {
2151-
Self(gas as u64)
2152-
}
2153-
}
2154-
impl From<i32> for GasLimit {
2155-
fn from(gas: i32) -> Self {
2156-
Self(gas as u64)
2157-
}
2158-
}
2159-
impl From<u32> for GasLimit {
2160-
fn from(gas: u32) -> Self {
2161-
Self(gas as u64)
2162-
}
2163-
}
21642149

21652150
impl From<GasLimit> for u64 {
21662151
fn from(gas: GasLimit) -> Self {
@@ -2173,40 +2158,16 @@ impl Serialize for GasLimit {
21732158
where
21742159
S: Serializer,
21752160
{
2176-
if self.0 > i64::MAX as u64 {
2161+
if self.0 == u64::MAX {
2162+
serializer.serialize_str("max")
2163+
} else if self.0 > i64::MAX as u64 {
21772164
serializer.serialize_str(&self.0.to_string())
21782165
} else {
21792166
serializer.serialize_u64(self.0)
21802167
}
21812168
}
21822169
}
21832170

2184-
impl<'de> Deserialize<'de> for GasLimit {
2185-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2186-
where
2187-
D: Deserializer<'de>,
2188-
{
2189-
use serde::de::Error;
2190-
2191-
#[derive(Deserialize)]
2192-
#[serde(untagged)]
2193-
enum Gas {
2194-
Number(u64),
2195-
Text(String),
2196-
}
2197-
2198-
let gas = match Gas::deserialize(deserializer)? {
2199-
Gas::Number(num) => Self(num),
2200-
Gas::Text(s) => match s.as_str() {
2201-
"max" | "MAX" | "Max" | "u64::MAX" | "u64::Max" => Self(u64::MAX),
2202-
s => Self(s.parse().map_err(D::Error::custom)?),
2203-
},
2204-
};
2205-
2206-
Ok(gas)
2207-
}
2208-
}
2209-
22102171
/// Variants for selecting the [`Solc`] instance
22112172
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
22122173
#[serde(untagged)]

crates/config/src/utils.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -236,31 +236,31 @@ where
236236
}
237237
}
238238

239-
/// Deserialize an usize or
240-
pub(crate) fn deserialize_usize_or_max<'de, D>(deserializer: D) -> Result<usize, D::Error>
239+
/// Deserialize a `u64` or "max" for `u64::MAX`.
240+
pub(crate) fn deserialize_u64_or_max<'de, D>(deserializer: D) -> Result<u64, D::Error>
241241
where
242242
D: Deserializer<'de>,
243243
{
244244
#[derive(Deserialize)]
245245
#[serde(untagged)]
246246
enum Val {
247-
Number(usize),
248-
Text(String),
247+
Number(u64),
248+
String(String),
249249
}
250250

251-
let num = match Val::deserialize(deserializer)? {
252-
Val::Number(num) => num,
253-
Val::Text(s) => {
254-
match s.as_str() {
255-
"max" | "MAX" | "Max" => {
256-
// toml limitation
257-
i64::MAX as usize
258-
}
259-
s => s.parse::<usize>().map_err(D::Error::custom).unwrap(),
260-
}
261-
}
262-
};
263-
Ok(num)
251+
match Val::deserialize(deserializer)? {
252+
Val::Number(num) => Ok(num),
253+
Val::String(s) if s.eq_ignore_ascii_case("max") => Ok(u64::MAX),
254+
Val::String(s) => s.parse::<u64>().map_err(D::Error::custom),
255+
}
256+
}
257+
258+
/// Deserialize a `usize` or "max" for `usize::MAX`.
259+
pub(crate) fn deserialize_usize_or_max<'de, D>(deserializer: D) -> Result<usize, D::Error>
260+
where
261+
D: Deserializer<'de>,
262+
{
263+
deserialize_u64_or_max(deserializer)?.try_into().map_err(D::Error::custom)
264264
}
265265

266266
/// Helper type to parse both `u64` and `U256`

crates/evm/evm/src/inspectors/stack.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,16 @@ impl<'a, DB: DatabaseExt> InspectorExt<DB> for InspectorStackRefMut<'a> {
926926
}
927927

928928
impl<DB: DatabaseExt> Inspector<DB> for InspectorStack {
929+
#[inline]
930+
fn step(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
931+
self.as_mut().step(interpreter, ecx)
932+
}
933+
934+
#[inline]
935+
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
936+
self.as_mut().step_end(interpreter, ecx)
937+
}
938+
929939
fn call(
930940
&mut self,
931941
context: &mut EvmContext<DB>,
@@ -971,14 +981,6 @@ impl<DB: DatabaseExt> Inspector<DB> for InspectorStack {
971981
fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
972982
Inspector::<DB>::selfdestruct(&mut self.as_mut(), contract, target, value)
973983
}
974-
975-
fn step(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
976-
self.as_mut().step(interpreter, ecx)
977-
}
978-
979-
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
980-
self.as_mut().step_end(interpreter, ecx)
981-
}
982984
}
983985

984986
impl<DB: DatabaseExt> InspectorExt<DB> for InspectorStack {

crates/forge/tests/cli/ext_integration.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ fn forge_std() {
1010

1111
#[test]
1212
fn solmate() {
13-
let tester =
13+
let mut tester =
1414
ExtTester::new("transmissions11", "solmate", "c892309933b25c03d32b1b0d674df7ae292ba925");
1515

16-
#[cfg(feature = "isolate-by-default")]
17-
let tester = tester.args(["--nmc", "ReentrancyGuardTest"]);
16+
if cfg!(feature = "isolate-by-default") {
17+
tester = tester.args(["--nmc", "ReentrancyGuardTest"]);
18+
}
1819

1920
tester.run();
2021
}
@@ -42,10 +43,12 @@ fn prb_proxy() {
4243
#[test]
4344
#[cfg_attr(windows, ignore = "Windows cannot find installed programs")]
4445
fn sablier_v2() {
45-
let tester =
46+
let mut tester =
4647
ExtTester::new("sablier-labs", "v2-core", "84758a40077bf3ccb1c8f7bb8d00278e672fbfef")
4748
// Skip fork tests.
4849
.args(["--nmc", "Fork"])
50+
// Increase the gas limit: https://github.com/sablier-labs/v2-core/issues/956
51+
.args(["--gas-limit", u64::MAX.to_string().as_str()])
4952
// Run tests without optimizations.
5053
.env("FOUNDRY_PROFILE", "lite")
5154
.install_command(&["bun", "install", "--prefer-offline"])
@@ -54,8 +57,9 @@ fn sablier_v2() {
5457

5558
// This test reverts due to memory limit without isolation. This revert is not reached with
5659
// isolation because memory is divided between separate EVMs created by inner calls.
57-
#[cfg(feature = "isolate-by-default")]
58-
let tester = tester.args(["--nmt", "test_RevertWhen_LoopCalculationOverflowsBlockGasLimit"]);
60+
if cfg!(feature = "isolate-by-default") {
61+
tester = tester.args(["--nmt", "test_RevertWhen_LoopCalculationOverflowsBlockGasLimit"]);
62+
}
5963

6064
tester.run();
6165
}

0 commit comments

Comments
 (0)