EIP-3978 · Stagnant · Core · 2021-09-16
EIP-3978: Gas refunds on reverts
Proposed that when a call reverts, users get back the gas paid for state changes that were thrown away (storage writes, logs, contract creation), keeping only the cost of access.
What problem does it solve?
When a transaction or a sub-call reverts, every state change it made is discarded. But the gas for those changes has already been charged as if they had persisted: a reverted SSTORE still costs the full write price, a reverted CREATE still costs 32,000 gas, and so on.
Worse, a revert also wipes out any gas refund the call had accumulated. The authors point out that it can be cheaper to undo work manually (for example, transfer tokens back) than to revert, just to keep the refund. That is the opposite of what a well-designed fee schedule should encourage.
How it works
Each call frame tracks a new counter, revert_gas_refund, starting at 0. Whenever one of these state-modifying operations runs, the counter grows by the operation's cost minus the warm-read cost (WARM_STORAGE_READ_COST, 100 gas, from EIP-2929):
SSTORELOG0toLOG4CALL(the value-transfer and new-account surcharges)CREATE,CREATE2SELFDESTRUCT
If the frame reverts, the existing behaviour (dropping that frame's normal refund) still happens, and then revert_gas_refund is credited to the transaction's refund. The net effect is that a reverted modification costs roughly what a read costs, about 100 gas, instead of its full price.
The motivation also suggests charging CREATE/CREATE2's 32,000 gas only when the returned bytecode is non-empty, though the specification section does not define this.
Sample code
Pseudocode of the accounting, adapted from the spec:
// on each SSTORE / LOGx / CALL / CREATE / CREATE2 / SELFDESTRUCT
call.revert_gas_refund += operation.gas - WARM_STORAGE_READ_COST; // 100
// when a call frame finishes
if (call.reverted) {
tx.gas_refund -= call.gas_refund; // existing behaviour
tx.gas_refund += call.revert_gas_refund; // new: refund discarded modifications
}
Things to know
- Status: Stagnant. It was not adopted; reverted operations are still charged in full on Ethereum.
- Test cases, reference implementation and security considerations were left as "TBD" in the spec.
- Refunds on Ethereum are capped as a fraction of gas used (EIP-3529 tightened this and removed most refund sources), so any revert refund would be subject to that cap as written.
- Related history: EIP-3298 proposed removing refunds; EIP-3529 is what shipped in London.