Skip to content

Commit 75b0e1e

Browse files
authored
fix(revm): drop reverted-batch refund on the frame result (#424)
On batch failure, rollback_state_gas and set_refunded(0) were applied to the transaction-level gas tracker, but last_frame_result had already copied it into the frame result that post-execution reads, so they had no effect. Apply them to the frame result's tracker instead. The validation-failure branch returns None and the handler rebuilds a pristine gas there, so its dead calls are removed. Adds a regression test: a reverted batch no longer keeps the EIP-3529 refund of a reverted storage clear.
1 parent cdad941 commit 75b0e1e

1 file changed

Lines changed: 116 additions & 6 deletions

File tree

‎crates/ev-revm/src/handler.rs‎

Lines changed: 116 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,12 @@ where
308308
evm.ctx_mut()
309309
.journal_mut()
310310
.checkpoint_revert(batch_checkpoint);
311-
gas.rollback_state_gas();
312-
gas.set_refunded(0);
311+
// last_frame_result already copied the tracker into the frame
312+
// result, so the rollback and refund drop must land there — it
313+
// is what post-execution reads.
314+
let frame_gas = frame_result.gas_mut().tracker_mut();
315+
frame_gas.rollback_state_gas();
316+
frame_gas.set_refunded(0);
313317
bump_create_nonce(evm, &base_tx);
314318
return Ok(Some(frame_result));
315319
}
@@ -320,8 +324,9 @@ where
320324
evm.ctx_mut()
321325
.journal_mut()
322326
.checkpoint_revert(batch_checkpoint);
323-
gas.rollback_state_gas();
324-
gas.set_refunded(0);
327+
// Returning None makes the handler rebuild a pristine
328+
// transaction-level gas (runtime_oog_result), so no gas
329+
// adjustment is needed here.
325330
evm.ctx_mut().set_tx(base_tx.clone());
326331
bump_create_nonce(evm, &base_tx);
327332
return Ok(None);
@@ -334,8 +339,9 @@ where
334339
evm.ctx_mut()
335340
.journal_mut()
336341
.checkpoint_revert(batch_checkpoint);
337-
gas.rollback_state_gas();
338-
gas.set_refunded(0);
342+
let frame_gas = frame_result.gas_mut().tracker_mut();
343+
frame_gas.rollback_state_gas();
344+
frame_gas.set_refunded(0);
339345
bump_create_nonce(evm, &base_tx);
340346
return Ok(Some(frame_result));
341347
}
@@ -859,6 +865,9 @@ mod tests {
859865
const GAS_PRICE: u128 = 200;
860866
const STORAGE_RUNTIME: [u8; 6] = [0x60, 0x01, 0x60, 0x00, 0x55, 0x00];
861867
const REVERT_RUNTIME: [u8; 5] = [0x60, 0x00, 0x60, 0x00, 0xfd];
868+
// PUSH1 0, PUSH1 0, SSTORE, STOP — clears slot 0 (accrues an EIP-3529 refund
869+
// when the slot held a non-zero value).
870+
const CLEAR_RUNTIME: [u8; 6] = [0x60, 0x00, 0x60, 0x00, 0x55, 0x00];
862871

863872
#[test]
864873
fn caller_fee_returns_error_on_payment_overflow() {
@@ -1406,6 +1415,107 @@ mod tests {
14061415
}
14071416
}
14081417

1418+
#[test]
1419+
fn batch_execution_drops_refund_on_failure() {
1420+
// First call clears a non-zero storage slot (EIP-3529 refund), second
1421+
// call reverts the batch. The refund counter lives in Gas, not the
1422+
// journal, so the checkpoint revert does not drop it: a failed batch
1423+
// must not keep the refund of a reverted call.
1424+
let caller = address!("0x0000000000000000000000000000000000000aaa");
1425+
let clear_contract = address!("0x0000000000000000000000000000000000000bbb");
1426+
let revert_contract = address!("0x0000000000000000000000000000000000000ccc");
1427+
1428+
let mut state = State::builder()
1429+
.with_database(CacheDB::<EmptyDB>::default())
1430+
.with_bundle_update()
1431+
.build();
1432+
1433+
state.insert_account(
1434+
caller,
1435+
AccountInfo {
1436+
balance: U256::from(10_000_000_000u64),
1437+
nonce: 0,
1438+
code_hash: KECCAK_EMPTY,
1439+
code: None,
1440+
account_id: None,
1441+
},
1442+
);
1443+
1444+
// clear_contract holds a non-zero slot 0 and sets it to zero when called.
1445+
state.insert_account_with_storage(
1446+
clear_contract,
1447+
AccountInfo {
1448+
balance: U256::ZERO,
1449+
nonce: 1,
1450+
code_hash: alloy_primitives::keccak256(CLEAR_RUNTIME.as_slice()),
1451+
code: Some(RevmBytecode::new_raw(Bytes::copy_from_slice(
1452+
CLEAR_RUNTIME.as_slice(),
1453+
))),
1454+
account_id: None,
1455+
},
1456+
std::iter::once((U256::ZERO, U256::from(1u64))).collect(),
1457+
);
1458+
1459+
state.insert_account(
1460+
revert_contract,
1461+
AccountInfo {
1462+
balance: U256::ZERO,
1463+
nonce: 1,
1464+
code_hash: alloy_primitives::keccak256(REVERT_RUNTIME.as_slice()),
1465+
code: Some(RevmBytecode::new_raw(Bytes::copy_from_slice(
1466+
REVERT_RUNTIME.as_slice(),
1467+
))),
1468+
account_id: None,
1469+
},
1470+
);
1471+
1472+
let mut evm_env: EvmEnv<SpecId> = EvmEnv::default();
1473+
evm_env.cfg_env.chain_id = 1;
1474+
evm_env.cfg_env.spec = SpecId::CANCUN;
1475+
evm_env.block_env.basefee = 1;
1476+
evm_env.block_env.gas_limit = 30_000_000;
1477+
evm_env.block_env.number = U256::from(1);
1478+
1479+
let mut evm = EvTxEvmFactory::default().create_evm(state, evm_env);
1480+
1481+
let calls = vec![
1482+
Call {
1483+
to: TxKind::Call(clear_contract),
1484+
value: U256::ZERO,
1485+
input: Bytes::new(),
1486+
},
1487+
Call {
1488+
to: TxKind::Call(revert_contract),
1489+
value: U256::ZERO,
1490+
input: Bytes::new(),
1491+
},
1492+
];
1493+
1494+
let tx_env = TxEnv {
1495+
caller,
1496+
gas_limit: 200_000,
1497+
gas_price: 1,
1498+
gas_priority_fee: Some(1),
1499+
chain_id: Some(1),
1500+
tx_type: TransactionType::Eip1559.into(),
1501+
..Default::default()
1502+
};
1503+
1504+
let result_and_state = evm
1505+
.transact_raw(EvTxEnv::with_calls(tx_env, calls))
1506+
.expect("batch execution should complete");
1507+
1508+
assert!(
1509+
matches!(result_and_state.result, ExecutionResult::Revert { .. }),
1510+
"unexpected execution result: {:?}",
1511+
result_and_state.result
1512+
);
1513+
// The reverted SSTORE-clear accrues a 4800-gas EIP-3529 refund. Without
1514+
// dropping it, that refund leaks into the charge (tx_gas_used 42_212);
1515+
// the failed batch must be charged the full 47_012.
1516+
assert_eq!(result_and_state.result.tx_gas_used(), 47_012);
1517+
}
1518+
14091519
#[test]
14101520
fn batch_execution_bumps_nonce_for_create_on_failure() {
14111521
let caller = address!("0x0000000000000000000000000000000000000aaa");

0 commit comments

Comments
 (0)