Skip to content

Commit a32d151

Browse files
authored
fix: gas estimation does not return revert when a previous iteration succeeded (#113)
* fix: gas estimation does not return revert when a previous iteration succeeded * chore: bump version
1 parent fc58b46 commit a32d151

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.23.7"
3+
version = "0.23.8"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/est.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ impl EstimationResult {
263263
if matches!(reason, HaltReason::OutOfGas(_) | HaltReason::InvalidFEOpcode) {
264264
range.set_min(*limit);
265265
} else {
266+
// NB: can't clone here as this is a const fn.
266267
return Err(Self::Halt { limit: *limit, reason: *reason, gas_used: *gas_used });
267268
}
268269
}

src/evm.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,8 @@ where
17291729
self,
17301730
filler: &crate::fillers::GasEstimationFiller,
17311731
) -> Result<(crate::EstimationResult, Self), EvmErrored<Db, Insp>> {
1732+
use tracing::trace;
1733+
17321734
let mut estimation = std::mem::MaybeUninit::uninit();
17331735

17341736
let this = self.try_with_estimate_gas_filler(filler, |this| match this.run() {
@@ -1744,6 +1746,7 @@ where
17441746
// SAFETY: if we did not shortcut return, then estimation was
17451747
// definitely written
17461748
Ok((unsafe { estimation.assume_init() }, this))
1749+
.inspect(|(est, _)| trace!(?est, "gas estimation result",))
17471750
}
17481751

17491752
/// Implements gas estimation. This will output an estimate of the minimum
@@ -1848,7 +1851,10 @@ where
18481851
trace!(%estimate, "optimistic estimate succeeded");
18491852

18501853
// Now we know that it succeeds at _some_ gas limit. We can now binary
1851-
// search.
1854+
// search. We start by recording the initial best estimate. We'll update
1855+
// this best-estimate as we go inside the `estimate_and_adjust` macro
1856+
// invocations.
1857+
let mut best = estimate.clone();
18521858
let mut gas_used = estimate.gas_used();
18531859
let gas_refunded = estimate.gas_refunded().expect("checked is_failure");
18541860

@@ -1868,7 +1874,7 @@ where
18681874

18691875
// If the first search is outside the range, we don't need to try it.
18701876
if search_range.contains(needle) {
1871-
estimate_and_adjust!(estimate, trevm, needle, search_range);
1877+
estimate_and_adjust!(best, estimate, trevm, needle, search_range);
18721878
// NB: `estimate` is rebound in the macro, so do not move this line
18731879
// up.
18741880
gas_used = estimate.gas_used();
@@ -1885,11 +1891,11 @@ where
18851891
// highest gas limit)
18861892
// <https://github.com/ethereum/go-ethereum/blob/a5a4fa7032bb248f5a7c40f4e8df2b131c4186
18871893
while search_range.size() > 1 && search_range.ratio() > 0.015 {
1888-
estimate_and_adjust!(estimate, trevm, needle, search_range);
1894+
estimate_and_adjust!(best, estimate, trevm, needle, search_range);
18891895
needle = search_range.midpoint();
18901896
}
18911897

1892-
Ok((estimate, trevm))
1898+
Ok((best, trevm))
18931899
}
18941900
}
18951901

src/macros.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,18 @@ macro_rules! trevm_bail {
4242
/// Macro for gas estimation binary search loop.
4343
#[cfg(feature = "estimate_gas")]
4444
macro_rules! estimate_and_adjust {
45-
($est:ident, $trevm:ident, $gas_limit:ident, $range:ident) => {
45+
($best:ident, $est:ident, $trevm:ident, $gas_limit:ident, $range:ident) => {
4646
::tracing::trace!(
47-
estimate = %$est,
47+
best = $best.gas_used(),
4848
max = %$range.max(),
4949
min = %$range.min(),
50-
"running gas estimate call"
50+
needle = $gas_limit,
51+
"running gas estimation"
5152
);
52-
5353
($est, $trevm) = $trevm.run_estimate(&$gas_limit.into())?;
54+
if $est.is_success() {
55+
$best = $est.clone();
56+
}
5457
if let Err(e) = $est.adjust_binary_search_range(&mut $range) {
5558
::tracing::trace!(
5659
%e,

0 commit comments

Comments
 (0)