Skip to content

Commit e41d52f

Browse files
committed
Fix total_gas_spent to exclude reservoir and use saturating arithmetic
Compute total_gas_spent as limit - remaining - reservoir in build_result_gas to correctly exclude unused state gas. Use saturating_sub in Gas::spent/total_gas_spent to prevent underflow. Add std feature to ee-tests revm dependency.
1 parent f77d65f commit e41d52f

3 files changed

Lines changed: 14 additions & 12 deletions

File tree

crates/ee-tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ workspace = true
1616
[dependencies]
1717
serde.workspace = true
1818
serde_json = { workspace = true, features = ["preserve_order"] }
19-
revm = { workspace = true, features = ["serde"] }
19+
revm = { workspace = true, features = ["serde", "std"] }
2020
op-revm = { workspace = true, features = ["serde"] }
2121

2222
[features]

crates/handler/src/post_execution.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ use primitives::{hardfork::SpecId, U256};
1010

1111
/// Builds a [`ResultGas`] from the execution [`Gas`] struct and [`InitialAndFloorGas`].
1212
pub fn build_result_gas(gas: &Gas, init_and_floor_gas: InitialAndFloorGas) -> ResultGas {
13-
// EIP-8037: tx_gas_used = tx.gas - gas_left - state_gas_left
14-
// total_gas_spent = limit - remaining = tx.gas - gas_left
15-
// Subtract reservoir to get the actual gas used (excluding unused state gas).
13+
let state_gas = gas
14+
.state_gas_spent()
15+
.saturating_add(init_and_floor_gas.initial_state_gas)
16+
.saturating_sub(init_and_floor_gas.eip7702_reservoir_refund);
17+
1618
ResultGas::default()
17-
.with_total_gas_spent(gas.total_gas_spent())
19+
.with_total_gas_spent(
20+
gas.limit()
21+
.saturating_sub(gas.remaining())
22+
.saturating_sub(gas.reservoir()),
23+
)
1824
.with_refunded(gas.refunded() as u64)
1925
.with_floor_gas(init_and_floor_gas.floor_gas)
20-
.with_state_gas_spent(
21-
gas.state_gas_spent()
22-
.saturating_add(init_and_floor_gas.initial_state_gas)
23-
.saturating_sub(init_and_floor_gas.eip7702_reservoir_refund),
24-
)
26+
.with_state_gas_spent(state_gas)
2527
}
2628

2729
/// Ensures minimum gas floor is spent according to EIP-7623.

crates/interpreter/src/gas.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ impl Gas {
112112
Use [`Gas::total_gas_spent`] instead"
113113
)]
114114
pub const fn spent(&self) -> u64 {
115-
self.limit - self.tracker.remaining()
115+
self.limit.saturating_sub(self.tracker.remaining())
116116
}
117117

118118
/// Returns the regular gas spent.
119119
#[inline]
120120
pub const fn total_gas_spent(&self) -> u64 {
121-
self.limit - self.tracker.remaining()
121+
self.limit.saturating_sub(self.tracker.remaining())
122122
}
123123

124124
/// Returns the final amount of gas used by subtracting the refund from spent gas.

0 commit comments

Comments
 (0)