Skip to content

Commit 2eb6654

Browse files
committed
refactor: remove state_gas field and rename cpu_gas to regular_gas
- Remove redundant `state_gas` accumulator from Gas struct (state gas is implicit: total_spent - regular_gas_spent) - Remove state_gas propagation between frames (add_state_gas, etc.) - state_gas! macro now uses record_remaining_cost directly - Rename cpu_gas_remaining → regular_gas_remaining throughout codebase - Update all related methods, constructors, and comments
1 parent 1246e88 commit 2eb6654

11 files changed

Lines changed: 94 additions & 138 deletions

File tree

crates/context/interface/src/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub trait Cfg {
9090

9191
/// Returns whether state gas (TIP-1016) is enabled.
9292
///
93-
/// When enabled, storage creation gas is tracked separately from execution gas.
93+
/// When enabled, storage creation gas is tracked separately from regular gas.
9494
fn is_state_gas_enabled(&self) -> bool;
9595
}
9696

crates/ee-tests/src/tip1016.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! TIP-1016 State Gas integration tests.
22
//!
33
//! Verifies dual-limit gas accounting where storage creation gas (state gas)
4-
//! is tracked separately from execution gas.
4+
//! is tracked separately from regular gas.
55
66
use revm::{
77
bytecode::opcode,
@@ -460,13 +460,13 @@ fn test_tip1016_call_existing_account() {
460460
);
461461
}
462462

463-
// ---- Category 4: CPU Gas Cap Enforcement ----
463+
// ---- Category 4: Regular Gas Cap Enforcement ----
464464

465-
/// 4.1 Tight CPU cap causes OOG.
466-
/// With gas_limit=cap=30,000, CPU budget = cap - initial_gas(21,000) = 9,000.
467-
/// The SSTORE execution needs ~22,106 gas beyond intrinsic, so 9,000 CPU is insufficient.
465+
/// 4.1 Tight regular gas cap causes OOG.
466+
/// With gas_limit=cap=30,000, regular gas budget = cap - initial_gas(21,000) = 9,000.
467+
/// The SSTORE needs ~22,106 regular gas beyond intrinsic, so 9,000 is insufficient.
468468
#[test]
469-
fn test_tip1016_cpu_cap_causes_oog() {
469+
fn test_tip1016_regular_gas_cap_causes_oog() {
470470
let bytecode = sstore_bytecode(0, 1);
471471

472472
// Baseline with 100k succeeds.
@@ -481,7 +481,7 @@ fn test_tip1016_cpu_cap_causes_oog() {
481481
.unwrap();
482482
assert!(baseline_result.is_success());
483483

484-
// State gas with gas_limit=cap=30,000 → CPU = 30k - 21k = 9k (insufficient).
484+
// State gas with gas_limit=cap=30,000 → regular gas = 30k - 21k = 9k (insufficient).
485485
let mut evm = state_gas_evm(bytecode, 30_000);
486486
let result = evm
487487
.transact_one(
@@ -494,7 +494,7 @@ fn test_tip1016_cpu_cap_causes_oog() {
494494

495495
assert!(
496496
result.is_halt(),
497-
"Expected OOG halt with tight CPU cap, got success with gas_used={}",
497+
"Expected OOG halt with tight regular gas cap, got success with gas_used={}",
498498
result.gas_used()
499499
);
500500
match &result {
@@ -508,9 +508,9 @@ fn test_tip1016_cpu_cap_causes_oog() {
508508
}
509509
}
510510

511-
/// 4.2 Adequate CPU cap: success.
511+
/// 4.2 Adequate regular gas cap: success.
512512
#[test]
513-
fn test_tip1016_cpu_cap_sufficient() {
513+
fn test_tip1016_regular_gas_cap_sufficient() {
514514
let bytecode = sstore_bytecode(0, 1);
515515

516516
let mut baseline = baseline_evm(bytecode.clone());
@@ -525,7 +525,7 @@ fn test_tip1016_cpu_cap_sufficient() {
525525
let baseline_gas = baseline_result.gas_used();
526526
assert!(baseline_result.is_success());
527527

528-
// cap=100,000 → cpu = 100,000 - 20,000 = 80,000. Plenty for execution.
528+
// cap=100,000 → regular gas = 100,000 - 20,000 = 80,000. Plenty.
529529
let mut evm = state_gas_evm(bytecode, 100_000);
530530
let result = evm
531531
.transact_one(
@@ -559,7 +559,7 @@ fn test_tip1016_state_gas_oog_remaining() {
559559
.unwrap();
560560
assert!(baseline_result.is_success());
561561

562-
// With state gas: gas_limit=50,000, cap=u64::MAX (no cpu constraint).
562+
// With state gas: gas_limit=50,000, cap=u64::MAX (no regular gas constraint).
563563
// Execution needs ~43,106 + 20,000 state gas = ~63,106 > 50,000 → OOG.
564564
let mut evm = state_gas_evm(bytecode, u64::MAX);
565565
let result = evm
@@ -674,14 +674,14 @@ fn test_tip1016_sstore_set_then_clear_refund() {
674674
);
675675
}
676676

677-
/// 6.2 State gas does not reduce CPU budget.
678-
/// With gas_limit=cap=50,000, CPU = cap - intrinsic(21,000) = 29,000.
679-
/// SSTORE execution gas ~22,106 fits in CPU. State gas (20,000) is separate.
677+
/// 6.2 State gas does not reduce regular gas budget.
678+
/// With gas_limit=cap=50,000, regular gas = cap - intrinsic(21,000) = 29,000.
679+
/// SSTORE regular gas ~22,106 fits in budget. State gas (20,000) is separate.
680680
/// Total gas_used = ~43,106 + 20,000 = ~63,106 which exceeds gas_limit=50,000.
681-
/// But this shows the *CPU* check passes — the OOG happens on `remaining`, not CPU.
681+
/// But this shows the *regular gas* check passes — the OOG happens on `remaining`, not regular gas.
682682
/// So we use gas_limit=100,000 and cap=100,000 to have enough remaining too.
683683
#[test]
684-
fn test_tip1016_state_gas_does_not_reduce_cpu() {
684+
fn test_tip1016_state_gas_does_not_reduce_regular_gas() {
685685
let bytecode = sstore_bytecode(0, 1);
686686

687687
// Baseline succeeds.
@@ -693,8 +693,8 @@ fn test_tip1016_state_gas_does_not_reduce_cpu() {
693693
let baseline_gas = baseline_result.gas_used();
694694

695695
// cap = baseline_gas + STATE_GAS_SSTORE_SET + 1 (just enough for execution + state).
696-
// This ensures the CPU budget = cap - intrinsic is tight but sufficient for execution,
697-
// proving state gas is not subtracted from the CPU budget.
696+
// This ensures regular gas budget = cap - intrinsic is tight but sufficient,
697+
// proving state gas is not subtracted from the regular gas budget.
698698
let tight_cap = baseline_gas + STATE_GAS_SSTORE_SET + 1;
699699
let mut evm = state_gas_evm(bytecode, tight_cap);
700700
let result = evm
@@ -708,7 +708,7 @@ fn test_tip1016_state_gas_does_not_reduce_cpu() {
708708

709709
assert!(
710710
result.is_success(),
711-
"Should succeed: state gas doesn't consume CPU budget. Gas used: {}",
711+
"Should succeed: state gas doesn't consume regular gas budget. Gas used: {}",
712712
result.gas_used()
713713
);
714714
let delta = result.gas_used() - baseline_gas;

crates/handler/src/frame.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl EthFrame<EthInterpreter> {
112112
is_static: bool,
113113
spec_id: SpecId,
114114
gas_limit: u64,
115-
cpu_gas_remaining: u64,
115+
regular_gas_remaining: u64,
116116
checkpoint: JournalCheckpoint,
117117
) {
118118
let Self {
@@ -134,7 +134,7 @@ impl EthFrame<EthInterpreter> {
134134
is_static,
135135
spec_id,
136136
gas_limit,
137-
cpu_gas_remaining,
137+
regular_gas_remaining,
138138
);
139139
*checkpoint_ref = checkpoint;
140140
}
@@ -152,7 +152,7 @@ impl EthFrame<EthInterpreter> {
152152
depth: usize,
153153
memory: SharedMemory,
154154
inputs: Box<CallInputs>,
155-
cpu_gas_remaining: u64,
155+
regular_gas_remaining: u64,
156156
) -> Result<ItemOrResult<FrameToken, FrameResult>, ERROR> {
157157
let gas = Gas::new(inputs.gas_limit);
158158
let return_result = |instruction_result: InstructionResult| {
@@ -251,7 +251,7 @@ impl EthFrame<EthInterpreter> {
251251
is_static,
252252
ctx.cfg().spec().into(),
253253
gas_limit,
254-
cpu_gas_remaining,
254+
regular_gas_remaining,
255255
checkpoint,
256256
);
257257
Ok(ItemOrResult::Item(this.consume()))
@@ -268,7 +268,7 @@ impl EthFrame<EthInterpreter> {
268268
depth: usize,
269269
memory: SharedMemory,
270270
inputs: Box<CreateInputs>,
271-
cpu_gas_remaining: u64,
271+
regular_gas_remaining: u64,
272272
) -> Result<ItemOrResult<FrameToken, FrameResult>, ERROR> {
273273
let spec = context.cfg().spec().into();
274274
let return_error = |e| {
@@ -354,7 +354,7 @@ impl EthFrame<EthInterpreter> {
354354
false,
355355
spec,
356356
gas_limit,
357-
cpu_gas_remaining,
357+
regular_gas_remaining,
358358
checkpoint,
359359
);
360360
Ok(ItemOrResult::Item(this.consume()))
@@ -378,7 +378,7 @@ impl EthFrame<EthInterpreter> {
378378
depth,
379379
memory,
380380
frame_input,
381-
cpu_gas_remaining,
381+
regular_gas_remaining,
382382
} = frame_init;
383383

384384
match frame_input {
@@ -389,10 +389,10 @@ impl EthFrame<EthInterpreter> {
389389
depth,
390390
memory,
391391
inputs,
392-
cpu_gas_remaining,
392+
regular_gas_remaining,
393393
),
394394
FrameInput::Create(inputs) => {
395-
Self::make_create_frame(this, ctx, depth, memory, inputs, cpu_gas_remaining)
395+
Self::make_create_frame(this, ctx, depth, memory, inputs, regular_gas_remaining)
396396
}
397397
FrameInput::Empty => unreachable!(),
398398
}
@@ -418,7 +418,7 @@ impl EthFrame<EthInterpreter> {
418418
frame_input,
419419
depth,
420420
memory: self.interpreter.memory.new_child_context(),
421-
cpu_gas_remaining: self.interpreter.gas.cpu_gas_remaining(),
421+
regular_gas_remaining: self.interpreter.gas.regular_gas_remaining(),
422422
}));
423423
}
424424
InterpreterAction::Return(result) => result,
@@ -501,15 +501,13 @@ impl EthFrame<EthInterpreter> {
501501
.memory
502502
.set(mem_start, &interpreter.return_data.buffer()[..target_len]);
503503
}
504-
// CPU gas ALWAYS propagates (CPU work happened regardless of outcome)
504+
// Regular gas ALWAYS propagates (work happened regardless of outcome)
505505
interpreter
506506
.gas
507-
.set_cpu_gas_remaining(out_gas.cpu_gas_remaining());
507+
.set_regular_gas_remaining(out_gas.regular_gas_remaining());
508508

509509
if ins_result.is_ok() {
510510
interpreter.gas.record_refund(out_gas.refunded());
511-
// State gas only propagates on success
512-
interpreter.gas.add_state_gas(out_gas.state_gas());
513511
}
514512
}
515513
FrameResult::Create(outcome) => {
@@ -536,13 +534,11 @@ impl EthFrame<EthInterpreter> {
536534
if instruction_result.is_ok_or_revert() {
537535
this_gas.erase_cost(outcome.gas().remaining());
538536
}
539-
// CPU gas ALWAYS propagates (CPU work happened regardless of outcome)
540-
this_gas.set_cpu_gas_remaining(outcome.gas().cpu_gas_remaining());
537+
// Regular gas ALWAYS propagates (work happened regardless of outcome)
538+
this_gas.set_regular_gas_remaining(outcome.gas().regular_gas_remaining());
541539

542540
let stack_item = if instruction_result.is_ok() {
543541
this_gas.record_refund(outcome.gas().refunded());
544-
// State gas only propagates on success
545-
this_gas.add_state_gas(outcome.gas().state_gas());
546542
outcome.address.unwrap_or_default().into_word().into()
547543
} else {
548544
U256::ZERO
@@ -615,7 +611,7 @@ pub fn return_create<JOURNAL: JournalTr, CFG: Cfg>(
615611
let state_gas_for_code = cfg
616612
.gas_params()
617613
.code_deposit_state_gas(interpreter_result.output.len());
618-
if state_gas_for_code > 0 && !interpreter_result.gas.record_state_gas(state_gas_for_code) {
614+
if state_gas_for_code > 0 && !interpreter_result.gas.record_remaining_cost(state_gas_for_code) {
619615
if spec_id.is_enabled_in(HOMESTEAD) {
620616
journal.checkpoint_revert(checkpoint);
621617
interpreter_result.result = InstructionResult::OutOfGas;

crates/handler/src/handler.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,10 @@ pub trait Handler {
322322
let mut memory = SharedMemory::new_with_buffer(ctx.local().shared_memory_buffer().clone());
323323
memory.set_memory_limit(ctx.cfg().memory_limit());
324324

325-
// For the first frame, determine CPU gas remaining.
326-
// When state gas is enabled, cpu_gas_remaining = min(tx_gas_limit_cap, tx.gas_limit) - initial_gas.
327-
// On mainnet (state gas disabled), cpu_gas_remaining = u64::MAX (no CPU cap tracking).
328-
let cpu_gas_remaining = if ctx.cfg().is_state_gas_enabled() {
325+
// For the first frame, determine regular gas remaining.
326+
// When state gas is enabled, regular_gas_remaining = min(tx_gas_limit_cap, tx.gas_limit) - initial_gas.
327+
// On mainnet (state gas disabled), regular_gas_remaining = u64::MAX (no regular gas cap tracking).
328+
let regular_gas_remaining = if ctx.cfg().is_state_gas_enabled() {
329329
let initial_gas = ctx.tx().gas_limit() - gas_limit;
330330
core::cmp::min(ctx.cfg().tx_gas_limit_cap(), ctx.tx().gas_limit())
331331
.saturating_sub(initial_gas)
@@ -359,7 +359,7 @@ pub trait Handler {
359359
depth: 0,
360360
memory,
361361
frame_input: execution::create_init_frame(tx, bytecode, gas_limit),
362-
cpu_gas_remaining,
362+
regular_gas_remaining,
363363
})
364364
}
365365

@@ -374,21 +374,19 @@ pub trait Handler {
374374
let gas = frame_result.gas_mut();
375375
let remaining = gas.remaining();
376376
let refunded = gas.refunded();
377-
let cpu_remaining = gas.cpu_gas_remaining();
378-
let state_gas_acc = gas.state_gas();
377+
let regular_remaining = gas.regular_gas_remaining();
379378

380379
// Spend the gas limit. Gas is reimbursed when the tx returns successfully.
381380
*gas = Gas::new_spent(evm.ctx().tx().gas_limit());
382381

383382
if instruction_result.is_ok_or_revert() {
384383
gas.erase_cost(remaining);
385384
}
386-
// CPU gas always preserved (reflects actual CPU consumption)
387-
gas.set_cpu_gas_remaining(cpu_remaining);
385+
// Regular gas always preserved (reflects actual consumption)
386+
gas.set_regular_gas_remaining(regular_remaining);
388387

389388
if instruction_result.is_ok() {
390389
gas.record_refund(refunded);
391-
gas.add_state_gas(state_gas_acc);
392390
}
393391
Ok(())
394392
}

0 commit comments

Comments
 (0)