Incorrect gas cost for SSTORE opcode when using fork feature
See original GitHub issueExecuting transactions that use SSTORE opcode costs less gas when using fork feature.
Expected Behavior
SSTORE that sets the value v
in the slot s
should cost 20k gas when the current value in slot s
is 0x0000000000000000000000000000000000000000000000000000000000000000
and v
is not 0x0000000000000000000000000000000000000000000000000000000000000000
.
Current Behavior
SSTORE mentioned above costs 5k gas.
Possible Solution
This is probably an issue with the gas calculator, SSTORE should cost 5k gas when the current value in the slot is not
0x0000000000000000000000000000000000000000000000000000000000000000
Steps to Reproduce (for bugs)
- ganache-cli -f mainnet_node --networkId 1
- truffle migrate
- Saving migration to the chain costs 27363 gas, but it should cost 42363 gas.
Your Environment
- Version used: Ganache CLI v6.10.1-beta.2 (ganache-core: 2.11.3-beta.0)
- Operating System and version: macOS Catalina 10.15.4
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:14 (12 by maintainers)
Top Results From Across the Web
Incorrect gas cost for SSTORE opcode when using fork feature
Saving migration to the chain costs 27363 gas, but it should cost 42363 gas. Your Environment. Version used: Ganache CLI v6.10.1-beta.2 (ganache ...
Read more >Ethereum's Istanbul Fork — Technical Explanation - Medium
Calculation of gas cost for the SSTORE opcode (which lets a contract store data) will become more complicated. If the previous value of...
Read more >Understanding gas costs after Berlin - HackMD
The Berlin hard fork changes the gas cost of some opcodes. If you have a hardcoded gas value in a dapp or a...
Read more >Istanbul Hardfork EIPs - Changing Gas Costs and More
This EIP simply adds more functionality and flexibility. There are no known security problems with BLAKE2. EIP-1344. With the new opcode CHAINID it...
Read more >EVM Opcodes for Gas Optimizations - YouTube
This tutorial explores low-level EVM syntax and how you can use it to make your contracts safer and more gas -efficient.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @seesemichaelj, sorry for the delays,
It was pretty hard to find the actual issue that led to the incorrect
SSTORE
gas usage, but I think I figured it out 😃We can use the test in forking.js on line 179 with modified Example.sol where the
constructor
function doesn’t set thevalue
property, the gas usage in the receipt in this test should be 42535, but atm it is 27535, the reason is that theSSTORE
gas calculator doesn’t realize that the original value is empty but instead goes to storage-slot-update, this is becauseoriginal
field has the value[0]
instead of[]
.StateManager is responsible for storage fetching during the execution of the transaction, this line will call ForkedStorageTrie implementation which is going to invoke web3.eth.getStorageAt that is going to return
0x0
, unfortunately, this is going to encode into[0]
and won’t trigger slot creation.The fix should be pretty straight forward, just make sure
0x0
encodes to[]
bufferI can confirm that the issue is still present if the state variable is set to the default value in the declaration statement; in this case, if you remove
= 0
inuint256 public x = 0;
the SSTORE opcode will cost 20k, which is correct behavior.