@@ -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
0 commit comments