Skip to content

Commit 829f7c2

Browse files
committed
feat(interpreter): add helpers for spending all gas
1 parent 1ca3d39 commit 829f7c2

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

crates/interpreter/src/gas.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ impl Gas {
3535
}
3636
}
3737

38+
/// Creates a new `Gas` struct with the given gas limit, but without any gas remaining.
39+
#[inline]
40+
pub const fn new_spent(limit: u64) -> Self {
41+
Self {
42+
limit,
43+
remaining: 0,
44+
remaining_nomem: 0,
45+
memory: 0,
46+
refunded: 0,
47+
}
48+
}
49+
3850
/// Returns the gas limit.
3951
#[inline]
4052
pub const fn limit(&self) -> u64 {
@@ -79,6 +91,13 @@ impl Gas {
7991
self.remaining += returned;
8092
}
8193

94+
/// Spends all remaining gas.
95+
#[inline]
96+
pub fn spend_all(&mut self) {
97+
self.remaining = 0;
98+
self.remaining_nomem = 0;
99+
}
100+
82101
/// Records a refund value.
83102
///
84103
/// `refund` can be negative but `self.refunded` should always be positive

crates/revm/src/handler/mainnet/execution.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub fn frame_return_with_refund_flag<SPEC: Spec>(
2424
let refunded = gas.refunded();
2525

2626
// Spend the gas limit. Gas is reimbursed when the tx returns successfully.
27-
*gas = Gas::new(env.tx.gas_limit);
28-
gas.record_cost(env.tx.gas_limit);
27+
*gas = Gas::new_spent(env.tx.gas_limit);
2928

3029
match instruction_result {
3130
return_ok!() => {

crates/revm/src/inspector/gas.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ impl<DB: Database> Inspector<DB> for GasInspector {
6060
mut outcome: CallOutcome,
6161
) -> CallOutcome {
6262
if outcome.result.result.is_error() {
63-
outcome
64-
.result
65-
.gas
66-
.record_cost(outcome.result.gas.remaining());
63+
outcome.result.gas.spend_all();
6764
self.gas_remaining = 0;
6865
}
6966
outcome
@@ -76,10 +73,7 @@ impl<DB: Database> Inspector<DB> for GasInspector {
7673
mut outcome: CreateOutcome,
7774
) -> CreateOutcome {
7875
if outcome.result.result.is_error() {
79-
outcome
80-
.result
81-
.gas
82-
.record_cost(outcome.result.gas.remaining());
76+
outcome.result.gas.spend_all();
8377
self.gas_remaining = 0;
8478
}
8579
outcome

crates/revm/src/optimism/handler_register.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ pub fn last_frame_return<SPEC: Spec, EXT, DB: Database>(
8282
let remaining = gas.remaining();
8383
let refunded = gas.refunded();
8484
// Spend the gas limit. Gas is reimbursed when the tx returns successfully.
85-
*gas = Gas::new(tx_gas_limit);
86-
gas.record_cost(tx_gas_limit);
85+
*gas = Gas::new_spent(tx_gas_limit);
8786

8887
match instruction_result {
8988
return_ok!() => {

0 commit comments

Comments
 (0)