@@ -95,8 +95,8 @@ impl SearchRange {
95
95
pub enum EstimationResult {
96
96
/// The estimation was successful, the result is the gas estimation.
97
97
Success {
98
- /// The gas estimation.
99
- estimation : u64 ,
98
+ /// The input estimation limit .
99
+ limit : u64 ,
100
100
/// The amount of gas that was refunded to the caller as unused.
101
101
refund : u64 ,
102
102
/// The amount of gas used in the execution.
@@ -106,13 +106,17 @@ pub enum EstimationResult {
106
106
} ,
107
107
/// Estimation failed due to contract revert.
108
108
Revert {
109
+ /// The input estimation limit.
110
+ limit : u64 ,
109
111
/// The revert reason.
110
112
reason : Bytes ,
111
113
/// The amount of gas used in the execution.
112
114
gas_used : u64 ,
113
115
} ,
114
116
/// The estimation failed due to EVM halt.
115
117
Halt {
118
+ /// The input estimation limit.
119
+ limit : u64 ,
116
120
/// The halt reason.
117
121
reason : HaltReason ,
118
122
/// The amount of gas used in the execution
@@ -123,18 +127,17 @@ pub enum EstimationResult {
123
127
impl core:: fmt:: Display for EstimationResult {
124
128
fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
125
129
match self {
126
- Self :: Success { estimation , refund, gas_used, .. } => {
130
+ Self :: Success { limit , refund, gas_used, .. } => {
127
131
write ! (
128
132
f,
129
- "Success {{ estimation: {}, refund: {}, gas_used: {}, .. }}" ,
130
- estimation, refund, gas_used
133
+ "Success {{ gas_limit: {limit}, refund: {refund}, gas_used: {gas_used}, .. }}" ,
131
134
)
132
135
}
133
- Self :: Revert { gas_used, .. } => {
134
- write ! ( f, "Revert {{ gas_used: {}, .. }}" , gas_used )
136
+ Self :: Revert { limit , gas_used, .. } => {
137
+ write ! ( f, "Revert {{ gas_limit: {limit}, gas_used: {gas_used }, .. }}" )
135
138
}
136
- Self :: Halt { reason, gas_used } => {
137
- write ! ( f, "Halt {{ reason: {:?}, gas_used: {} }}" , reason , gas_used )
139
+ Self :: Halt { limit , reason, gas_used } => {
140
+ write ! ( f, "Halt {{ gas_limit: {limit}, reason: {reason :?}, gas_used: {gas_used } }}" )
138
141
}
139
142
}
140
143
}
@@ -146,24 +149,24 @@ impl EstimationResult {
146
149
pub fn from_limit_and_execution_result ( limit : u64 , value : & ExecutionResult ) -> Self {
147
150
match value {
148
151
ExecutionResult :: Success { gas_used, output, gas_refunded, .. } => Self :: Success {
149
- estimation : limit,
152
+ limit,
150
153
refund : * gas_refunded,
151
154
gas_used : * gas_used,
152
155
output : output. clone ( ) ,
153
156
} ,
154
157
ExecutionResult :: Revert { output, gas_used } => {
155
- Self :: Revert { reason : output. clone ( ) , gas_used : * gas_used }
158
+ Self :: Revert { limit , reason : output. clone ( ) , gas_used : * gas_used }
156
159
}
157
160
ExecutionResult :: Halt { reason, gas_used } => {
158
- Self :: Halt { reason : * reason, gas_used : * gas_used }
161
+ Self :: Halt { limit , reason : * reason, gas_used : * gas_used }
159
162
}
160
163
}
161
164
}
162
165
163
166
/// Create a successful estimation result with a gas estimation of 21000.
164
167
pub const fn basic_transfer_success ( estimation : u64 ) -> Self {
165
168
Self :: Success {
166
- estimation,
169
+ limit : estimation,
167
170
refund : 0 ,
168
171
gas_used : estimation,
169
172
output : Output :: Call ( Bytes :: new ( ) ) ,
@@ -180,11 +183,13 @@ impl EstimationResult {
180
183
!self . is_success ( )
181
184
}
182
185
183
- /// Get the gas estimation, if the execution was successful.
184
- pub const fn gas_estimation ( & self ) -> Option < u64 > {
186
+ /// Get the gas limit that was set in the EVM when the estimation was
187
+ /// produced.
188
+ pub const fn limit ( & self ) -> u64 {
185
189
match self {
186
- Self :: Success { estimation, .. } => Some ( * estimation) ,
187
- _ => None ,
190
+ Self :: Success { limit, .. } => * limit,
191
+ Self :: Revert { limit, .. } => * limit,
192
+ Self :: Halt { limit, .. } => * limit,
188
193
}
189
194
}
190
195
@@ -242,13 +247,12 @@ impl EstimationResult {
242
247
/// Adjust the binary search range based on the estimation outcome.
243
248
pub ( crate ) const fn adjust_binary_search_range (
244
249
& self ,
245
- limit : u64 ,
246
250
range : & mut SearchRange ,
247
251
) -> Result < ( ) , Self > {
248
252
match self {
249
- Self :: Success { .. } => range. set_max ( limit) ,
250
- Self :: Revert { .. } => range. set_min ( limit) ,
251
- Self :: Halt { reason, gas_used } => {
253
+ Self :: Success { limit , .. } => range. set_max ( * limit) ,
254
+ Self :: Revert { limit , .. } => range. set_min ( * limit) ,
255
+ Self :: Halt { limit , reason, gas_used } => {
252
256
// Both `OutOfGas` and `InvalidEFOpcode` can occur dynamically
253
257
// if the gas left is too low. Treat this as an out of gas
254
258
// condition, knowing that the call succeeds with a
@@ -257,9 +261,9 @@ impl EstimationResult {
257
261
// Common usage of invalid opcode in OpenZeppelin:
258
262
// <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/94697be8a3f0dfcd95dfb13ffbd39b5973f5c65d/contracts/metatx/ERC2771Forwarder.sol#L360-L367>
259
263
if matches ! ( reason, HaltReason :: OutOfGas ( _) | HaltReason :: InvalidFEOpcode ) {
260
- range. set_min ( limit) ;
264
+ range. set_min ( * limit) ;
261
265
} else {
262
- return Err ( Self :: Halt { reason : * reason, gas_used : * gas_used } ) ;
266
+ return Err ( Self :: Halt { limit : * limit , reason : * reason, gas_used : * gas_used } ) ;
263
267
}
264
268
}
265
269
}
0 commit comments