Skip to content

Commit 7a999fc

Browse files
committed
Simplify last_frame_result: remove initial_reservoir parameter and fix reservoir handling
- Remove unused `initial_reservoir` parameter from `last_frame_result` across all handlers - Fix precompile provider to save reservoir before overwriting gas tracker - Add deprecated `gas_used()` method on ExecutionResult pointing to `tx_gas_used()`
1 parent 712dac7 commit 7a999fc

5 files changed

Lines changed: 21 additions & 24 deletions

File tree

crates/context/interface/src/result.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ impl<HaltReasonTy> ExecutionResult<HaltReasonTy> {
503503
pub fn tx_gas_used(&self) -> u64 {
504504
self.gas().tx_gas_used()
505505
}
506+
507+
/// Returns the gas used.
508+
#[inline]
509+
#[deprecated(
510+
since = "32.0.0",
511+
note = "Use `tx_gas_used()` instead, `gas_used` is ambiguous after EIP-8037 state gas split"
512+
)]
513+
pub fn gas_used(&self) -> u64 {
514+
self.tx_gas_used()
515+
}
506516
}
507517

508518
impl<HaltReasonTy: fmt::Display> fmt::Display for ExecutionResult<HaltReasonTy> {

crates/handler/src/handler.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,12 @@ pub trait Handler {
220220
// Create first frame action
221221
// Note: first_frame_input now handles state gas deduction from the reservoir
222222
let first_frame_input = self.first_frame_input(evm, gas_limit, init_and_floor_gas)?;
223-
let initial_reservoir = first_frame_input.frame_input.reservoir();
224223

225224
// Run execution loop
226225
let mut frame_result = self.run_exec_loop(evm, first_frame_input)?;
227226

228227
// Handle last frame result
229-
self.last_frame_result(evm, &mut frame_result, initial_reservoir)?;
228+
self.last_frame_result(evm, &mut frame_result)?;
230229
Ok(frame_result)
231230
}
232231

@@ -448,7 +447,6 @@ pub trait Handler {
448447
&mut self,
449448
evm: &mut Self::Evm,
450449
frame_result: &mut <<Self::Evm as EvmTr>::Frame as FrameTr>::FrameResult,
451-
initial_reservoir: u64,
452450
) -> Result<(), Self::Error> {
453451
let instruction_result = frame_result.interpreter_result().result;
454452
let gas = frame_result.gas_mut();

crates/handler/src/precompile_provider.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
9494
let Some(precompile) = self.precompiles.get(&inputs.bytecode_address) else {
9595
return Ok(None);
9696
};
97-
97+
let reservoir = inputs.reservoir;
9898
let mut result = InterpreterResult {
9999
result: InstructionResult::Return,
100-
gas: Gas::new_with_regular_gas_and_reservoir(inputs.gas_limit, inputs.reservoir),
100+
gas: Gas::new_with_regular_gas_and_reservoir(inputs.gas_limit, reservoir),
101101
output: Bytes::new(),
102102
};
103103

@@ -119,11 +119,10 @@ impl<CTX: ContextTr> PrecompileProvider<CTX> for EthPrecompiles {
119119

120120
match exec_result {
121121
Ok(output) => {
122+
*result.gas.tracker_mut() = output.gas;
122123
// Preserve the reservoir before replacing the tracker.
123124
// Precompile output doesn't track reservoir, but we need to
124125
// propagate it back to the parent frame.
125-
let reservoir = result.gas.reservoir();
126-
*result.gas.tracker_mut() = output.gas;
127126
result.gas.set_reservoir(reservoir);
128127
result.result = if output.reverted {
129128
InstructionResult::Revert

crates/inspector/src/handler.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@ where
8989
// Note: first_frame_input already handles initial state gas deduction
9090
// from the reservoir (or gas_limit deficit).
9191
let first_frame_input = self.first_frame_input(evm, gas_limit, init_and_floor_gas)?;
92-
let initial_reservoir = first_frame_input.frame_input.reservoir();
9392

9493
// Run execution loop
9594
let mut frame_result = self.inspect_run_exec_loop(evm, first_frame_input)?;
9695

9796
// Handle last frame result
98-
self.last_frame_result(evm, &mut frame_result, initial_reservoir)?;
97+
self.last_frame_result(evm, &mut frame_result)?;
9998
Ok(frame_result)
10099
}
101100

crates/op-revm/src/handler.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ where
186186
&mut self,
187187
evm: &mut Self::Evm,
188188
frame_result: &mut <<Self::Evm as EvmTr>::Frame as FrameTr>::FrameResult,
189-
initial_reservoir: u64,
190189
) -> Result<(), Self::Error> {
191190
let ctx = evm.ctx();
192191
let tx = ctx.tx();
@@ -220,8 +219,7 @@ where
220219
// - Regular transactions report their gas used as normal.
221220
if !is_deposit || is_regolith {
222221
// Return unused regular gas and unused reservoir gas.
223-
gas.erase_cost(remaining + reservoir);
224-
gas.set_reservoir(reservoir);
222+
gas.erase_cost(remaining);
225223
gas.record_refund(refunded);
226224
} else if is_deposit && tx.is_system_transaction() {
227225
// System transactions were a special type of deposit transaction in
@@ -242,20 +240,14 @@ where
242240
// gas used on failure. Refunds on remaining gas enabled.
243241
// - Regular transactions receive a refund on remaining gas as normal.
244242
if !is_deposit || is_regolith {
245-
// Return unused regular gas and unused reservoir gas.
246-
gas.erase_cost(remaining + reservoir);
243+
// Return unused regular gas.
244+
gas.erase_cost(remaining);
247245
}
248-
// On revert, refill reservoir: state gas that spilled into regular gas
249-
// gets returned to the reservoir.
250-
gas.set_reservoir(initial_reservoir + state_gas_spent);
251-
} else {
252-
// On halt, refill reservoir.
253-
// TODO(state-gas)handle state gas.
254-
gas.set_reservoir(initial_reservoir.max(state_gas_spent));
255246
}
256247

257248
// Restore state_gas_spent on all paths (lost by Gas::new_spent overwrite).
258249
gas.set_state_gas_spent(state_gas_spent);
250+
gas.set_reservoir(reservoir);
259251

260252
Ok(())
261253
}
@@ -438,8 +430,7 @@ where
438430
// clear the journal
439431
output = Ok(ExecutionResult::Halt {
440432
reason: OpHaltReason::FailedDeposit,
441-
gas: ResultGas::default()
442-
.with_total_gas_spent(gas_used),
433+
gas: ResultGas::default().with_total_gas_spent(gas_used),
443434
logs: Vec::new(),
444435
})
445436
}
@@ -511,7 +502,7 @@ mod tests {
511502
OpHandler::<_, EVMError<_, OpTransactionError>, EthFrame<EthInterpreter>>::new();
512503

513504
handler
514-
.last_frame_result(&mut evm, &mut exec_result, 0)
505+
.last_frame_result(&mut evm, &mut exec_result)
515506
.unwrap();
516507
handler.refund(&mut evm, &mut exec_result, 0);
517508
*exec_result.gas()

0 commit comments

Comments
 (0)