question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect gas cost for SSTORE opcode when using fork feature

See original GitHub issue

Executing 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)

  1. ganache-cli -f mainnet_node --networkId 1
  2. truffle migrate
  3. 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:open
  • Created 3 years ago
  • Reactions:3
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

5reactions
nebojsa94commented, Oct 30, 2020

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 the value property, the gas usage in the receipt in this test should be 42535, but atm it is 27535, the reason is that the SSTORE gas calculator doesn’t realize that the original value is empty but instead goes to storage-slot-update, this is because original 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 [] buffer

1reaction
nebojsa94commented, Jan 19, 2021

I 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 in uint256 public x = 0; the SSTORE opcode will cost 20k, which is correct behavior.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found