Skip to content

Commit 1fefdbe

Browse files
committed
feat: reduce default gas limit to ~1B
1 parent bf51895 commit 1fefdbe

File tree

4 files changed

+39
-75
lines changed

4 files changed

+39
-75
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,
@@ -2070,11 +2070,11 @@ impl Default for Config {
20702070
prompt_timeout: 120,
20712071
sender: Self::DEFAULT_SENDER,
20722072
tx_origin: Self::DEFAULT_SENDER,
2073-
initial_balance: U256::from(0xffffffffffffffffffffffffu128),
2073+
initial_balance: U256::from((1u128 << 96) - 1),
20742074
block_number: 1,
20752075
fork_block_number: None,
20762076
chain: None,
2077-
gas_limit: i64::MAX.into(),
2077+
gas_limit: (1u64 << 30).into(),
20782078
code_size_limit: None,
20792079
gas_price: None,
20802080
block_base_fee_per_gas: 0,
@@ -2132,29 +2132,14 @@ impl Default for Config {
21322132
///
21332133
/// Due to this limitation this type will be serialized/deserialized as String if it's larger than
21342134
/// `i64`
2135-
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2136-
pub struct GasLimit(pub u64);
2135+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize)]
2136+
pub struct GasLimit(#[serde(deserialize_with = "crate::deserialize_u64_or_max")] pub u64);
21372137

21382138
impl From<u64> for GasLimit {
21392139
fn from(gas: u64) -> Self {
21402140
Self(gas)
21412141
}
21422142
}
2143-
impl From<i64> for GasLimit {
2144-
fn from(gas: i64) -> Self {
2145-
Self(gas as u64)
2146-
}
2147-
}
2148-
impl From<i32> for GasLimit {
2149-
fn from(gas: i32) -> Self {
2150-
Self(gas as u64)
2151-
}
2152-
}
2153-
impl From<u32> for GasLimit {
2154-
fn from(gas: u32) -> Self {
2155-
Self(gas as u64)
2156-
}
2157-
}
21582143

21592144
impl From<GasLimit> for u64 {
21602145
fn from(gas: GasLimit) -> Self {
@@ -2167,40 +2152,16 @@ impl Serialize for GasLimit {
21672152
where
21682153
S: Serializer,
21692154
{
2170-
if self.0 > i64::MAX as u64 {
2155+
if self.0 == u64::MAX {
2156+
serializer.serialize_str("max")
2157+
} else if self.0 > i64::MAX as u64 {
21712158
serializer.serialize_str(&self.0.to_string())
21722159
} else {
21732160
serializer.serialize_u64(self.0)
21742161
}
21752162
}
21762163
}
21772164

2178-
impl<'de> Deserialize<'de> for GasLimit {
2179-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2180-
where
2181-
D: Deserializer<'de>,
2182-
{
2183-
use serde::de::Error;
2184-
2185-
#[derive(Deserialize)]
2186-
#[serde(untagged)]
2187-
enum Gas {
2188-
Number(u64),
2189-
Text(String),
2190-
}
2191-
2192-
let gas = match Gas::deserialize(deserializer)? {
2193-
Gas::Number(num) => Self(num),
2194-
Gas::Text(s) => match s.as_str() {
2195-
"max" | "MAX" | "Max" | "u64::MAX" | "u64::Max" => Self(u64::MAX),
2196-
s => Self(s.parse().map_err(D::Error::custom)?),
2197-
},
2198-
};
2199-
2200-
Ok(gas)
2201-
}
2202-
}
2203-
22042165
/// Variants for selecting the [`Solc`] instance
22052166
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
22062167
#[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
@@ -851,6 +851,16 @@ impl<'a, DB: DatabaseExt> InspectorExt<DB> for InspectorStackRefMut<'a> {
851851
}
852852

853853
impl<DB: DatabaseExt> Inspector<DB> for InspectorStack {
854+
#[inline]
855+
fn step(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
856+
self.as_mut().step(interpreter, ecx)
857+
}
858+
859+
#[inline]
860+
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
861+
self.as_mut().step_end(interpreter, ecx)
862+
}
863+
854864
fn call(
855865
&mut self,
856866
context: &mut EvmContext<DB>,
@@ -896,14 +906,6 @@ impl<DB: DatabaseExt> Inspector<DB> for InspectorStack {
896906
fn selfdestruct(&mut self, contract: Address, target: Address, value: U256) {
897907
Inspector::<DB>::selfdestruct(&mut self.as_mut(), contract, target, value)
898908
}
899-
900-
fn step(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
901-
self.as_mut().step(interpreter, ecx)
902-
}
903-
904-
fn step_end(&mut self, interpreter: &mut Interpreter, ecx: &mut EvmContext<DB>) {
905-
self.as_mut().step_end(interpreter, ecx)
906-
}
907909
}
908910

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

0 commit comments

Comments
 (0)