Skip to content

Commit 12ebc19

Browse files
committed
refactor: ♻️ add with_floor parameter to effective_gas
1 parent c1f288a commit 12ebc19

File tree

4 files changed

+20
-37
lines changed

4 files changed

+20
-37
lines changed

src/standard/gasometer/mod.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod costs;
33
mod utils;
44

55
use alloc::vec::Vec;
6-
use core::cmp::{max, min};
6+
use core::cmp::max;
77

88
use evm_interpreter::{
99
Control, ExitError, ExitException, Machine, Opcode, Stack,
@@ -166,23 +166,23 @@ impl GasometerState {
166166
/// The effective used gas at the end of the transaction.
167167
///
168168
/// In case of revert, refunded gas are not taken into account.
169-
pub fn effective_gas(&self, with_refund: bool, config: &Config) -> U256 {
170-
let refunded_gas = if self.refunded_gas >= 0 {
171-
self.refunded_gas as u64
169+
pub fn effective_gas(&self, with_refund: bool, with_floor: bool, config: &Config) -> U256 {
170+
let refunded_gas = self.refunded_gas.max(0) as u64;
171+
172+
let used_gas = if with_refund {
173+
let max_refund = self.total_used_gas() / config.max_refund_quotient();
174+
self.total_used_gas() - refunded_gas.min(max_refund)
172175
} else {
173-
0
176+
self.total_used_gas()
174177
};
175178

176-
U256::from(if with_refund {
177-
self.gas_limit
178-
- (self.total_used_gas()
179-
- min(
180-
self.total_used_gas() / config.max_refund_quotient(),
181-
refunded_gas,
182-
))
179+
let used_gas = if with_floor {
180+
used_gas.max(self.floor_gas)
183181
} else {
184-
self.gas_limit - self.total_used_gas()
185-
})
182+
used_gas
183+
};
184+
185+
U256::from(self.gas_limit - used_gas)
186186
}
187187

188188
/// Create a submeter.
@@ -221,11 +221,6 @@ impl GasometerState {
221221
MergeStrategy::Discard => {}
222222
}
223223
}
224-
225-
/// Apply the floor gas cost for calldata as defined in EIP-7623
226-
pub fn apply_transaction_floor_cost(&mut self) {
227-
self.used_gas = max(self.used_gas, self.floor_gas);
228-
}
229224
}
230225

231226
/// The eval function of the entire gasometer.

src/standard/invoker/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -510,23 +510,16 @@ where
510510

511511
let result = work();
512512

513-
// Apply EIP-7623 floor cost before calculating effective gas
514-
if let Some(substate) = exit.substate.as_mut()
515-
&& invoke.config.eip7623_calldata_floor
516-
{
517-
substate.apply_transaction_floor_cost();
518-
}
519-
520513
let effective_gas = match result {
521514
Ok(_) => exit
522515
.substate
523516
.as_ref()
524-
.map(|s| s.effective_gas(true))
517+
.map(|s| s.effective_gas(true, invoke.config.eip7623_calldata_floor))
525518
.unwrap_or_default(),
526519
Err(ExitError::Reverted) => exit
527520
.substate
528521
.as_ref()
529-
.map(|s| s.effective_gas(false))
522+
.map(|s| s.effective_gas(false, invoke.config.eip7623_calldata_floor))
530523
.unwrap_or_default(),
531524
Err(_) => U256::zero(),
532525
};

src/standard/invoker/state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,5 @@ pub trait InvokerState: GasState + Sized {
4646
/// Whether the current state is in the static frame.
4747
fn is_static(&self) -> bool;
4848
/// Effective gas. The final used gas as reported by the transaction.
49-
fn effective_gas(&self, with_refund: bool) -> U256;
50-
/// Apply transaction floor cost for EIP-7623.
51-
fn apply_transaction_floor_cost(&mut self);
49+
fn effective_gas(&self, with_refund: bool, with_floor: bool) -> U256;
5250
}

src/standard/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,8 @@ impl<'config> InvokerState for State<'config> {
227227
self.gasometer.is_static
228228
}
229229

230-
fn effective_gas(&self, with_refund: bool) -> U256 {
231-
self.gasometer.effective_gas(with_refund, self.config)
232-
}
233-
234-
fn apply_transaction_floor_cost(&mut self) {
235-
self.gasometer.apply_transaction_floor_cost()
230+
fn effective_gas(&self, with_refund: bool, with_floor: bool) -> U256 {
231+
self.gasometer
232+
.effective_gas(with_refund, with_floor, self.config)
236233
}
237234
}

0 commit comments

Comments
 (0)