In the debugger layout, the SSTORE and JUMP opcode are 3969 and 6 gas, respectively.
Issues Identified:
The reported gas cost for the JUMP opcode is incorrect. It should be 8 gas, but it is currently reported as 6 gas.
The reported gas cost for the SSTORE opcode is incorrect. It should be 3967 gas (5000 - refund gas calculated in foundry test, see below explanation), but it is currently reported as 3969, leading to the inaccurate display of a 6-gas cost for the JUMP opcode.
Foundry does not include intrinsic gas, leading to miscalculations in refund gas when the total transaction cost is small.
Additional Info
The gas cost for the SSTORE opcode is dynamic and depends on various factors such as refund gas, warm access, and cold access.
Foundry only calculates the sum of opcode cost and does not include the intrinsic gas
Therefore, Foundry might fail to calculate the correct refund gas when the total transaction cost is low
The refund gas formula is min(refund_gas_accmulated_in_refund_counter, tx.gas_consumed // 5)
In this case,
On-chain: refund_gas_accmulated_in_refund_counter is 4800 and tx.gas_consumed is 26358 (before get refund gas)
Component
Forge
Have you ensured that all of these are up to date?
What version of Foundry are you on?
forge 0.2.0 (55dd5de 2023-11-24T00:21:15.586516000Z)
What command(s) is the bug in?
forge debug
Operating System
macOS (Apple Silicon)
Describe the bug
Repro example
Paste the contract
setNumber.sol
insrc
folderRun:
In the debugger layout, the
SSTORE
andJUMP
opcode are 3969 and 6 gas, respectively.Issues Identified:
JUMP
opcode is incorrect. It should be 8 gas, but it is currently reported as 6 gas.SSTORE
opcode is incorrect. It should be 3967 gas (5000 - refund gas calculated in foundry test, see below explanation), but it is currently reported as 3969, leading to the inaccurate display of a 6-gas cost for theJUMP
opcode.Additional Info
min(refund_gas_accmulated_in_refund_counter, tx.gas_consumed // 5)
refund_gas_accmulated_in_refund_counter
is 4800 andtx.gas_consumed
is 26358 (before get refund gas)refund_gas_accmulated_in_refund_counter
is 4800 andtx.gas_consumed
is 5166 (the sum of all opcode cost in this example)4800
refund gas (4800 = min(4800, 26358//5)
)1033
refund gas (1033 = min(4800, 5166//5)
)SSTORE
opcode costs 3967 (5000-1033) gasFurther Question
Is there a way to include intrinsic gas when calculating the refund gas or even include it in the gas report?